-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathMain.java
100 lines (85 loc) · 2.49 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
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
import java.util.*;
import java.util.concurrent.*;
class Main {
static Queue<Integer> queue;
static CombiningTree<Integer> tree;
static int TH = 25, ARY = 3, NUM = 100;
// queue: used to store old (get) values (unique check)
// tree: combining tree where threads do increment ops
// TH: number of threads
// ARY: arity of combining tree
// NUM: number of increment ops each thread performs
// Each thread performs increment op using the combining
// tree, and saves old (get) values in the queue, for
// uniqueness check later.
static Thread thread(int id) {
return new Thread(() -> {
try {
long start = System.currentTimeMillis();
for (int i=0; i<NUM; i++) {
Integer r = tree.getAndOp(1,
(Integer x, Integer y) -> x + y);
queue.add(r);
Thread.yield();
}
long stop = System.currentTimeMillis();
log(id()+": done in "+(stop-start)+"ms");
} catch (InterruptedException e) {}
});
}
// Check if total sum is as expected.
// Check if all old values are unique.
static boolean wasValid() {
int a = tree.get().intValue();
if (a != TH*NUM) return false;
Set<Integer> s = new HashSet<>();
while (queue.size()>0) {
Integer n = queue.remove();
if (s.contains(n)) return false;
s.add(n);
}
return true;
}
// Setup the combining tree for threads.
static void setupTreeAndQueue() {
int depth = (int) Math.ceil(Math.log(TH)/Math.log(ARY));
tree = new CombiningTree<>(depth, ARY);
tree.set(0);
}
// Setup the queue for storing old values.
static void setupQueue() {
queue = new ConcurrentLinkedQueue<>();
}
// Start threads doing increments using tree.
static Thread[] startOps() {
Thread[] t = new Thread[TH];
for (int i=0; i<TH; i++)
t[i] = thread(i);
for (int i=0; i<TH; i++)
t[i].start();
return t;
}
// Wait until all threads done with increments.
static void awaitOps(Thread[] t) {
try {
for (int i=0; i<TH; i++)
t[i].join();
} catch (InterruptedException e) {}
}
public static void main(String[] args) {
setupTree();
setupQueue();
log(ARY+"-ary "+depth+"-depth Combining tree.");
log("Starting "+TH+" threads doing increments ...");
Thread[] t = startOps();
awaitOps(t);
log("Total: "+tree.get());
log("\nWas valid? "+wasValid());
}
static void log(String x) {
System.out.println(x);
}
static long id() {
return Thread.currentThread().getId();
}
}