-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockingQueueDemo.java
More file actions
164 lines (147 loc) · 6.25 KB
/
Copy pathBlockingQueueDemo.java
File metadata and controls
164 lines (147 loc) · 6.25 KB
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package Phase5_CollectionsLambdasStreams.Collections;
import java.util.concurrent.*;
/**
* java.util.concurrent.BlockingQueue<E>
* -------------------------------------
* BlockingQueue is a Queue that BLOCKS the caller when an operation cannot
* proceed immediately:
* <p>
*
* put(e) - blocks the producer if the queue is FULL
* take() - blocks the consumer if the queue is EMPTY
* <p>
*
* That makes BlockingQueue the FUNDAMENTAL BUILDING BLOCK for the
* producer/consumer pattern in concurrent code.
* <p>
*
* Four Operation Modes Per Operation
* ----------------------------------
* <p>
*
* Throws Returns special Blocks Times out
* ----------------- ----------------- ----------------- -----------------------------
* insert | add(e) | offer(e) | put(e) | offer(e, time, TimeUnit)
* remove | remove() | poll() | take() | poll(time, TimeUnit)
* peek | element() | peek() | — | —
* <p>
*
* Implementations
* ---------------
* ArrayBlockingQueue - bounded, array-backed, fair option, FIFO
* LinkedBlockingQueue - optionally bounded, linked nodes, FIFO,
* separate locks for head and tail (good
* throughput on producer/consumer)
* SynchronousQueue - capacity ZERO! every put hands directly to a take
* PriorityBlockingQueue- unbounded, heap-ordered, blocking
* DelayQueue - elements have a delay; take blocks until expiry
* LinkedTransferQueue - extended transfer semantics (Java 7+)
* <p>
*
* Why It Exists
* -------------
* Without BlockingQueue you would write loops like:
* <p>
*
* while (queue.isEmpty()) Thread.sleep(1); // CPU-burning poll
* <p>
*
* BlockingQueue offloads that wait into the OS scheduler - the thread
* sleeps until there is something to do, no CPU spent waiting.
* <p>
*
* When To Use Which
* -----------------
* - Bounded capacity (backpressure) -> ArrayBlockingQueue
* - High-throughput producer/consumer -> LinkedBlockingQueue
* - Direct hand-off, no buffering -> SynchronousQueue
* - Priority order -> PriorityBlockingQueue
* - Delayed-action scheduling -> DelayQueue
* <p>
*
* Demo: A small producer / consumer with two threads.
*/
public class BlockingQueueDemo {
public static void main(String[] args) throws InterruptedException {
section("1) ArrayBlockingQueue - bounded, fair");
final int CAPACITY = 3;
BlockingQueue<Integer> bq = new ArrayBlockingQueue<>(CAPACITY, /*fair*/ true);
// Fill it up to capacity, then attempt one more with each method family.
for (int i = 0; i < CAPACITY; i++) bq.put(i);
System.out.println("filled to capacity = " + bq);
// offer returns false instead of throwing when full
System.out.println("offer(99) = " + bq.offer(99));
// add throws when full
try { bq.add(99); }
catch (IllegalStateException e) {
System.out.println("add(99) -> " + e.getClass().getSimpleName());
}
// offer with timeout
long t = System.currentTimeMillis();
boolean ok = bq.offer(99, 50, TimeUnit.MILLISECONDS);
System.out.println("offer(99, 50ms) = " + ok +
" (waited ~" + (System.currentTimeMillis() - t) + " ms)");
// Drain
System.out.print("drained: ");
Integer v;
while ((v = bq.poll()) != null) System.out.print(v + " ");
System.out.println();
section("2) Producer / Consumer with put / take");
BlockingQueue<String> pipe = new LinkedBlockingQueue<>(2);
Thread producer = new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
String msg = "item-" + i;
pipe.put(msg); // blocks if pipe is full
System.out.println("produced " + msg);
Thread.sleep(30);
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
return;
}
}
}, "producer");
Thread consumer = new Thread(() -> {
for (int i = 0; i < 5; i++) {
try {
String msg = pipe.take(); // blocks if pipe is empty
System.out.println("consumed " + msg);
Thread.sleep(70); // slower than producer
} catch (InterruptedException ie) {
Thread.currentThread().interrupt();
return;
}
}
}, "consumer");
producer.start();
consumer.start();
producer.join();
consumer.join();
section("3) LinkedBlockingQueue - unbounded by default");
BlockingQueue<Integer> lbq = new LinkedBlockingQueue<>(); // Integer.MAX_VALUE capacity
for (int i = 0; i < 10_000; i++) lbq.offer(i);
System.out.println("LinkedBlockingQueue size = " + lbq.size());
section("4) SynchronousQueue - zero capacity, direct hand-off");
// Put blocks until another thread is ready to take. We hand one over
// here using a tiny background thread.
BlockingQueue<String> sq = new SynchronousQueue<>();
new Thread(() -> {
try {
Thread.sleep(50);
sq.put("handed-off");
} catch (InterruptedException ignored) {}
}).start();
long t2 = System.currentTimeMillis();
String got = sq.take();
System.out.println("got '" + got + "' after ~" + (System.currentTimeMillis() - t2) + " ms");
section("5) drainTo - move everything to another collection at once");
java.util.List<Integer> dump = new java.util.ArrayList<>();
lbq.drainTo(dump, 5); // move at most 5 items
System.out.println("drained 5 = " + dump);
System.out.println("queue size = " + lbq.size());
// OUTPUT (timings + interleaving vary)
}
private static void section(String title) {
System.out.println("\n====== " + title + " ======");
}
}