多线程设计模式之——Producer-Consumer Pattern
来源:互联网 发布:url编码 java 编辑:程序博客网 时间:2024/06/04 18:55
此模式是在生产者与消费者之间加入一个“桥梁参与者”来缓冲线程之间的处理速度差。一般可以存在多个生产者与消费者,但当双方都只有一个的时候,又称为Pipe Pattern。
例子:假设有2个生产者,2个消费者,仓库里只能放4个产品。(这里的产品就是String类型的名字而已)
<script type="text/javascript"><!--google_ad_client = "ca-pub-1944176156128447";/* cnblogs 首页横幅 */google_ad_slot = "5419468456";google_ad_width = 728;google_ad_height = 90;//--></script><script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"></script>例子:假设有2个生产者,2个消费者,仓库里只能放4个产品。(这里的产品就是String类型的名字而已)
- //Storage.java
- public class Storage {
- private String[] buffer;
- private int head;//取走一个商品的位置
- private int tail;//存入一个商品的位置
- private int count;//buffer内的商品数量
- public Storage(int count){
- this.buffer = new String[count];
- this.count = 0;
- this.head = 0;
- this.tail = 0;
- }
- //这里的if警戒条件就运用了Guarded Suspension Pattern,要求不满足条件,便等待
- public synchronized void put(String goods) throws InterruptedException{
- System.out.println(Thread.currentThread().getName() + " produce the goods:" + goods);
- if(count >= buffer.length){
- System.out.println("the storage is full!");
- wait();
- }
- buffer[tail] = goods;
- tail = (tail + 1) % buffer.length;
- count++;
- notifyAll();
- }
- public synchronized String get() throws InterruptedException{
- if(count <= 0){
- System.out.println("the storage is empty!");
- wait();
- }
- String goods = buffer[head];
- head = (head + 1) % buffer.length;
- count--;
- notifyAll();
- System.out.println(Thread.currentThread().getName() + " consume the goods:" + goods );
- return goods;
- }
- }
- //ProducerThread.java
- import java.util.Random;
- public class ProducerThread extends Thread {
- private Storage storage;
- private Random random;
- private static int pid; //产品编号
- public ProducerThread(String name , Storage storage){
- super(name);
- this.storage = storage;
- this.random = new Random();
- }
- public void run(){
- try{
- for(int i=0; i<10; i++){
- Thread.sleep(random.nextInt(1000));//模拟生产时间
- String goods = getName() + " produce the goods" + nextPId();
- storage.put(goods);
- }
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- private static synchronized int nextPId(){
- return pid++;
- }
- }
- //ConsumerThread.java
- import java.util.Random;
- public class ConsumerThread extends Thread {
- private Storage storage;
- private Random random;
- public ConsumerThread(String name , Storage storage){
- super(name);
- this.random = new Random();
- this.storage = storage;
- }
- public void run(){
- try{
- while(true){
- storage.get();
- Thread.sleep(random.nextInt(2000));//模拟商品使用时间
- }
- }catch(InterruptedException e){
- e.printStackTrace();
- }
- }
- }
- //Main.java
- public class Main {
- public static void main(String[] args) {
- Storage storage = new Storage(4);
- new ProducerThread("producer_lulu01" , storage).start();
- new ProducerThread("producer_lulu02" , storage).start();
- new ConsumerThread("consumer_fang01" , storage).start();
- new ConsumerThread("consumer_fang02" , storage).start();
- }
- }
- 多线程设计模式之——Producer-Consumer Pattern
- 多线程设计模式之Producer Consumer
- Java 多线程设计模式之Producer-Consumer
- 多线程设计模式——Producer-Consumer生产者消费者模式
- java多线程设计模式之Producer-Consumer模式(一)
- java多线程设计模式之Producer-Consumer模式(二)
- Java多线程设计模式详解学习笔记七——Producer-Consumer
- producer-consumer Pattern ---java多线程编程模式(五)
- 多线程设计模式:Producer-Consumer生产者-消费者模式的C++
- Java并发编程实践笔记之—阻塞队列和生产者-消费者模式(Blocking Queues and the Producer-consumer Pattern)
- 多线程的Producer/Consumer模式
- 《java多线程设计模式 第五章Producer Consumer》
- Java线程之Producer-Consumer Pattern
- Producer-Consumer Pattern
- Producer-Consumer Pattern
- Producer-Consumer Pattern
- Producer Consumer 模式
- 多线程设计模式之——Balking Pattern
- 开启与关闭硬件加速
- HTTP 请求报头详解
- OpenGL ES系列之基本-2:初始化 GLES
- oracle游标
- nand相关
- 多线程设计模式之——Producer-Consumer Pattern
- OpenGL ES系列之提高-2:材质纹理
- 多线程设计模式之——Balking Pattern
- C++面试宝典2011版 (1)
- java线程池
- 计算机视觉、模式识别、机器学习常用牛人主页链接
- awk与shell之间的变量传递方法
- c++面试宝典(2)
- CodeStyleConventions 代码风格约定