【问题标题】:Why do wait and notify function not working properly on same class lock为什么等待和通知功能在同一个类锁上不能正常工作
【发布时间】:2016-06-07 07:21:04
【问题描述】:

为什么等待和通知功能在同一个类锁上不能正常工作?

请查看下面的代码以了解等待和通知功能及其输出。

输出:

Thread-1
Thread-2
Thread-2 after notify

预期结果:

Thread-1
Thread-2
Thread-2 after notify
Thread-1 after wait    

代码:

public class WaitAndNotify1 {    
    public static void main(String[] args) {        
        Thread t1=new Thread(new Runnable(){            
            @Override
            public void run(){                
                System.out.println("Thread-1");                
                try {
                    synchronized (this) {
                        wait();
                        System.out.println("Thread-1 after wait");
                    }                    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2=new Thread(new Runnable(){            
            @Override
            public void run(){
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread-2");
                synchronized (this) {
                    notify();
                    System.out.println("Thread-2 after notify");
                }
            }
        });
        t1.start();
        t2.start();
    }
}

【问题讨论】:

  • 如果你以后能正确地格式化你的代码,那真的很有帮助......

标签: java multithreading java-threads


【解决方案1】:

您正在使用来自匿名内部类的this - 所以它指的是该匿名内部类的实例。有两个不同的实例(不同的匿名内部类),因此您在不同的对象上调用 wait(),而不是在您调用 notify() 的对象上。

您目前实际上没有要同步的WaitAndNotify1 实例。您可以将代码移至实例方法,然后使用WaitAndNotify1.this 来引用该实例 - 此时您将获得预期的输出:

public class WaitAndNotify1 {
    public static void main(String[] args) {
        new WaitAndNotify1().test();
    }

    public void test() {
        Thread t1=new Thread(new Runnable(){            
            @Override
            public void run(){                
                System.out.println("Thread-1");                
                try {
                    synchronized (WaitAndNotify1.this) {
                        WaitAndNotify1.this.wait();
                        System.out.println("Thread-1 after wait");
                    }                    
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Thread t2=new Thread(new Runnable(){            
            @Override
            public void run(){
                try {
                    Thread.sleep(4000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                System.out.println("Thread-2");
                synchronized (WaitAndNotify1.this) {
                    WaitAndNotify1.this.notify();
                    System.out.println("Thread-2 after notify");
                }
            }
        });
        t1.start();
        t2.start();
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2021-09-21
    • 1970-01-01
    • 2017-02-06
    • 2019-09-11
    • 1970-01-01
    相关资源
    最近更新 更多