我对等待和通知/通知所有有点困惑。
我知道每个 java 对象都有一个锁。我知道等待会释放其他线程的锁。 notify/notifyall 怎么样? notify/notifyAll 是否释放它为其他线程持有的锁?
最佳答案
No -- notify
/notifyAll
不像 wait
那样释放锁。在调用notify
的代码释放锁之前,被唤醒的线程无法运行。
Javadoc 是这么说的:
The thread releases ownership of this monitor and waits until another thread notifies threads waiting on this object's monitor to wake up either through a call to the notify method or the notifyAll method. The thread then waits until it can re-obtain ownership of the monitor and resumes execution.
关于java - notify/notifyall 是否释放被持有的锁,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5999193/