-
Notifications
You must be signed in to change notification settings - Fork 0
/
WorkerThread.java
executable file
·72 lines (63 loc) · 1.91 KB
/
WorkerThread.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
package data_structures;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.BrokenBarrierException;
public class WorkerThread extends Thread {
private int id;
private int nrIterations;
private Sorted<Integer> sorted;
private int[] itemsToAdd;
private int[] itemsToRemove;
private int workTime;
private boolean doWork;
private boolean doDebug;
private CyclicBarrier barrier;
WorkerThread(int id, Sorted<Integer> list, int nrIterations, int[] itemsToAdd, int[] itemsToRemove, int workTime, CyclicBarrier barrier, boolean debug) {
this.sorted = list;
this.id = id;
this.nrIterations = nrIterations;
this.itemsToAdd = itemsToAdd;
this.itemsToRemove = itemsToRemove;
this.workTime = workTime;
this.doWork = workTime > 0;
this.barrier = barrier;
this.doDebug = debug;
}
public void run() {
int startIndex = nrIterations * id;
add(sorted, startIndex, nrIterations, itemsToAdd);
try {
barrier.await();
if (this.doDebug) {
if (this.id == 0) {
System.out.printf("Output after adding, before removing:\n%s\n", sorted.toString());
}
barrier.await();
}
}
catch (InterruptedException e) {
e.printStackTrace();
}
catch (BrokenBarrierException e) {
e.printStackTrace();
}
remove(sorted, startIndex, nrIterations, itemsToRemove);
}
private void remove(Sorted<Integer> sorted, int startIndex, int nrIterations, int[] itemsToRemove) {
for (int i = startIndex; i < startIndex + nrIterations; i++) {
doWork();
sorted.remove(itemsToRemove[i]);
}
}
private void add(Sorted<Integer> sorted, int startIndex, int nrIterations, int[] itemsToAdd) {
for (int i = startIndex; i < startIndex + nrIterations; i++) {
doWork();
sorted.add(itemsToAdd[i]);
}
}
private void doWork() {
if (doWork) {
long end = System.nanoTime() + workTime * 1000;
while (System.nanoTime() < end); // busy wait
}
}
}