-
Notifications
You must be signed in to change notification settings - Fork 0
/
Threads_and_Synchronization_1.java
288 lines (255 loc) · 8.7 KB
/
Threads_and_Synchronization_1.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
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package sync1;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.concurrent.Semaphore;
/**
*
* @author ristes
*/
public class TancSoStudentite {
//TODO: Definicija na globalni promenlivi i semafori
static Semaphore mLock;
static Semaphore zLock;
static Semaphore salaLock;
static Semaphore partnerLock;
static Semaphore tancLock;
public void init() {
//TODO: da se implementira
mLock = new Semaphore(10);
zLock = new Semaphore(10);
salaLock = new Semaphore(3);
partnerLock = new Semaphore(0);
tancLock = new Semaphore(0);
}
class Masko extends Thread {
//TODO: Definicija na promenlivi za sostojbata
public void ucestvo() throws InterruptedException {
//TODO: da se implementira
mLock.acquire();
show.presobleci();
partnerLock.acquire();
mLock.release();
// samo maskoto go povikuva metodot tancuvaj
salaLock.acquire();
show.tancuvaj();
salaLock.release();
tancLock.release();
}
@Override
public void run() {
try {
ucestvo();
} catch (InterruptedException e) {
// Do nothing
} catch (Exception e) {
exception = e;
hasException = true;
}
}
@Override
public String toString() {
return String.format("m\t%d", getId());
}
public Exception exception = null;
}
class Zensko extends Thread {
//TODO: Definicija na promenlivi za sostojbata
public void ucestvo() throws InterruptedException {
//TODO: da se implementira
zLock.acquire();
show.presobleci();
zLock.release();
partnerLock.release();
tancLock.acquire();
}
@Override
public void run() {
try {
ucestvo();
} catch (InterruptedException e) {
// Do nothing
} catch (Exception e) {
exception = e;
hasException = true;
}
}
@Override
public String toString() {
return String.format("z\t%d", getId());
}
public Exception exception = null;
}
public static void main(String[] args) {
try {
TancSoStudentite environment = new TancSoStudentite();
environment.start();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void start() throws Exception {
show = new Show();
init();
HashSet<Thread> threads = new HashSet<Thread>();
for (int i = 0; i < BROJ_INSTANCI; i++) {
Zensko z = new Zensko();
Masko m = new Masko();
threads.add(z);
threads.add(m);
}
for (Thread t : threads) {
t.start();
}
boolean valid = true;
for (Thread t : threads) {
if (!hasException) {
t.join();
} else {
t.interrupt();
}
}
show.printStatus();
}
public class Show {
public static final int BROJ_GARDEROBA = 10;
public static final int BROJ_TEREN = 3;
public static final int TYPE_MASKO = 1;
public static final int TYPE_ZENSKO = 2;
public static final int TYPE_UNKNOWN = -1;
public Show() {
}
public int brojMaskiGarderoba = 0;
public int brojZenskiGarderoba = 0;
public int brojTancuvanja = 0;
public int maxMaskiGarderoba = 0;
public int maxZenskiGarderoba = 0;
public int maxTancuvanja = 0;
public void presobleci() throws RuntimeException {
log(null, "presobleci start");
Thread t = Thread.currentThread();
if (t instanceof Masko) {
synchronized (RANDOM) {
brojMaskiGarderoba++;
if (brojMaskiGarderoba > 10) {
exception("Ne moze da ima poveke od 10 maski vo maskata garderoba.");
}
if (brojMaskiGarderoba > maxMaskiGarderoba) {
maxMaskiGarderoba = brojMaskiGarderoba;
}
}
waitRandom();
synchronized (RANDOM) {
brojMaskiGarderoba--;
}
} else {
synchronized (RANDOM) {
brojZenskiGarderoba++;
if (brojZenskiGarderoba > 10) {
exception("Ne moze da ima poveke od 10 zenski vo zenskata garderoba.");
}
if (brojZenskiGarderoba > maxZenskiGarderoba) {
maxZenskiGarderoba = brojZenskiGarderoba;
}
}
waitRandom();
synchronized (RANDOM) {
brojZenskiGarderoba--;
}
}
log(null, "presobleci kraj");
}
public void tancuvaj() throws RuntimeException {
log(null, "tancuvaj start");
synchronized (RANDOM) {
brojTancuvanja++;
if (brojTancuvanja > BROJ_TEREN) {
exception("Ne moze paralelno da tancuvaat poveke od 3 para.");
}
if (brojTancuvanja > maxTancuvanja) {
maxTancuvanja = brojTancuvanja;
}
}
waitRandom();
synchronized (RANDOM) {
brojTancuvanja--;
}
log(null, "tancuvaj kraj");
}
private void waitRandom() {
try {
int r;
synchronized (RANDOM) {
r = RANDOM.nextInt(RANDOM_RANGE);
}
Thread.sleep(r);
} catch (Exception e) {
//do nothing
}
}
private void exception(String message) {
RuntimeException e = new RuntimeException(message);
log(e, null);
hasError = true;
throw e;
}
public int getType() {
Thread t = Thread.currentThread();
if (t instanceof Masko) {
return TYPE_MASKO;
} else if (t instanceof Zensko) {
return TYPE_ZENSKO;
} else {
return TYPE_UNKNOWN;
}
}
private synchronized void log(RuntimeException e, String action) {
Thread t = Thread.currentThread();
if (e == null) {
actions.add(t.toString() + "\t(a): " + action);
} else {
actions.add(t.toString() + "\t(e): " + e.getMessage());
}
}
public synchronized void printLog() {
System.out.println("Poradi konkurentnosta za pristap za pecatenje, mozno e nekoja od porakite da ne e na soodvetnoto mesto.");
System.out.println("Log na izvrsuvanje na akciite:");
System.out.println("=========================");
System.out.println("(tip m<=>Masko, tip z<=>Zensko)");
System.out.println("tip\tid\takcija/error");
System.out.println("=========================");
for (String l : actions) {
System.out.println(l);
}
}
public void printStatus() {
if (!hasError) {
int poeni = 25;
System.out.println("Procesot e uspesno sinhroniziran");
if (show.maxMaskiGarderoba == 1 || show.maxZenskiGarderoba == 1) {
System.out.println("\t-no ima maksimum eden ucesnik vo garderobata.");
poeni -= 5;
}
if (show.maxTancuvanja == 1) {
System.out.println("\t-no ima maksimum edna proverka vo eden moment.");
poeni -= 5;
}
System.out.println("Osvoeni poeni: " + poeni);
} else {
System.out.println("Procesot ne e sinhroniziran spored uslovite na zadacata");
show.printLog();
System.out.println("Maksimum mozni poeni: 15");
}
}
private List<String> actions = new ArrayList<String>();
private boolean hasError = false;
}
// Konstanti
public static int BROJ_INSTANCI = 1000;
public static final Random RANDOM = new Random();
public static final int RANDOM_RANGE = 3;
// Instanca od bafferot
public Show show;
public boolean hasException = false;
}