生产者消费者模型(基于标准库提供的阻塞队列、基于环形数组自实现的阻塞队列)

目录:

一、基于标准库提供的阻塞队列实现生产者消费者模型
二、基于环形数组自实现的阻塞队列实现生产者消费者模型

一、基于标准库提供的阻塞队列实现生产者消费者模型

    public static void main(String[] args) {
        BlockingQueue<Integer> blockingQueue = new LinkedBlockingDeque<>();

        //消费者
        Thread customer = new Thread(()->{
           while (true){
               try {
                   Integer ret = blockingQueue.take();
                   System.out.println("消费元素:"+ret);
               } catch (InterruptedException e) {
                   e.printStackTrace();
               }
           }
        });
        customer.start();

        //生产者
        Thread producer = new Thread(()->{
            int count = 0;
            while (true){
                try {
                    blockingQueue.put(count);
                    System.out.println("生产元素:"+count);
                    count++;
                    //为了看到效果,生产元素这里间隔500毫秒
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();
    }

二、基于环形数组自实现的阻塞队列实现生产者消费者模型

class MyBlockingQueue{
    private int[] items = new int[1000];
    private int head = 0;
    private int tail = 0;
    private int size = 0;

    //入队列
    public void put(int val) throws InterruptedException {
        synchronized (this){
            while (size == items.length){
                //此时队列满了,需要阻塞,等待出队列的时候来唤醒
                this.wait();
            }
            items[tail] = val;
            tail++;
            if (tail >= items.length){
                tail = 0;
            }
            size++;
            //唤醒take中的wait
            this.notify();
        }
    }

    //出队列
    public Integer take() throws InterruptedException {
        int ret = 0;
        synchronized (this){
            while (size == 0){
                //此时队列为空,需要阻塞,等待入队列的时候唤醒
                this.wait();
            }
            ret = items[head];
            head++;
            if (head >= items.length){
                head = 0;
            }
            size--;
            //唤醒put中的wait
            this.notify();
        }
        return ret;
    }
}
public class ThreadDemo22 {
    public static void main(String[] args) {
        MyBlockingQueue myBlockingQueue = new MyBlockingQueue();

        //消费者
        Thread customer = new Thread(()->{
            while (true){
                try {
                    Integer ret = myBlockingQueue.take();
                    System.out.println("消费元素:"+ret);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        customer.start();

        //生产者
        Thread producer = new Thread(()->{
            int count = 0;
            while (true){
                try {
                    myBlockingQueue.put(count);
                    System.out.println("生产元素:"+count);
                    count++;
                    //为了看到效果,生产元素这里间隔500毫秒
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });
        producer.start();
    }
}

  • 17
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 16
    评论
评论 16
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

crazy_xieyi

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值