Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

notify() versus notifyAll()

 
amol k
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are there any *specific* cases where one would need to use notify() instead of notifyAll()?
I was just wondering!!
thnx
amol
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13074
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I suppose that notify could be faster than notifyAll in a situation where you had a lot of Threads waiting.
Bill
 
David Weitzman
Ranch Hand
Posts: 1365
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2120
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic