生产者消费者ReentrantLock实现以及BlockingQueue实现
生活随笔
收集整理的這篇文章主要介紹了
生产者消费者ReentrantLock实现以及BlockingQueue实现
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
1.ReentrantLock實現(xiàn)
/*** 描述: 一個初始值為0的變量,兩個線程對其交替操作,一個加一個減** @author xinjiao.yu@marketin.cn* @create 2020/2/22 11:55* @since 2.16.3*/ class ShareData{private int number = 0;private Lock lock = new ReentrantLock();private Condition condition = lock.newCondition();public void increment() throws Exception{lock.lock();try{// 1判斷while(number != 0){// 等待,不能生產(chǎn)condition.await();}number++;System.out.println(Thread.currentThread().getName()+"\t"+number);// 通知喚醒condition.signalAll();}catch (Exception e){e.printStackTrace();}finally {lock.unlock();}}public void decrement() throws Exception{lock.lock();try{// 1判斷while(number == 0){// 等待,不能生產(chǎn)condition.await();}number--;System.out.println(Thread.currentThread().getName()+"\t"+number);// 通知喚醒condition.signalAll();}catch (Exception e){e.printStackTrace();}finally {lock.unlock();}} } public class ProConsumer_ReentrantLockDemo {public static void main(String[] args) {ShareData shareData = new ShareData();new Thread(()->{for(int i=1;i<=5;i++){try{shareData.increment();}catch (Exception e){e.printStackTrace();}}},"AA").start();new Thread(()->{for(int i=1;i<=5;i++){try{shareData.decrement();}catch (Exception e){e.printStackTrace();}}},"BB").start();} }2.BlockingQueue實現(xiàn)
class MyResource{private volatile boolean FLAG = true;private AtomicInteger atomicInteger = new AtomicInteger();private BlockingQueue<String> blockingQueue = null;// 構(gòu)造方法注入public MyResource(BlockingQueue<String> blockingQueue){this.blockingQueue = blockingQueue;System.out.println(blockingQueue.getClass().getName());}public void myProd() throws Exception{String data = null;boolean retValue;while (FLAG){data = atomicInteger.incrementAndGet()+"";retValue = blockingQueue.offer(data,2L, TimeUnit.SECONDS);if(retValue){System.out.println(Thread.currentThread().getName()+"\t 插入隊列"+data+"成功");}else {System.out.println(Thread.currentThread().getName()+"\t 插入隊列"+data+"失敗");}TimeUnit.SECONDS.sleep(1);}System.out.println(Thread.currentThread().getName()+"\t 大老板叫停了,表示FLAG=false,生產(chǎn)動作結(jié)束");}public void myConsumer() throws Exception{String result = null;while (FLAG){result = blockingQueue.poll(2L,TimeUnit.SECONDS);if(null == result || result.equalsIgnoreCase("")){FLAG = false;System.out.println(Thread.currentThread().getName()+"\t 超過2秒鐘沒有取到蛋糕,消費退出");System.out.println();return;}System.out.println(Thread.currentThread().getName()+"\t 消費隊列"+result+"成功");}}public void myStop() throws Exception{this.FLAG=false;} } public class ProConsumer_BlockQueueDemo {public static void main(String[] args) throws Exception{MyResource myResource = new MyResource(new ArrayBlockingQueue<>(3));new Thread(()->{System.out.println(Thread.currentThread().getName()+"\t 生產(chǎn)者線程啟動");try {myResource.myProd();} catch (Exception e) {e.printStackTrace();}},"Prod").start();new Thread(()->{System.out.println(Thread.currentThread().getName()+"\t 消費者線程啟動");try {myResource.myConsumer();} catch (Exception e) {e.printStackTrace();}},"Consumer").start();// 暫停一會線程TimeUnit.SECONDS.sleep(5);System.out.println();System.out.println();System.out.println("5秒鐘時間到,大老板main線程叫停,活動結(jié)束");myResource.myStop();} }?
?
總結(jié)
以上是生活随笔為你收集整理的生产者消费者ReentrantLock实现以及BlockingQueue实现的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Lambda表达式用法超详细整理!!!
- 下一篇: 微服务系统错误码设计