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

时间:2023-03-08 15:35:24
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(); }
}

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