forked from Java2ArkTS/Java2ArkTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmokers.java
More file actions
149 lines (128 loc) · 3.29 KB
/
Copy pathSmokers.java
File metadata and controls
149 lines (128 loc) · 3.29 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
public class Smokers {
public static void main(String[] args) {
Semaphores semaphores = new Semaphores();
Thread thread1 = new Thread(new P1(semaphores));
Thread thread2 = new Thread(new P2(semaphores));
Thread thread3 = new Thread(new P3(semaphores));
Thread thread4 = new Thread(new P4(semaphores));
thread1.start();
thread2.start();
thread3.start();
thread4.start();
}
}
class Semaphores {
public int num = 0;
public boolean offer1 = false;
public boolean offer2 = false;
public boolean offer3 = false;
public boolean finish = false;
public synchronized void V_offer1() {
offer1 = true;
}
public synchronized void V_offer2() {
offer2 = true;
}
public synchronized void V_offer3() {
offer3 = true;
}
public synchronized void V_finish() {
finish = true;
}
public synchronized boolean P_finish() {
if (finish) {
finish = false;
return true;
} else {
return false;
}
}
public synchronized boolean P_offer1() {
if (offer1) {
offer1 = false;
return true;
} else {
return false;
}
}
public synchronized boolean P_offer2() {
if (offer2) {
offer2 = false;
return true;
} else {
return false;
}
}
public synchronized boolean P_offer3() {
if (offer3) {
offer3 = false;
return true;
} else {
return false;
}
}
}
class P1 implements Runnable {
public Semaphores semaphores;
public P1(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (true) {
semaphores.num++;
semaphores.num %= 3;
if (semaphores.num == 0) {
semaphores.V_offer1();
} else if (semaphores.num == 1) {
semaphores.V_offer2();
} else {
semaphores.V_offer3();
}
System.out.println("P1 put things onto the table.");
while (!semaphores.P_finish()) {
}
}
}
}
class P2 implements Runnable {
public Semaphores semaphores;
public P2(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (true) {
while (!semaphores.P_offer3()) {
}
System.out.println("P2 is smoking.");
semaphores.V_finish();
}
}
}
class P3 implements Runnable {
public Semaphores semaphores;
public P3(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (true) {
while (!semaphores.P_offer2()) {
}
System.out.println("P3 is smoking.");
semaphores.V_finish();
}
}
}
class P4 implements Runnable {
public Semaphores semaphores;
public P4(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (true) {
while (!semaphores.P_offer1()) {
}
System.out.println("P4 is smoking.");
semaphores.V_finish();
}
}
}