用BlockingQueue实现一个简单的生产者-消费者模型

  1. package kevin;  
  2.   
  3. import java.util.concurrent.ArrayBlockingQueue;  
  4. import java.util.concurrent.BlockingQueue;  
  5. import java.util.concurrent.TimeUnit;  
  6.   
  7. /** 
  8.  * 一个简单的生产者-消费者demo 
  9.  *  
  10.  * @author KevinJom 
  11.  */  
  12. public class BlockingQueueDemo {  
  13.     public static void main(String[] args) {  
  14.         new BlockingQueueDemo().go();  
  15.     }  
  16.   
  17.     private void go() {  
  18.         // 这里简单的说一下BlockingQueue的实现,它基于生产者-消费者模式,其中有两个重要的阻塞方法  
  19.         // put()和take(),而这两个方法的实现用到了Lock和Condition,具体实现请参考API  
  20.         BlockingQueue<String> queue = new ArrayBlockingQueue<String>(10);  
  21.         Thread t1 = new Thread(new Producer(queue, 500"peak")); // 生产者线程,来,生产一些i  
  22.                                                                     // can  
  23.                                                                     // play吧,并且要比nike生产的快  
  24.         Thread t2 = new Thread(new Producer(queue, 1000"nike")); // 第二个生产者线程  
  25.         Thread t3 = new Thread(new Customer(queue)); // 消费者线程  
  26.         t1.start();  
  27.         t2.start();  
  28.         t3.start();  
  29.     }  
  30.   
  31.     private class Producer implements Runnable {  
  32.         private BlockingQueue<String> queue;  
  33.         private int timeout; // 生产一个产品后暂停的时间  
  34.         private String category; // 仅仅起标记产品作用  
  35.   
  36.         public Producer(BlockingQueue<String> queue, int timeout,  
  37.                 String category) {  
  38.             super();  
  39.             this.queue = queue;  
  40.             this.timeout = timeout;  
  41.             this.category = category;  
  42.         }  
  43.   
  44.         @Override  
  45.         public void run() {  
  46.             while (!Thread.currentThread().isInterrupted()) {  
  47.                 try {  
  48.                     // put()方法也是一个会阻塞的方法,如果队列已满的时候这个方法会一起阻塞直到  
  49.                     // 队列中重新出现空间为止  
  50.                     queue.put("product " + category);  
  51.                 } catch (InterruptedException e1) {  
  52.                     e1.printStackTrace();  
  53.                 }  
  54.                 try {  
  55.                     TimeUnit.MILLISECONDS.sleep(timeout); // 每生产一个产品就暂停timeout毫秒  
  56.                 } catch (InterruptedException e) {  
  57.                     e.printStackTrace();  
  58.                 }  
  59.             }  
  60.         }  
  61.     }  
  62.   
  63.     private class Customer implements Runnable {  
  64.         private BlockingQueue<String> queue;  
  65.   
  66.         public Customer(BlockingQueue<String> queue) {  
  67.             super();  
  68.             this.queue = queue;  
  69.         }  
  70.   
  71.         @Override  
  72.         public void run() {  
  73.             while (!Thread.currentThread().isInterrupted()) {  
  74.                 try {  
  75.                     System.out.println("product got:" + queue.take());  
  76.                 } catch (InterruptedException e1) {  
  77.                     e1.printStackTrace();  
  78.                 }  
  79.                 try {  
  80.                     // 暂停10毫秒,这里主要是为了证明take()是一个阻塞方法,如果 BlockingQueue中  
  81.                     // 没有元素,它会一起阻塞直到队列中有元素为止  
  82.                     TimeUnit.MILLISECONDS.sleep(10);  
  83.                 } catch (InterruptedException e) {  
  84.                     e.printStackTrace();  
  85.                 }  
  86.             }  
  87.   
  88.         }  
  89.   
  90.     }  
  91.   
  92. }  


有一点这里要说明 一下,其它 的在代码里我都 有简单的说明 了,这个demo比较简单,所以就不废话了。~~

NOTE:

 因为BlockingQueue接口扩展了Queue接口,所以它从Queue接口那里得到了一些简单的队列操作方法
 有个重载方法在使用的过程中应该注意。比如:offer(E)这个方法是从Queue那里得到的,它并不具有
 阻塞性,而BlokingQueue提供的重载方法offer(E,long,TimeUnit)这个方法是一个阻塞方法,它具有
和put相似的功能,不过它提供了一个阻塞时间,时间过后如果还不能成功操作则放弃,返回false。
同样的问题在poll()和poll(long,TimeUnit)之间也存在,在使用的过程中请务必留意。

转载地址:http://blog.csdn.net/moreevan/article/details/6763508


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值