基于 wait/notify 实现“生产者/消费者”模式

avatar 2018年03月14日10:08:28 6 2259 views
博主分享免费Java教学视频,B站账号:Java刘哥
生产消费互相竞争,最多生产20个
  1. package thread.test;
  2. /**
  3.  * @author 言曌
  4.  * @date 2018/3/14 上午9:19
  5.  */
  6. public class ProductTest {
  7.     public static void main(String args[]) {
  8.         Product product = new Product();
  9.         new Thread(new Producer(product)).start();
  10.         new Thread(new Consumer(product)).start();
  11.     }
  12.     static class Product {
  13.         private static final int MAX_PRODUCT = 20;
  14.         private static final int MIN_PRODUCT = 0;
  15.         private int PRODUCT = 0;
  16.         synchronized public void add() {
  17.             if (this.PRODUCT >= MAX_PRODUCT) {
  18.                 try {
  19.                     System.out.println("产品已满,请稍后生产");
  20.                     wait();
  21.                 } catch (InterruptedException e) {
  22.                     e.printStackTrace();
  23.                 }
  24.                 return;
  25.             }
  26.             this.PRODUCT++;
  27.             System.out.println("生产者生产了第" + this.PRODUCT + "个产品" + ",时间:" + System.currentTimeMillis());
  28.             notifyAll();
  29.         }
  30.         synchronized public void sub() {
  31.             if (this.PRODUCT <= MIN_PRODUCT) {
  32.                 try {
  33.                     System.out.println("产品处于缺货状态");
  34.                     wait();
  35.                 } catch (InterruptedException e) {
  36.                     e.printStackTrace();
  37.                 }
  38.                 return;
  39.             }
  40.             System.out.println("消费者消费了第" + this.PRODUCT + "个产品" + ",时间:" + System.currentTimeMillis());
  41.             this.PRODUCT--;
  42.             notifyAll();
  43.         }
  44.     }
  45.     static class Producer implements Runnable {
  46.         private Product product;
  47.         public Producer(Product product) {
  48.             this.product = product;
  49.         }
  50.         @Override
  51.         public void run() {
  52.             System.out.println("生产者开始生产产品");
  53.             while (true) {
  54.                 try {
  55.                     Thread.sleep(1000);
  56.                 } catch (InterruptedException e) {
  57.                     e.printStackTrace();
  58.                 }
  59.                 product.add();
  60.             }
  61.         }
  62.     }
  63.     static class Consumer implements Runnable {
  64.         private Product product;
  65.         public Consumer(Product product) {
  66.             this.product = product;
  67.         }
  68.         @Override
  69.         public void run() {
  70.             System.out.println("消费者开始消费产品");
  71.             while (true) {
  72.                 try {
  73.                     Thread.sleep(1000);
  74.                 } catch (InterruptedException e) {
  75.                     e.printStackTrace();
  76.                 }
  77.                 product.sub();
  78.             }
  79.         }
  80.     }
  81. }
  生产一个消费一个
  1. package thread.test;
  2. /**
  3.  * @author 言曌
  4.  * @date 2018/3/14 上午9:19
  5.  */
  6. public class ProductTest2 {
  7.     public static void main(String args[]) {
  8.         Product product = new Product();
  9.         new Thread(new Producer(product)).start();
  10.         new Thread(new Consumer(product)).start();
  11.     }
  12.     static class Product {
  13.         volatile private boolean isEmpty = true;
  14.         synchronized public void add() {
  15.             while (!isEmpty) {
  16.                 try {
  17.                     System.out.println("已经有了产品,等待消费");
  18.                     wait();
  19.                 } catch (InterruptedException e) {
  20.                     e.printStackTrace();
  21.                 }
  22.             }
  23.             System.out.println("生产者生产了一个产品,时间" + System.currentTimeMillis());
  24.             isEmpty = false;
  25.             notify();
  26.         }
  27.         synchronized public void sub() {
  28.             while (isEmpty) {
  29.                 try {
  30.                     System.out.println("没有了产品,等待生产");
  31.                     wait();
  32.                 } catch (InterruptedException e) {
  33.                     e.printStackTrace();
  34.                 }
  35.             }
  36.             System.out.println("消费者消费了一个产品,时间" + System.currentTimeMillis());
  37.             isEmpty = true;
  38.             notify();
  39.         }
  40.     }
  41.     static class Producer implements Runnable {
  42.         private Product product;
  43.         public Producer(Product product) {
  44.             this.product = product;
  45.         }
  46.         @Override
  47.         synchronized public void run() {
  48.             while (true) {
  49.                 try {
  50.                     Thread.sleep(1);//强制切换线程
  51.                 } catch (InterruptedException e) {
  52.                     e.printStackTrace();
  53.                 }
  54.                 product.add();//添加一个商品
  55.             }
  56.         }
  57.     }
  58.     static class Consumer implements Runnable {
  59.         private Product product;
  60.         public Consumer(Product product) {
  61.             this.product = product;
  62.         }
  63.         @Override
  64.         synchronized public void run() {
  65.             while (true) {
  66.                 try {
  67.                     Thread.sleep(1);//强制切换线程
  68.                 } catch (InterruptedException e) {
  69.                     e.printStackTrace();
  70.                 }
  71.                 product.sub();//减少一个商品
  72.             }
  73.         }
  74.     }
  75. }
  本文地址:https://liuyanzhao.com/7757.html  
  • 微信
  • 交流学习,有偿服务
  • weinxin
  • 博客/Java交流群
  • 资源分享,问题解决,技术交流。群号:590480292
  • weinxin
avatar

发表评论

avatar 登录者:匿名
匿名评论,评论回复后会有邮件通知

  

已通过评论:0   待审核评论数:0