-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWaiter.java
More file actions
48 lines (39 loc) · 1.09 KB
/
Waiter.java
File metadata and controls
48 lines (39 loc) · 1.09 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 Thread.ProductAndResumer;
import java.util.concurrent.TimeUnit;
//消费者
public class Waiter implements Runnable{
private Resturant resturant;
public Waiter(Resturant resturant){
this.resturant=resturant;
}
@Override
public void run() {
try{
while(!Thread.interrupted()){
//要把sleep放在上面,不这样,chef还在阻塞状态,waiter的wait出错
TimeUnit.MILLISECONDS.sleep(1);
//1.检差如果没有食物,挂起自己
synchronized (this) {
while(resturant.meal==null){
try {
this.wait();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
//2.有食物,输出食物信息
System.out.println("客人获得"+resturant.meal.toString());
TimeUnit.MILLISECONDS.sleep(200);
//3.食物吃完,通知厨师做菜
synchronized (resturant.chef) {
resturant.meal=null;
resturant.chef.notifyAll();
}
}
}catch(InterruptedException exception){
System.out.println("服务生线程结束");
}
}
}