消費者與生產者

 1 import java.util.concurrent.ExecutorService;
 2 import java.util.concurrent.Executors;
 3 import java.util.concurrent.TimeUnit;
 4 
 5 class Meal{
 6     private final int orderNum;
 7     public Meal(int orderNum){
 8         this.orderNum = orderNum;
 9     }
10     public String toString(){
11         return "Meal" + orderNum;
12     }
13 }
14 
15 class WaitPerson implements Runnable{
16     private Restaurant restaurant;
17     public WaitPerson(Restaurant r){ restaurant = r; }
18     
19     @Override
20     public void run() {
21         // TODO Auto-generated method stub
22         try{
23             while(!Thread.interrupted()){
24                 synchronized(this){
25                     while(restaurant.meal == null){ //餐館沒有菜單就等待,廚師生產出來。
26                         wait();
27                     }
28                 }
29                 System.out.println("服務員得到菜單:" + restaurant.meal);
30                 synchronized(restaurant.chef){
31                     restaurant.meal = null;
32                     restaurant.chef.notifyAll();
33                 }
34             }
35             
36         }catch(InterruptedException e){
37             System.out.println("WaitPerson interrupted");
38         }
39     }
40     
41 }
42 
43 class Chef implements Runnable{
44     private Restaurant restaurant;
45     private int count = 0;
46     public Chef(Restaurant r) { restaurant = r; }
47     
48     @Override
49     public void run() {
50         // TODO Auto-generated method stub
51         try{
52             while(!Thread.interrupted()){
53                 synchronized(this){
54                     while(restaurant.meal != null){  //生產者,當飯店有菜單時,就等待服務員拿走
55                         wait();
56                     }
57                 }
58                 if(++count == 10){
59                     System.out.println("生產的食品超量!");
60                     restaurant.exec.shutdownNow();
61                 }
62                 System.out.println("Order up");
63                 synchronized(restaurant.waitPerson){ //生產餐廳的菜單,這時要保證其間服務員不會介入,而後再通知服務員取菜單
64                     restaurant.meal = new Meal(count);
65                     restaurant.waitPerson.notifyAll();
66                 }
67                 TimeUnit.MILLISECONDS.sleep(100);
68             }
69         }catch(InterruptedException e){
70             System.out.println("Chef interrupted");
71         }
72     }
73 }
74 public class Restaurant {
75     Meal meal;
76     
77     ExecutorService exec = Executors.newCachedThreadPool();
78     
79     WaitPerson waitPerson = new WaitPerson(this);
80     Chef chef = new Chef(this);
81     public Restaurant(){
82         exec.execute(chef);
83         exec.execute(waitPerson);
84         
85     }
86     public static void main(String[] args){
87         new Restaurant();
88     }
89 }

 

 

運行結果:java

Order up
服務員得到菜單:Meal1
Order up
服務員得到菜單:Meal2
Order up
服務員得到菜單:Meal3
Order up
服務員得到菜單:Meal4
Order up
服務員得到菜單:Meal5
Order up
服務員得到菜單:Meal6
Order up
服務員得到菜單:Meal7
Order up
服務員得到菜單:Meal8
Order up
服務員得到菜單:Meal9
生產的食品超量!
WaitPerson interrupted
Order up
Chef interruptedide

相關文章
相關標籤/搜索