声明:文章内容全都是自己的学习总结,如有不对的地方请大家帮忙指出。有需要沟通交流的可加我QQ群:425120333
这个仿真系统相较于前一个会稍微麻烦一下(看个人理解),接下来看下代码:
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.TimeUnit;
public class RestaurantPractice {
public static void main(String[] args) {
ExecutorService service = Executors.newCachedThreadPool();
new RestaurantDemo(service, 5, 3);
try {
TimeUnit.SECONDS.sleep(5);
} catch (InterruptedException e) {
e.printStackTrace();
}
service.shutdownNow();
}
}
class Menu {
public static String[] foodMenu = getSingleDemo();
public String[] getFoodMenu() {
return foodMenu;
}
private static String[] getSingleDemo() {
foodMenu = new String[50];
for (int i = 0; i < foodMenu.length; i++) {
foodMenu[i] = "Food_" + i;
}
return foodMenu;
}
}
class CustomerRes implements Runnable {
Random random = new Random();
private static int count = 0;
private final int id = count++;
SynchronousQueue<String> foodQueue = new SynchronousQueue<String>();
WaitPerson waiter;
public CustomerRes(WaitPerson waiter) {
this.waiter = waiter;
}
public void foodPut(String foodName) {
try {
foodQueue.put(foodName);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public void run() {
int allFoodCount = random.nextInt(7) + 3;
System.out.println(this + " 点了" + allFoodCount + "个菜");
String[] menu = Menu.foodMenu;
for (int i = 0; i < allFoodCount; i++) {
String foodName = menu[random.nextInt(menu.length)];
waiter.orderFood(this, foodName);
}
int getFoodCount = 0;
try {
while (getFoodCount < allFoodCount) {
String foodName = foodQueue.take();
getFoodCount++;
int nextFoodCount = allFoodCount - getFoodCount;
if (nextFoodCount > 0) {
System.out.println(this + "点的" + foodName + "上了,还差" + nextFoodCount + "个没上。");
} else {
System.out.println(this + "点的" + foodName + "上了,所有的菜都上齐了");
}
}
} catch (InterruptedException e) {
System.out.println(this + "用餐被打断!!!!!");
}
System.out.println(this + "用餐完,离开了!");
}
@Override
public String toString() {
return "Customer_" + id;
}
}
class OrderInfo {
private String foodName;
private CustomerRes customer;
private WaitPerson waiter;
public OrderInfo(String foodName, CustomerRes customer, WaitPerson waiter) {
this.foodName = foodName;
this.customer = customer;
this.waiter = waiter;
}
public String getFoodName() {
return foodName;
}
public CustomerRes getCustomer() {
return customer;
}
public WaitPerson getWaiter() {
return waiter;
}
}
class PlateInfo {
private String foodName;
private OrderInfo orderInfo;
public PlateInfo(String foodName, OrderInfo orderInfo) {
this.foodName = foodName;
this.orderInfo = orderInfo;
}
public String getFoodName() {
return foodName;
}
public OrderInfo getOrderInfo() {
return orderInfo;
}
}
class WaitPerson implements Runnable {
private static int count = 0;
private final int id = count++;
LinkedBlockingQueue<PlateInfo> plateInfoQueue = new LinkedBlockingQueue<PlateInfo>();
RestaurantDemo rest;
public WaitPerson(RestaurantDemo rest) {
this.rest = rest;
}
@Override
public void run() {
try {
while (!Thread.interrupted()) {
PlateInfo plateInfo = plateInfoQueue.take();
System.out.println(this + "将" + plateInfo.getFoodName() + "交给了"
+ plateInfo.getOrderInfo().getCustomer());
plateInfo.getOrderInfo().getCustomer().foodPut(plateInfo.getFoodName());
}
} catch (InterruptedException e) {
System.out.println(this + "服务员被打断。。。");
}
System.out.println(this + "回家休息了");
}
public void orderFood(CustomerRes customer, String foodName) {
try {
rest.orderInfoQueue.put(new OrderInfo(foodName, customer, this));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
@Override
public String toString() {
return "WaitPerson_" + id;
}
}
class ChefMan implements Runnable {
RestaurantDemo rest;
public ChefMan(RestaurantDemo rest) {
this.rest = rest;
}
@Override
public void run() {
try {
while (!Thread.interrupted()) {
OrderInfo orderInfo = rest.orderInfoQueue.take();
String foodName = orderInfo.getFoodName();
TimeUnit.MILLISECONDS.sleep(1000);
orderInfo.getWaiter().plateInfoQueue.put(new PlateInfo(foodName, orderInfo));
}
} catch (InterruptedException e) {
System.out.println("厨师工作被打断。。。");
}
System.out.println("厨师下班回家了、、、");
}
}
class RestaurantDemo implements Runnable {
ArrayBlockingQueue<OrderInfo> orderInfoQueue = new ArrayBlockingQueue<OrderInfo>(50);
private List<WaitPerson> personList;
private ExecutorService service;
public RestaurantDemo(ExecutorService service, int waitPersonNum, int chefManNum) {
this.service = service;
for (int i = 0; i < chefManNum; i++) {
service.execute(new ChefMan(this));
}
personList = new ArrayList<WaitPerson>(waitPersonNum);
for (int i = 0; i < waitPersonNum; i++) {
WaitPerson waiter = new WaitPerson(this);
personList.add(waiter);
service.execute(waiter);
}
service.execute(this);
}
@Override
public void run() {
Random random = new Random();
try {
while (!Thread.interrupted()) {
WaitPerson waiter = personList.get(random.nextInt(personList.size()));
CustomerRes customer = new CustomerRes(waiter);
service.execute(customer);
TimeUnit.SECONDS.sleep(1);
}
} catch (InterruptedException e) {
System.out.println("餐馆结束服务。。");
}
System.out.println("餐馆关门休息了!!!!");
}
}
控制台输出:
Customer_0 点了9个菜
Customer_1 点了3个菜
WaitPerson_4将Food_1交给了Customer_0
WaitPerson_4将Food_18交给了Customer_0
Customer_0点的Food_1上了,还差8个没上。
Customer_0点的Food_18上了,还差7个没上。
WaitPerson_4将Food_1交给了Customer_0
Customer_0点的Food_1上了,还差6个没上。
Customer_2 点了5个菜
WaitPerson_4将Food_26交给了Customer_0
Customer_0点的Food_26上了,还差5个没上。
WaitPerson_4将Food_1交给了Customer_0
Customer_0点的Food_1上了,还差4个没上。
WaitPerson_4将Food_48交给了Customer_0
Customer_0点的Food_48上了,还差3个没上。
Customer_3 点了6个菜
WaitPerson_4将Food_13交给了Customer_0
WaitPerson_4将Food_6交给了Customer_0
Customer_0点的Food_13上了,还差2个没上。
Customer_0点的Food_6上了,还差1个没上。
WaitPerson_4将Food_11交给了Customer_0
Customer_0点的Food_11上了,所有的菜都上齐了
Customer_0用餐完,离开了!
Customer_4 点了4个菜
WaitPerson_4将Food_20交给了Customer_1
Customer_1点的Food_20上了,还差2个没上。
WaitPerson_4将Food_3交给了Customer_1
Customer_1点的Food_3上了,还差1个没上。
WaitPerson_4将Food_43交给了Customer_1
Customer_1点的Food_43上了,所有的菜都上齐了
Customer_1用餐完,离开了!
Customer_2用餐被打断!!!!!
厨师工作被打断。。。
Customer_3用餐被打断!!!!!
餐馆结束服务。。
WaitPerson_4服务员被打断。。。
WaitPerson_0服务员被打断。。。
厨师工作被打断。。。
厨师下班回家了、、、
厨师工作被打断。。。
厨师下班回家了、、、
WaitPerson_0回家休息了
WaitPerson_4回家休息了
WaitPerson_1服务员被打断。。。
餐馆关门休息了!!!!
Customer_3用餐完,离开了!
WaitPerson_3服务员被打断。。。
Customer_4用餐被打断!!!!!
厨师下班回家了、、、
Customer_2用餐完,离开了!
WaitPerson_2服务员被打断。。。
Customer_4用餐完,离开了!
WaitPerson_3回家休息了
WaitPerson_1回家休息了
WaitPerson_2回家休息了
从输出中可以看到达到了想要的效果,这些代码我都没加注释,只是希望大家能自己练习,实在弄不懂了可以联系我,我可以帮你解释下。