Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

dubt in notifyAll

 
Sarath Koiloth Ramath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a doubt in notifyAll.I refering K&B.In the notify section i done a program in the text.i had shown the code below



In the above code.i replaced the notifyAll() with notify().but i did not found any difference in the output.I was thinking that, only one thread will be notified and other two will remain as waiting.But all worked.Please explain ?
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


What you are seeing is the result of an implementation detail of Thread. When a Thread comes to an end it calls notifyAll() on itself behind the scenes to wake up any threads that may be waiting on it. Since you synchronize on a subclass of Thread, when the Thread completes all waiters get notified and wake up. Try commenting out all notify() calls and you will probably see very similar results.

However, if you change your Calculator to implement Runnable instead of extending Thread, you would find different behavior.

You should not rely on this sort of thing. The fact that notifyAll() is called on the Thread Object when it dies is an undocumented implementation detail, and so you can't rely on it working all the time. You should explicitly call notifyAll() if you want all waiters to be notified.
 
Sarath Koiloth Ramath
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you steve for your valuable reply.I changed the code with Runnable.Now its working perfectly.How to get the implementaion details.i also check the source file of class thread.but didnt get.

 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There is a lot of the Thread class which is delegated to native code, and some that is performed in the ThreadGroup methods. I am not sure where the notifyAll() call is implemented, but to me it isn't all that interesting (after all I don't have to maintain the Thread class), the fact that it isn't documented means more.
 
Chris Hurst
Ranch Hand
Posts: 443
3
C++ Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The notifyAll on thread death is strictly regarded as a spurious thread wake up, ie line 25 of the origonal listing is bugged if you look at the documentation on wait this is clearly documented.

eg look at the following and read the description of wait, it tells you to put your wait in a loop ie your supposed to assume your thread can just wake up and this is one such condition (there are others)


http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Object.html
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic