package Thread.ProductAndResumer; import java.util.concurrent.TimeUnit; //æ¶è´¹è public class Waiter implements Runnable{ private Resturant resturant; public Waiter(Resturant resturant){ this.resturant=resturant; } @Override public void run() { try{ while(!Thread.interrupted()){ //è¦æsleepæ¾å¨ä¸é¢ï¼ä¸è¿æ ·ï¼chefè¿å¨é»å¡ç¶æï¼waiterçwaitåºé TimeUnit.MILLISECONDS.sleep(1); //1.æ£å·®å¦ææ²¡æé£ç©ï¼æèµ·èªå·± synchronized (this) { while(resturant.meal==null){ try { this.wait(); } catch (InterruptedException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } //2.æé£ç©ï¼è¾åºé£ç©ä¿¡æ¯ System.out.println("客人è·å¾"+resturant.meal.toString()); TimeUnit.MILLISECONDS.sleep(200); //3.é£ç©åå®ï¼éç¥å¨å¸åè synchronized (resturant.chef) { resturant.meal=null; resturant.chef.notifyAll(); } } }catch(InterruptedException exception){ System.out.println("æå¡ç线ç¨ç»æ"); } } }