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

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

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

    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();
    }
}


目录
打赏
0
0
0
0
2
分享
相关文章
|
10月前
|
Java集合篇之深度解析Queue,单端队列、双端队列、优先级队列、阻塞队列
Java集合篇之深度解析Queue,单端队列、双端队列、优先级队列、阻塞队列
68 0
基于数组的阻塞队列 ,ArrayBlockingQueue 原理
基于数组的阻塞队列 ,ArrayBlockingQueue 原理
基于数组的阻塞队列 ,ArrayBlockingQueue 原理
多线程(初阶七:阻塞队列和生产者消费者模型)
多线程(初阶七:阻塞队列和生产者消费者模型)
87 0
【Java数据结构及算法实战】系列012:Java队列06——数组实现的优先级阻塞队列PriorityBlockingQueue
【Java数据结构及算法实战】系列012:Java队列06——数组实现的优先级阻塞队列PriorityBlockingQueue
157 0
基于数组的有界阻塞队列 —— ArrayBlockingQueue
在阅读完和 AQS 相关的锁以及同步辅助器之后,来一起阅读 JUC 下的和队列相关的源码。先从第一个开始:ArrayBlockingQueue。
127 0
并发程序设计——三个顺序线程练习
question:     有三个线程 t1,t2,t3,如何保证这三个线程顺序执行?        public class ShunXuThreadTest { @Test public void threadTest() throws Interrupted...
878 0
Java数据结构:使用数组模拟队列(队列与环形队列)
文章目录 1 队列 1.1 何为队列及实现思路 1.2 数组模拟队列ArrayQueue的实现 1.3 测试队列ArrayQueueDemo测试类的实现 2 环形队列 2.1 环形队列简介及实现思路 2.2 数组模拟环形队列CircleArrayQueue的实现 2.3 测试队列CircleArrayQueueDemo测试类的实现 写在最后
Java数据结构:使用数组模拟队列(队列与环形队列)
基于链表的有界阻塞队列 —— LinkedBlockingQueue
上一节看了基于数据的有界阻塞队列 ArrayBlockingQueue 的源码,通过阅读源码了解到在 ArrayBlockingQueue 中入队列和出队列操作都是用了 ReentrantLock 来保证线程安全。下面咱们看另一种有界阻塞队列:LinkedBlockingQueue。
210 0