• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

wait() method in Threads

 
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,

Please check the following code:

On every execution this code results to 999999.

On line 18 wait() is called and notify() has never been called in this code. So, the code should be waiting forever. How could it reach to println()?

Javadoc for method wait() says:
wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

Please explain me the line in bold. Does it means that if no agrument wait() is called it will wait for 0 milliseconds after calling thread gets completed?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Astha Sharma wrote:Please check the following code:

On every execution this code results to 999999.

On line 18 wait() is called and notify() has never been called in this code. So, the code should be waiting forever. How could it reach to println()?

Javadoc for method wait() says:
wait() causes the current thread to wait until another thread invokes the notify() method or the notifyAll() method for this object. In other words, this method behaves exactly as if it simply performs the call wait(0).

Please explain me the line in bold. Does it means that if no agrument wait() is called it will wait for 0 milliseconds after calling thread gets completed?




Basically, you hit a conflict (which is due to implementation detail). Unfortunately, the thread object is already in use as a notification object. The core java library uses the thread object to wake up threads doing a join(). When any thread completes, as part of the clean up process, a notifyAll() is done to wake up the threads waiting to join().

So, when the thread completed, a notification is sent, and your waiting thread also woke up.

Henry
 
Greenhorn
Posts: 19
Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for the insight Henry !!
For proof, I tested that these two variants of the Test class and they DO wait forever:






Now, I have a question about the following point:

Henry Wong wrote:When any thread completes, as part of the clean up process, a notifyAll() is done to wake up the threads waiting to join().


Does this mean that every thread (or the cleaner object) must acquire a lock on itself (or the thread object) before the thread finishes its work? (because it must make a call to notifyAll(); )

 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Arnab Sunny Rakshit wrote:
Now, I have a question about the following point:

Henry Wong wrote:When any thread completes, as part of the clean up process, a notifyAll() is done to wake up the threads waiting to join().


Does this mean that every thread (or the cleaner object) must acquire a lock on itself (or the thread object) before the thread finishes its work? (because it must make a call to notifyAll(); )




Well, yeah, the lock is needed -- and for a bit more than that. The lock is needed to protect the period where the state of the thread is changed to no longer alive and the notification regarding it (these two actions must be atomic). On the other side, the lock is needed for the period where the join() method checks to make sure that the thread is still alive and to execute the wait() call.

There is real communication going on -- it isn't just a blind notification.

Henry
 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the nice explanation Henry
 
Ranch Hand
Posts: 75
Tomcat Server Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Henry,

Sorry I didn't understood this concept well, can you please explain me what is the difference between the these two variants of Test class.


The one that is not waiting.



The one that is waiting.


Thanks...
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Khuzema Dharwala wrote:Hello Henry,

Sorry I didn't understood this concept well, can you please explain me what is the difference between the these two variants of Test class.



You are asking the wrong person here. I didn't write the examples. And if I did, my examples would have simply used a different object to notify on -- an object that isn't in usage conflict with the java library.

Khuzema Dharwala wrote:
The one that is not waiting.



The one that is waiting.


Thanks...




The first is returning from wait() as already explained. The second is not returning from wait() because the thread was never started, and hence, can never terminate and run into the issue. And as I already mentioned, I don't like these examples at all.

Henry
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hey guyz there are some silly things about our beloved JVM that it call an automatic notify if it seems no code is running other than a waiting thread,this automatic call to end wait called spontaneous wake up or spurious wake up. And this is mentioned in API of Thread.wait().
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic