ArrayBlockingQueue
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的更多相关文章
- 【JUC】JDK1.8源码分析之ArrayBlockingQueue(三)
一.前言 在完成Map下的并发集合后,现在来分析ArrayBlockingQueue,ArrayBlockingQueue可以用作一个阻塞型队列,支持多任务并发操作,有了之前看源码的积累,再看Arra ...
- 阅读ArrayBlockingQueue源码了解如何利用锁实现BlockingQueue
BlockingQueue是多线程里面一个非常重要的数据结构.在面试的时候,也常会被问到怎么实现BlockingQueue.本篇根据Java7里ArrayBlockingQueue的源码,简单介绍一下 ...
- JAVA可阻塞队列-ArrayBlockingQueue子类BlockingQueue的应用,使用它来实现子线程打印10次,主线程打印100次,如此反复
/** * 使用BlockingQueue实现主子线程互相打印 * @author duwenlei * */ public class BlockingQueueTest { public stat ...
- JAVA可阻塞队列-ArrayBlockingQueue
在前面的的文章,写了一个带有缓冲区的队列,是用JAVA的Lock下的Condition实现的,但是JAVA类中提供了这项功能,就是ArrayBlockingQueue, ArrayBlockingQu ...
- Java多线程系列--“JUC集合”07之 ArrayBlockingQueue
概要 本章对Java.util.concurrent包中的ArrayBlockingQueue类进行详细的介绍.内容包括:ArrayBlockingQueue介绍ArrayBlockingQueue原 ...
- Java并发之BlockingQueue 阻塞队列(ArrayBlockingQueue、LinkedBlockingQueue、DelayQueue、PriorityBlockingQueue、SynchronousQueue)
package com.thread.test.thread; import java.util.Random; import java.util.concurrent.*; /** * Create ...
- ArrayBlockingQueue跟LinkedBlockingQueue的区别
.队列中的锁的实现不同 ArrayBlockingQueue中的锁是没有分离的,即生产和消费用的是同一个锁: LinkedBlockingQueue中的锁是分离的,即生产用的是putLock,消费是t ...
- ArrayBlockingQueue,BlockingQueue分析
BlockingQueue接口定义了一种阻塞的FIFO queue,每一个BlockingQueue都有一个容量,让容量满时往BlockingQueue中添加数据时会造成阻塞,当容量为空时取元素操作会 ...
- ArrayBlockingQueue-我们到底能走多远系列(42)
我们到底能走多远系列(42) 扯淡: 乘着有空,读些juc的源码学习下.后续把juc大致走一边,反正以后肯定要再来. 主题: BlockingQueue 是什么 A java.util.Queue t ...
随机推荐
- Python 基础 - 随机列表最大的两个值
# -*- coding: utf-8 -*- #author:v def sywmemeda(l): #list 冒泡排序 length = len(l) for i in range(length ...
- html第一天
html为超文本标记语言, html下的内容不去分大小写: 标签必须要有开始和结束,<br>和<hr>特殊,在标签内结束<br/>,<hr/>:下划线 ...
- 20145334实验三《敏捷开发与XP实践》
实验内容 初步掌握单元测试和TDD 理解并掌握面向对象三要素:封装.继承.多态 初步掌握UML建模 熟悉S.O.L.I.D原则 了解设计模式 实验步骤 1.敏捷开发与XP 敏捷开发(Agile Dev ...
- response的outputStream输出数据的问题
package cn.itcast.response; import java.io.IOException; import java.io.OutputStream; import java.io. ...
- 佛祖保佑 永无BUG 永不修改
// // _oo0oo_ // o8888888o // 88" . "88 // (| -_- |) // 0\ = /0 // ___/`---'\___ // .' \\| ...
- Wordpress 标题设置
使用标题格式:首页(网站标题 - 网站副标题),其他页面(页面标题 | 网站标题) 在后台找到头部文件head.php <?php wp_title('|', true, 'right'); e ...
- Sublime Text 3 3126 注册码
转载自:https://fatesinger.com/78252 Sublime Text 3 3126 注册码 第一个测试通过 -– BEGIN LICENSE -– Michael Barnes ...
- LeetCode Reconstruct Itinerary
原题链接在这里:https://leetcode.com/problems/reconstruct-itinerary/ 题目: Given a list of airline tickets rep ...
- zabbix的一些优化参数随笔
StartDBSyncers=12 如果proxy过多 可以适当加大这个参数 ProxyConfigFrequency=60ProxyDataFrequency=60 这两个参数很重要,一个是ser ...
- [Android Tips] 6. Parallax ViewPager
文章 http://ryanhoo.github.io/blog/2014/07/16/step-by-step-implement-parallax-animation-for-splash-scr ...