-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPCBlockingQueue.java
More file actions
52 lines (45 loc) · 1.21 KB
/
PCBlockingQueue.java
File metadata and controls
52 lines (45 loc) · 1.21 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
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
class ProducerB implements Runnable {
BlockingQueue q;
ProducerB(BlockingQueue q) {
this.q = q;
new Thread(this, "Producer").start();
}
public void run() {
int i = 0;
try {
while (true) {
q.put(i++ + "");
System.out.println("Put: " + (i-1));
Thread.sleep(1000);
}
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
class ConsumerB implements Runnable {
BlockingQueue q;
ConsumerB(BlockingQueue q) {
this.q = q;
new Thread(this, "Consumer").start();
}
public void run() {
try {
while (true) {
System.out.println("Got: " + q.take());
}
} catch(InterruptedException e) {
System.out.println("Interrupted");
}
}
}
public class PCBlockingQueue {
public static void main(String[] args) {
BlockingQueue q = new ArrayBlockingQueue(1);
new ProducerB(q);
new ConsumerB(q);
System.out.println("Press Control-C to stop.");
}
}