• Post Reply Bookmark Topic Watch Topic
  • New Topic

notify() method

 
Rajeshwari, Ramamurthi
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
If there are more than one thread waiting to own the monitor of that object, which one will be notified first when notify() method is encountered?
Is it the first thread that called wait() for that monitor or is it arbitarly chosen?
Thanks
Raji
 
Val Dra
Ranch Hand
Posts: 439
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It's unknown what thread will be notofied to run, it's best to use in that situation notifyAll() method.
 
kiran babu
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
you can get detailed explanation in page 695 of Java How To Program by Deitel&Deitel.
when a running thread calls wait the thread enter waiting state where it waits in a que associated with the particular object on which wait was called . The first thread in the wait queue for a particular object becomes ready on a call to notify issued by another thread associated with the object. in more than one thread is in queue then it is better to call notifyAll method.
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If there are multiple threads waiting on a lock, there is no guarantee what thread will run after a call to notify() or notifyAll(). It will NOT necessarily be the one with the highest priority, or the one that has been waiting the longest.
notify() will wake up one thread, you have no control over which one, unless you implement the Specific Notification Pattern found here.
notifyAll() wakes up all threads, but only one gets the lock, the others block. Again, you can't control this unless you implement the above pattern.
For more on this, see my post here.
Peter Haggar
------------------
Senior Software Engineer, IBM
author of: Practical Java
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!