哪个线程通知唤醒?
Which thread does notify wake up?
假设我有 3 个线程具有等待条件,第 4 个线程具有通知条件。
现在,所有 3 个等待线程 运行 并进入等待状态。完成此操作后,第 4 个线程 运行 会调用一次通知。
notify将如何确定唤醒哪个线程?是先调用wait的线程,还是最后调用wait的线程,还是根据其他线程条件?
假设等待和通知使用相同的锁。
据我所知,没有 特殊簿记 - 这意味着选择 "randomly"。
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.
因此,理论上 JVM 实现可能会决定执行特定的命令;但如图所示 - 默认情况下您不能期望任何订单。所以你也不应该编写依赖于这种特定顺序的代码。
请参阅 notify() 方法的文档。
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.
假设我有 3 个线程具有等待条件,第 4 个线程具有通知条件。
现在,所有 3 个等待线程 运行 并进入等待状态。完成此操作后,第 4 个线程 运行 会调用一次通知。
notify将如何确定唤醒哪个线程?是先调用wait的线程,还是最后调用wait的线程,还是根据其他线程条件?
假设等待和通知使用相同的锁。
据我所知,没有 特殊簿记 - 这意味着选择 "randomly"。
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.
因此,理论上 JVM 实现可能会决定执行特定的命令;但如图所示 - 默认情况下您不能期望任何订单。所以你也不应该编写依赖于这种特定顺序的代码。
请参阅 notify() 方法的文档。
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.