调用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(); } }
如果文章对您有所帮助,可以点一下推荐