*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "wait()" Watch "wait()" New topic
Author

wait()

Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127

Quoted from JCHQ:

As true is notorious for staying true this, code looks at first glance like it will just loop forever. The wait method however effectively means give up the lock on the object and wait until the notify or notifyAll method tells you to wake up.

My question is, when the waiting thread gets notified, does the flow go to line 3 in the catch block, or, go to line 1 in the loop? I'm very confused.
Thanks,
Jenny
Reid M. Pinchback
Ranch Hand

Joined: Jan 25, 2002
Posts: 775
Answer: it depends why you stopped waiting. Given a thread 't', if t.interrupt() was invoked, then you'll be in the catch. If you were notified or the wait timed out, that isn't the same as being interrupted. You'll go to the statement after the wait; in this case, you'll evaluate the loop condition.


Reid - SCJP2 (April 2002)
Jian Yi
Ranch Hand

Joined: Feb 01, 2002
Posts: 127
Reid,

Given a thread 't', if t.interrupt() was invoked, then you'll be in the catch.

so, after the catch block, it will continue the loop?

If you were notified or the wait timed out, that isn't the same as being interrupted. You'll go to the statement after the wait; in this case, you'll evaluate the loop condition.

so, it will be an infinite loop?
Thanks,
Jenny
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
Yes after the catch clause it will continue in the loop. After the execution of the code in an exception handler, the following sentence to the catch clause is executed, but here the catch is still within the same loop.


SCJP2. Please Indent your code using UBB Code
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait()
 
Similar Threads
Thread Communication using wait() and notify()
Marcus Green tutorial on Thread
What exactly makes notifyAll() expensive?
Regarding Notify
Removing entry from HashMap when storing ReentrantLocks safely