ArrayBlockingQueue原理

简介: 文章主要介绍了ArrayBlockingQueue的工作原理。ArrayBlockingQueue通过ReentrantLock和Condition实现了高效的阻塞队列,能够有效地避免CPU资源浪费。它非常适合用于生产者-消费者模型的应用场景,特别是需要控制生产者和消费者线程同步的场合。

阻塞队列ArrayBlockingQueue是对生产者消费者模型的实现,可以实现生产者和消费者通信。

在队列空的时候,消费者线程可以阻塞,不为空被唤醒。

在队列满的时候,生产者线程阻塞功能。不满的时候被唤醒。

ArrayBlockingQueue底层使用了独占锁ReentrantLock,和两个Conditon条件实现生产者和消费者互斥。 下面将通过查看ArrayBlockingQueue源码来了解实现细节。

依赖的锁:

    /** Main lock guarding all access */    final ReentrantLock lock;    /** Condition for waiting takes */    private final Condition notEmpty;​    /** Condition for waiting puts */    private final Condition notFull;
AI 代码解读

put方法:

//如果队列满了,会等待notFullpublic void put(E e) throws InterruptedException {
           checkNotNull(e);        //入队修改先获取锁        final ReentrantLock lock = this.lock;        lock.lockInterruptibly();        try {
               while (count == items.length)                notFull.await();            enqueue(e);        } finally {
               lock.unlock();        }    }    //数据成功入队列后,通知notEmpty条件,阻塞了的消费者线程可以继续消费private void enqueue(E x) {
           // assert lock.getHoldCount() == 1;        // assert items[putIndex] == null;        final Object[] items = this.items;        items[putIndex] = x;        if (++putIndex == items.length)            putIndex = 0;        count++;        notEmpty.signal();    }
AI 代码解读

put方法在队列满的情况下会进入等待状态,会等到take线程唤醒。

take方法:

//如果队列为空,notEmpty条件等待。public E take() throws InterruptedException {        //出队先获取锁        final ReentrantLock lock = this.lock;        lock.lockInterruptibly();        try {            while (count == 0)                notEmpty.await();            return dequeue();        } finally {            lock.unlock();        }    }    //获取元素,获取后,通知notFull条件private E dequeue() {        // assert lock.getHoldCount() == 1;        // assert items[takeIndex] != null;        final Object[] items = this.items;        @SuppressWarnings("unchecked")        E x = (E) items[takeIndex];        items[takeIndex] = null;        if (++takeIndex == items.length)            takeIndex = 0;        count--;        if (itrs != null)            itrs.elementDequeued();        notFull.signal();        return x;}
AI 代码解读

总结:

ArrayBlockingQueue是对生产者消费者模型的实现,并借助ReentrantLock实现了阻塞功能,通过阻塞来避免cpu资源滥用。

目录
打赏
0
4
3
0
41
分享
相关文章
【面试题精讲】ArrayBlockingQueue 和 LinkedBlockingQueue 区别
【面试题精讲】ArrayBlockingQueue 和 LinkedBlockingQueue 区别
Java并发基础:ArrayBlockingQueue全面解析!
ArrayBlockingQueue类是一个高效、线程安全的队列实现,它基于数组,提供了快速的元素访问,并支持多线程间的同步操作,作为有界队列,它能有效防止内存溢出,并通过阻塞机制平衡生产者和消费者的速度差异,它还提供了公平性和非公平性策略,满足不同场景下的需求。
159 1
Java并发基础:ArrayBlockingQueue全面解析!
Java线程池ThreadPoolExcutor源码解读详解02-阻塞队列之ArrayBlockingQueue
`ArrayBlockingQueue` 是Java中一个基于数组的并发队列,具有线程安全的性质。以下是其关键信息的摘要: - **继承实现关系**:它扩展了`AbstractQueue`并实现了`BlockingQueue`接口,确保线程安全的入队和出队操作。 - **数据结构**:内部由固定大小的数组支撑,有`takeIndex`和`putIndex`跟踪元素的添加和移除位置,`count`记录队列中的元素数量。 - **特点**:队列长度在创建时必须指定且不可变,遵循先进先出(FIFO)原则,当队列满时,添加元素会阻塞,空时,移除元素会阻塞。
100 0
CountDownLatch原理
文章讲述了CountDownLatch的工作原理及其用途: 1. CountDownLatch是一个简单的同步工具,用于等待一组操作完成。 2. 它通过AQS框架实现,利用共享锁模式控制线程的等待与唤醒。 3. 适用于多线程环境下,一个线程需要等待多个其他线程完成各自的任务后再继续执行的场景。
|
7月前
ArrayBlockingQueue原理解析
该文章主要讲述了ArrayBlockingQueue的实现原理。
BlockingQueue阻塞队列原理以及实现
BlockingQueue阻塞队列原理以及实现
142 0
JUC系列学习(四):线程池阻塞队列BlockingQueue及其相关实现ArrayBlockingQueue、LinkedBlockingQueue
线程池阻塞队列BlockingQueue及其相关实现ArrayBlockingQueue、LinkedBlockingQueue
133 0

热门文章

最新文章

AI助理

你好,我是AI助理

可以解答问题、推荐解决方案等