-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChildCareSolution_Synchronized.java
More file actions
191 lines (149 loc) · 6.15 KB
/
ChildCareSolution_Synchronized.java
File metadata and controls
191 lines (149 loc) · 6.15 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
package mk.ukim.finki.os.synchronization.problems.ChildCare;
import static java.lang.Integer.min;
import java.util.HashSet;
import java.util.concurrent.Semaphore;
import mk.ukim.finki.os.synchronization.ProblemExecution;
import mk.ukim.finki.os.synchronization.TemplateThread;
/**
*
* @author Nikolina
*/
// Синхронизирано решение.
public class ChildCareSolution_Synchronized {
public static int adults;
public static int children;
public static Semaphore adultQueue;
public static Semaphore childQueue;
public static Semaphore mutex;
public static void init()
{
adults = 0;
children = 0;
adultQueue = new Semaphore(0);
childQueue = new Semaphore(0);
mutex = new Semaphore(1);
}
// Нитка за воспитувач.
public static class Adult extends TemplateThread
{
public Adult(int numberOfRuns)
{
super(numberOfRuns);
}
@Override
public void execute() throws InterruptedException {
// Код за влегување на воспитувачот.
mutex.acquire();
// Воспитувачот влегува - не вршиме проверки, воспитувачот може
// да влезе било кога.
state.adultEntered();
adults++;
// Ако има деца што чекаат да влезат...
if(childQueue.getQueueLength() > 0)
{
// Може да влезат максимум три деца...
int n = min(3, childQueue.getQueueLength());
// Им се дозволува на n деца кои чекаат да влезат, затоа што
// влегол уште еден воспитувач.
state.childrenEntered(n);
childQueue.release(n);
children += n;
}
mutex.release();
// Критичен регион.
// Код за излегување на воспитувачот.
mutex.acquire();
// Ако има доволно воспитувачи за да ги чуваат децата...
if(children <= 3 * (adults - 1))
{
// Воспитувачот излегува.
state.adultLeft();
adults--;
mutex.release();
}
// Ако нема доволно воспитувачи за да ги чуваат децата...
else
{
// Воспитувачот не може да излезе, се става во редицата на
// воспитувачи кои чекаат да излезат.
state.adultLeaving();
mutex.release();
adultQueue.acquire();
}
}
}
// Нитка за дете.
public static class Child extends TemplateThread
{
public Child(int numberOfRuns)
{
super(numberOfRuns);
}
@Override
public void execute() throws InterruptedException {
// Код за влегување на детето.
mutex.acquire();
// Ако има доволно воспитувачи за децата кои се внатре и детето
// кое сака да влезе...
if(children < 3 * adults)
{
// Детето влегува.
state.childrenEntered(1);
children++;
mutex.release();
}
else
// Ако би немало доволно воспитувачи доколку би влегло детето... кои се внатре и детето
// кое сака да влезе...
{
// Детето не може да влезе, се става во редицата на деца кои
// чекаат да влезат.
state.childEntering();
mutex.release();
childQueue.acquire();
}
// Критичен регион.
// Код за излегување на детето.
mutex.acquire();
// Детето излегува - не вршиме проверки, детето може да излезе
// било кога.
state.childLeft();
children--;
// Ако има воспитувачи кои чекаат да излезат и притоа има доволно
// воспитувачи за децата, откако си отишло едно дете...
if((adultQueue.getQueueLength() > 0) && children <= 3 * (adults - 1))
{
// Му се дозволува на еден воспитувач да излезе, затоа што
// откако си отишло едно дете има доволно воспитувачи.
state.adultLeft();
adults--;
adultQueue.release();
}
mutex.release();
}
}
static ChildCareState state = new ChildCareState();
public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println("Run: " + i);
run();
}
}
public static void run() {
try {
int numChildren = 100;
HashSet<Thread> threads = new HashSet<>();
ChildCareSolution_Synchronized.Adult adult = new ChildCareSolution_Synchronized.Adult((numChildren / 3) + 1);
threads.add(adult);
int numRuns = 1;
for (int i = 0; i < numChildren; i++) {
ChildCareSolution_Synchronized.Child child = new ChildCareSolution_Synchronized.Child(numRuns);
threads.add(child);
}
init();
ProblemExecution.start(threads, state);
} catch (Exception e) {
e.printStackTrace();
}
}
}