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

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

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

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


相关文章
|
7天前
|
存储 Java C++
Java集合篇之深度解析Queue,单端队列、双端队列、优先级队列、阻塞队列
Java集合篇之深度解析Queue,单端队列、双端队列、优先级队列、阻塞队列
20 0
|
3月前
|
消息中间件 安全 Java
多线程(初阶七:阻塞队列和生产者消费者模型)
多线程(初阶七:阻塞队列和生产者消费者模型)
31 0
|
5月前
|
安全 Java
队列的学习(三) 手写一个阻塞队列
队列的学习(三) 手写一个阻塞队列 本文将介绍如何手写一个阻塞队列。阻塞队列是一种线程安全的队列,当队列为空时,消费者线程将被阻塞直到队列中有元素可供消费;当队列已满时,生产者线程将被阻塞直到队列有空闲位置可供插入元素。
|
12月前
|
Python
Python生产者-消费者队列、优先级队列
Python生产者-消费者队列、优先级队列
|
存储 算法 前端开发
队列的典型题:栈实现队列、双端队列
队列的典型题:栈实现队列、双端队列
78 0
|
存储 Java 索引
不可上位!数据结构队列,老实排队,Java实现数组模拟队列及可复用环形队列
不可上位!数据结构队列,老实排队,Java实现数组模拟队列及可复用环形队列
116 0
不可上位!数据结构队列,老实排队,Java实现数组模拟队列及可复用环形队列
|
存储 算法 Java
Java数据结构:使用数组模拟队列(队列与环形队列)
文章目录 1 队列 1.1 何为队列及实现思路 1.2 数组模拟队列ArrayQueue的实现 1.3 测试队列ArrayQueueDemo测试类的实现 2 环形队列 2.1 环形队列简介及实现思路 2.2 数组模拟环形队列CircleArrayQueue的实现 2.3 测试队列CircleArrayQueueDemo测试类的实现 写在最后
Java数据结构:使用数组模拟队列(队列与环形队列)
基于数组的阻塞队列 ,ArrayBlockingQueue 原理
基于数组的阻塞队列 ,ArrayBlockingQueue 原理
基于数组的阻塞队列 ,ArrayBlockingQueue 原理
|
安全
基于数组的有界阻塞队列 —— ArrayBlockingQueue
在阅读完和 AQS 相关的锁以及同步辅助器之后,来一起阅读 JUC 下的和队列相关的源码。先从第一个开始:ArrayBlockingQueue。
96 0
|
安全
基于链表的有界阻塞队列 —— LinkedBlockingQueue
上一节看了基于数据的有界阻塞队列 ArrayBlockingQueue 的源码,通过阅读源码了解到在 ArrayBlockingQueue 中入队列和出队列操作都是用了 ReentrantLock 来保证线程安全。下面咱们看另一种有界阻塞队列:LinkedBlockingQueue。
147 0