自习室有30个座位,50个学生需先预订才能在自习室上自习。 当自习室的座位被全部预订后,其余学生需要等待。当学生完成学习任务后,需要取消预订的座位,以便其他等待的学生能够预订

public class SeatorderedCase {
    private int seatResource;     //共享缓冲区
    private boolean empty=true;    //是否为空的信号量

    public void setEmpty(){
        empty=true;
    }

    public synchronized void push(int pubResource){
        while (!empty){                       //当缓冲区满的时候等待
            try {                             //阻塞自己
                wait();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        seatResource = pubResource;        //将生成的座位号放到缓冲区中
        empty = false;
        notify();            //唤醒其他等待的线程
    }

    public synchronized int pop(){       //从缓冲区中定座位
        while (empty){
            try {
                wait();                  //当缓冲区为空的时候等待
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        int popResource = seatResource;
        seatResource = 0;
        empty = true;                  //设置缓冲区为空的状态
        notify();
        return popResource;            //返回所定的座位号
    }

    //生成空座位线程(生产者)
    static class SeatProcedure extends Thread{
        private SeatorderedCase so;
        public SeatProcedure(SeatorderedCase so){
            this.so = so;
        }

        public void run() {
            for (int i = 0; i <= 30 ; i++) {    //连续向缓冲区中生成空座位号
                int pubResource = i;
                so.push(pubResource);
                System.out.println("第"+pubResource+"号座位为空");
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //预订座位线程(消费者)
    static class SeatConsumer extends Thread{
        private SeatorderedCase so;
        public SeatConsumer(SeatorderedCase so){
            this.so = so;
        }

        public void run() {
            for (int i = 1; i <= 50 ; i++) {       //生成50个学生,50个学生连续从缓冲区中取出座位号
                synchronized (so){
                    int sh = so.pop();
                    if (sh != 0){
                        System.out.println("学生"+ i +"占了第" + sh +"号座位");
                    } else {
                        System.out.println("没有空座位,请等待");
                    }
                }
                try {
                    sleep(200);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    //释放座位线程
    static class SeatRelease extends Thread{
        private SeatorderedCase so;
        public SeatRelease(SeatorderedCase so){
            this.so = so;
        }

        public void run() {
            try {
                sleep(20000);
                this.so.setEmpty();
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            for (int i = 1; i <= 30; i++) {         //从第一个开始,连续释放已预订的座位
                int pubResource = i;
                so.push(pubResource);
                System.out.println("第"+pubResource+"号座位取消预订");
                try {
                    sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public static void main(String[] args) {
        SeatorderedCase so = new SeatorderedCase();

        SeatProcedure sp = new SeatProcedure(so);
        sp.start();

        SeatConsumer sc = new SeatConsumer(so);
        sc.start();

        SeatRelease sr = new SeatRelease(so);
        sr.start();
    }
}
  • 3
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值