Hi all, The problem I am having is that the thread I need to call join() on so as to wait for completion may not even yet be started when I call join(). Is it legal in Java to call join() on a thread which is not running, but which will eventually be run by a third thread?
Thanks, JG 2012/2/9 John Goche <[email protected]>: > Basically sendRequest() invokes a thread which calls runOnUiThread with > a new Runnable() which in turn also invokes a new Thread(). It is this latter > thread which I need to wait for to finish. I thought of using the join() > method > to force thread completion prior to continuation at a specific spot but I am > somewhat new to thread programming and I am not entirely sure > about how to set things up. Will give it another go... > > Thanks for your help, > > JG > > 2012/2/9 John Goche <[email protected]>: >> Thanks Kostya! >> >> I have traced the code with the debugger and found that even though >> notify() is being called from within a thread instantiated from sendRequest() >> (which is not the same thread as sendRequest()'s thread), the notify() is >> being called before the wait(). >> >> I am assuming that if I call notify() before wait() the notification gets >> lost? >> If this is not so, someone please let me know. >> >> The problem I am having is that sendRequest() launches a new thead s, >> and I need to be sure thread s completes before the code proceeds past >> what I have marked with wait(). >> >> Any ideas on how to solve this synchronization problem? >> >> Thanks, >> >> John Goche >> >> >> 2012/2/9 Kostya Vasilyev <[email protected]>: >>> Are you calling notify() from inside sendRequest()? >>> >>> >>> If so, it's the same thread, and notify has nothing to do. >>> >>> http://developer.android.com/reference/java/lang/Object.html#notify() >>> >>> Causes a thread which is waiting on this object's monitor (by means of >>> calling one of the wait() methods) to be woken up >>> >>> >>> From looking at the code you posted, there is no thread waiting on the lock >>> because you only have one worker thread, and when it's calling notify it's, >>> well, calling notify and not waiting on a wait(). >>> >>> >>> For debugging, set a breakpoint at lock.notify, and use the DDMS thread >>> monitor or the debugger window to examine your threads. >>> >>> >>> You might want to look at this package, which provides higher-level >>> concurrency classes, and try to find one that fits what you're trying to do: >>> >>> http://developer.android.com/reference/java/util/concurrent/package-summary.html >>> >>> ( ThreadPoolExecutor? BlockingQueue? ) >>> >>> -- Kostya >>> >>> 10 февраля 2012 г. 0:41 пользователь John Goche <[email protected]> >>> написал: >>>> >>>> Dear Android developers, >>>> >>>> I am having the following issue in Android: >>>> >>>> In one class I am having: >>>> >>>> -------------------------------------------- >>>> >>>> final Object lock = new Object(); >>>> >>>> Thread thread; >>>> >>>> @Override >>>> public void onCreate(Bundle savedInstanceState) { >>>> >>>> super.onCreate(savedInstanceState); >>>> >>>> thread = new Thread() { >>>> >>>> @Override >>>> public void run() { >>>> >>>> sendRequest("foo"); >>>> >>>> synchronized(lock) { >>>> >>>> try { lock.wait(); } catch (InterruptedException e) { >>>> e.printStackTrace(); } >>>> >>>> } >>>> >>>> System.out.println("GOT HERE!!!"); >>>> >>>> ------------------------ >>>> >>>> which causes the sendRequest() to do a bunch of stuff. When such stuff >>>> finishes I call: >>>> >>>> System.out.println("Notifying thread..."); >>>> synchronized(lock) { >>>> lock.notify(); >>>> } >>>> System.out.println("Thread notified."); >>>> >>>> ------------------------- >>>> >>>> and this code executed until the "Thread notified." println() >>>> statement. But then I do >>>> not see the waiting thread resume from where I called wait(). Here is the >>>> output >>>> from adb logcat: >>>> >>>> I/System.out( 2395): Thread notified. >>>> W/ActivityManager( 60): Launch timeout has expired, giving up wake lock! >>>> >>>> I do not see where I am going wrong. Any ideas why the code is not >>>> working as expected? >>>> >>>> Thanks, >>>> >>>> John Goche >>>> >>>> -- >>>> You received this message because you are subscribed to the Google >>>> Groups "Android Developers" group. >>>> To post to this group, send email to [email protected] >>>> To unsubscribe from this group, send email to >>>> [email protected] >>>> For more options, visit this group at >>>> http://groups.google.com/group/android-developers?hl=en >>> >>> >>> -- >>> You received this message because you are subscribed to the Google >>> Groups "Android Developers" group. >>> To post to this group, send email to [email protected] >>> To unsubscribe from this group, send email to >>> [email protected] >>> For more options, visit this group at >>> http://groups.google.com/group/android-developers?hl=en -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to [email protected] To unsubscribe from this group, send email to [email protected] For more options, visit this group at http://groups.google.com/group/android-developers?hl=en

