ArrayBlockingQueue简介
- ArrayBlockingQueue基于数组,先进先出,从尾部插入到队列,从头部开始返回。
- 线程安全的有序阻塞队列,内部通过“互斥锁”保护竞争资源。
- 指定时间的阻塞读写
- 容量可限制
定义
ArrayBlockingQueue继承AbstractQueue,实现了BlockingQueue,Serializable接口,内部元素使用Object[]数组保存。初始化时候需要指定容量ArrayBlockingQueue(int capacity),ArrayBlockingQueue默认会使用非公平锁。
ArrayBlockingQueue只使用一把锁,造成在存取两种操作时会竞争同一把锁,而使得性能相对低下。
add(E)方法和offer(E)
调用父类中的add方法,查看源码可知父类中的add方法是调用offer方法实现,所以查看offer方法源码,如下:
1 |
public boolean offer(E e) {
|
insert源码如下:
1 |
private void insert(E x) {
|
take()方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
public E take() throws InterruptedException {
//获取独占锁,加锁,线程是中断状态的话会抛异常
final ReentrantLock lock = this.lock;
lock.lockInterruptibly();
try {
//队列为空,会一直等待
while (count == 0)
notEmpty.await();
//取元素的方法
return extract();
} finally {
//释放锁
lock.unlock();
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
private E extract() {
final Object[] items = this.items;
E x = this.<E>cast(items[takeIndex]);
//取完之后,删除元素
items[takeIndex] = null;
//设置下一个被取出的元素索引,若是最后一个元素,下一个被取出的元素索引为0
takeIndex = inc(takeIndex);
//元素数减1
--count;
//唤醒添加元素的线程
notFull.signal();
return x;
}
源码分析
jdk1.7.0_71
1
2
3
4
5
6
7
8
9
10
11
12
13
14
//队列元素
final Object[] items;
//下次被take,poll,remove的索引
int takeIndex;
//下次被put,offer,add的索引
int putIndex;
//队列中元素的个数
int count;
//保护所有访问的主锁
final ReentrantLock lock;
//等待take锁,读线程条件
private final Condition notEmpty;
//等待put锁,写线程条件
private final Condition notFull;
ArrayBlockingQueue(int capacity) 给定容量和默认的访问规则初始化
1
public ArrayBlockingQueue(int capacity){}
ArrayBlockingQueue(int capacity, boolean fair)知道你跟容量和访问规则
1
2
3
4
5
6
7
8
9
//fair为true,在插入和删除时,线程的队列访问会阻塞,并且按照先进先出的顺序,false,访问顺序是不确定的
public ArrayBlockingQueue(int capacity, boolean fair) {
if (capacity <= 0)
throw new IllegalArgumentException();
this.items = new Object[capacity];
lock = new ReentrantLock(fair);
notEmpty = lock.newCondition();
notFull = lock.newCondition();
}
c) 指定容量,访问规则,集合来初始化" style="color: rgb(85, 85, 85); text-decoration: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); word-wrap: break-word; background-color: transparent;">ArrayBlockingQueue(int capacity, boolean fair,Collection<? extends E> c) 指定容量,访问规则,集合来初始化
1
2
public ArrayBlockingQueue(int capacity, boolean fair,
Collection<? extends E> c) {}
add(E e) 添加元素到队列末尾,成功返回true,队列满了抛异常IllegalStateException
1
2
3
public boolean add(E e) {
return super.add(e);
}
offer(E e)添加元素到队列末尾,成功返回true,队列满了返回false
1
public boolean offer(E e) {}
put(E e) 添加元素到队列末尾,队列满了,等待.
1
public void put(E e) throws InterruptedException {}
offer(E e, long timeout, TimeUnit unit)添加元素到队列末尾,如果队列满了,等待指定的时间
1
public boolean offer(E e, long timeout, TimeUnit unit){}
poll() 移除队列头
1
public E poll() {}
take() 移除队列头,队列为空的话就等待
1
public E take() throws InterruptedException {}
poll(long timeout, TimeUnit unit)移除队列头,队列为空,等待指定的时间
1
public E poll(long timeout, TimeUnit unit) throws InterruptedException {}
peek()返回队列头,不删除
1
public E peek() {}
size()
1
public int size(){}
remainingCapacity() 返回无阻塞情况下队列能接受容量的大小
1
public int remainingCapacity() {}
remove(Object o)从队列中删除元素
1
public boolean remove(Object o) {}
contains(Object o) 是否包含元素
1
public boolean contains(Object o) {}
toArray()
1
public Object[] toArray(){}
toArray(T[] a)
1
public <T> T[] toArray(T[] a) {}
toString()
1
public String toString(){}
clear()
1
public void clear(){}
c)移除队列中可用元素,添加到集合中" style="color: rgb(85, 85, 85); text-decoration: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); word-wrap: break-word; background-color: transparent;">drainTo(Collection<? super E> c)移除队列中可用元素,添加到集合中
1
public int drainTo(Collection<? super E> c) {}
c, int maxElements)移除队列中给定数量的可用元素,添加到集合中" style="color: rgb(85, 85, 85); text-decoration: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); word-wrap: break-word; background-color: transparent;">drainTo(Collection<? super E> c, int maxElements)移除队列中给定数量的可用元素,添加到集合中
1
public int drainTo(Collection<? super E> c, int maxElements) {}
iterator() 返回一个迭代器
1
2
3
public Iterator<E> iterator() {
return new Itr();
}
参考
1 |
public E take() throws InterruptedException {
|
1 |
private E extract() {
|
jdk1.7.0_71
1 |
//队列元素 |
ArrayBlockingQueue(int capacity) 给定容量和默认的访问规则初始化
1 |
public ArrayBlockingQueue(int capacity){}
|
ArrayBlockingQueue(int capacity, boolean fair)知道你跟容量和访问规则
1 |
//fair为true,在插入和删除时,线程的队列访问会阻塞,并且按照先进先出的顺序,false,访问顺序是不确定的 |
c) 指定容量,访问规则,集合来初始化" style="color: rgb(85, 85, 85); text-decoration: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); word-wrap: break-word; background-color: transparent;">ArrayBlockingQueue(int capacity, boolean fair,Collection<? extends E> c) 指定容量,访问规则,集合来初始化
1 |
public ArrayBlockingQueue(int capacity, boolean fair, |
add(E e) 添加元素到队列末尾,成功返回true,队列满了抛异常IllegalStateException
1 |
public boolean add(E e) {
|
offer(E e)添加元素到队列末尾,成功返回true,队列满了返回false
1 |
public boolean offer(E e) {}
|
put(E e) 添加元素到队列末尾,队列满了,等待.
1 |
public void put(E e) throws InterruptedException {}
|
offer(E e, long timeout, TimeUnit unit)添加元素到队列末尾,如果队列满了,等待指定的时间
1 |
public boolean offer(E e, long timeout, TimeUnit unit){}
|
poll() 移除队列头
1 |
public E poll() {}
|
take() 移除队列头,队列为空的话就等待
1 |
public E take() throws InterruptedException {}
|
poll(long timeout, TimeUnit unit)移除队列头,队列为空,等待指定的时间
1 |
public E poll(long timeout, TimeUnit unit) throws InterruptedException {}
|
peek()返回队列头,不删除
1 |
public E peek() {}
|
size()
1 |
public int size(){}
|
remainingCapacity() 返回无阻塞情况下队列能接受容量的大小
1 |
public int remainingCapacity() {}
|
remove(Object o)从队列中删除元素
1 |
public boolean remove(Object o) {}
|
contains(Object o) 是否包含元素
1 |
public boolean contains(Object o) {}
|
toArray()
1 |
public Object[] toArray(){}
|
toArray(T[] a)
1 |
public <T> T[] toArray(T[] a) {}
|
toString()
1 |
public String toString(){}
|
clear()
1 |
public void clear(){}
|
c)移除队列中可用元素,添加到集合中" style="color: rgb(85, 85, 85); text-decoration: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); word-wrap: break-word; background-color: transparent;">drainTo(Collection<? super E> c)移除队列中可用元素,添加到集合中
1 |
public int drainTo(Collection<? super E> c) {}
|
c, int maxElements)移除队列中给定数量的可用元素,添加到集合中" style="color: rgb(85, 85, 85); text-decoration: none; border-bottom-width: 1px; border-bottom-style: solid; border-bottom-color: rgb(204, 204, 204); word-wrap: break-word; background-color: transparent;">drainTo(Collection<? super E> c, int maxElements)移除队列中给定数量的可用元素,添加到集合中
1 |
public int drainTo(Collection<? super E> c, int maxElements) {}
|
iterator() 返回一个迭代器
1 |
public Iterator<E> iterator() {
|
参考
ArrayBlockingQueue简介的更多相关文章
- 20.并发容器之ArrayBlockingQueue和LinkedBlockingQueue实现原理详解
1. ArrayBlockingQueue简介 在多线程编程过程中,为了业务解耦和架构设计,经常会使用并发容器用于存储多线程间的共享数据,这样不仅可以保证线程安全,还可以简化各个线程操作.例如在“生产 ...
- 阻塞队列之四:ArrayBlockingQueue
一.ArrayBlockingQueue简介 一个由循环数组支持的有界阻塞队列.它的本质是一个基于数组的BlockingQueue的实现. 它的容纳大小是固定的.此队列按 FIFO(先进先出)原则对元 ...
- J.U.C并发框架源码阅读(八)ArrayBlockingQueue
基于版本jdk1.7.0_80 java.util.concurrent.ArrayBlockingQueue 代码如下 /* * ORACLE PROPRIETARY/CONFIDENTIAL. U ...
- java线程池ThreadPoolExecutor使用简介
一.简介线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为:ThreadPoolExecutor(int corePoolSize, int m ...
- Java集合容器简介
Java集合容器主要有以下几类: 1,内置容器:数组 2,list容器:Vetor,Stack,ArrayList,LinkedList, CopyOnWriteArrayList(1.5),Attr ...
- 线程池ThreadPoolExecutor使用简介
一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...
- 线程池ThreadPoolExecutor使用简介(转)
一.简介 线程池类为 java.util.concurrent.ThreadPoolExecutor,常用构造方法为: ThreadPoolExecutor(int corePoolSize, int ...
- Jdk1.6 JUC源码解析(12)-ArrayBlockingQueue
功能简介: ArrayBlockingQueue是一种基于数组实现的有界的阻塞队列.队列中的元素遵循先入先出(FIFO)的规则.新元素插入到队列的尾部,从队列头部取出元素. 和普通队列有所不同,该队列 ...
- ThreadPoolExecutor简介
ThreadPoolExecutor简介 并发包中提供的一个线程池服务 23456789 public ThreadPoolExecutor(int corePoolSize,//线程池维护线程的最少 ...
随机推荐
- same tree(判断两颗二叉树是否相等)
Input: 1 1 / \ / \ 2 3 2 3 [1,2,3], [1,2,3] Output: true Example 2: Input: 1 1 / \ 2 2 [1,2], [1,nul ...
- Bash里面如何返回绝对路径
1.返回当前目录的绝对路径: basepath=$(cd `dirname $0`; pwd) echo $basepath 2.返回当前路径的上一级目录: xp_path=`dirname &quo ...
- Java学习不走弯路教程(7.Eclipse环境搭建)
7.Eclipse环境搭建 在前几章,我们熟悉了DOS环境下编译和运行Java程序,对于大规模的程序编写,开发工具是必不可少的.Java的开发工具比较常用的是Eclipse.在接下来的教程中,我们将基 ...
- python的logging模块之读取yaml配置文件。
python的logging模块是用来记录应用程序的日志的.关于logging模块的介绍,我这里不赘述,请参见其他资料.这里主要讲讲如何来读取yaml配置文件进行定制化的日志输出. python要读取 ...
- 对于程序员在boss直聘求职的建议
最近为一个岗位的招聘,在直聘伤刷了三百份简历 0.上传简历最好是PDF,word简历在不同的系统和软件下排版可能会出问题. 1.新职位投得要快,后面投的,有可能看不到. 为了投的命中率,投之前最好看一 ...
- AUTOSAR-关于配置文件的思考
基于Can: 1. Can_Cfg.h contains compile time configurations. It should be included by Can.h which is sp ...
- 在AspNetCore 中 使用Redis实现分布式缓存
AspNetCore 使用Redis实现分布式缓存 上一篇讲到了,Core的内置缓存:IMemoryCache,以及缓存的基础概念.本篇会进行一些概念上的补充. 本篇我们记录的内容是怎么在Core中使 ...
- springcloud(十):服务网关zuul(转)
前面的文章我们介绍了,Eureka用于服务的注册于发现,Feign支持服务的调用以及均衡负载,Hystrix处理服务的熔断防止故障扩散,Spring Cloud Config服务集群配置中心,似乎一个 ...
- redis主从相关问题
redis主从是如何实现同步的 第一次.Slave向Master同步的实现是: Slave向Master发出同步请求(发送sync命令),Master先dump出rdb文件,然后将rdb ...
- 免密登录-python
要完成后台管理系统登录功能,通过查看登录页面,我们可以了解到,我们需要编写验证码图片获取接口和登录处理接口,然后在登录页面的HTML上编写AJAX. 在进行接口开发之前,还有一个重要的事情要处理,那就 ...