package Thread.ProductAndResumer; import java.util.concurrent.TimeUnit; /* * çäº§è æ¶è´¹è 模åæåä¸ªå¯¹è±¡ï¼ * ç产è ï¼chefï¼ * æ¶è´¹è ï¼waiterï¼ * ä»åºï¼resturantï¼ * 产åï¼mealï¼ * * æ¤ä¾å±ç¤ºå个产åçç产è 䏿¶è´¹è * ç产è * * */ public class Chef implements Runnable { private Resturant resturant; private int num = 0; public Chef(Resturant resturant) { this.resturant = resturant; } public void run() { try{ while (!Thread.interrupted()) { // 1.æ£æ¥é¤å 䏿¯å¦æé£ç©,没æé£ç©ï¼chefèªå·±è¿å ¥çå¾ ç¶æ synchronized (this) { while (resturant.meal!= null) { try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //2.设置é£ç©è¶ è¿åå·ï¼ä»£è¡¨é£æç¨å® if(++num==4){ System.out.println("é£ç©å·²ç»åå®"); resturant.executorService.shutdownNow(); } System.out.println("订åå·²ç»å°è¾¾å¨æ¿"); //4.ç产é£ç©çæ¶é´ TimeUnit.MILLISECONDS.sleep(100); //3.ç产å®é£ç©ï¼éç¥waiter线ç¨å¯ä»¥åé¤ synchronized (resturant.waiter) { resturant.meal=new Meal(num); resturant.waiter.notifyAll(); } } }catch(InterruptedException e){ System.out.println("å¨å¸çº¿ç¨ç»æ"); } } }