forked from greasymolue/SF-Int-Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUseAQueue.java
More file actions
34 lines (31 loc) · 1.05 KB
/
UseAQueue.java
File metadata and controls
34 lines (31 loc) · 1.05 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
package prodcons;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.BlockingQueue;
public class UseAQueue {
public static void main(String[] args) {
BlockingQueue<String> bqs = new ArrayBlockingQueue<>(10);
Runnable r = () -> {
System.out.println("worker starting");
int delay = 2000 + (int)(Math.random() * 2000);
try {
Thread.sleep(delay);
System.out.println("worker about to write message");
bqs.put("DIE!");
System.out.println("Message sent");
} catch (InterruptedException e) {
System.out.println("huh? who interrupted me!?");
}
System.out.println("worker finishing...");
};
System.out.println("main about to launch worker");
new Thread(r).start();
System.out.println("worker launched...");
try {
String msg = bqs.take();
System.out.println("main received message: " + msg);
} catch (InterruptedException e) {
System.out.println("Huh? main interrupted.");;
}
System.out.println("main exiting....");
}
}