• Post Reply Bookmark Topic Watch Topic
  • New Topic

notify and notifyAll

 
Deepika Wadhwa
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is the difference between the two different method calls to notify() and notifyAll() from the same object?
thanx in advance
Deepika
 
Leslie Chaim
Ranch Hand
Posts: 336
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notifyAll() wakes up all threads waiting for this object.
notify() wakes up one arbitrary thread waiting for this object.
Was there something more to your question?
 
Mr. C Lamont Gilbert
Ranch Hand
Posts: 1170
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand the essence of his question. I ask myself this question about once every 6 months...
But her answer is complete. Usually notify all is not what you want. If your threads are waiting on the same lock, the notify all will waste time since only 1 thread can get through anyway. However, if your threads are simply waiting for some other reason, then notify all will get them all going.
 
Jose Botella
Ranch Hand
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notify should be used only when you are sure that only one thread is waiting on an object's monitor. If there are more use notifyAll.
Using notfyAll has the risk of excessive context switching when only one of the dormant threads is able to proceed. In order to alleviate this, use more than one monitor to wait on; specially to avoid several threads waiting for different conditions on the same monitor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!