aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling notify() invoking all waiting threads Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling notify() invoking all waiting threads" Watch "Calling notify() invoking all waiting threads" New topic
Author

Calling notify() invoking all waiting threads

Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

Hi,
Please see the example below. Here line-1 sometimes gets printed 3 times, sometime 2 and sometime only 1.
In this code notify() is called only one time it means out of three waiting threads, only one should come in runnable state. Then why the code shows such random behavior?



Astha - OCPJP 6 (90%)
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

Think of a scenario where the Calculator thread completes before one or more of the Reader threads. What will happen in that case? The wait() method would be called after the notify() method is called and the wait() method will never be notified. Did you notice when the output doesn't show the total 3 times the program never terminates?


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Dan Drillich
Ranch Hand

Joined: Jul 09, 2001
Posts: 1175
Good Day,

When I comment out the notify() call, I still see consistently the three totals. So, it seems to me that when Calculator is no longer in runnable state, the locks get released.

Regards,
Dan

William Butler Yeats: All life is a preparation for something that probably will never happen. Unless you make it happen.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9293
    
  17

That behavior is just an implementation detail of Thread class and is not guaranteed behavior as per JLS. When a thread completes all threads waiting on it are automatically notified. This happens due to the way join() method of Thread class is implemented. But you should not rely on this and the SCJP exam won't test you to wait/notify on Thread objects. In fact wait/notify is not on the SCJP objectives anymore. But as for the confusion you are having, this behavior of no need to notify waiting threads is only applicable to Thread objects. If you are waiting on any object other than a Thread object you won't see this type of behavior...
vishal mishra
Ranch Hand

Joined: Jul 12, 2010
Posts: 103
Ankit Garg wrote:That behavior is just an implementation detail of Thread class and is not guaranteed behavior as per JLS. When a thread completes all threads waiting on it are automatically notified. This happens due to the way join() method of Thread class is implemented. But you should not rely on this and the SCJP exam won't test you to wait/notify on Thread objects. In fact wait/notify is not on the SCJP objectives anymore. But as for the confusion you are having, this behavior of no need to notify waiting threads is only applicable to Thread objects. If you are waiting on any object other than a Thread object you won't see this type of behavior...


I also had the same confusion Thanks for your explanation
Astha Sharma
Ranch Hand

Joined: Oct 15, 2011
Posts: 245

Its very information Ankit. Thanks for the explanation
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Calling notify() invoking all waiting threads