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





最新文章

  1. MySQL数据库5 - 插入数据,修改数据,删除数据
  2. spring 部分配置内容备忘
  3. windows系统调用 临界区机制
  4. UICollectionView高级实践
  5. Js脚本选取iframe中的元素
  6. 何时要打开stm32的AFIO时钟
  7. WebLogic集群案例分析
  8. tomcat优化系列:修改运行内存
  9. Hadoop HDFS (3) JAVA訪问HDFS
  10. linux popen函数
  11. Python dir()/help()
  12. 记录maven 整合SSM框架
  13. spark2.1:rdd.combineByKeyWithClassTag的用法示例
  14. 2015 多校联赛 ——HDU5402(模拟)
  15. oracle 关于对时间操作的汇总
  16. BBS论坛(二十五)
  17. centos 7修改时区
  18. windows/linux VPS云服务器限制IP访问,限制别人的IP访问网站方法
  19. GNU C 与 ANSI C(上)
  20. ANOVA (paper from the onlinestat)

热门文章

  1. Python 字符串概念和操作
  2. 电子商务的几种模式,b2b,c2c等
  3. 初始化dataframe
  4. 谷歌地图OGC WMTS服务规则
  5. CSS之动态相册
  6. oracle 索引(3)
  7. Django-进阶
  8. Servlet细节处理
  9. C#中DEV控件,XtraTabPage得小方法
  10. skynet coroutine 运行笔记