-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMuseum.java
More file actions
129 lines (109 loc) · 3.06 KB
/
Museum.java
File metadata and controls
129 lines (109 loc) · 3.06 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
/*
* Instructions
* Use javac then java to compile and run Museum.java like a regular java file
*
* Run the program a few different times to see how different threads interleave
*
* Assumptions
* The Museum starts out empty and closed. If the Museum is closed and empty the
* Museum is automatically signaled to open. 2 interleaving threads randomly generate
* enter or exit actions (if logical conditions for entering or exiting are satisfied)
* There can be a maximum of 5 people in the museum at a time. The signal to close the
* Museum is generated randomly. The system runs for a finite time and terminates only
* when the Museum is empty.
*/
import java.util.concurrent.Semaphore;
public class Museum extends Thread{
public static final int N = 5;
public static class Counter{
private int value = 0;
private int Iter = 0;
private boolean open = false;
public int getValue() {
return value;
}
public void enter() {
int reg = this.value;
reg += 1;
this.value = reg;
}
public void exit() {
int reg = this.value;
reg -= 1;
this.value = reg;
}
public void closeSignal() {
this.open = false;
}
public void openSignal() {
this.open = true;
}
}
static Semaphore mutex = new Semaphore(1);
Counter counter = null;
public Museum(Counter sharedCounter) {
super();
this.counter = sharedCounter;
System.out.println("Thread Created");
}
@Override
public void run() {
boolean done = false;
while(!done) {
try {
mutex.acquire();
} catch (InterruptedException e) {
e.printStackTrace();
}
//the museum is closed
if (!this.counter.open) {
//if the director has signalled that the museum is closed and the person is still inside then the person will exit
if (this.counter.getValue() > 0) {
this.counter.exit();
System.out.println(this.getName() + " exit");
}
//the museum is open
} else {
int randomNum = (Math.random() <= 0.5) ? 1 : 2;
if (randomNum == 1) {
if (this.counter.getValue() < N) {
this.counter.enter();
System.out.println(this.getName() + " enter");
this.counter.Iter +=1;
}
} else {
if (this.counter.getValue() > 0) {
this.counter.exit();
System.out.println(this.getName() + " exit");
}
}
}
//if the museum is closed and has been emptied automatically signal for it to open
if (this.counter.getValue() == 0 && !this.counter.open) {
System.out.println("Museum is open");
this.counter.openSignal();
}
if (((int)(Math.random() * ((5 - 1) + 1)) + 1) == 1 && this.counter.open) {
System.out.println("Museum is closed");
this.counter.closeSignal();
}
if (this.counter.Iter > 5 && this.counter.getValue() == 0) {
done = true;
}
mutex.release();
}
}
public static void main(String[] args) {
Counter counter = new Counter();
Museum p1 = new Museum(counter);
Museum p2 = new Museum(counter);
p1.start();
p2.start();
try {
p1.join();
p2.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}