forked from Java2ArkTS/Java2ArkTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBike.java
More file actions
157 lines (136 loc) · 3.67 KB
/
Copy pathBike.java
File metadata and controls
157 lines (136 loc) · 3.67 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
155
156
157
/*
* There is a box in the bicycle production line,
* which has five positions, each of which can store a frame or a wheel.
* There are three workers,
* the workers who produce the wheels,
* the workers who produce the frames,
* and the workers who assemble the bicycles.
* Enables three workers to cooperate without deadlock.
*/
public class Bike {
public static void main(String[] args) {
Semaphores semaphores = new Semaphores();
Thread worker1 = new Thread(new Worker1(semaphores));
Thread worker2 = new Thread(new Worker2(semaphores));
Thread worker3 = new Thread(new Worker3(semaphores));
worker1.start();
worker2.start();
worker3.start();
}
}
class Semaphores {
public int empty = 5;
public int wheel = 0;
public int frame = 0;
public int s1 = 3;
public int s2 = 4;
public synchronized boolean ps1() {
if (s1 > 0) {
s1--;
return true;
}
return false;
}
public synchronized boolean ps2() {
if (s2 > 0) {
s2--;
return true;
}
return false;
}
public synchronized boolean pEmpty() {
if (empty > 0) {
empty--;
return true;
}
return false;
}
public synchronized boolean pWheel() {
if (wheel > 0) {
wheel--;
return true;
}
return false;
}
public synchronized boolean pFrame() {
if (frame > 0) {
frame--;
return true;
}
return false;
}
public synchronized void vs1() {
s1++;
}
public synchronized void vs2() {
s2++;
}
public synchronized void vEmpty() {
empty++;
}
public synchronized void vWheel() {
wheel++;
}
public synchronized void vFrame() {
frame++;
}
}
class Worker1 implements Runnable {
public Semaphores semaphores;
public Worker1(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (true) {
System.out.println("Worker1 produces a frame.");
while (!semaphores.ps1()) {
}
while (!semaphores.pEmpty()) {
}
System.out.println("Worker1 puts a frame into the box.");
semaphores.vFrame();
}
}
}
class Worker2 implements Runnable {
public Semaphores semaphores;
public Worker2(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (true) {
System.out.println("Worker2 produces a wheel.");
while (!semaphores.ps2()) {
}
while (!semaphores.pEmpty()) {
}
System.out.println("Worker2 puts a wheel into the box.");
semaphores.vWheel();
}
}
}
class Worker3 implements Runnable {
public Semaphores semaphores;
public Worker3(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (true) {
while (!semaphores.pFrame()) {
}
System.out.println("Worker3 gets a frame from the box.");
semaphores.vEmpty();
semaphores.vs1();
while (!semaphores.pWheel()) {
}
while (!semaphores.pWheel()) {
}
System.out.println("Worker3 gets two wheels from the box.");
semaphores.vEmpty();
semaphores.vEmpty();
semaphores.vs2();
semaphores.vs2();
System.out.println("Worker3 assembles a frame and two wheels to produce a bike.");
}
}
}