Friday, 22 April 2016

notify() vs. notifyAll()


notify() vs. notifyAll()
  • notify causes at most one thread waiting on the same object to be notified (i.e., the object that calls notify must be the same as the object that called wait) 
  • notifyAll causes all threads waiting on the same object to be notified.

Which is better ?
  • If more than one thread is waiting on that object, there is no way to control which of them is notified by a call to notify.
    So it is often better to use notifyAll() than notify()
  • notify() is useful when you know that there is only one thread waiting, because it doesn't provide any way to choose a particular thread.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.