调用notify()后,当前线程执行完synchronized块中的所有代码才会释放锁

package com.pinnet.test;

public class Demo {

    public static void main(String[] args) {

        Demo demo = new Demo();

        new Thread(new Runnable() {

            @Override
            public void run() {
                synchronized (demo) {
                    System.out.println("before wait ");
                    try {
                        demo.wait();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println("after wait ");
                }
            }
        }).start();

        new Thread(new Runnable() {

            @Override
            public void run() {
                synchronized (demo) {
                    System.out.println("before notify ");
                    demo.notify();
                    System.out.println("after notify ");
                }
            }
        }).start();

    }
}

猜你喜欢

转载自www.cnblogs.com/virgosnail/p/9446090.html