-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathVideoShow.java
More file actions
174 lines (155 loc) · 4.53 KB
/
Copy pathVideoShow.java
File metadata and controls
174 lines (155 loc) · 4.53 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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*
* Suppose a video hall has 1, 2, 3 different video films
* that can be selected by the audience.
* The screening rules of the video hall are as follows:
* 1. A maximum of one video film can be shown at any given time.
* The current video film is played in an automatic loop
* until the last member of the audience voluntarily leaves.
* 2. Viewers who choose the current video film playing can enter immediately,
* allowing multiple viewers who choose the same video to watch at the same time,
* and the number of viewers watching at the same time is unlimited.
* 3. The audience waiting to watch other video films will line up in the order of arrival.
* When a new video film starts to be shown,
* all the audience waiting to watch that video film can enter the video hall
* in the order to watch it at the same time.
* Use a process to represent an audience.
*/
public class VideoShow {
public static void main(String[] args) {
Semaphores semaphores = new Semaphores();
Thread show1 = new Thread(new VideoShow1(semaphores));
Thread show2 = new Thread(new VideoShow2(semaphores));
Thread show3 = new Thread(new VideoShow3(semaphores));
show1.start();
show2.start();
show3.start();
}
}
class Semaphores {
public boolean s = true;
public boolean s0 = true;
public boolean s1 = true;
public boolean s2 = true;
public int count0 = 0;
public int count1 = 0;
public int count2 = 0;
public synchronized boolean ps() {
if (s) {
s = false;
return true;
} else {
return false;
}
}
public synchronized boolean ps0() {
if (s0) {
s0 = false;
return true;
} else {
return false;
}
}
public synchronized boolean ps1() {
if (s1) {
s1 = false;
return true;
} else {
return false;
}
}
public synchronized boolean ps2() {
if (s2) {
s2 = false;
return true;
} else {
return false;
}
}
public synchronized void vs() {
s = true;
}
public synchronized void vs0() {
s0 = true;
}
public synchronized void vs1() {
s1 = true;
}
public synchronized void vs2() {
s2 = true;
}
}
class VideoShow1 implements Runnable {
public Semaphores semaphores;
public VideoShow1(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (!semaphores.ps0()) {
}
semaphores.count0++;
if (semaphores.count0 == 1) {
while (!semaphores.ps()) {
}
}
semaphores.vs0();
System.out.println("Video show 1 begins.");
while (!semaphores.ps0()) {
}
semaphores.count0--;
if (semaphores.count0 == 0) {
System.out.println("Video show 1 ends.");
semaphores.vs();
}
semaphores.vs0();
}
}
class VideoShow2 implements Runnable {
public Semaphores semaphores;
public VideoShow2(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (!semaphores.ps1()) {
}
semaphores.count1++;
if (semaphores.count1 == 1) {
while (!semaphores.ps()) {
}
}
semaphores.vs1();
System.out.println("Video show 2 begins.");
while (!semaphores.ps1()) {
}
semaphores.count1--;
if (semaphores.count1 == 0) {
System.out.println("Video show 2 ends.");
semaphores.vs();
}
semaphores.vs1();
}
}
class VideoShow3 implements Runnable {
public Semaphores semaphores;
public VideoShow3(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (!semaphores.ps2()) {
}
semaphores.count2++;
if (semaphores.count2 == 1) {
while (!semaphores.ps()) {
}
}
semaphores.vs2();
System.out.println("Video show 3 begins.");
while (!semaphores.ps2()) {
}
semaphores.count2--;
if (semaphores.count2 == 0) {
System.out.println("Video show 3 ends.");
semaphores.vs();
}
semaphores.vs2();
}
}