2 回答
TA贡献1829条经验 获得超9个赞
当线程同时接收到中断和通知时,行为可能会有所不同。
请参考https://docs.oracle.com/javase/specs/jls/se7/html/jls-17.html#jls-17.2.3
信用 - 并发兴趣邮件列表上的 Alex Otenko
TA贡献1810条经验 获得超5个赞
因为重新排序。在正常终止编译器重新排序指令中断和通知中,中断在工作线程上调用并且没有中断异常抛出。尝试通过读取 volatile 变量来禁止重新排序,您总是会遇到异常中断。
public class WaitNotifyAll {
private static volatile Object resourceA = new Object();
public static void main(String[] args) throws Exception {
Thread threadA = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resourceA) {
try {
System.out.println("threadA begin wait");
resourceA.wait();
System.out.println("threadA end wait");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
});
Thread threaB = new Thread(new Runnable() {
@Override
public void run() {
synchronized (resourceA) {
System.out.println("threadC begin notify");
threadA.interrupt();
System.out.print(resourceA);
resourceA.notify();
}
}
});
threadA.start();
Thread.sleep(1000);
threaB.start();
System.out.println("main over");
}
}
添加回答
举报