ITPub博客

首页 > 应用开发 > Java > 生产者与消费者

生产者与消费者

原创 Java 作者:541732025 时间:2014-04-16 11:36:08 0 删除 编辑

点击(此处)折叠或打开

  1. package test.java.util.concurrent;

  2. import java.util.concurrent.ExecutorService;
  3. import java.util.concurrent.Executors;
  4. import java.util.concurrent.TimeUnit;

  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. class WaitPerson implements Runnable {
  15.     private Restaurant restaurant;

  16.     public WaitPerson(Restaurant r) {
  17.         restaurant = r;
  18.     }

  19.     public void run() {
  20.         try {
  21.             while (!Thread.interrupted()) {
  22.                 synchronized (this) {
  23.                     while (restaurant.meal == null) {
  24.                         wait();//...wait the chef to produce a meal
  25.                     }
  26.                 }
  27.                 System.out.println(\"Waitperson got \" + restaurant.meal);
  28.                 synchronized (restaurant.chef) {
  29.                     restaurant.meal = null;
  30.                     restaurant.chef.notifyAll();//ready for another
  31.                 }
  32.             }
  33.         } catch (InterruptedException e) {
  34.             System.out.println(\"Waitperson interrupted\");
  35.         }
  36.     }
  37. }

  38. class Chef implements Runnable {
  39.     private Restaurant restaurant;
  40.     private int count = 0;

  41.     public Chef(Restaurant r) {
  42.         restaurant = r;
  43.     }

  44.     public void run() {
  45.         try {
  46.             while (!Thread.interrupted()) {
  47.                 synchronized (this) {
  48.                     while (restaurant.meal != null) {
  49.                         wait();//...for the meal to be taken
  50.                     }
  51.                 }
  52.                 if (++count == 10) {
  53.                     System.out.println(\"Out of food,closing\");
  54.                     restaurant.exec.shutdownNow();
  55.                 }
  56.                 System.out.println(\"Order up! \");
  57.                 synchronized (restaurant.waitPerson) {
  58.                     restaurant.meal = new Meal(count);
  59.                     restaurant.waitPerson.notifyAll();
  60.                 }
  61.                 TimeUnit.MILLISECONDS.sleep(100);
  62.             }
  63.         } catch (InterruptedException e) {
  64.             System.out.println(\"Chef interrupted\");
  65.         }
  66.     }
  67. }


  68. public class Restaurant {
  69.     Meal meal;
  70.     WaitPerson waitPerson = new WaitPerson(this);
  71.     Chef chef = new Chef(this);
  72.     ExecutorService exec = Executors.newCachedThreadPool();

  73.     public Restaurant() {
  74.         exec.execute(chef);
  75.         exec.execute(waitPerson);
  76.     }

  77.     public static void main(String[] args) {
  78.         new Restaurant();
  79.     }
  80. }


来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/28912557/viewspace-1143088/ ,如需转载,请注明出处,否则将追究法律责任。

上一篇: Java实现归并排序
下一篇: MySQL水平切分
请登录后发表评论 登录
全部评论

注册时间:2013-05-23

  • 博文量
    127
  • 访问量
    515727