1 Star 4 Fork 0

sumAll/Java编程思想(第四版)

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
practice27.java 2.96 KB
一键复制 编辑 原始数据 按行查看 历史
sumAll 提交于 4年前 . java
package com.my.chapter21;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;
public class practice27 {
public static void main(String[] args) {
new Restaurant3();
}
}
class WaitPerson3 implements Runnable {
private Restaurant3 restaurant;
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
public WaitPerson3(Restaurant3 r) { restaurant = r; }
public void run() {
try {
while(!Thread.interrupted()) {
lock.lock();
try {
while(restaurant.meal == null)
condition.await();//等待厨师制作meal
} finally {
lock.unlock();
}
System.out.println("Waitperson got " + restaurant.meal);
restaurant.chef.lock.lock();
try {
restaurant.meal = null;//拿走meal并通知厨师制作下一份
restaurant.chef.condition.signalAll();
} finally {
restaurant.chef.lock.unlock();
}
}
} catch(InterruptedException e) {
System.out.println("WaitPerson interrupted");
}
}
}
class Chef3 implements Runnable {
private Restaurant3 restaurant;
private int count;
Lock lock = new ReentrantLock();
Condition condition = lock.newCondition();
public Chef3(Restaurant3 r) { restaurant = r; }
public void run() {
try {
while(!Thread.interrupted()) {
lock.lock();
try {
while(restaurant.meal != null)
condition.await();//等待服务员拿走meal
} finally {
lock.unlock();
}
if(++count == 10) {
System.out.println("Out of food, closing");
restaurant.exec.shutdownNow();
}
System.out.print("Order up! ");
restaurant.waitPerson.lock.lock();
try {
restaurant.meal = new Meal(count);//制作meal,并通知服务员拿走meal
restaurant.waitPerson.condition.signalAll();
} finally {
restaurant.waitPerson.lock.unlock();
}
TimeUnit.MILLISECONDS.sleep(100);
}
} catch(InterruptedException e) {
System.out.println("Chef interrupted");
}
}
}
class Restaurant3 {
Meal meal;
ExecutorService exec = Executors.newCachedThreadPool();
WaitPerson3 waitPerson = new WaitPerson3(this);
Chef3 chef = new Chef3(this);
public Restaurant3() {
exec.execute(chef);
exec.execute(waitPerson);
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/sumall/Thinking-in-Java.git
git@gitee.com:sumall/Thinking-in-Java.git
sumall
Thinking-in-Java
Java编程思想(第四版)
master

搜索帮助