Forums Register Login

wait()

+Pie Number of slices to send: Send
 


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
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
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.
Time is the best teacher, but unfortunately, it kills all of its students - Robin Williams. tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 486 times.
Similar Threads
Marcus Green tutorial on Thread
Thread Communication using wait() and notify()
What exactly makes notifyAll() expensive?
Removing entry from HashMap when storing ReentrantLocks safely
Regarding Notify
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 03:43:19.