forked from JustinSDK/JavaSE6Tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClerk.java
More file actions
48 lines (40 loc) · 1.26 KB
/
Clerk.java
File metadata and controls
48 lines (40 loc) · 1.26 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
package onlyfun.caterpillar;
public class Clerk {
// -1 表示目前沒有產品
private int product = -1;
// 這個方法由生產者呼叫
public synchronized void setProduct(int product) {
if(this.product != -1) {
try {
// 目前店員沒有空間收產品,請稍候!
wait();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
this.product = product;
System.out.printf("生產者設定 (%d)%n", this.product);
// 通知等待區中的一個消費者可以繼續工作了
notify();
}
// 這個方法由消費者呼叫
public synchronized int getProduct() {
if(this.product == -1) {
try {
// 缺貨了,請稍候!
wait();
}
catch(InterruptedException e) {
e.printStackTrace();
}
}
int p = this.product;
System.out.printf(
"消費者取走 (%d)%n", this.product);
this.product = -1;
// 通知等待區中的一個生產者可以繼續工作了
notify();
return p;
}
}