3种消费者生产者实现方式 {signal, blockingQueue, synchronized }
来源:互联网 发布:linux自启动脚本编写 编辑:程序博客网 时间:2024/05/01 17:27
经典的消费者生产者问题:一、利用阻塞队列实现:这种方式最为简单/** * Created with IntelliJ IDEA. * User: yujj * Date: 2/14/14 * Time: 5:27 PM * To change this template use File | Settings | File Templates. */ public class App { public static void main(String[] args) throws FileNotFoundException { new App().run(); } public void run() { ExecutorService service = Executors.newCachedThreadPool(); BlockingDeque<Integer> blockingDeque = new LinkedBlockingDeque<Integer>(10); service.submit(new Consumer(blockingDeque)); service.submit(new Producer(blockingDeque)); service.shutdown(); } static class Consumer implements Runnable { private BlockingDeque<Integer> blockingDeque; Consumer(BlockingDeque<Integer> blockingDeque) { this.blockingDeque = blockingDeque; } @Override public void run() { while (true) { try { System.out.println("consumer: " + blockingDeque.take()); } catch (InterruptedException e) { e.printStackTrace(); } } } } static class Producer implements Runnable { private BlockingDeque<Integer> blockingDeque; private Integer count = 0; Producer(BlockingDeque<Integer> blockingDeque) { this.blockingDeque = blockingDeque; } @Override public void run() { while (true) { try { count++; System.out.println("producer: " + count); blockingDeque.put(count); Thread.sleep(1000); } catch (InterruptedException e) { e.printStackTrace(); } } } }}二、利用synchronized实现/** * Created with IntelliJ IDEA. * User: yujj * Date: 2/14/14 * Time: 7:32 PM * To change this template use File | Settings | File Templates. */public class App { private static final Object SYNFLAG = new Object(); public static List<Integer> integerList = new ArrayList<Integer>(); public static void main(String[] args) { App app = new App(); app.run(); } public void run() { ExecutorService executorService = Executors.newCachedThreadPool(); for (Integer i = 0; i < 10; i++) { Producer producer = new Producer("producer" + i); Consumer consumer = new Consumer("consumer" + i); executorService.submit(producer); executorService.submit(consumer); } executorService.shutdown(); } class Producer implements Runnable { private String name; Producer(String name) { this.name = name; } @Override public void run() { while (true) { synchronized (SYNFLAG) { if (integerList.size() < 3) { Integer integer = integerList.size(); integerList.add(integer); System.out.println(name + " insert " + integer); } } } } } class Consumer implements Runnable { private String name; Consumer(String name) { this.name = name; } @Override public void run() { while (true) { synchronized (SYNFLAG) { if (integerList.size() > 0) { Integer integer = integerList.remove(0); System.out.println(name + " remove " + integer); } } } } }}
三、利用Condition实现, 这种方式稍微复杂一点,需要借助 ReentrantLock 封装一个阻塞队列
首先实现模拟阻塞队列封装:/** * Created with IntelliJ IDEA. * User: yujj * Date: 2/15/14 * Time: 3:37 PM * To change this template use File | Settings | File Templates. */public class BoundedBuffer { private final Lock lock = new ReentrantLock(); private final Condition full = lock.newCondition(); private final Condition empty = lock.newCondition(); private Integer capacity = 0; private List<Integer> integerList = new ArrayList<Integer>(); public BoundedBuffer(Integer capacity) { this.capacity = capacity; } public void put(Integer integer) throws InterruptedException { try { lock.lock(); while (integerList.size() >= capacity) { full.await(); } integerList.add(integer); empty.signal(); System.out.println("insert " + integer); } finally { lock.unlock(); } } public Integer take() throws InterruptedException { Integer toBeReturn = null; try { lock.lock(); while (integerList.size() < 1) { empty.await(); } toBeReturn = integerList.remove(0); full.signal(); } finally { lock.unlock(); } return toBeReturn; }}
消费者、生产者部分:
/** * Created with IntelliJ IDEA. * User: yujj * Date: 2/14/14 * Time: 10:49 PM * To change this template use File | Settings | File Templates. */public class App { private static final Object object = new Object(); private static BoundedBuffer storage = new BoundedBuffer(1); public static void main(String[] args) { new App().run(); } public void run() { ExecutorService service = Executors.newCachedThreadPool(); for (Integer i = 0; i < 100; i++) { Producer producer = new Producer("xxx", 1); Consumer consumer = new Consumer("xxx"); service.submit(producer); service.submit(consumer); } } class Producer implements Runnable { AtomicInteger atomicInteger = new AtomicInteger(10); private String name; private Integer putId; Producer(String name, Integer putId) { this.name = name; this.putId = putId; } @Override public void run() { while (true) { putId += 1; try { storage.put(putId); } catch (InterruptedException e) { e.printStackTrace(); } } } } class Consumer implements Runnable { private String name; Consumer(String name) { this.name = name; } @Override public void run() { while (true) { try { System.out.println("take " + storage.take()); } catch (InterruptedException e) { e.printStackTrace(); } } } }}
0 0
- 3种消费者生产者实现方式 {signal, blockingQueue, synchronized }
- synchronized方式实现生产者-消费者模式
- 生产者,消费者的BlockingQueue实现
- 利用BlockingQueue实现生产者消费者
- BlockingQueue实现生产者、消费者模式
- BlockingQueue实现生产者消费者模式
- synchronized 实现生产者消费者问题
- 基于BlockingQueue的生产者消费者模式实现
- Java实现生产者消费者模型:多线程+BlockingQueue
- 使用BlockingQueue实现生产者消费者模式
- 使用Java的BlockingQueue实现生产者-消费者
- Java使用BlockingQueue实现生产者消费者模式
- 使用Java的BlockingQueue实现生产者-消费者
- 使用Java的BlockingQueue实现生产者-消费者
- 生产者/消费者可以参考BlockingQueue实现
- 使用Java的BlockingQueue实现生产者-消费者
- 使用阻塞队列BlockingQueue实现生产者消费者
- 使用BlockingQueue实现生产者和消费者模型
- C语言中 获取当前绝对路径
- 云计算读书笔记(一)之Google云计算技术GFS
- 问题描述: Android SDK Manager 无法下载更新,或者更新速度超慢,或者待安装包列表不显示 解决方法: 第一步:修改hosts文件 修改后的hosts 文件内容为
- 【BFS&&优先队列】HDU 1180 诡异的楼梯
- "app_name" is not translated in af, am, ar, bg, ca, cs, da, de, el, en-rGB,
- 3种消费者生产者实现方式 {signal, blockingQueue, synchronized }
- android如果重写onDraw实现一个类似TextView可以显示表情和链接的控件(一)
- 快速获取联系人数据
- 机房重构总结
- 游戏玩到这种程度,酱紫真的好吗?
- const
- poj 2942&&uva 3523 Knights of the Round Table(图的连通性)
- 我读C++ Primer - 基本类型与变量
- Clojure笔记——apply