使用Lock锁生产者消费者模式

Posted fliay

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用Lock锁生产者消费者模式相关的知识,希望对你有一定的参考价值。

  1 package com.java.concurrent;
  2 
  3 import java.util.concurrent.locks.Condition;
  4 import java.util.concurrent.locks.Lock;
  5 import java.util.concurrent.locks.ReentrantLock;
  6 
  7 /**
  8  * 生产者消费者模式
  9  * @author fliay
 10  *
 11  */
 12 public class TestProductorAndConsumerByLock {
 13 
 14     public static void main(String[] args) {
 15         ClerkByLock c = new ClerkByLock();
 16         ProductorByLock pro = new ProductorByLock(c);
 17         ConsumerByLock con = new ConsumerByLock(c);
 18         new Thread(pro,"生产者A").start();
 19         new Thread(con,"消费者B").start();
 20         new Thread(pro,"生产者C").start();
 21         new Thread(con,"消费者D").start();
 22     }
 23     
 24     
 25     
 26 
 27 }
 28 
 29 class  ClerkByLock{
 30     //初始化产品
 31     private int product = 0;
 32     //定义一个Lock锁对象
 33     private Lock lock = new ReentrantLock();
 34     //创建condition对象
 35     private Condition condition = lock.newCondition();
 36     
 37     
 38     //进货
 39     public  void get(){
 40         lock.lock();
 41         
 42         try{
 43             while(product>=10){
 44                 System.out.println("产品已满!");
 45                 try {
 46                     //使用condition进行线程等待
 47                     condition.await();
 48                 } catch (InterruptedException e) {
 49                     // TODO Auto-generated catch block
 50                     e.printStackTrace();
 51                 }
 52             }
 53             condition.signalAll();
 54             System.out.println(Thread.currentThread().getName()+":"+ ++product);
 55         }finally{
 56             //始终会解锁
 57             lock.unlock();
 58         }
 59         
 60         
 61         
 62         
 63     }
 64     
 65     //卖货
 66     public  void sale(){
 67         lock.lock();
 68         try{
 69             
 70             while(product<=0){
 71                 System.out.println("补货中!");
 72                 try {
 73                     condition.await();
 74                 } catch (InterruptedException e) {
 75                     // TODO Auto-generated catch block
 76                     e.printStackTrace();
 77                 }
 78             }
 79             condition.signalAll();
 80             System.out.println(Thread.currentThread().getName()+":"+ --product);
 81         }finally{
 82             lock.unlock();
 83         }
 84         
 85         
 86     }
 87 }
 88 
 89 class ProductorByLock implements Runnable{
 90     
 91     private ClerkByLock clerk;
 92     
 93     
 94     public ProductorByLock(ClerkByLock clerk) {
 95         this.clerk = clerk;
 96     }
 97 
 98 
 99 
100     public void run() {
101         for(int i=0;i<20;i++){
102             clerk.get();
103         }
104     }
105 }
106 
107 
108 class ConsumerByLock implements Runnable{
109     
110     private ClerkByLock clerk;
111     
112     
113     
114     public ConsumerByLock(ClerkByLock clerk) {
115         this.clerk = clerk;
116     }
117 
118 
119 
120     public void run() {
121         for(int i=0;i<20;i++){
122             clerk.sale();
123         }
124     }
125 }

 

以上是关于使用Lock锁生产者消费者模式的主要内容,如果未能解决你的问题,请参考以下文章

Lock版本生产者和消费者模式

多线程--简单生产者与消费者(Lock锁)

Java多线程-Lock锁的使用,以及生产者和消费者的实现

python 并发编程 锁 / 信号量 / 事件 / 队列(进程间通信(IPC)) /生产者消费者模式

python 多线程笔记-- 生产者/消费者模式

python Lock锁