zoukankan      html  css  js  c++  java
  • 生产者消费者模式案例

    该例子利用多线程实现生产者消费者模型。

    两个线程:

    • 生产者;
    • 消费者。

    实现的功能:

    • 生产者生产满,通知消费者去消费;
    • 仓库没有产品,通知生产者去生产。

    为了使代码可读性强,清晰易懂,楼主建了四个Java类:

    1.EventStorage:仓储模型,用于定义生产和消费的方法;

    2.Producer:生产者;

    3.Consumer:消费者;

    4.TestThread:用于测试。

     1 /**
     2  * 仓储模型
     3  */
     4 public class EventStorage {
     5     
     6     //用于打印时间
     7     private List<Date> storage;
     8     
     9     //最大生产数量
    10     private int maxSize;
    11     
    12     public EventStorage() {
    13         
    14         maxSize = 10;
    15         storage = new LinkedList<Date>();
    16     }
    17     
    18     //模拟生产动作
    19     public synchronized void set() {
    20         while (storage.size() == maxSize) {
    21             try {
    22                 wait();//如果生产满,通知生产者等待
    23             } catch (InterruptedException e) {
    24                 e.printStackTrace();
    25             }
    26         }
    27         
    28         storage.add(new Date());
    29         System.out.println("生产: "+ storage.size());
    30         notifyAll();//唤醒在等待操作资源的线程(队列)
    31     }
    32     
    33     //模拟消费动作
    34     public synchronized void get() {
    35         while (storage.size() == 0) {
    36             try {
    37                 wait();//如果为空,通知消费者等待
    38             } catch (InterruptedException e) {
    39                 e.printStackTrace();
    40             }
    41         }
    42         
    43         System.out.println("剩余: "+ storage.size()+ ",Time: " +((LinkedList<?>)storage).poll());  
    44         notifyAll();//唤醒在等待操作资源的线程(队列)  
    45     }    
    46 }
     1 /**
     2  * 生产者
     3  * @author ycchia
     4  *
     5  */
     6 public class Producer implements Runnable {
     7     
     8     //仓储模型
     9     private EventStorage storage;
    10 
    11     //构造函数
    12     public Producer(EventStorage storage) {
    13         this.storage = storage;
    14     }
    15 
    16     @Override
    17     public void run() {
    18         for (int i = 0; i < 100; i++) {
    19             //调用生产方法
    20             storage.set();
    21         }
    22     }
    23 }
     1 /**
     2  * 消费者
     3  * @author ycchia
     4  *
     5  */
     6 public class Consumer implements Runnable {
     7 
     8     //仓储模型
     9     private EventStorage storage;
    10     
    11     //构造函数
    12     public Consumer(EventStorage storage) {
    13         this.storage = storage;
    14     }
    15     
    16     @Override
    17     public void run() {
    18         for (int i = 0; i < 100; i++) {
    19             //调用消费方法
    20             storage.get();
    21         }
    22     }
    23 }
     1 public class TestThread {
     2 
     3     public static void main(String[] args) {
     4         
     5         EventStorage storage = new EventStorage();
     6         Producer producer = new Producer(storage);
     7         //生产者线程
     8         Thread t1 = new Thread(producer);
     9         Consumer consumer = new Consumer(storage);
    10         //消费者线程
    11         Thread t2 = new Thread(consumer);
    12 
    13         //启动线程
    14         t2.start();
    15         t1.start();
    16     }
    17 }

    启动程序,console信息如下:

    完美实现多线程同步。快去试一下吧!

  • 相关阅读:
    SQL2005 镜像配置
    子窗体关闭程序
    asp.net 输出微信自定义菜单json
    教是最好的学
    人为什么要努力?
    《雪国列车》制度与自由
    时间记录APP———Time Meter
    饭饭
    Android编译程序报错:Re-installation failed due to different application signatures.
    我的GTD起步
  • 原文地址:https://www.cnblogs.com/lingluo2017/p/7196860.html
Copyright © 2011-2022 走看看