话不多说,先上代码
public class Demo { public static void main(String[] args) { final Object lock = new Object(); new Thread(new Runnable() { public void run() { synchronized (lock){ System.out.println("1.告诉老板我要烧饼"); try { lock.wait(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("3.拿到烧饼开始吃"); } } },"顾客").start(); new Thread(new Runnable() { public void run() { try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lock){ System.out.println("2.老板开始打烧饼"); lock.notify();