孤寡小白学计科 2022-04-10 22:04 采纳率: 100%
浏览 174
已结题

一餐厅有50个座位可供用餐者就餐,超过50人时只能排队等候,待空出座位方可就餐

一餐厅有50个座位可供用餐者就餐,超过50人时只能排队等候,待空出座位方可就餐,如何用管程实现?

  • 写回答

5条回答 默认 最新

  • ==失去 2022-04-12 00:42
    关注
    
    
    public class Restaurant {
    
    
        /**
         * 最大座位数
         */
        static private int MAX_SEAT = 50;
    
        /**
         * 当前已经来的客人
         */
        static private AtomicInteger SEAT_CUSTOMER = new AtomicInteger(0);
    
        /**
         * 排队队列
         */
        LinkedBlockingQueue SEAT_QUEUE = new LinkedBlockingQueue();
    
        public static void main(String[] args) {
            Restaurant restaurant = new Restaurant();
    
            //放入1000个顾客测试 1秒放入一个
            Thread customer = new Thread(() -> {
                for (int i = 0; i < 500; i++) {
                    restaurant.customerComIn(System.currentTimeMillis());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            customer.start();
    
            Thread customer2 = new Thread(() -> {
                for (int i = 0; i < 500; i++) {
                    restaurant.customerComIn(System.currentTimeMillis());
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            });
            customer2.start();
    
    
            //此线程定时移除乘客,代表结账 没2秒执行一次
            new Thread(() -> {
                while (true) {
                    restaurant.customerComInLeave();
                    try {
                        Thread.sleep(2000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
    
            }).start();
    
        }
    
        /**
         * 顾客离开
         */
        private void customerComInLeave() {
            synchronized (SEAT_CUSTOMER) {
                if (SEAT_CUSTOMER.get() <= 0) {
                    return;
                }
                SEAT_CUSTOMER.decrementAndGet();
                System.out.println(Thread.currentThread().getName() + "当前离开顾客1位");
                //安排队列中的乘客落座
                customerSeat();
            }
        }
    
        /**
         * 从队列中安排顾客落座
         */
        private void customerSeat() {
            while (true) {
                //如果没有等待的顾客则返回
                if (SEAT_QUEUE.size() == 0) {
                    System.out.println("没有顾客等待");
                    return;
                }
                Object num = SEAT_QUEUE.peek();
                if (num != null) {
                    boolean seatResult = seat(Long.valueOf(num.toString()));
                    if (seatResult) {
                        SEAT_CUSTOMER.getAndIncrement();
                        System.out.println("有顾客结账,队列中的: " + num + "落座;队列中还有人: " + SEAT_QUEUE.toString());
                        SEAT_QUEUE.remove(num);
                    } else {
                        SEAT_QUEUE.add(num);
                        return;
                    }
                }
    
            }
    
        }
    
    
        /**
         * 顾客来了
         * 这地方暂时用一个int 可以改造成一个对象
         * 目前只考虑单客人
         *
         * @return true:代表落座成功,false:代表等待
         */
        private boolean customerComIn(Long num) {
            synchronized (SEAT_CUSTOMER) {
                boolean seatResult = seat(num);
                if (seatResult) {
                    SEAT_CUSTOMER.getAndIncrement();
                } else {
                    SEAT_QUEUE.add(num);
                }
                return seatResult;
            }
        }
    
        private boolean seat(Long num) {
            //超过50人的时候
            if (SEAT_CUSTOMER.get() > MAX_SEAT) {
                System.out.println(Thread.currentThread().getName() + "当前人数: " + SEAT_CUSTOMER.get() + ";请等待: " + SEAT_QUEUE.size());
                return false;
            } else {
                System.out.println(Thread.currentThread().getName() + "当前人数: " + SEAT_CUSTOMER.get() + ";请入座");
                return true;
            }
        }
    }
    
    运行结果: 
    Thread-2当前人数: 51;请等待: 17
    Thread-0当前人数: 51;请等待: 18
    Thread-1当前人数: 51;请等待: 19
    Thread-0当前人数: 51;请等待: 20
    Thread-1当前人数: 51;请等待: 21
    Thread-2当前离开顾客1位
    Thread-2当前人数: 50;请入座
    有顾客结账,队列中的: 1649695145832落座;队列中还有人:
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

问题事件

  • 系统已结题 4月20日
  • 已采纳回答 4月12日
  • 创建了问题 4月10日

悬赏问题

  • ¥15 TLS1.2协议通信解密
  • ¥40 图书信息管理系统程序编写
  • ¥20 Qcustomplot缩小曲线形状问题
  • ¥15 企业资源规划ERP沙盘模拟
  • ¥15 树莓派控制机械臂传输命令报错,显示摄像头不存在
  • ¥15 前端echarts坐标轴问题
  • ¥15 ad5933的I2C
  • ¥15 请问RTX4060的笔记本电脑可以训练yolov5模型吗?
  • ¥15 数学建模求思路及代码
  • ¥50 silvaco GaN HEMT有栅极场板的击穿电压仿真问题