-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBlockingQueueTest1.java
More file actions
70 lines (60 loc) · 1.6 KB
/
BlockingQueueTest1.java
File metadata and controls
70 lines (60 loc) · 1.6 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
package Thread.ProductAndResumer;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;
import Thread.createThread.LiftOff;
public class BlockingQueueTest1 {
static void getKey(){
try {
new BufferedReader((new InputStreamReader(System.in))).readLine();
} catch (IOException e) {
throw new RuntimeException();
}
}
static void getKey(String message){
System.out.println(message);
getKey();
}
static void test(String message,BlockingQueue<LiftOff> queue){
System.out.println(message);
LiftOffRunnable runner=new LiftOffRunnable(queue);
Thread t=new Thread(runner);
t.start();
for(int i=0;i<5;i++)
runner.add(new LiftOff(5));
getKey("Press 'Enter' ("+message+")");
t.interrupt();
System.out.println("Finished"+message+"test");
}
public static void main(String[] args) {
test("LinkedBlockingQueue",new LinkedBlockingQueue<>());
}
}
//消费者
class LiftOffRunnable implements Runnable{
private BlockingQueue<LiftOff> rockets;
public LiftOffRunnable(BlockingQueue<LiftOff> rockets){
this.rockets=rockets;
}
public void run() {
while(!Thread.interrupted()){
try {
rockets.take();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void add(LiftOff liftOff){
try {
rockets.put(liftOff);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
System.out.println("Waking from take()");
}
System.out.println("Exiting LiftOffRunner()");
}
}