多線程學習使用(二)——仿真之飯店服務系統

    聲明:文章內容全都是自己的學習總結,如有不對的地方請大家幫忙指出。有需要溝通交流的可加我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回家休息了

從輸出中可以看到達到了想要的效果,這些代碼我都沒加註釋,只是希望大家能自己練習,實在弄不懂了可以聯繫我,我可以幫你解釋下。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章