-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathExit.java
More file actions
77 lines (68 loc) · 2.05 KB
/
Copy pathExit.java
File metadata and controls
77 lines (68 loc) · 2.05 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
/*
* A museum can accommodate up to 500 people at a time and has one entrance,
* which only allows one person to pass through at a time.
* Synchronization between visitors.
*/
public class Exit {
public static void main(String[] args) {
Semaphores semaphores = new Semaphores();
Thread visitor1 = new Thread(new Visitor(semaphores, 1));
Thread visitor2 = new Thread(new Visitor(semaphores, 2));
Thread visitor3 = new Thread(new Visitor(semaphores, 3));
Thread visitor4 = new Thread(new Visitor(semaphores, 4));
Thread visitor5 = new Thread(new Visitor(semaphores, 5));
visitor1.start();
visitor2.start();
visitor3.start();
visitor4.start();
visitor5.start();
}
}
class Semaphores {
public int empty = 500;
public boolean mutex = true;
public synchronized boolean pEmpty() {
if (empty > 0) {
empty--;
return true;
} else {
return false;
}
}
public synchronized boolean pMutex() {
if (mutex) {
mutex = false;
return true;
} else {
return false;
}
}
public synchronized void vEmpty() {
empty++;
}
public synchronized void vMutex() {
mutex = true;
}
}
class Visitor implements Runnable {
public Semaphores semaphores;
public int rank;
public Visitor(Semaphores semaphores, int rank) {
this.semaphores = semaphores;
this.rank = rank;
}
public void run() {
while (!semaphores.pEmpty()) {
}
while (!semaphores.pMutex()) {
}
System.out.println("Visitor " + rank + " enters the museum.");
semaphores.vMutex();
System.out.println("Visitor " + rank + " visits the museum.");
while (!semaphores.pMutex()) {
}
System.out.println("Visitor " + rank + " leaves the museum.");
semaphores.vMutex();
semaphores.vEmpty();
}
}