• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

NX: notify() VS notifyAll() ???

 
Ranch Hand
Posts: 222
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
People usually use notify() and ignore notifyAll() but I have a book it says notifyAll() is better than notify() and it mentions a phrase "miss notification", what does it mean? Is it ture? In the book, it says if there is only one thread waiting, notify() is OK, notifyAll() is OK but it uses more resource than notify(), that's a waste, but if there are more than one thread.
two methods in ClassX

and, there's a class ClassY

If threadA is runing in ClassY, it synchronize cx, invokes wait(), if threadB invokes waitUntilTrue(), it's now waiting, too. if threadC uses setValue(), and pass true (new value), threadC will just notify one thread, because it didn't use notifyAll(), cannot make sure whether threadA or threadB receive the notification.
 
Bartender
Posts: 1872
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi HaoZhe,

People usually use notify() and ignore notifyAll() but I have a book it says notifyAll() is better than notify() and it mentions a phrase "miss notification", what does it mean? Is it ture? In the book, it says if there is only one thread waiting, notify() is OK, notifyAll() is OK but it uses more resource than notify(), that's a waste, but if there are more than one thread.


You have three possible cases :
  • You know that only one thread is waiting to be notified on a given object. In this case, notify() is better because more efficient.
  • You know that multiple threads may be waiting to be notified on a given object, but all threads are interchangeable : you need one of them to perform a job, but it can be *any* of them. In this case, notify() is still better, for the same reasons.
  • Multiple threads may be waiting to be notified on a given object, but they are not interchangeable for two possible reasons : they have a different interest in the notification (that's what happens with locking - each thread may be waiting on a different record lock) or you need to awake all waiting threads for any reason of yours. In that case, notifyAll() *must* be used.


  • Best regards,
    Phil.
     
    There are 29 Knuts in one Sickle, and 17 Sickles make up a Galleon. 42 tiny ads in a knut:
    a bit of art, as a gift, that will fit in a stocking
    https://gardener-gift.com
    reply
      Bookmark Topic Watch Topic
    • New Topic