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
posted
0
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.