-
Notifications
You must be signed in to change notification settings - Fork 0
/
BeerDrinkers.java
116 lines (93 loc) · 2.46 KB
/
BeerDrinkers.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
/*
Juan Guillen Scholten.
Leiden University.
An example of monitor use in Java.
There are N drinkers drinking beer in a pub. After a while some of them want to
go to the toilet, unfortunately there is just one available. So only
one drinker can enjoy the toilet at the same time and the rest must wait.
*/
// The monitor class.
class Toilet {
private boolean occupied = false;
// Try to enter the toilet. If occupied then drinker has to wait, else drinker
// can enter.
synchronized void try_to_enter() {
while (occupied) {
try {
wait();
} catch (InterruptedException e) {
// In Java you always have to catch an exception when doing a wait.
System.out.println("InterruptedException caught");
}
}
occupied = true;
}
// Leave the toilet. Do a notify for if somebody else is waiting.
synchronized void leave_toilet() {
occupied = false;
notify(); // same as signal
}
}
class Drinker extends Thread {
int id;
private Toilet toilet;
// constructor
Drinker(int number, Toilet a) {
super("Drinker" + number);
this.id = number;
toilet = a;
}
public void run() {
int times;
int index = 0;
int sleeptime;
// number of times going to the toilet
times = (int) (Math.random() * 20);
while (index < times) {
// Drinking beer.
sleeptime = (int) (Math.random() * 2000); // 2000 = 2 sec.
try {
sleep(sleeptime);
} catch (Exception e) {
}
// Trying to enter the toilet.
System.out.println("Drinker " + id + " wants to go to the toilet.");
toilet.try_to_enter();
// Using the toilet.
System.out.println("Drinker " + id + " enters the toilet.");
sleeptime = (int) (Math.random() * 2000);
try {
sleep(sleeptime);
} catch (Exception e) {
}
// Leaving the toilet.
System.out.println("Drinker " + id + " leaves the toilet and says: NEXT!");
System.out.println(" ");
toilet.leave_toilet();
index++;
}
System.out.println("Drinker " + id + " goes home.");
System.out.println(" ");
}
}
public class BeerDrinkers {
// The entry point of the program
public static void main(String args[]) {
Toilet toilet;
// Number of drinkers
int N = 4;
int i;
System.out.println("Amount of drinkers : " + N + ".");
System.out.println(" ");
toilet = new Toilet();
Drinker[] drinker = new Drinker[N];
// Creating the drinkers.
for (i = 0; i < N; i++) {
drinker[i] = new Drinker(i, toilet);
}
// Starting the drinkers.
for (i = 0; i < N; i++) {
drinker[i].start();
}
}
}