wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes wait() and join() 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() and join()" Watch "wait() and join()" New topic
Author

wait() and join()

K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71


Is this a guranteed behaviour that line 9 will be reached before run method completes?
What I want to ask is in line 4 start() is called so run() will become Runnable and finally running.Simultaneosly line 6, 7, 8 and 9 will get executed where wait() is called.
What will happen if control reaches line 24 before it reaches 6.
Is that a possiblilty?

In other words if i write sleep() before line 6 it just hangs. Is the above mentioned the reason for this or am i getting it all wrong?




But if i write sleep within synchronized code it works fine.
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Thread b's run() could complete even before line 5 is reached.

If the notify() in thread b is executed before the wait() in the main thread then it is just thrown away.

So the main thread could execute the wait() with no notify arriving to release it. wait() is the same as wait(0) which means wait until notified (or interruptrd).
[ June 23, 2004: Message edited by: Barry Gaunt ]

Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
sylvia greene
Ranch Hand

Joined: Jun 23, 2004
Posts: 40
Then what would happen to the above code?
Barry Gaunt
Ranch Hand

Joined: Aug 03, 2002
Posts: 7729
Originally posted by sylvia greene:
Then what would happen to the above code?



Try the practical approach: compile and run the program inserting sleep calls and println statements when the thread exits the run() method.
K Anshul
Ranch Hand

Joined: May 19, 2004
Posts: 71
i tried calling sleep() before the synchronized code(show in above post) and it simply hangs.
Is it because notify() is called before wait() or is there some other reason?
Robert Miller
Ranch Hand

Joined: Jun 18, 2004
Posts: 56
You have no guarantee the wait() will be called before the notify. In this particular example join() would be more appropriate, because you are waiting for ThreadB to finish calculating an answer and exit.

You need to be more careful if you need to use wait(). I suspect the reason your code is hanging when you add the sleep() is that it is forcing (on your particular JVM) the notify() to execute before the wait(). Therefore wait() will block forever waiting for a notify() call it isn't going to get. In such cases it is better to have a construct such as:

synchronized(b)
{
while (b.answerCalculated == false)
b.wait();
}

where answerCalculated is an instance variable of ThreadB that ThreadB sets to true just before calling notify(). This should cover the situation where notify() is called first, and the situation where some other method calls notify() even though run() hasn't finished with the calculation yet.

Robert
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: wait() and join()
 
Similar Threads
notify(); doesn't work?
Thread wait() and notify()
Threads
Problem On using wait() in THreads
Lock Mechanism ?