-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBarrier.java
More file actions
62 lines (54 loc) · 1.67 KB
/
Copy pathBarrier.java
File metadata and controls
62 lines (54 loc) · 1.67 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
/**
* Lab 9: Thread Barrier
* Barrier Class
*
* Processes join the barrier and are held until barrierSize processes have
* joined.
*
*
*/
public class Barrier {
/**
* Size of the barrier, which is the minimum number of processes to proceed.
*/
private int barrierSize;
int numProcesses = 0;
int val = 0;
/**
* Create a barrier of a given size
*
* @param size
*/
public Barrier(int size) {
barrierSize = size;
System.out.println("Barrier size = " + barrierSize);
}
/**
* Processes join at barrier and either wait or are allowed past.
*
* @param p The process joining
*/
public void joinBarrier(Process p) {
System.out.println(p.getName() + " waiting on barrier");
synchronized (p) {
p.numQueue = numProcesses; // Queue number of process (Doesnt use java data structure)
numProcesses = numProcesses + 1; // Increment counter for processes in barrier
}
while(numProcesses <= barrierSize){
// Wait
try {
Thread.sleep(100);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}
// Wait for ms (depends on queue number so FIFO) (Doesnt use java data structure)
try {
Thread.sleep(p.numQueue*100); // Wait for time depending on queue order of process
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// Barrier passed
System.out.println(p.getName() + " passed the barrier");
}
}