• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Jeanne Boyarsky
  • Tim Cooke
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Frits Walraven
Bartenders:
  • Piet Souris
  • Himai Minh

wait() and join()

 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 40
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Then what would happen to the above code?
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 56
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Do not meddle in the affairs of dragons - for you are crunchy and good with ketchup. Crunchy tiny ad:
free, earth-friendly heat - a kickstarter for putting coin in your pocket while saving the earth
https://coderanch.com/t/751654/free-earth-friendly-heat-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic