-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
101 lines (80 loc) · 2.32 KB
/
Main.java
File metadata and controls
101 lines (80 loc) · 2.32 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
import java.util.concurrent.locks.*;
import java.util.concurrent.atomic.*;
class Main {
static BathroomLock lock;
static AtomicInteger males, females, clashes;
static int ML = 100, FM = 100;
static int CS = 2;
// males: number of males in bathroom
// females: number of females in bathroom
// clashes: times males & females were together
// ML: number of males
// FM: number of females
// CS: checks per person (thread)
static Thread male(int id, boolean safe) {
return new Thread(() -> {
if (safe) lock.genderLock(0).lock();
males.incrementAndGet();
for (int i=0; i<CS; i++) {
sleep(1);
int f = females.get();
if (f == 0) continue;
clashes.incrementAndGet();
log("M"+id+": saw "+f+" females");
}
males.decrementAndGet();
if (safe) lock.genderLock(0).unlock();
});
}
static Thread female(int id, boolean safe) {
return new Thread(() -> {
if (safe) lock.genderLock(1).lock();
females.incrementAndGet();
for (int i=0; i<CS; i++) {
sleep(1);
int m = males.get();
if (m == 0) continue;
clashes.incrementAndGet();
log("F"+id+": saw "+m+" males");
}
females.decrementAndGet();
if (safe) lock.genderLock(1).unlock();
});
}
// Tests to see if males and females entered
// bathroom separately.
static void testThreads(boolean safe) {
String type = safe? "safe" : "unsafe";
log("Starting "+ML+" "+type+" males ...");
log("Starting "+FM+" "+type+" females ...");
males.set(0);
females.set(0);
clashes.set(0);
Thread[] t = new Thread[ML+FM];
for (int i=0; i<ML+FM; i++) {
t[i] = i<ML? male(i, safe) : female(i, safe);
t[i].start();
}
try {
for (int i=0; i<ML+FM; i++)
t[i].join();
}
catch(InterruptedException e) {}
log("Clashes occurred: "+clashes.get()+"\n");
}
public static void main(String[] args) {
lock = new BathroomLock(2);
males = new AtomicInteger(0);
females = new AtomicInteger(0);
clashes = new AtomicInteger(0);
testThreads(false);
testThreads(true);
}
static void sleep(long ms) {
try { Thread.sleep(ms); }
catch (InterruptedException e) {}
}
static void log(String x) {
System.out.println(x);
}
}