-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMonitor.java
executable file
·72 lines (64 loc) · 1.99 KB
/
Monitor.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 concurrentcube;
public class Monitor {
private GroupType activeGroup = GroupType.NONE;
private int activeProcesses = 0;
private final boolean[] rotatedLayers;
public Monitor(int size) {
rotatedLayers = new boolean[size];
}
public synchronized void syncGroupsBefore(GroupType myGroup) throws InterruptedException {
while (this.activeGroup != myGroup && this.activeGroup != GroupType.NONE) {
wait();
}
activeProcesses++;
this.activeGroup = myGroup;
}
public synchronized void syncGroupsAfter() {
activeProcesses--;
if (activeProcesses == 0) {
this.activeGroup = GroupType.NONE;
notifyAll();
}
}
public synchronized void syncBeforeRotate(int layer) throws InterruptedException {
while (rotatedLayers[layer]) {
try {
wait();
} catch (InterruptedException e) {
activeProcesses--;
if (activeProcesses == 0) {
activeGroup = GroupType.NONE;
notifyAll();
}
throw new InterruptedException();
}
}
rotatedLayers[layer] = true;
}
public synchronized void syncAfterRotate(int layer) {
rotatedLayers[layer] = false;
notifyAll();
}
public int getStandardisedLayer(int side, int layer, int size) {
switch (side) {
case 0:
case 1:
case 2:
return layer;
default:
return size - 1 - layer;
}
}
public GroupType getRotateGroupType(int layer) {
switch (layer) {
case 0:
case 5:
return GroupType.ROTATE_Z;
case 1:
case 3:
return GroupType.ROTATE_X;
default:
return GroupType.ROTATE_Y;
}
}
}