gpt4 book ai didi

java - notify/notifyall 是否释放被持有的锁

转载 作者:IT老高 更新时间:2023-10-28 20:31:21 24 4
gpt4 key购买 nike

我对等待和通知/通知所有有点困惑。

我知道每个 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com