Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

what is the difference between notify() and notifyAll()  RSS feed

 
feng chou
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the difference between notify() and notifyAll().I mean when we use notify(),and when
we use notifyAll().(i know notify() will wake up
one thread,and notifyAll() will wake up all threads)
[ October 22, 2002: Message edited by: feng chou ]
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
notify will wake one thread, but you can not predict which one or that it will be the right one.
notifyAll will wake all threads that are waiting on some lock.
Best practice is usually to always invoke wait in a while loop with some condition test ("spin lock"), and use notifyAll.
Having wait in a loop protects against accidental notifications by an unrelated thread. Using notifyAll protects against notifiying an unrelated thread accidentally.
For two good discussions of this, see Effective Java by Josh Bloch and Practical Java by Peter Haggar.
 
feng chou
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but at one time,only one thread can use the object
lock.that means an when unralated thead acquare the object lock,the related thead must wait until
the unrelated thread release the lock.so the related thread can't go into the while loop.so i think it's the same with notify().
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Read the discussions in Bloch's and Haggar's books - these are invaluable books and you should have a copy or at least check them out from the library or borrow one from a friend.
You are right that notify will let one thread out of its wait, and thus will probably do the job. But it would be really easy to have another unrelated thread wait on the same lock and sometimes it might receive the notify - stealing it from your threads. Then the code you thought would run will not run and everything is broken and locked up. This gets really difficult to debug.
So notifyAll is just safer - in the above case, the unrelated thread receives the notifyAll, but so does your intended threads. The spin lock loop keeps extra threads from doing extra work, and all is well.
 
Dave Landers
Ranch Hand
Posts: 401
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another note-
The difference between notify and notifyAll is not about what will work, but it is about which makes for more robust and maintainable code.
Yes, you can make your code work using just notify.
But by adding spin locks and notifyAll, that same code can be made more robust. Robust code is easier to maintain (because it is less likely to fail) and easier to reuse, since it works in more situations.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!