-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMonitor.java
More file actions
238 lines (188 loc) · 6.35 KB
/
Monitor.java
File metadata and controls
238 lines (188 loc) · 6.35 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
import java.util.HashMap;
import java.util.Random;
import java.util.Vector;
public class Monitor {
public Vector waitingStudents1 = new Vector();
public Vector waitingStudents2 = new Vector();
public Vector waitingStudents3 = new Vector();
public Vector waitingPrincipal = new Vector();
public Vector waitingNurse = new Vector();
public static Random rand = new Random();
HashMap<Object, String> H = new HashMap<>();
HashMap<Object, String> H2 = new HashMap<>();
public Vector waitingInstructor = new Vector();
private int picked = 1;
public boolean found = false;
public boolean isPostive = false;
public int max = 0;
public Vector numELA = new Vector();
public Vector numMath = new Vector();
public Vector numPHY = new Vector();
public void getInLineQ(Students student) {
Object convey = new Object();
synchronized (convey) {
if (cannotGoIn(convey, student.getName())) {
while (true) //wait to be notified
try {
msg(student.getName() + " waiting for principle to check if test for COVID");
convey.wait();
found = pickedOneForTest();
break;
} catch (InterruptedException e) {
continue;
}
} else {
msg(student.getName() + " is sent to go home by principle.");
}
}
}
private boolean cannotGoIn(Object convey, String name) {
boolean status;
if (Project1.isSubmit) {
waitingStudents1.addElement(convey);
H.put(convey, name);
//System.out.println(H.get(convey));
status = true;
} else {
status = false;
}
return status;
}
public void PrincipleGetIn(Principal principal) {
Object convey = new Object();
synchronized (convey) {
if (noPrincipleAvaliable(convey)) {
while (true)
try {
convey.wait(); //principal is waiting
break;
} catch (InterruptedException e) {
continue;
}
}
}
}
private boolean noPrincipleAvaliable(Object convey) {
boolean status;
if (waitingStudents1.size() >= 10) {
status = false;
} else {
status = true;
waitingPrincipal.addElement(convey);
}
return status;
}
public void getInLineC(Principal principal) { //go to nurse room or go to classroom
Object convey = new Object();
int num = rand.nextInt(3);
if (!waitingStudents1.isEmpty()) {
Object Next_objectA = waitingStudents1.elementAt(0);
waitingStudents1.remove(0);
synchronized (Next_objectA) {
Next_objectA.notify();//waiting notify
}
}
}
private synchronized boolean pickedOneForTest() {
boolean status = false;
if (picked == 1) {
status = true;
picked++;
} else if (picked == 2 || picked == 3) {
status = false;
picked++;
if (picked == 4) {
picked = 1;
}
}
return status;
}
private void msg(String s) {
System.out.println("[" + (System.currentTimeMillis() - Project1.time) + "]: " + s);
}
public void studentGoToNurseRoom(Students students) {
Object convey = new Object();
synchronized (convey) {
if (cannotGoIn2(convey)) {
while (true) //waitingStudents2 wait to be notified
try {
convey.wait();
isPostive = cannotGoIn3();
break;
} catch (InterruptedException e) {
continue;
}
}
}
}
private boolean cannotGoIn2(Object convey) {
boolean status;
status = true;
waitingStudents2.addElement(convey);
return status;
}
public synchronized void studentGoToClassroom(Students students) {
if (numELA.size() < 4) {
int roomLeft = 3 - numELA.size();
msg(students.getName() + " go to ELA and there are " + roomLeft + " room left.");
checkWhichClass(students, "ELA");
} else if (numMath.size() < 4) {
int roomLeft2 = 3 - numMath.size();
msg(students.getName() + " go to Math and there are " + roomLeft2 + " room left.");
checkWhichClass(students, "MATH");
}else{
msg(students.getName() + " go to Phy.");
checkWhichClass(students, "PHY");
}
}
public void checkWhichClass(Students students, String className) {
Object convey = new Object();
synchronized (convey) {
if (cannotGoIn4(convey, className)) {
while (true) //
try {
convey.wait(); //
break;
} catch (InterruptedException e) {
continue;
}
}
}
}
private synchronized boolean cannotGoIn4(Object convey, String className) {
boolean status;
if(className == "ELA"){
numELA.addElement(convey);
status = true;
}else if(className == "MATH"){
numMath.addElement(convey);
status = true;
}else{
numPHY.addElement(convey);
status = true;
}
return status;
}
public void wakeUPStudent(Nurse nurse) {
if(!waitingStudents2.isEmpty()){
Object Next_objectA = waitingStudents2.elementAt(0);
waitingStudents2.remove(0);
synchronized (Next_objectA) {
Next_objectA.notify();//waiting notify
}
}
}
private boolean cannotGoIn3() {
boolean status;
double num = rand.nextDouble();
if (num < 0.03) {
status = true;
max++;
} else {
status = false;
}
return status;
}
public void wakeUpInstructorAndStudents(Instructor instructor) {
}
}