多线程-等待唤醒和阻塞队列

public class eat extends Thread{
    //四部曲:循环,同步代码块,判断共享数据是否到末尾(到),判断共享数据是否到了末尾(未到,则执行核心逻辑)
    @Override
    public void run() {
        while(true){
            synchronized(Desk.lock){
                if(Desk.mount == 0){
                    break;
                }else{
                    if(Desk.foodflag == 0){
                        try {
                            Desk.lock.wait();
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                    }else{
                        Desk.mount--;
                        System.out.println(getName() + "还能吃" + Desk.mount + "碗");
                        Desk.lock.notifyAll();
                        Desk.foodflag = 0;
                    }


                }
            }
        }
    }
}




public class test {
    public static void main(String[] args) {
        cook c = new cook();
        eat e = new eat();
       c.setName("厨师");
       e.setName("吃货");
       c.start();
       e.start();

    }



public class cook extends Thread{
    @Override
    public void run() {
        while (true){
            if (Desk.mount == 0) {
                break;
            }else {
                    synchronized(Desk.lock){
                        if(Desk.foodflag == 0){
                            System.out.println(getName() + "煮了面");
                            Desk.foodflag = 1;
                            Desk.lock.notifyAll();
                        }else{
                            try {
                                Desk.lock.wait();
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }

                        }
                    }
                }
            }
        }
    }





public class Desk {
    public static int mount = 10;
    public static Object lock = new Object();//锁对象
    public static int foodflag = 0;
}




 阻塞队列:两者必须使用同一个阻塞队列

 

public class cook extends Thread{
    ArrayBlockingQueue<String> queue;

    public cook(ArrayBlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {
        while (true) {
            try {
                queue.put("面条");
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
            System.out.println("做了一碗面条");
        }
    }
}



public class eat extends Thread{
    ArrayBlockingQueue<String> queue;

    public eat(ArrayBlockingQueue<String> queue) {
        this.queue = queue;
    }

    @Override
    public void run() {

        while (true) {
            try {
                System.out.println(queue.take());
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
        }
    }

public class test {
    public static void main(String[] args) {
        ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<>(1);
         cook c = new cook(queue);
         eat e = new eat(queue);
         c.start();
         e.start();

    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值