使用线程模拟餐厅的运营(必须用线程池): 服务员: 1. 餐厅有两位长期服务员 (核心线程) 2. 餐厅可以多招一个临时服务员 3. 临时服务员如果六十秒没活干就炒掉 4. 在餐厅门外只有十个位置让客人等待, 满了再来客直接抛异常 AbortPolicy 流程: 1. 每位服务员都需要去厨房取食物 2. 厨房需要预备食物让服务员领取 - 只有一位厨师预备食物 - 每秒随机炒一个菜, 最多可存十个菜 3. 每次只能一位服务员进厨房, 取得食物后打印出食物名称, 耗时三秒 4. 食客命令开始上菜, 菜品直接从厨房拿随机的, 不断运行 5. 食客手动不断点菜, 直至他想自己离开,请用java代码实现
时间: 2023-03-28 07:01:16 浏览: 88
Java多线程:多生产者和消费者的餐厅运行模拟
好的,我可以回答这个问题。以下是使用线程模拟餐厅运营的Java代码:
```java
import java.util.concurrent.*;
public class Restaurant {
private static final int CORE_THREADS = 2;
private static final int MAX_THREADS = 3;
private static final int QUEUE_CAPACITY = 10;
private static final int WAITING_CAPACITY = 10;
private static final int COOK_TIME = 100;
private static final int WAIT_TIME = 300;
private static final int TEMP_WAIT_TIME = 60000;
public static void main(String[] args) {
ExecutorService executor = new ThreadPoolExecutor(CORE_THREADS, MAX_THREADS, L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<>(QUEUE_CAPACITY), new ThreadPoolExecutor.AbortPolicy());
BlockingQueue<String> kitchen = new ArrayBlockingQueue<>(10);
Semaphore waitingArea = new Semaphore(WAITING_CAPACITY);
Cook cook = new Cook(kitchen);
executor.execute(cook);
for (int i = 1; i <= CORE_THREADS; i++) {
Waiter waiter = new Waiter(i, kitchen, waitingArea);
executor.execute(waiter);
}
executor.execute(new TempWaiter(kitchen, waitingArea, TEMP_WAIT_TIME));
while (true) {
try {
waitingArea.acquire();
executor.execute(new Customer(kitchen, waitingArea));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
static class Cook implements Runnable {
private final BlockingQueue<String> kitchen;
public Cook(BlockingQueue<String> kitchen) {
this.kitchen = kitchen;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(COOK_TIME);
if (kitchen.size() < 10) {
kitchen.put("菜品" + (kitchen.size() + 1));
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class Waiter implements Runnable {
private final int id;
private final BlockingQueue<String> kitchen;
private final Semaphore waitingArea;
public Waiter(int id, BlockingQueue<String> kitchen, Semaphore waitingArea) {
this.id = id;
this.kitchen = kitchen;
this.waitingArea = waitingArea;
}
@Override
public void run() {
while (true) {
try {
String food = kitchen.take();
System.out.println("服务员" + id + "取得" + food + ",准备上菜");
Thread.sleep(WAIT_TIME);
waitingArea.release();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class TempWaiter implements Runnable {
private final BlockingQueue<String> kitchen;
private final Semaphore waitingArea;
private final int waitTime;
public TempWaiter(BlockingQueue<String> kitchen, Semaphore waitingArea, int waitTime) {
this.kitchen = kitchen;
this.waitingArea = waitingArea;
this.waitTime = waitTime;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(waitTime);
if (waitingArea.availablePermits() == WAITING_CAPACITY) {
System.out.println("临时服务员炒掉了");
break;
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
static class Customer implements Runnable {
private final BlockingQueue<String> kitchen;
private final Semaphore waitingArea;
public Customer(BlockingQueue<String> kitchen, Semaphore waitingArea) {
this.kitchen = kitchen;
this.waitingArea = waitingArea;
}
@Override
public void run() {
try {
System.out.println("有顾客进入餐厅");
String food = kitchen.take();
System.out.println("顾客点了" + food);
Thread.sleep(WAIT_TIME);
System.out.println("顾客吃完了" + food);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
waitingArea.release();
}
}
}
}
```
在这个代码中,我们使用了线程池来管理服务员和临时服务员的线程。厨师、服务员和临时服务员都是实现了Runnable接口的类,它们的run()方法中包含了具体的逻辑。我们使用了BlockingQueue来模拟厨房和服务员之间的通信,Semaphore来模拟等待区域的限制。在主函数中,我们首先创建了一个线程池,然后创建了一个厨师线程和两个服务员线程,以及一个临时服务员线程。最后,我们使用一个无限循环来模拟顾客的不断到来。当顾客到来时,我们首先尝试获取一个等待区域的许可,然后创建一个顾客线程,让它去取菜、吃饭和离开。当顾客离开时,我们释放一个等待区域的许可,以便下一个顾客可以进来。
阅读全文