When calling notify() in a thread in which there are multiple instances waiting, how does Java(or JVM) choose which thread to wake up?
Asked
Active
Viewed 183 times
-2
-
3It doesn't; the result is nondeterministic. Which is why you should not use that but `.notifyAll()`. Unless you are absolutely sure that you have only one `.wait()`er. – fge May 19 '14 at 11:22
-
1Note: if there is no threads waiting on the object, the notify is discarded. – Peter Lawrey May 19 '14 at 11:31
2 Answers
2
notify method wakes up only one thread waiting on the object and that thread starts execution. So if there are multiple threads waiting for an object, this method will wake up only one of them. The choice of the thread to wake depends on the OS implementation of thread management.

Gopal Barge
- 61
- 6
2
Sources you can use for an answer:
Java Language Specification chapter 17.2.2 Notification:
There is no guarantee about which thread in the wait set is selected.Javadoc on
Object.notify()
reads:
Wakes up a single thread that is waiting on this object's monitor. If any threads are waiting on this object, one of them is chosen to be awakened. The choice is arbitrary and occurs at the discretion of the implementation.
-
If you think the prior-research is lacking, down-vote the question. I like your approach of pointing to the resources where this could have been found. +1 – Duncan Jones May 19 '14 at 11:52