-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathBlockingQueueDemo.java
87 lines (76 loc) · 2.81 KB
/
BlockingQueueDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
/**
* Simple sample of Producer/Consumer using ArrayBlockingQueue (ThreadSafe)
* - One Producer adds items in a pipeline in a frequency of 200ms
* - The pipeline has a limit of 5 items
* - Two consumers take items from the pipeline and consumes it in 500ms
*/
public class BlockingQueueDemo {
private static final String STOP = "producer stopped";
private static final int CONSUMERS = 2;
private static final int CAPACITY = 5;
private static class Producer extends Thread {
private final BlockingQueue<String> pipeline;
Producer(BlockingQueue<String> pipeline) {
this. pipeline = pipeline;
}
public void run() {
int nItems = 1;
while (nItems <= 20) {
try {
String item = "item" + nItems;
pipeline.offer(item);
String capacity = String.format(" [%d/%d]", pipeline.size(), CAPACITY);
System.out.println("Producer is adding " + item + capacity );
nItems++;
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Wait for the items to be consume
while (pipeline.remainingCapacity() != CAPACITY) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
// Send command to stop consumers
for (int i = 0; i < CONSUMERS; i++) {
pipeline.add(STOP);
}
}
}
private static class Consumer extends Thread {
private final BlockingQueue<String> pipeline;
private final String name;
Consumer(String name, BlockingQueue<String> pipeline) {
this.name = name;
this. pipeline = pipeline;
}
public void run() {
while (true) {
try {
String item = pipeline.take();
if (item.equals(STOP)) {
break;
}
System.out.println(name + " took " + item);
Thread.sleep(500);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public static void main(String[] args) throws InterruptedException {
BlockingQueue<String> pipeline = new ArrayBlockingQueue<>(CAPACITY);
new Producer(pipeline).start();
Thread.sleep(100);
for (int i = 0; i < CONSUMERS; i++) {
new Consumer("Consumer" + i, pipeline).start();
}
}
}