开发者社区 问答 正文

Java wait() notify()循环打印A和B

public class Test {
    public static Object object = new Object();
    public static boolean printA = true;
    
    public static void main(String[] args) {
        ThreadA threadA = new ThreadA();
        threadA.start();
        
        ThreadB threadB = new ThreadB();
        threadB.start();
    }
}
 class ThreadA extends Thread {
    @Override
    public void run() {
        for(int i = 0; i < 10; i++) {
            synchronized (Test.object) {
                if(!Test.printA) {
                    try {
                        Test.object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("A");
                    Test.printA = false;
                    Test.object.notify();
                }
            }
        }
    }
}
class ThreadB extends Thread {
    @Override
    public void run() {
        for(int i = 0; i < 10; i++) {
            synchronized (Test.object) {
                if(Test.printA) {
                    try {
                        Test.object.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                } else {
                    System.out.println("B");
                    Test.printA = true;
                    Test.object.notify();
                }
            }
        }
    }
}

运行以上代码,为什么会一直不结束无限wait下去?

展开
收起
蛮大人123 2016-03-10 13:20:02 2331 分享 版权
1 条回答
写回答
取消 提交回答
  • 我说我不帅他们就打我,还说我虚伪

    线程中不应该有else,把else注释掉就正确了。 如果加上else后,该线程会在wait后不再notify,导致另一个线程无限wait。

    public class Test {
        
        public static Object object = new Object();
        public static boolean printA = true;
        
        public static void main(String[] args) {
            ThreadA threadA = new ThreadA();
            threadA.start();
            
            ThreadB threadB = new ThreadB();
            threadB.start();
        }
    }
     
    class ThreadA extends Thread {
        
        @Override
        public void run() {
            for(int i = 0; i < 10; i++) {
                synchronized (Test.object) {
                    if(!Test.printA) {
                        try {
                            Test.object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }/* else {*/
                        System.out.println("A");
                        Test.printA = false;
                        Test.object.notify();
                    /*}*/
                }
            }
        }
    }
    
    class ThreadB extends Thread {
        
        @Override
        public void run() {
            for(int i = 0; i < 10; i++) {
                synchronized (Test.object) {
                    if(Test.printA) {
                        try {
                            Test.object.wait();
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }/* else { */
                        System.out.println("B");
                        Test.printA = true;
                        Test.object.notify();
                    /*}*/
                }
            }
        }
    }
    2019-07-17 18:57:26
    赞同 展开评论