多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
``` public static void main(String[] args) { Baozi bz = new Baozi(); Baozipu bzp = new Baozipu("包子铺", bz); Chi chi = new Chi("吃货", bz); bzp.start(); chi.start(); } ``` ``` public class Baozi { String jiucai; String jidan; boolean flag = false; //是否还有包子 } ``` ``` public class Baozipu extends Thread{ private Baozi bz; public Baozipu(String name, Baozi bz) { super(name); this.bz = bz; } @Override public void run() { while (true){ synchronized (bz){ if (bz.flag == true){ try { bz.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("开始做包子"); try { sleep(3000); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println("包子造好了"); bz.flag = true; bz.notify(); } } } } ``` ``` public class Chi extends Thread{ private Baozi bz; public Chi(String name, Baozi bz) { super(name); this.bz = bz; } @Override public void run() { while (true){ synchronized (bz){ if (bz.flag == false){ try { bz.wait(); } catch (InterruptedException e) { e.printStackTrace(); } } System.out.println("吃货正在吃"); bz.flag = false; bz.notify(); } } } } ```