notify / notifyall是否释放正在进行的锁定 [英] Does notify/notifyall release the lock being held
问题描述
我对等待和notify / notifyAll感到困惑。
I am confused a bit about wait and notify/notifyAll.
我知道每个java对象都有一个锁。我知道等待将释放其他线程的锁。通知/ notifyall怎么样? notify / notifyAll会释放它为其他线程持有的锁吗?
I know there is a lock for every java object. I know wait will release the lock for other thread. How about notify/notifyall? Does notify/notifyAll release the lock it is holding for other thread?
推荐答案
否 - 通知
/ notifyAll
不要释放像等
那样的锁。唤醒的线程无法运行,直到调用 notify
的代码释放其锁定。
No -- notify
/notifyAll
don't release locks like wait
does. The awakened thread can't run until the code which called notify
releases its lock.
这就是Javadoc说:
This is what the Javadoc says:
线程释放此监视器的所有权并等待,直到另一个线程通知通过调用唤醒等待此对象监视器的线程到notify方法或notifyAll方法。然后线程等待,直到它可以重新获得监视器的所有权并继续执行。
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.
这篇关于notify / notifyall是否释放正在进行的锁定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!