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

标签 java multithreading locking

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

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

相关文章:

C++ 锁定后读取文件

java - 非托管线程 Spring Quartz Websphere Hibernate

java - gradle如何在Java上获取有关项目的信息(依赖项目artifactID)

java - 如何在 Java 中将一个 JSON 字符串拆分为两个 JSON 对象

c - 使用 CreateThread() 在 C 中线程化

android - Android SQLite 中的并发问题

java - 以十六进制读入 Java

java - 如何在Android中使用on循环在onCreate()中启动多个线程而又未使用for循环启动线程(IllegalThreadStateException)?

c - 使用 pthreads 在客户端-服务器设置中终止客户端线程

Java 类级锁与对象级锁