File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes notify() versus notifyAll() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "notify() versus notifyAll()" Watch "notify() versus notifyAll()" New topic

notify() versus notifyAll()

amol k

Joined: Dec 28, 2001
Posts: 1
Are there any *specific* cases where one would need to use notify() instead of notifyAll()?
I was just wondering!!
William Brogden
Author and all-around good cowpoke

Joined: Mar 22, 2000
Posts: 13036
I suppose that notify could be faster than notifyAll in a situation where you had a lot of Threads waiting.
David Weitzman
Ranch Hand

Joined: Jul 27, 2001
Posts: 1365
I just read Effective Java and it has a little section devoted to notify vs. notifyAll. It looks like notify can be better for performance in certain cases (although perhaps a little more risky in certain cases) but they both get the job done. I don't think you would ever need to use notify instead of notifyAll though, but there can be benifits to each.
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
There is a reason to use notifyAll instead of notify. The latter is only advisable if it is known that there is only a thread waiting for the lock. If notify were used in a situation where several threads are waiting, there could be a problem in a implementation in which the last thread to arrive to the waiting state is chosen among the others. If there would be always threads in the waiting state, notify would only mark for trying to adquire the lock the last of them to arrive in that state, and the threads that were already there for a long time would stay in waiting state for even more time; just because new threads keep coming in the waiting state.
Thus is advisable to use notifyAll if the programmer knows there is more than one thread in the wainting state.

SCJP2. Please Indent your code using UBB Code
I agree. Here's the link:
subject: notify() versus notifyAll()
jQuery in Action, 3rd edition