Java 同时执行Thread.interrupt()对象.notify(),为什么会有两个结果?

Java 同时执行Thread.interrupt()对象.notify(),为什么会有两个结果?,java,concurrency,interrupt,notify,Java,Concurrency,Interrupt,Notify,这里有两种可能的结果: 抛出中断异常 正常终止 为什么? 我不明白。当threadA被中断时,结果应该抛出InterruptedException。但有时执行这个程序,它可以正常完成 环境:java8,mac因为重新排序。在正常的终止编译器重新排序指令中断和通知中,中断在工作线程上调用,而没有中断的异常抛出。 尝试禁止读取volatile变量进行重新排序,您总是会遇到中断异常 public class WaitNotifyAll { private static volatile Obj

这里有两种可能的结果:

  • 抛出中断异常

  • 正常终止

  • 为什么?

    我不明白。当threadA被中断时,结果应该抛出InterruptedException。但有时执行这个程序,它可以正常完成


    环境:java8,mac

    因为重新排序。在正常的终止编译器重新排序指令中断和通知中,中断在工作线程上调用,而没有中断的异常抛出。 尝试禁止读取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();
                        resourceA.notify();
                    }
                }
            });
    
            threadA.start();
    
            Thread.sleep(1000);
    
            threaB.start();
    
            System.out.println("main over");
        }
     }
    

    }当线程同时收到中断和通知时,行为可能会有所不同

    请参阅

    信贷-Alex Otenko在并发兴趣邮件列表中

    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");
    }