• 利用J.U.C中的lock和condition实现生产者消费者模式


      1 package lockTest;
      2 
      3 import java.util.LinkedHashMap;
      4 import java.util.LinkedList;
      5 import java.util.concurrent.locks.Condition;
      6 import java.util.concurrent.locks.Lock;
      7 import java.util.concurrent.locks.ReentrantLock;
      8 
      9 public class ConditionTest {
     10 
     11     private Lock lock;
     12 
     13     private LinkedList<String> buffer;
     14 
     15     private int maxSize;
     16 
     17     private Condition notfullCondition;
     18 
     19     private Condition fullCondition;
     20 
     21     public ConditionTest(int maxSize) {
     22         this.maxSize = maxSize;
     23         lock = new ReentrantLock();
     24         buffer = new LinkedList<>();
     25         this.maxSize = maxSize;
     26         notfullCondition = lock.newCondition();
     27         fullCondition = lock.newCondition();
     28     }
     29 
     30     public void produce(String goods) throws InterruptedException {
     31 
     32         lock.lock();
     33 
     34         try {
     35 
     36             while (maxSize == buffer.size()) {
     37                 notfullCondition.await();
     38                 System.out.println("工厂产能达到极限,不能继续进行生产了,停工一段时间");
     39             }
     40 
     41             //队列没有满,可以生产
     42             buffer.add(goods);
     43 
     44             System.out.println("哈哈,我生产了" + goods + ",通知消费者进行消费...");
     45             //通知消费者线程进行消费
     46             fullCondition.signalAll();
     47 
     48         } finally {
     49             lock.unlock();
     50         }
     51     }
     52 
     53     public String consume() throws InterruptedException {
     54 
     55         String goods = null;
     56         lock.lock();
     57 
     58         try {
     59 
     60             while (buffer.size() == 0) {
     61                 System.out.println("工厂的产品已经消费完了,暂时不能剁手了");
     62                 fullCondition.await();
     63             }
     64 
     65             //开始消费
     66             goods = buffer.poll();
     67             System.out.println("哈哈,我消费" + goods + ",通知工厂进行生产...");
     68             //通知生成者
     69             notfullCondition.signalAll();
     70 
     71         } finally {
     72             lock.unlock();
     73         }
     74 
     75         return goods;
     76     }
     77 
     78 
     79     public static void main(String[] args) {
     80 
     81         ConditionTest conditionTest = new ConditionTest(100);
     82 
     83         new Thread(() -> {
     84 
     85             for (int i = 0; i < 1000000; i++) {
     86                 try {
     87                     conditionTest.produce("笔记本电脑" + i);
     88                 } catch (InterruptedException e) {
     89                     e.printStackTrace();
     90                 }
     91             }
     92 
     93         }, "produce").start();
     94 
     95 
     96         new Thread(() -> {
     97 
     98             while (true) {
     99                 try {
    100                     conditionTest.consume();
    101                 } catch (InterruptedException e) {
    102                     e.printStackTrace();
    103                 }
    104             }
    105 
    106         }, "consume").start();
    107 
    108     }
    109 
    110 }
  • 相关阅读:
    Filecoin:一种去中心化的存储网络(二)
    Filecoin:一种去中心化的存储网络(一)
    HTTP
    数据结构中的查找
    剑指offer-高质量的代码
    C++中STL容器的比较
    PBFT算法的相关问题
    springmvc最全约束
    springmvc入门(一)
    spring入门(一)
  • 原文地址:https://www.cnblogs.com/java-le/p/11365216.html
Copyright © 2020-2023  润新知