问题描述
在下面的示例中,由于未从子线程获得主线程的通知,因此应永远等待。但是主线程正在执行,下面示例的输出是:
c
l
total: 19900
为什么要执行主线程?
public class ThreadX extends Thread {
static int total = 0;
public void run() {
synchronized (this) {
for (int i = 0; i < 200; i++) {
total = total + i;
}
System.out.println("c");
}
}
public static void main(String[] args) throws InterruptedException {
ThreadX t = new ThreadX();
t.start();
synchronized (t) {
t.wait();
System.out.println("l");
}
System.out.println("total: " + total);
}
}
解决方法
回答问题正文
[...]当线程终止时,将调用
this.notifyAll
方法。 [...]
请注意,Thread#join()
用0
调用该函数,这意味着永远。
[...]超时为0表示永远等待。
因此,在您的情况下,t
仅在终止时调用notifyAll
,这会通知正在等待t
的主线程。
这种不直观的行为是他们在文档中编写以下内容的原因:
建议应用程序不要在
wait
实例上使用notify
,notifyAll
或Thread
。
回答问题
签出Object#wait
(或JLS (17.2.1. Wait)):
线程可以被唤醒,而不会被通知,中断或超时,即所谓的虚假唤醒。尽管实际上这种情况很少发生,但应用程序必须通过测试应该导致线程唤醒的条件来防范它,并在条件不满足时继续等待。
因此Java中的线程可以随时唤醒。 A spurious wakeup is not very likely,但有可能发生。