使用线程模拟餐厅的运营(必须用线程池): 服务员: 1. 餐厅有两位长期服务员 (核心线程) 2. 餐厅可以多招一个临时服务员 3. 临时服务员如果六十秒没活干就炒掉 4. 在餐厅门外只有十个位置让客人等待, 满了再来客直接抛异常 AbortPolicy 流程: 1. 每位服务员都需要去厨房取食物 2. 厨房需要预备食物让服务员领取 - 只有一位厨师预备食物 - 每秒随机炒一个菜, 最多可存十个菜 3. 每次只能一位服务员进厨房, 取得食物后打印出食物名称, 耗时三秒 进阶: 用Scanner 提供用户介面 1. 食客命令开始上菜, 菜品直接从厨房拿随机的, 不断运行 2. 食客手动不断点菜, 直至他想自己离开
时间: 2023-03-25 10:00:43 浏览: 76
使用线程池模拟餐厅运营的流程如下:
1. 创建一个线程池,包含两个核心线程和一个最大线程数为3的线程池,使用AbortPolicy拒绝策略。
2. 创建两个服务员线程,作为核心线程,一直运行。
3. 创建一个临时服务员线程,作为非核心线程,如果六十秒没有活干则被炒掉。
4. 创建一个厨师线程,预备食物,每秒随机炒一个菜,最多可存十个菜。
5. 创建一个等待队列,最多容纳十个客人,如果队列已满,则抛出异常。
6. 客人可以通过Scanner输入命令,开始上菜或点菜。
7. 如果客人选择开始上菜,则服务员从厨房拿随机的菜品上菜。
8. 如果客人选择点菜,则服务员从客人手中取得菜单,进入厨房取得食物后打印出食物名称,耗时三秒。
9. 如果客人想自己离开,则程序结束。
以上就是使用线程池模拟餐厅运营的流程。
相关问题
利用java代码使用线程模拟餐厅的运营(必须用线程池): 服务员: 1. 餐厅有两位长期服务员 (核心线程) 2. 餐厅可以多招一个临时服务员 3. 临时服务员如果六十秒没活干就炒掉 4. 在餐厅门外只有十个位置让客人等待, 满了再来客直接抛异常 AbortPolicy 流程: 1. 每位服务员都需要去厨房取食物 2. 厨房需要预备食物让服务员领取 - 只有一位厨师预备食物 - 每秒随机炒一个菜, 最多可存十个菜 3. 每次只能一位服务员进厨房, 取得食物后打印出食物名称, 耗时三秒 进阶: 用Scanner 提供用户介面 1. 食客命令开始上菜, 菜品直接从厨房拿随机的, 不断运行 2. 食客手动不断点菜, 直至他想自己离开
以下是利用Java代码使用线程池模拟餐厅运营的流程:
1. 创建一个线程池,其中核心线程数为2,最大线程数为3,线程空闲时间为60秒,使用AbortPolicy拒绝策略。
```java
ExecutorService executorService = new ThreadPoolExecutor(2, 3, 60, TimeUnit.SECONDS, new LinkedBlockingQueue<>(), new ThreadPoolExecutor.AbortPolicy());
```
2. 创建一个厨师线程,每秒随机炒一个菜,最多可存十个菜。
```java
class Chef implements Runnable {
private BlockingQueue<String> dishes = new ArrayBlockingQueue<>(10);
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
String dish = "菜品" + new Random().nextInt(10);
dishes.put(dish);
System.out.println("厨师炒了一道" + dish);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public String getDish() throws InterruptedException {
return dishes.take();
}
}
Chef chef = new Chef();
executorService.execute(chef);
```
3. 创建服务员线程,每次只能一位服务员进厨房,取得食物后打印出食物名称,耗时三秒。
```java
class Waiter implements Runnable {
private Chef chef;
private String name;
public Waiter(Chef chef, String name) {
this.chef = chef;
this.name = name;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(100);
String dish = chef.getDish();
System.out.println(name + "取到了" + dish);
Thread.sleep(300);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
Waiter waiter1 = new Waiter(chef, "服务员1");
Waiter waiter2 = new Waiter(chef, "服务员2");
Waiter waiter3 = new Waiter(chef, "临时服务员");
executorService.execute(waiter1);
executorService.execute(waiter2);
```
4. 在餐厅门外只有十个位置让客人等待,满了再来客直接抛异常AbortPolicy。
```java
class Customer implements Runnable {
private String name;
public Customer(String name) {
this.name = name;
}
@Override
public void run() {
try {
Thread.sleep(100);
System.out.println(name + "进入了餐厅");
executorService.execute(waiter3);
} catch (RejectedExecutionException e) {
System.out.println(name + "被拒绝了");
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
for (int i = 1; i <= 15; i++) {
executorService.execute(new Customer("客人" + i));
}
```
进阶:
1. 食客命令开始上菜,菜品直接从厨房拿随机的,不断运行。
```java
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("输入1开始上菜,输入退出");
int command = scanner.nextInt();
if (command == 1) {
try {
String dish = chef.getDish();
System.out.println("上了一道" + dish);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (command == ) {
break;
}
}
```
2. 食客手动不断点菜,直至他想自己离开。
```java
while (true) {
System.out.println("输入菜品编号点菜,输入退出");
int dishNumber = scanner.nextInt();
if (dishNumber == ) {
break;
} else {
System.out.println("点了一道菜品" + dishNumber);
}
}
```
完整代码如下:
使用线程模拟餐厅的运营: 服务员(必须用线程池): 1. 餐厅有两位长期服务员 (核心线程) 2. 餐厅可以多招一个临时服务员 3. 临时服务员如果六十秒没活干就炒掉 4. 在餐厅门外只有十个位置让客人等待, 满了再来客直接抛异常 AbortPolicy 流程: 1. 每位服务员都需要去厨房取食物 2. 厨房需要预备食物让服务员领取 - 只有一位厨师预备食物 - 每秒随机炒一个菜, 最多可存十个菜 3. 每次只能一位服务员进厨房, 取得食物后打印出食物名称, 耗时三秒 进阶: 用Scanner 提供用户介面 1. 食客命令开始上菜, 菜品直接从厨房拿随机的, 不断运行 2. 食客手动不断点菜, 直至他想自己离开
以下是使用Java线程池模拟餐厅运营的示例代码,包括服务员、厨师和客人的行为,以及用户界面:
```java
import java.util.Random;
import java.util.Scanner;
import java.util.concurrent.*;
public class RestaurantSimulation {
public static void main(String[] args) {
int MAX_WAITING_CUSTOMERS = 10; // 餐厅门外最多可以容纳的等待客人数
int MAX_DISHES = 10; // 厨房最多可以存储的菜品数
int MAX_IDLE_TIME = 60; // 临时服务员的最大空闲时间,单位为秒
int NUM_CORE_WAITERS = 2; // 长期服务员数量
int NUM_MAX_WAITERS = 3; // 长期服务员和临时服务员的最大总数量
int NUM_DISHES_PER_SECOND = 1; // 每秒钟随机炒的菜品数量
BlockingQueue<String> dishQueue = new ArrayBlockingQueue<>(MAX_DISHES); // 菜品队列
ExecutorService waiterPool = Executors.newFixedThreadPool(NUM_CORE_WAITERS); // 服务员线程池
Chef chef = new Chef(dishQueue, NUM_DISHES_PER_SECOND); // 厨师线程
for (int i = 0; i < NUM_CORE_WAITERS; i++) {
waiterPool.submit(new Waiter(dishQueue));
}
Scanner scanner = new Scanner(System.in); // 创建Scanner对象
while (true) {
System.out.println("请选择要进行的操作:");
System.out.println("1. 自动上菜");
System.out.println("2. 点菜");
System.out.println("3. 离开");
String input = scanner.nextLine();
switch (input) {
case "1":
System.out.println("自动上菜已开始");
while (true) {
try {
String dish = dishQueue.take(); // 从菜品队列中取出一个菜品
System.out.println("上菜:" + dish);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
case "2":
System.out.println("点菜已开始,请输入菜名,输入quit结束:");
while (true) {
String dish = scanner.nextLine();
if (dish.equals("quit")) {
break;
}
try {
dishQueue.put(dish); // 将菜品放入菜品队列中
} catch (InterruptedException e) {
e.printStackTrace();
}
}
break;
case "3":
scanner.close();
chef.stop();
waiterPool.shutdownNow();
return;
default:
System.out.println("无效的输入,请重新输入");
}
}
}
static class Waiter implements Runnable {
private BlockingQueue<String> dishQueue;
private boolean isIdle;
public Waiter(BlockingQueue<String> dishQueue) {
this.dishQueue = dishQueue;
this.isIdle = false;
}
@Override
public void run() {
while (true) {
try {
Thread.sleep(1000); // 模拟服务员从厨房取菜的时间
if (dishQueue.isEmpty()) {
if (!isIdle
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![7z](https://img-home.csdnimg.cn/images/20210720083312.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)