Repository navigation
Expand file tree
/
Copy pathDriver.java
More file actions
59 lines (46 loc) · 1.53 KB
/
Copy pathDriver.java
File metadata and controls
59 lines (46 loc) · 1.53 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
// Michael Harris
// COP4520 - pa2
// Driver for AtomicStackRDCSS.java
import java.util.*;
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
public class Driver<T> {
public static final int MAX_NUM_NODES = 100000;
public static final int THREADS = 32;
public static void main(String[] args) throws Exception {
Thread threads[] = new Thread[THREADS];
AtomicStackRDCSS<Integer> AtomicStackRDCSS = new AtomicStackRDCSS<>();
long begin = System.currentTimeMillis();
for (int i = 0; i < THREADS; i++) {
threads[i] = new Thread(new AtomicDriver(AtomicStackRDCSS, i));
threads[i].start();
}
for (int i = 0; i < THREADS; i++)
threads[i].join();
long end = System.currentTimeMillis();
System.out.println("took " + (end - begin) + "ms");
}
}
// runnable interface
class AtomicDriver implements Runnable {
private final int tid;
public AtomicReference<AtomicStackRDCSS<Integer>> stack;
public AtomicDriver(AtomicStackRDCSS<Integer> stack, int tid) {
this.stack = new AtomicReference<>(stack);
this.tid = tid;
}
// get a 'random' number and do a corresponding operation
public void run() {
int randomOp = (int)(Math.random() * 3);
if (randomOp == 1) {
int r = ThreadLocalRandom.current().nextInt(100, 10000);
boolean push = stack.get().push(r);
}
else if (randomOp == 2) {
Integer pop = stack.get().pop();
}
else {
int size = stack.get().size();
}
}
}