阻塞队列和生产者-消费者模式
2008-11-09 13:46
507 查看
自Java5以来提供的BlockingQueue是一种特殊的队列, 它 是支持两个附加操作的
以JDK中的例子略加改写如下
1 import java.util.concurrent.ArrayBlockingQueue;
2 import java.util.concurrent.BlockingQueue;
3
4 class Producer implements Runnable {
5 private final BlockingQueue queue;
6
7 Producer(BlockingQueue q) {
8 queue = q;
9 }
10
11 public void run() {
12 try {
13 while (true) {
14 queue.put(produce());
15 }
16 } catch (InterruptedException ex) {
17 System.out.println("produce interrupted " + ex.getMessage());
18 Thread.currentThread().interrupt();
19 //return;
20 }
21 }
22
23 Object produce() {
24 System.out.println("produce laugh");
25 return "haha";
26 }
27 }
28
29 class Consumer implements Runnable {
30 private final BlockingQueue queue;
31
32 Consumer(BlockingQueue q) {
33 queue = q;
34 }
35
36 public void run() {
37 try {
38 while (true) {
39 consume(queue.take());
40 }
41 } catch (InterruptedException ex) {
42 System.out.println("consume interrupted " + ex.getMessage());
43 Thread.currentThread().interrupt();
44 }
45 }
46
47 void consume(Object x) {
48 System.out.println("consume laugh "+ x);
49 }
50 }
51
52 public class BlockingQueueTest {
53 public static void main(String args[]) {
54 BlockingQueue q = new ArrayBlockingQueue(10);
55 Producer p = new Producer(q);
56 Consumer c1 = new Consumer(q);
57 Consumer c2 = new Consumer(q);
58 Thread pTh = new Thread(p);
59 pTh.start();
60 Thread cTh1 = new Thread(c1);
61 cTh1.start();
62 Thread cTh2 = new Thread(c2);
63 cTh2.start();
64 try {
65 Thread.sleep(3000);
66 }catch (Exception e) {
67 // TODO: handle exception
68 }
69 pTh.interrupt();
70 cTh1.interrupt();
71 cTh2.interrupt();
72 }
73 }
74
posted on 2008-07-26 23:42
Queue,这两个操作是:检索元素时等待队列变为非空,以及存储元素时等待空间变得可用。
以JDK中的例子略加改写如下
1 import java.util.concurrent.ArrayBlockingQueue;
2 import java.util.concurrent.BlockingQueue;
3
4 class Producer implements Runnable {
5 private final BlockingQueue queue;
6
7 Producer(BlockingQueue q) {
8 queue = q;
9 }
10
11 public void run() {
12 try {
13 while (true) {
14 queue.put(produce());
15 }
16 } catch (InterruptedException ex) {
17 System.out.println("produce interrupted " + ex.getMessage());
18 Thread.currentThread().interrupt();
19 //return;
20 }
21 }
22
23 Object produce() {
24 System.out.println("produce laugh");
25 return "haha";
26 }
27 }
28
29 class Consumer implements Runnable {
30 private final BlockingQueue queue;
31
32 Consumer(BlockingQueue q) {
33 queue = q;
34 }
35
36 public void run() {
37 try {
38 while (true) {
39 consume(queue.take());
40 }
41 } catch (InterruptedException ex) {
42 System.out.println("consume interrupted " + ex.getMessage());
43 Thread.currentThread().interrupt();
44 }
45 }
46
47 void consume(Object x) {
48 System.out.println("consume laugh "+ x);
49 }
50 }
51
52 public class BlockingQueueTest {
53 public static void main(String args[]) {
54 BlockingQueue q = new ArrayBlockingQueue(10);
55 Producer p = new Producer(q);
56 Consumer c1 = new Consumer(q);
57 Consumer c2 = new Consumer(q);
58 Thread pTh = new Thread(p);
59 pTh.start();
60 Thread cTh1 = new Thread(c1);
61 cTh1.start();
62 Thread cTh2 = new Thread(c2);
63 cTh2.start();
64 try {
65 Thread.sleep(3000);
66 }catch (Exception e) {
67 // TODO: handle exception
68 }
69 pTh.interrupt();
70 cTh1.interrupt();
71 cTh2.interrupt();
72 }
73 }
74
posted on 2008-07-26 23:42
相关文章推荐
- Java生产者-消费者模式的非阻塞队列实现
- 阻塞队列和生产者-消费者模式、DelayQueue
- 基于Java阻塞队列实现生产者与消费者模式
- 多线程之消费者生产者模式加入阻塞队列
- 生产者/消费者模式(阻塞队列)
- 阻塞队列和生产者-消费者模式
- JAVA 阻塞队列实现 生产者和消费者 模式
- 生产者/消费者模式(阻塞队列) 一个经典的并发模型
- Java并发编程:阻塞队列及实现生产者-消费者模式
- java消费者生产者模式及JDK之阻塞队列LinkedBlockingQueue实现
- 生产者/消费者模式(阻塞队列)
- 【java】生产者/消费者模式--非阻塞队列
- java技术深入(一)——java多线程(七)——阻塞队列,生产者-消费者模式
- Java 并发编程(四)阻塞队列和生产者-消费者模式
- Java并发编程实践笔记之—阻塞队列和生产者-消费者模式(Blocking Queues and the Producer-consumer Pattern)
- 高并发:阻塞队列 实现生产者-消费者模式
- 生产者/消费者模式(阻塞队列)
- BlockingQueue阻塞队列和生产者-消费者模式
- 生产者/消费者模式(阻塞队列)
- 生产者/消费者模式(阻塞队列)