aspose file tools*
The moose likes Threads and Synchronization and the fly likes dubt in notifyAll Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Threads and Synchronization
Bookmark "dubt in notifyAll" Watch "dubt in notifyAll" New topic
Author

dubt in notifyAll

Sarath Koiloth Ramath
Ranch Hand

Joined: May 07, 2008
Posts: 52
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 ?


J 4 Java
Steve Luke
Bartender

Joined: Jan 28, 2003
Posts: 4181
    
  21



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.


Steve
Sarath Koiloth Ramath
Ranch Hand

Joined: May 07, 2008
Posts: 52
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

Joined: Jan 28, 2003
Posts: 4181
    
  21

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

Joined: Oct 26, 2003
Posts: 418
    
    2

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


"Eagles may soar but weasels don't get sucked into jet engines" SCJP 1.6, SCWCD 1.4, SCJD 1.5,SCBCD 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: dubt in notifyAll