-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChildCareState.java
More file actions
115 lines (95 loc) · 3.42 KB
/
ChildCareState.java
File metadata and controls
115 lines (95 loc) · 3.42 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
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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package mk.ukim.finki.os.synchronization.problems.ChildCare;
import mk.ukim.finki.os.synchronization.AbstractState;
import mk.ukim.finki.os.synchronization.BoundCounterWithRaceConditionCheck;
import mk.ukim.finki.os.synchronization.PointsException;
import mk.ukim.finki.os.synchronization.Switcher;
import static java.lang.Integer.max;
/**
*
* @author Nikolina
*/
public class ChildCareState extends AbstractState {
private static final String ADULTS_LEAVING_DEADLOCK
= "Adult leaving deadlock... Adults should be able to leave when numberOfChildren <= 3 * (numberOfAdults - 1).";
private static final String CHILDREN_ENTERING_DEADLOCK
= "Children entering deadlock... Children should be able to enter when numberOfChildren <= 3 * (numberOfAdults - 1)";
private static final int ADULTS_LEAVING_DEADLOCK_POINTS = 15;
private static final int CHILDREN_ENTERING_DEADLOCK_POINTS = 15;
private int adults;
private int children;
public int adultsLeaving;
public int childrenEntering;
public ChildCareState() {
adults = 0;
children = 0;
adultsLeaving = 0;
childrenEntering = 0;
}
public void adultEntered() {
synchronized (this) {
//System.out.println("Adult entered!");
log(null, "Adult entered!");
adults++;
}
}
public void adultLeft() {
synchronized (this) {
//System.out.println("Adult left!");
log(null, "Adult left!");
if (adultsLeaving > 0)
adultsLeaving--;
adults--;
}
}
public void adultLeaving() {
synchronized (this) {
//System.out.println("Adult leaving...");
log(null, "Adult leaving...");
adultsLeaving++;
if(children <= 3 * (adults - 1))
{
PointsException e = new PointsException(ADULTS_LEAVING_DEADLOCK_POINTS, ADULTS_LEAVING_DEADLOCK);
log(e, null);
}
}
}
public void childrenEntered(int n) {
synchronized (this) {
//System.out.println(n == 1 ? "Child entered!" : (n + " children entered!"));
log(null, (n == 1 ? "Child entered!" : (n + " children entered!")));
if (childrenEntering > 0)
childrenEntering -= max(n, childrenEntering);
children += n;
}
}
public void childLeft() {
synchronized (this) {
//System.out.println("Child left!");
log(null, "Child left!");
children--;
}
}
public void childEntering() {
synchronized (this) {
//System.out.println("Child entering...");
log(null, "Child entering...");
childrenEntering++;
if(children <= 3 * (adults - 1))
{
PointsException e = new PointsException(CHILDREN_ENTERING_DEADLOCK_POINTS, CHILDREN_ENTERING_DEADLOCK);
log(e, null);
}
}
}
public void reset() {
adults = 0;
children = 0;
adultsLeaving = 0;
childrenEntering = 0;
}
@Override
public void finalize() {
reset();
// printLog();
}
}