Java生产者与消费者模式的简单写法
来源:互联网 发布:python可以建网站吗 编辑:程序博客网 时间:2024/05/19 01:14
生产者消费者模式是研究多线程程序的经典问题之一,它描述是有一块缓冲区作为缓存队列/仓库,生产者可以将产品放入队列,消费者则可以从队列中取走产品。大多数的后台服务程序并发控制的基本原理都可以归纳为生产者消费者模式。
1、使用Synchronized()、wait() 、 notify()、notifyAll()方法实现:
package proAndCsmModel01;import java.util.LinkedList;/** * 实现缓冲区 * */public class Resource01 { //最大缓冲区 private final int MAX_SIZE = 10; //缓冲区队列 LinkedList<datatype> list = new LinkedList<>(); /** * 生产资料同步方法 */ public synchronized void increaseData(){ while (list.size() >= MAX_SIZE){ try { System.out.println(Thread.currentThread().getId()+"资料仓库已满!"); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } datatype d = new datatype(); d.setData((int) (Math.random()*1000)); list.add(d); System.out.println(Thread.currentThread().getId()+"生产:"+d.getData()+" 库存量:"+list.size()); notifyAll(); } /** * 消费资料同步方法 */ public synchronized void decreaseData(){ while (list.size() <= 0){ try { System.out.println(Thread.currentThread().getId()+"资料仓库为空!"); wait(); } catch (InterruptedException e) { e.printStackTrace(); } } datatype d = list.poll(); System.out.println(Thread.currentThread().getId()+"消费:"+d.getData()+" 库存量:"+list.size()); notifyAll(); }}package proAndCsmModel01;/** * 生产者:生产资料 * */public class producer01 implements Runnable { private Resource01 resource01; producer01(Resource01 resource01){ this.resource01 = resource01; } @Override public void run() { while (true){ try { //随机休眠后在生产资料 Thread.sleep((long) (Math.random()*1000)); } catch (InterruptedException e) { e.printStackTrace(); } //调用同步方法进行生产资料 resource01.increaseData(); } }}package proAndCsmModel01;/** * 消费者:消费资料 * */public class consumer01 implements Runnable { private Resource01 resource01; consumer01(Resource01 resource01){ this.resource01 = resource01; } @Override public void run() { while (true){ try { //随机休眠后再消费 Thread.sleep((long) (Math.random()*1000)); } catch (InterruptedException e) { e.printStackTrace(); } //调用同步方法进行消费资料 resource01.decreaseData(); } }}package proAndCsmModel01;/** * 基本数据类型 * */public class datatype { private int data; public void setData(int data) { this.data = data; } public int getData() { return data; }}package proAndCsmModel01;/** * 调用 * */public class test { public static void main(String agrs[]){ Resource01 resource01 = new Resource01();; System.out.println(Thread.currentThread().getName()); new Thread(new producer01(resource01)).start(); new Thread(new producer01(resource01)).start(); new Thread(new producer01(resource01)).start(); new Thread(new producer01(resource01)).start(); new Thread(new producer01(resource01)).start(); new Thread(new producer01(resource01)).start(); new Thread(new consumer01(resource01)).start(); new Thread(new consumer01(resource01)).start(); new Thread(new consumer01(resource01)).start(); }}
2、ReentrantLock、await() 、signal()、signalAll()方法实现:
package exampletest.proAndCsmModel01;import java.util.LinkedList;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 实现缓冲区 * */public class Resource02 { //最大缓冲区 private final int MAX_SIZE = 10; //缓冲区队列 LinkedList<datatype> list = new LinkedList<>(); //同步锁和条件 private Lock lock = new ReentrantLock(); private Condition full_cd = lock.newCondition(); private Condition empty_cd = lock.newCondition(); /** * 生产资料同步方法 */ public void increaseData(){ lock.lock(); while (list.size() >= MAX_SIZE){ try { System.out.println(Thread.currentThread().getId()+"资料仓库已满!"); full_cd.await(); } catch (InterruptedException e) { e.printStackTrace(); } } datatype d = new datatype(); d.setData((int) (Math.random()*1000)); list.add(d); System.out.println(Thread.currentThread().getId()+"生产:"+d.getData()+" 库存量:"+list.size()); empty_cd.signalAll(); lock.unlock(); } /** * 消费资料同步方法 */ public void decreaseData(){ lock.lock(); while (list.size() <= 0){ try { System.out.println(Thread.currentThread().getId()+"资料仓库为空!"); empty_cd.await(); } catch (InterruptedException e) { e.printStackTrace(); } } datatype d = list.poll(); System.out.println(Thread.currentThread().getId()+"消费:"+d.getData()+" 库存量:"+list.size()); full_cd.signalAll(); lock.unlock(); }}
3、使用BlockingQueue阻塞队列方法实现
package exampletest.proAndCsmModel01;import java.util.LinkedList;import java.util.concurrent.LinkedBlockingDeque;import java.util.concurrent.locks.Condition;import java.util.concurrent.locks.Lock;import java.util.concurrent.locks.ReentrantLock;/** * 实现缓冲区 * */public class Resource03 extends Resource{ //最大缓冲区 private final int MAX_SIZE = 10; //缓冲区队列 LinkedBlockingDeque<datatype> list = new LinkedBlockingDeque<>(MAX_SIZE); /** * 生产资料同步方法 */ @Override public void increaseData(){ if (list.size() >= MAX_SIZE){ System.out.println(Thread.currentThread().getId()+"资料仓库已满!"); } datatype d = new datatype(); d.setData((int) (Math.random()*1000)); try { list.put(d); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getId()+"生产:"+d.getData()+" 库存量:"+list.size()); } /** * 消费资料同步方法 */ @Override public void decreaseData(){ if (list.size() <= 0){ System.out.println(Thread.currentThread().getId()+"资料仓库为空!"); } datatype d = null; try { d = list.take(); } catch (InterruptedException e) { e.printStackTrace(); } System.out.println(Thread.currentThread().getId()+"消费:"+d.getData()+" 库存量:"+list.size()); }}
阅读全文
0 0
- Java生产者与消费者模式的简单写法
- 简单的生产者消费者模式java实现
- Java简单的生产者与消费者实现
- JAVA 生产者与消费者模式
- wait与notifyAll实现简单的生产者消费者模式
- java:一个生产者消费者模式的简单实现
- java 编码实现简单的生产者-消费者模式
- Java多线程实现简单生产者消费者模式
- java多线程之生产者与消费者问题的简单模拟
- 自己用Java写的简单生产者与消费者模型
- java多线程之生产者与消费者问题的简单模拟
- 设计模式-生产者与消费者Java实现
- java线程通信 生产者与消费者模式
- java线程之生产者与消费者模式
- 消费者与生产者模式
- 消费者与生产者模式
- 消费者与生产者模式
- 生产者与消费者模式
- NYOJ 15 括号匹配(二) POJ 2955 Brackets(区间dp)
- lucence基础知识
- 使用gethostnamebyname获取百度官方域名及ip地址
- C#--WinForm基于泛型集合动态填充下拉框
- Linux下Memcache服务器端的安装
- Java生产者与消费者模式的简单写法
- c的部分算术运算符和算术表达式:
- DES算法的实现
- 【深入PHP 面向对象】读书笔记(八)
- 数据库的锁机制
- leetcode之链表逆序翻转类-----92/206 逆序 24/25/61/143 按规则翻转 86/234 双指针分治 19/82/83/203 按规则删除
- POJ 2965--The Pilots Brothers' refrigerator
- 《java基础与案例开发详解》(四)
- Mac下Nginx安装环境配置详解