`
lastsoul
  • 浏览: 33428 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

多线程-生产者消费者

 
阅读更多
转自:http://bijian1013.iteye.com/blog/1871763

ArrayBlockingQueue:

       一个由数组支持的有界阻塞队列。此队列按 FIFO(先进先出)原则对元素进行排序。队列的头部 是在队列中存在时间最长的元素。队列的尾部 是在队列中存在时间最短的元素。新元素插入到队列的尾部,队列检索操作则是从队列头部开始获得元素。

ArrayBlockingQueue的常用方法:

       put:将指定的元素添加到此队列的尾部,如果必要,将等待可用的空间。

       take:检索并移除此队列的头部,如果此队列不存在任何元素,则一直等待。

       offer:将指定的元素插入到此队列的尾部(如果可能),如果此队列已满,则立即返回。

       poll:检索并移除此队列的头,如果此队列为空,则返回null。

说明:put、offer都是向队列中添加元素,take、poll都是从队列中移除元素。put和take对应一组,是阻塞式的;offer、poll对应一组,是非阻塞式的。



实例:



Java代码 
1.package com.bijian.thread; 
2. 
3.import java.util.concurrent.BlockingQueue; 
4. 
5.public class Producer implements Runnable { 
6. 
7.    private String name = null; 
8.    private BlockingQueue<String> queue = null; 
9. 
10.    public Producer(String name, BlockingQueue<String> queue) { 
11.        this.name = name; 
12.        this.queue = queue; 
13.    } 
14. 
15.    @Override 
16.    public void run() { 
17. 
18.        try { 
19.            for (int i = 0; i < 10; i++) { 
20.                queue.put(name + ": " + i); 
21.                System.out.println("Product:" + name + ": " + i); 
22.                Thread.sleep((long) (Math.random() * 1000)); 
23.            } 
24.        } catch (Exception e) { 
25.            e.printStackTrace(); 
26.        } 
27.    } 
28.} 





Java代码 
1.package com.bijian.thread; 
2. 
3.import java.util.concurrent.BlockingQueue; 
4. 
5.public class Consumer implements Runnable { 
6.     
7.    private String name; 
8.    private BlockingQueue<String> queue = null; 
9.     
10.    public Consumer(String name, BlockingQueue<String> queue) { 
11.        this.name = name; 
12.        this.queue = queue; 
13.    } 
14. 
15.    @Override 
16.    public void run() { 
17.         
18.        try { 
19.            for (int i = 0; i < 5; i++) { 
20.                String prod = queue.take(); 
21.                System.out.println("Consumer:" + name + ": " + prod); 
22.                Thread.sleep((long) (Math.random() * 1000)); 
23.            } 
24.        } catch (Exception e) { 
25.            e.printStackTrace(); 
26.        } 
27.    } 
28.} 





Java代码 
1.package com.bijian.thread; 
2. 
3.import java.util.concurrent.ArrayBlockingQueue; 
4.import java.util.concurrent.BlockingQueue; 
5.import java.util.concurrent.ExecutorService; 
6.import java.util.concurrent.Executors; 
7. 
8.public class Main { 
9. 
10.    public static void main(String[] args) { 
11.         
12.        BlockingQueue<String> queue = new ArrayBlockingQueue<String> (3); 
13.         
14.        ExecutorService service = Executors.newCachedThreadPool();     
15.        Producer producer = new Producer("P1", queue); 
16.        Consumer consumer = new Consumer("C1", queue); 
17.        Consumer consumer1 = new Consumer("C2", queue); 
18.        service.execute(producer); 
19.        service.execute(consumer); 
20.        service.execute(consumer1); 
21.        service.shutdown();     
22.    } 
23.} 


运行结果:


Text代码 
1.Product:P1: 0 
2.Consumer:C1: P1: 0 
3.Product:P1: 1 
4.Consumer:C2: P1: 1 
5.Product:P1: 2 
6.Consumer:C1: P1: 2 
7.Product:P1: 3 
8.Consumer:C2: P1: 3 
9.Product:P1: 4 
10.Consumer:C1: P1: 4 
11.Product:P1: 5 
12.Consumer:C2: P1: 5 
13.Product:P1: 6 
14.Consumer:C1: P1: 6 
15.Product:P1: 7 
16.Consumer:C2: P1: 7 
17.Product:P1: 8 
18.Product:P1: 9 
19.Consumer:C1: P1: 8 
20.Consumer:C2: P1: 9 
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics