Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

wait()

 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 775
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jian Yi
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic