代码拉取完成,页面将自动刷新
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);
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。