Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling notify() invoking all waiting threads

 
Astha Sharma
Ranch Hand
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?

 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dan Drillich
Ranch Hand
Posts: 1183
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ankit Garg
Sheriff
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 103
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 250
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Its very information Ankit. Thanks for the explanation
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic