java - 等待线程是否重新访问同步方法中的代码

标签 java multithreading wait

我正在从 tutorial 中阅读有关线程同步和等待/通知构造的信息.它指出

When wait is invoked, the thread releases the lock and suspends execution. At some future time, another thread will acquire the same lock and invoke Object.notifyAll, informing all threads waiting on that lock that something important has happened.

Some time after the second thread has released the lock, the first thread reacquires the lock and resumes by returning from the invocation of wait.

AFAIK,如果有多个线程可以在第一个线程被 notify 唤醒时竞争锁,那么它们中的任何一个都可以拥有该对象的锁。我的问题是,如果第一个线程本身重新获取锁,它是否必须从同步方法的开头重新开始(这意味着,它再次执行 while 循环检查 wait() 条件之前的代码)还是它只是在 wait() 行暂停?

// Does the waiting thread come back here while trying to own the
// lock (competing with others)?
public synchronized notifyJoy() {
    // Some code  => Does this piece of code gets executed again then in case
    // waiting thread restarts its execution from the method after it is notified?
    while (!joy) {
        try {
            // Does the waiting thread stay here while trying to re-acquire
            // the lock?
            wait();
        } catch(InterruptedException e) {}
    }
    // Some other code
}
        

最佳答案

方法只有在执行它的线程完成其 run 方法时才会退出,无论是通过正常返回还是抛出在该 run 方法中未捕获的异常。在上面的事情之一发生之前​​,你的方法被执行的唯一方法是让 JVM 从你下面被杀死(使用 java.lang.System.exit,杀死 java 进程kill -9 等),或者如果该方法正在 JVM 关闭的守护线程中运行。这里没有什么奇怪的事情发生。等待的线程放弃锁并进入 hibernate 状态,但它不会以某种方式停止执行该方法。

从 wait 调用中唤醒的线程永远不会去任何地方;在线程等待的整个过程中,它仍然处于 wait 方法中。在它可以离开 wait 方法之前,它首先必须获得它放弃的锁以开始等待。然后它需要重新测试它需要检查的任何条件,然后才能知道是否继续等待。

这就是为什么 the guarded blocks tutorial告诉你等待必须在一个循环中完成:

The invocation of wait does not return until another thread has issued a notification that some special event may have occurred — though not necessarily the event this thread is waiting for:

public synchronized void guardedJoy() {
    // This guard only loops once for each special event, which may not
    // be the event we're waiting for.
    while(!joy) {
        try {
            wait();
        } catch (InterruptedException e) {}
    }
    System.out.println("Joy and efficiency have been achieved!");
}

Note: Always invoke wait inside a loop that tests for the condition being waited for. Don't assume that the interrupt was for the particular condition you were waiting for, or that the condition is still true.

(本教程使用的措辞具有误导性;“中断”一词应该是“通知”。同样不幸的是,教程代码显示了在没有设置中断标志的情况下吃掉了 InterruptedException,最好让 InterruptedException从这个方法中抛出并且根本没有捕获它。)

如果线程确实“重新开始”,那么就不需要这个循环;您的代码将从方法的开头开始,获取锁并测试正在等待的条件。

关于java - 等待线程是否重新访问同步方法中的代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25474721/

相关文章:

java - 仅对连接字符串的一部分进行样式设置 (Android)

java - 数组似乎在 Java 中通过引用传递,这怎么可能?

java - 如何使用 get 和 put 作为原子操作使并发 HashMap 线程安全?

python - 在给定的超时时间内等待条件

java - 如何在多线程中使用等待和通知协议(protocol)

java - 使用日历函数替代Java中的 sleep /等待函数

java - 是否有类似 Spark 的 Kafka Streams 累加器?

java - Logstash 日期过滤器解析失败

java - 尝试理解同步方法

python - Tornado 与 ThreadPoolExecutor