多线程设计模式之——Producer-Consumer Pattern

来源:互联网 发布:url编码 java 编辑:程序博客网 时间:2024/06/04 18:55
此模式是在生产者与消费者之间加入一个“桥梁参与者”来缓冲线程之间的处理速度差。一般可以存在多个生产者与消费者,但当双方都只有一个的时候,又称为Pipe Pattern。 

例子:假设有2个生产者,2个消费者,仓库里只能放4个产品。(这里的产品就是String类型的名字而已) 


Java代码  收藏代码
  1. //Storage.java  
  2. public class Storage {  
  3.     private String[] buffer;  
  4.     private int head;//取走一个商品的位置  
  5.     private int tail;//存入一个商品的位置  
  6.     private int count;//buffer内的商品数量  
  7.       
  8.     public Storage(int count){  
  9.         this.buffer = new String[count];  
  10.         this.count = 0;  
  11.         this.head = 0;  
  12.         this.tail = 0;  
  13.     }  
  14.       
  15.     //这里的if警戒条件就运用了Guarded Suspension Pattern,要求不满足条件,便等待  
  16.     public synchronized void put(String goods) throws InterruptedException{  
  17.         System.out.println(Thread.currentThread().getName() + " produce the goods:" + goods);  
  18.         if(count >= buffer.length){  
  19.             System.out.println("the storage is full!");  
  20.             wait();  
  21.         }  
  22.         buffer[tail] = goods;  
  23.         tail = (tail + 1) % buffer.length;  
  24.         count++;  
  25.         notifyAll();  
  26.     }  
  27.       
  28.     public synchronized String get() throws InterruptedException{  
  29.         if(count <= 0){  
  30.             System.out.println("the storage is empty!");  
  31.             wait();  
  32.         }  
  33.         String goods = buffer[head];  
  34.         head = (head + 1) % buffer.length;  
  35.         count--;  
  36.         notifyAll();  
  37.         System.out.println(Thread.currentThread().getName() + " consume the goods:" + goods );  
  38.         return goods;  
  39.     }  
  40.   
  41. }  




Java代码  收藏代码
  1. //ProducerThread.java  
  2. import java.util.Random;  
  3.   
  4.   
  5. public class ProducerThread extends Thread {  
  6.     private Storage storage;  
  7.     private Random random;  
  8.     private static int pid;  //产品编号  
  9.       
  10.     public ProducerThread(String name , Storage storage){  
  11.         super(name);  
  12.         this.storage = storage;  
  13.         this.random = new Random();  
  14.     }  
  15.       
  16.     public void run(){  
  17.         try{  
  18.             for(int i=0; i<10; i++){  
  19.                 Thread.sleep(random.nextInt(1000));//模拟生产时间  
  20.                 String goods = getName() + " produce the goods" + nextPId();  
  21.                 storage.put(goods);  
  22.             }  
  23.         }catch(InterruptedException e){  
  24.             e.printStackTrace();  
  25.         }  
  26.     }  
  27.       
  28.     private static synchronized int nextPId(){  
  29.         return pid++;  
  30.     }  
  31.   
  32. }  




Java代码  收藏代码
  1. //ConsumerThread.java  
  2. import java.util.Random;  
  3.   
  4.   
  5. public class ConsumerThread extends Thread {  
  6.     private Storage storage;  
  7.     private Random random;  
  8.       
  9.     public ConsumerThread(String name , Storage storage){  
  10.         super(name);  
  11.         this.random = new Random();  
  12.         this.storage = storage;  
  13.     }  
  14.       
  15.     public void run(){  
  16.         try{  
  17.             while(true){  
  18.                 storage.get();  
  19.                 Thread.sleep(random.nextInt(2000));//模拟商品使用时间  
  20.             }  
  21.         }catch(InterruptedException e){  
  22.             e.printStackTrace();  
  23.         }  
  24.     }  
  25.   
  26. }  




Java代码  收藏代码
  1. //Main.java  
  2. public class Main {  
  3.   
  4.     public static void main(String[] args) {  
  5.         Storage storage = new Storage(4);  
  6.         new ProducerThread("producer_lulu01" , storage).start();  
  7.         new ProducerThread("producer_lulu02" , storage).start();  
  8.         new ConsumerThread("consumer_fang01" , storage).start();  
  9.         new ConsumerThread("consumer_fang02" , storage).start();  
  10.           
  11.   
  12.     }  
  13.   
  14. }  
<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>