• 消费者与生产者


     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 }

    运行结果:

    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 interrupted

  • 相关阅读:
    要回家了,想到以后..
    面试应注意的问题by JohnPhilips(转自matrix论坛)
    在痛苦中坚持,翻译啊
    数据抓取和分析~~
    不考研,就很闲吗?
    在Swing中使用高级的MVC和POJOs
    测试驱动
    开始找工作了,先记个流水帐
    又是开学的时候了
    开发者想要什么算是我的翻译作品的处女作吧,呵呵,致力于英语学习..
  • 原文地址:https://www.cnblogs.com/_programmer/p/3394513.html
Copyright © 2020-2023  润新知