ArrayBlockingQueue是阻塞队列的一种,基于数组实现,长度固定,队尾添加,队首获取,

构造函数:

ArrayBlockingQueue(int capacity)

ArrayBlockingQueue(int capacity, boolean fair)

ArrayBlockingQueue(int capacity, boolean fair, Collection<? extends E> c)

其中capacity为队列的容量,初始化后不可变化。

fair表示多线程操作时是否排队,默认为false,即不保证等待最久的线程优先唤醒。

public方法:

boolean add(E e)  在队尾添加,若队列已满则抛出异常,成功返回true

void put(E e)      在队尾添加,成功返回true,队列已满则等待

boolean offer(E e)  在队尾添加,成功返回true,队列已满返回false

boolean offer(E e, long timeout, TimeUnit unit)  在队尾添加,成功返回true,队列已满等待时间为timeout

E take()    从队首取元素,如果队列为空,则等待;

E peek()    获取队首元素,若成功,则返回队首元素;否则返回null

E poll()    移除并获取队首元素,若成功,则返回队首元素;否则返回null

E poll(long timeout, TimeUnit unit)         移除并获取队首元素,队列已满等待时间为timeout

int size()         返回已使用空间大小

int remainingCapacity()    返回剩余空间大小

boolean remove(Object o)   移除一个equals(o)的元素

boolean contains(Object o)   返回是否包含equals(o)

void clear()  清空队列

实现原理:

--------put

public 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();
}
}
private void enqueue(E x) {
final Object[] items = this.items;
items[putIndex] = x;
if (++putIndex == items.length)
putIndex = 0;
count++;
notEmpty.signal();
}

--------

首先元素判空,然后获取了单线程可中断锁,然后判断队列是否已满,是则notFull状态等待,否则放入元素并激活等待notEmpty状态的线程,最后解锁。

-------take

public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
while (count == 0)
notEmpty.await();
return dequeue();
} finally {
lock.unlock();
}
}
private E dequeue() {
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;
}

-------

首先获取可中断锁,然后判断队列中是否为空,是则notEmpty状态等待,否则取出元素并激活等待notFull状态的线程,最后解锁。

其他阻塞队列:

----------------------------

//链表实现的队列,动态大小

BlockingQueue<String> queue2 = new LinkedBlockingQueue<String>();

//有优先级的阻塞队列

BlockingQueue<String> queue3 = new PriorityBlockingQueue();

//队列中只能有一个元素

BlockingQueue<String> queue4 = new SynchronousQueue();

---------------------------

一个例子:

------

------

1





ArrayBlockingQueue的更多相关文章

  1. 【JUC】JDK1.8源码分析之ArrayBlockingQueue(三)

    一.前言 在完成Map下的并发集合后,现在来分析ArrayBlockingQueue,ArrayBlockingQueue可以用作一个阻塞型队列,支持多任务并发操作,有了之前看源码的积累,再看Arra ...

  2. 阅读ArrayBlockingQueue源码了解如何利用锁实现BlockingQueue

    BlockingQueue是多线程里面一个非常重要的数据结构.在面试的时候,也常会被问到怎么实现BlockingQueue.本篇根据Java7里ArrayBlockingQueue的源码,简单介绍一下 ...

  3. JAVA可阻塞队列-ArrayBlockingQueue子类BlockingQueue的应用,使用它来实现子线程打印10次,主线程打印100次,如此反复

    /** * 使用BlockingQueue实现主子线程互相打印 * @author duwenlei * */ public class BlockingQueueTest { public stat ...

  4. JAVA可阻塞队列-ArrayBlockingQueue

    在前面的的文章,写了一个带有缓冲区的队列,是用JAVA的Lock下的Condition实现的,但是JAVA类中提供了这项功能,就是ArrayBlockingQueue, ArrayBlockingQu ...

  5. Java多线程系列--“JUC集合”07之 ArrayBlockingQueue

    概要 本章对Java.util.concurrent包中的ArrayBlockingQueue类进行详细的介绍.内容包括:ArrayBlockingQueue介绍ArrayBlockingQueue原 ...

  6. Java并发之BlockingQueue 阻塞队列(ArrayBlockingQueue、LinkedBlockingQueue、DelayQueue、PriorityBlockingQueue、SynchronousQueue)

    package com.thread.test.thread; import java.util.Random; import java.util.concurrent.*; /** * Create ...

  7. ArrayBlockingQueue跟LinkedBlockingQueue的区别

    .队列中的锁的实现不同 ArrayBlockingQueue中的锁是没有分离的,即生产和消费用的是同一个锁: LinkedBlockingQueue中的锁是分离的,即生产用的是putLock,消费是t ...

  8. ArrayBlockingQueue,BlockingQueue分析

    BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,让容量满时往BlockingQueue中添加数据时会造成阻塞,当容量为空时取元素操作会 ...

  9. ArrayBlockingQueue-我们到底能走多远系列(42)

    我们到底能走多远系列(42) 扯淡: 乘着有空,读些juc的源码学习下.后续把juc大致走一边,反正以后肯定要再来. 主题: BlockingQueue 是什么 A java.util.Queue t ...

随机推荐

  1. Greenplum各种Tips(不定时更新)

    Greenplum接触也有一段时间了,在使用过程中积累了一些命令,在此分享给大家. 1. 查看segment是否有切换(没有记录则没有切换) SELECT * from gp_segment_conf ...

  2. 测试的程序 test.php,保存放IIS的根目录下

    IIS+PHP的配置的方法,试过之后很多都不能达到效果.于是总结了大部分的文章后就得出了这样的方法 一.下载必须的程序:(1) 先到PHP的官方网站下载一个PHP(本文就以PHP 4.4.2为例).网 ...

  3. EL表达式Expression Language

    表达式语言Expression Language目的:简化jsp代码 EL内置对象 1.pageContext2.pageScope3.requestScope4.sessionScope5.appl ...

  4. android发送/接收json数据

    客户端向服务器端发送数据,这里用到了两种,一种是在url中带参数,一种是json数据发送方式: url带参数的写法: url+/?r=m/calendar/contact_list&uid=3 ...

  5. yii2知识点详解

    yii2错误处理机制: 错误处理器将所有非致命PHP错误转换成可获取异常, 也就是说可以使用如下代码处理PHP错误 use Yii; use yii\base\ErrorException; try ...

  6. php基础篇-二维数组排序 array_multisort

    原文:php基础篇-二维数组排序 array_multisort 对2维数组或者多维数组排序是常见的问题,在php中我们有个专门的多维数组排序函数,下面简单介绍下: array_multisort(a ...

  7. oracle 用户锁定及到期

    select * from dba_users where username='HR'--查询用户状态 alter user HR identified by HR;--重新更新密码alter use ...

  8. 判断日期是否符合yyyy-mm格式

    !Regex.IsMatch(dr["DMAKEDATE"].ToString(),@"^(?<year>\\d{2,4})-(?<month>\ ...

  9. C++程序设计(一)

    1. 函数指针 程序运行期间,每个函数都会占用一段连续的内存空间.而函数名就是该函数所占内存区域的起始地址(也称"入口地址").我们可以将函数的入口地址赋给一个指针变量,使该指针变 ...

  10. LeetCode Graph Valid Tree

    原题链接在这里:https://leetcode.com/problems/graph-valid-tree/ 题目: Given n nodes labeled from 0 to n - 1 an ...