简体   繁体   中英

Notification in Java thread synchronization

When there are multiple threads in waiting state; and if I call notify() rather than calling notifyAll() Which one is going to be notified among several threads in waiting state?

That's undefined behavior. Anyone could be picked up. From the JavaDoc :

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.

And yes, this can lead to thread starvation .

The javadoc says:

The choice is arbitrary and occurs at the discretion of the implementation.

When there are multiple threads in waiting state; and if I call notify() rather than calling notifyAll() Which one is going to be notified among several threads in waiting state?

Although as has been mentioned, the particular behavior is not defined by the Java spec, I suspect that JDC implementations will take the thread at the front of the WAIT queue for the particular monitor. However this should not be relied on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM