-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
72 lines (66 loc) · 1.63 KB
/
Main.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
import java.util.*;
class Main {
static Queue<String> queue;
static int CAP = 2;
static int OPS = 4;
// CAP: capacity of queue
// OPS: number to add()s / remove()s
static Thread consumer(String id, long wait) {
Thread t = new Thread(() -> {
try {
for(int i=0; i<OPS; i++) {
log(id+": remove()\t"+queue);
String x = queue.remove();
log(id+": sleep()\t"+x);
Thread.sleep(wait);
}
}
catch(InterruptedException e) {}
});
t.start();
return t;
}
static Thread producer(String id, long wait) {
Thread t = new Thread(() -> {
try {
for(int i=0; i<OPS; i++) {
log(id+": add()\t"+queue);
queue.add(id);
log(id+": sleep()\t"+queue);
Thread.sleep(wait);
}
}
catch(InterruptedException e) {}
});
t.start();
return t;
}
// For test to pass, consumers A, B
// should not remove same value.
static void testThreads(long waitc, long waitp) {
log("For test to pass, consumers A, B");
log("should not remove same value.");
Thread A = consumer("A", waitc);
Thread B = consumer("B", waitc);
Thread C = producer("C", waitp);
Thread D = producer("D", waitp);
try {
A.join();
B.join();
C.join();
D.join();
}
catch(InterruptedException e) {}
log("Test done.\n");
}
public static void main(String[] args) {
queue = new LockedQueue<>(CAP);
log("Starting fast consumers test ...");
testThreads(10, 20);
log("Starting fast producers test ...");
testThreads(20, 10);
}
static void log(String x) {
System.out.println(x);
}
}