生产者消费者模式的三种实现方式
2019-01-06 14:06
85 查看
原作者:老铁123
出处:https://blog.csdn.net/qewgd/article/details/85926275
本文归作者【老铁123】和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
生产者消费者模式
1、生产者只在仓库未满时进行生产,仓库满时生产者进程被阻塞;
2、消费者只在仓库非空时进行消费,仓库为空时消费者进程被阻塞;
一、synchronized方式实现
代码如下
**ProduceAndConsumeBySynchronized.java** public class ProduceAndConsumeBySynchronized{ // TODO多线程调用 } class StoreWN { public int capacity = 10; public int index = 0; public synchronized void produce() { while (index >= capacity) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } index++; notifyAll(); } public synchronized void consume() { while (index <= 0) { try { wait(); } catch (InterruptedException e) { e.printStackTrace(); } } index--; notifyAll(); } } class Producer implements Runnable { StoreWN swn = null; public Producer(StoreWN swn) { this.swn = swn; } public void run() { swn.produce(); } } class Consumer implements Runnable { StoreWN swn = null; public Consumer(StoreWN swn) { this.swn = swn; } public void run() { swn.consume(); } }
二、Lock API方式实现
代码如下
**ProduceAndConsumeByLockApi.java** import java.util.concurrent.locks.Condition; import java.util.concurrent.locks.Lock; import java.util.concurrent.locks.ReentrantLock; public class ProduceAndConsumeByLockApi { // TODO多线程调用 } class StoreAS { public int capacity = 10; public int index = 0; Lock lock = new ReentrantLock(); Condition full = lock.newCondition(); Condition empty = lock.newCondition(); public void produce() { lock.lock(); try { while (index >= capacity) { try { full.await(); } catch (InterruptedException e) { e.printStackTrace(); } } index++; empty.signalAll(); } finally { lock.unlock(); } } public void consume() { lock.lock(); try { while (index <= capacity) { try { empty.await(); } catch (InterruptedException e) { e.printStackTrace(); } } index--; full.signalAll(); } finally { lock.unlock(); } } } class ProducerAS implements Runnable { StoreAS swn = null; public ProducerAS(StoreAS swn) { this.swn = swn; } public void run() { swn.produce(); } } class ConsumerAS implements Runnable { StoreAS swn = null; public ConsumerAS(StoreAS swn) { this.swn = swn; } public void run() { swn.consume(); } }
三、阻塞队列方式实现(底层就是实现方式二)
代码如下
**ProduceAndConsumeByBlockingQueue.java** import java.util.concurrent.ArrayBlockingQueue; import java.util.concurrent.BlockingQueue; public class ProduceAndConsumeByBlockingQueue { // TODO多线程调用 } class StoreBQ { BlockingQueue<Integer> bq = new ArrayBlockingQueue<Integer>(10); public void produce(){ try { bq.put(1); } catch (InterruptedException e) { } } public void consume(){ try { bq.take(); } catch (InterruptedException e) { } } } class ProducerBQ implements Runnable { StoreBQ swn = null; public ProducerBQ(StoreBQ swn) { this.swn = swn; } public void run() { swn.produce(); } } class ConsumerBQ implements Runnable { StoreBQ swn = null; public ConsumerBQ(StoreBQ swn) { this.swn = swn; } public void run() { swn.consume(); } }
相关文章推荐
- 生产者消费者模式的三种实现方式
- webservice 三种实现方式
- 实现下拉列表,点击其他位置自动隐藏效果的三种方式比较
- 行转列的三种实现方式
- java多线程实现的三种方式
- 单点登陆的三种实现方式
- php实现递归的三种基本方式及其说明
- JavaScript面向对象(三)——继承与闭包、JS实现继承的三种方式
- 总结用三种方式实现类似MSN的消息弹出框
- HTML5实现动画三种方式
- 使用javascript实现在页面打印的效果的三种方式
- Volley-XUtils-OkHttp三种方式实现单张多张图片上传
- 实现Servlet的三种方式
- 模拟淘宝侧边服务模块鼠标悬停效果的三种实现方式总结
- 详解三种C#实现数组反转方式
- JAVA多线程的三种实现方式
- JAVA多线程的三种实现方式
- 【转载】spring aop 三种实现方式示例
- APP页面的三种基本实现方式:storyboard,xib,代码实现 与自动布局
- java三种方式实现键盘录入