forked from Java2ArkTS/Java2ArkTS
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLetterBox.java
More file actions
169 lines (149 loc) · 4.22 KB
/
Copy pathLetterBox.java
File metadata and controls
169 lines (149 loc) · 4.22 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
/*
* There are two people, A and B, debating through the mailbox,
* each taking the other's question from their own mailbox.
* Put the answers and new questions in an email in their inbox.
* Assume that A's mailbox stores a maximum of M emails,
* and B's mailbox stores a maximum of N emails.
* At the beginning, x emails are in the mailbox of A
* and y emails are in the mailbox of B.
* Each time the debater takes out a message,
* the number of messages is reduced by one.
* When the mailbox is not empty, the debater can get the mail from the mailbox,
* otherwise wait.
* When the mailbox is not satisfied, the debater can put a new mail into the mailbox,
* otherwise wait.
* Synchronize A and B.
*/
public class LetterBox {
public static void main(String[] args) {
Semaphores semaphores = new Semaphores();
Thread a = new Thread(new A(semaphores));
Thread b = new Thread(new B(semaphores));
a.start();
b.start();
}
}
class Semaphores {
public int fullA = 1; // x
public int emptyA = 2; // M - x = 3 - 1 = 2
public int fullB = 2; // y
public int emptyB = 3; // N - y = 5 - 2 = 3
public boolean mutexA = true;
public boolean mutexB = true;
public synchronized boolean pFullA() {
if (fullA > 0) {
fullA--;
return true;
} else {
return false;
}
}
public synchronized boolean pEmptyA() {
if (emptyA > 0) {
emptyA--;
return true;
} else {
return false;
}
}
public synchronized boolean pFullB() {
if (fullB > 0) {
fullB--;
return true;
} else {
return false;
}
}
public synchronized boolean pEmptyB() {
if (emptyB > 0) {
emptyB--;
return true;
} else {
return false;
}
}
public synchronized boolean pMutexA() {
if (mutexA) {
mutexA = false;
return true;
} else {
return false;
}
}
public synchronized boolean pMutexB() {
if (mutexB) {
mutexB = false;
return true;
} else {
return false;
}
}
public synchronized void vFullA() {
fullA++;
}
public synchronized void vEmptyA() {
emptyA++;
}
public synchronized void vFullB() {
fullB++;
}
public synchronized void vEmptyB() {
emptyB++;
}
public synchronized void vMutexA() {
mutexA = true;
}
public synchronized void vMutexB() {
mutexB = true;
}
}
class A implements Runnable {
public Semaphores semaphores;
public A(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (true) {
while (!semaphores.pFullA()) {
}
while (!semaphores.pMutexA()) {
}
System.out.println("A gets a letter from email box A.");
semaphores.vMutexA();
semaphores.vEmptyA();
System.out.println("A answers the question and raises a new question.");
while (!semaphores.pEmptyB()) {
}
while (!semaphores.pMutexB()) {
}
System.out.println("A puts the new letter into email box B.");
semaphores.vMutexB();
semaphores.vFullB();
}
}
}
class B implements Runnable {
public Semaphores semaphores;
public B(Semaphores semaphores) {
this.semaphores = semaphores;
}
public void run() {
while (true) {
while (!semaphores.pFullB()) {
}
while (!semaphores.pMutexB()) {
}
System.out.println("B gets a letter from email box B.");
semaphores.vMutexB();
semaphores.vEmptyB();
System.out.println("B answers the question and raises a new question.");
while (!semaphores.pEmptyA()) {
}
while (!semaphores.pMutexA()) {
}
System.out.println("B puts the new letter into email box A.");
semaphores.vMutexA();
semaphores.vFullA();
}
}
}