Forums Register Login

Clarification of notify( ) vs. notifyAll( )

+Pie Number of slices to send: Send
I am currently studying threads and synchronization. What I have not been able to figure out a detail in the difference between notify and notifyAll.
I read that notifyAll() notifies all the threads that are waiting, and that notify() notifies only one thread waiting. I have no problem understanding who notifyAll() works.
- What if I call notify() and there are several threads waiting?
* How would the system determine what thread must be notified?
* Would it be an error to call notify() if there are several threads waiting?
Thanks in advance for your feedback!!
-Juan Carlos
+Pie Number of slices to send: Send
Hi Juan
Welcome to JavaRanch!


- What if I call notify() and there are several threads waiting?


Then only one gets notified. The others stay suspended, and won't wake up until they are notified in turn.


* How would the system determine what thread must be notified?


As it says in the Javadoc, "The choice is arbitrary and occurs at the discretion of the implementation."


* Would it be an error to call notify() if there are several threads waiting?


It depends on the application. notify() might mean that, for example, one unit of work was ready to process; the notified threads might be "worker threads" waiting to do work. So in this case "notifyAll" would clearly be wrong; you'd just want to notify one waiting thread, and the others could stay suspended.
If you live in a cold climate and on the grid, incandescent light can use less energy than LED. Tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 1161 times.
Similar Threads
Interrupt method + volatile modifier?
synchronization - cannot wake a sleeping thread?
Doubt in Thread Chapter
Doubt in Thread Chapter
Threads notify ( )
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 28, 2024 06:08:32.