-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathTemple.java
More file actions
154 lines (136 loc) · 3.98 KB
/
Copy pathTemple.java
File metadata and controls
154 lines (136 loc) · 3.98 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
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
/*
* A temple has a number of young monks and old monks,
* there is a water tank,
* by the young monk to carry water into the tank for the old monk to drink.
* The water tank can hold 10 barrels of water,
* and the water is taken from the same well.
* The well is narrow and can only hold one bucket at a time.
* The total number of buckets is 3.
* Only one bucket of water is taken each time,
* and cannot be carried out at the same time.
* The algorithm description of water intake and water intake from cylinder is given.
*/
public class Temple {
public static void main(String[] args) {
Semaphores semaphores = new Semaphores();
Thread younger1 = new Thread(new Younger(semaphores, 1));
Thread younger2 = new Thread(new Younger(semaphores, 2));
Thread older1 = new Thread(new Older(semaphores, 1));
Thread older2 = new Thread(new Older(semaphores, 2));
younger1.start();
younger2.start();
older1.start();
older2.start();
}
}
class Semaphores {
public boolean well = true;
public boolean vat = true;
public int empty = 10;
public int full = 0;
public int pail = 3;
public synchronized boolean P_well() {
if (well) {
well = false;
return true;
} else {
return false;
}
}
public synchronized boolean P_vat() {
if (vat) {
vat = false;
return true;
} else {
return false;
}
}
public synchronized boolean P_empty() {
if (empty > 0) {
empty--;
return true;
} else {
return false;
}
}
public synchronized boolean P_full() {
if (full > 0) {
full--;
return true;
} else {
return false;
}
}
public synchronized boolean P_pail() {
if (pail > 0) {
pail--;
return true;
} else {
return false;
}
}
public synchronized void V_well() {
well = true;
}
public synchronized void V_vat() {
vat = true;
}
public synchronized void V_empty() {
empty++;
}
public synchronized void V_full() {
full++;
}
public synchronized void V_pail() {
pail++;
}
}
class Older implements Runnable {
public Semaphores semaphores;
public int rank;
public Older(Semaphores semaphores, int rank) {
this.semaphores = semaphores;
this.rank = rank;
}
public void run() {
while (true) {
while (!semaphores.P_full()) {
}
while (!semaphores.P_pail()) {
}
while (!semaphores.P_vat()) {
}
System.out.println("Old monk " + rank + " gets a bucket of water from the tank.");
semaphores.V_vat();
semaphores.V_empty();
System.out.println("Old monk " + rank + " drinks water.");
semaphores.V_pail();
}
}
}
class Younger implements Runnable {
public Semaphores semaphores;
public int rank;
public Younger(Semaphores semaphores, int rank) {
this.semaphores = semaphores;
this.rank = rank;
}
public void run() {
while (true) {
while (!semaphores.P_empty()) {
}
while (!semaphores.P_pail()) {
}
while (!semaphores.P_well()) {
}
System.out.println("Young monk " + rank + " gets a bucket of water from the well.");
semaphores.V_well();
while (!semaphores.P_vat()) {
}
System.out.println("Younge monk " + rank + " pour the water to the tank.");
semaphores.V_vat();
semaphores.V_full();
semaphores.V_pail();
}
}
}