阻塞队列
阻塞队列(BlockingQueue)是一个支持两个附加操作的队列。这两个附加的操作支持阻塞的插入和移除方法,即针对一个有界队列,当前队列满时,添加元素的操作会被阻塞;当前队列空时,从队列中获取元素的操作也会被阻塞。
- 1、支持阻塞的插入方法:意思是当队列满时,队列会阻塞插入元素的线程,直到队列不满。
- 2、支持阻塞的移除方法:意思是在队列为空时,获取元素的线程会等待队列变为非空。
阻塞队列常用于生产者和消费者的场景,生产者是向队列里添加元素的线程,消费者是从队列里取元素的线程。阻塞队列就是生产者用来存放元素、消费者用来获取元素的容器。
在阻塞队列不可用时,这两个附加操作提供了4种处理方式:
- 抛出异常:是指当阻塞队列满时候,再往队列里插入元素,会抛出
IllegalStateException("Queue full")
异常。当队列为空时,从队列里获取元素时会抛出NoSuchElementException
异常。 - 返回特殊值:插入方法会返回是否成功,成功则返回 true。移除方法,则是从队列里拿出一个元素,如果没有则返回 null。
- 一直阻塞:当阻塞队列满时,如果生产者线程往队列里 put 元素,队列会一直阻塞生产者线程,直到拿到数据,或者响应中断退出。当队列空时,消费者线程试图从队列里 take 元素,队列也会阻塞消费者线程,直到队列可用。
- 超时退出:当阻塞队列满时,队列会阻塞生产者线程一段时间,如果超过一定的时间,生产者线程就会退出。
如果是无界阻塞队列,队列不可能会出现满的情况,所以使用put或offer方法永远不会被阻塞,而且使用offer方法时,该方法永远返回true。
常用的阻塞队列
JDK7 提供了 7 个阻塞队列。分别是:
ArrayBlockingQueue
:一个由数组结构组成的有界阻塞队列。LinkedBlockingQueue
:一个由链表结构组成的有界阻塞队列。PriorityBlockingQueue
:一个支持优先级排序的无界阻塞队列。DelayQueue
:一个使用优先级队列实现的无界阻塞队列。SynchronousQueue
:一个不存储元素的阻塞队列。LinkedTransferQueue
:一个由链表结构组成的无界阻塞队列。LinkedBlockingDeque
:一个由链表结构组成的双向阻塞队列。
ArrayBlockingQueue
ArrayBlockingQueue
是一个用数组实现的有界阻塞队列。此队列按照先进先出(FIFO)的原则对元素进行排序。
/** The queued items */
final Object[] items;
/** items index for next take, poll, peek or remove */
int takeIndex;
/** items index for next put, offer, or add */
int putIndex;
/** Number of elements in the queue */
int count;
items
:一个Object的数组。tackIndex
:出队列的下标。putIndex
:入队列的下标。count
:队列中元素的数量。
ArrayBlockingQueue
是一个用数组实现的有界阻塞队列。此队列按照先进先出(FIFO)
的原则对元素进行排序。默认情况下不保证访问者公平的访问队列,所谓公平访问队列是指阻塞的所有生产者线程或消费者线程,当队列可用时,可以按照阻塞的先后顺序访问队列,即先阻塞的生产者线程,可以先往队列里插入元素,先阻塞的消费者线程,可以先从队列里获取元素。通常情况下为了保证公平性会降低吞吐量。我们可以使用以下代码创建一个公平的阻塞队列。
ArrayBlockingQueue fairQueue = new ArrayBlockingQueue(1000,true);
- 查看ArrayBlockingQueue 的构造函数:
/**
* Creates an {@code ArrayBlockingQueue} with the given (fixed)
* capacity and default access policy.
*
* @param capacity the capacity of this queue
* @throws IllegalArgumentException if {@code capacity < 1}
*/
public ArrayBlockingQueue(int capacity) {
this(capacity, false);
}
/**
* Creates an {@code ArrayBlockingQueue} with the given (fixed)
* capacity and the specified access policy.
*
* @param capacity the capacity of this queue
* @param fair if {@code true} then queue accesses for threads blocked
* on insertion or removal, are processed in FIFO order;
* if {@code false} the access order is unspecified.
* @throws IllegalArgumentException if {@code capacity < 1}
*/
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();
}
- 设置ReentrantLock的锁模式为公平锁:
/**
* Creates an instance of {@code ReentrantLock} with the
* given fairness policy.
*
* @param fair {@code true} if this lock should use a fair ordering policy
*/
public ReentrantLock(boolean fair) {
sync = fair ? new FairSync() : new NonfairSync();
}
LinkedBlockingQueue
LinkedBlockingQueue
是一个用链表实现的有界阻塞队列。此队列的默认和最大长度为 Integer.MAX_VALUE
。此队列按照先进先出的原则对元素进行排序。
/**
* Linked list node class
*/
static class Node<E> {
E item;
/**
* One of:
* - the real successor Node
* - this Node, meaning the successor is head.next
* - null, meaning there is no successor (this is the last node)
*/
Node<E> next;
Node(E x) { item = x; }
}
ArrayBlockingQueue和LinkedBlockingQueue区别
1、底层实现不同。
ArrayBlockingQueue
底层使用数组来维护队列,是一个循环数组。LinkedBlockingQueue
底层使用链表来维护队列,在添加和删除队列中的元素的时候,会创建和销毁节点对象,在高并发和大量数据的时候,GC压力很大。
2、锁的方式不同。
ArrayBlockingQueue
获取数据和添加数据都是使用同一个锁对象,这样添加和获取就不是一个并发的过程,不过,在ArrayBlockingQueue
中使用Condition
的等待/通知机制,这样使得ArrayBlockingQueue
的数据写入和获取操作已经足够轻巧,以至于引入独立的锁机制,除了给代码带来额外的复杂性外,其在性能上完全占不到任何便宜。LinkedBlockingQueue
获取数据和添加数据使用不同的锁对象。
ArrayBlockingQueue
能够实现锁分离吗?
- 答:不能。原因是因为
ArrayBlockingQueue
底层是循环数组,位置会从最后一个位置返回到第一个位置,这样的操作没有办法进行原子化。
LinkedBlockingQueue
为什么需要两把锁,一把锁行不行?
- 答:是可以的,不过使用一把锁的效率会远远低于两把锁。
PriorityBlockingQueue
PriorityBlockingQueue
是一个支持优先级的无界阻塞队列。默认情况下元素采取自然顺序升序排列。继承Comparable
类实现compareTo()
方法来指定元素排序规则,或者初始化PriorityBlockingQueue
时,指定构造参数Comparator
来对元素进行排序。需要注意的是不能保证同优先级元素的顺序。
例:学生实体类存入PriorityBlockingQueue 队列按照年龄升序排序。
public class TestDemo {
public static class Student implements Comparable<Student> {
private String name;
private int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public int compareTo(Student o) {
return this.age > o.age ? 1 : this.age < o.age ? -1 : 0;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
public static void main(String[] args) {
PriorityBlockingQueue<Student> queue = new PriorityBlockingQueue<>();
queue.put(new Student("小明", 12));
queue.put(new Student("小张", 23));
queue.put(new Student("小王", 11));
queue.put(new Student("小天", 45));
try {
for (; ; ) {
System.out.println(queue.take().toString());
}
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
- 运行结果:
Student{name='小王', age=11}
Student{name='小明', age=12}
Student{name='小张', age=23}
Student{name='小天', age=45}
DelayQueue
DelayQueue
是一个支持延时获取元素的无界阻塞队列。队列使用PriorityQueue
来实现。队列中的元素必须实现Delayed
接口,在创建元素时可以指定多久才能从队列中获取当前元素。只有在延迟期满时才能从队列中提取元素。
DelayQueue,延时阻塞队列,DelayQueue中的元素只有当前指令的延时时间到了,才能从队列中获取到元素,DelayQueue也是一个无界队列,插入数据的操作不会被阻塞的,只有获取数据的操作才会被阻塞。
DelayQueue
非常有用,可以将DelayQueue
运用在以下应用场景:
- 缓存系统的设计:可以用
DelayQueue
保存缓存元素的有效期,使用一个线程循环查询DelayQueue
,一旦能从DelayQueue
中获取元素时,表示缓存有效期到了。 - 定时任务调度:使用
DelayQueue
保存当天将会执行的任务和执行时间,一旦从DelayQueue
中获取到任务就开始执行,比如TimerQueue
就是使用DelayQueue
实现的。
实现DelayQueue
的三个步骤:
- 第一步:继承
Delayed
接口。 - 第二步:实现
getDelay(TimeUnit unit)
,该方法返回当前元素还需要延时多长时间,单位是纳秒。 - 第三步:实现
compareTo()
方法来指定元素的顺序。
class Test implements Delayed{
private long time;
public Test(long time, TimeUnit unit){
this.time = System.currentTimeMillis() + unit.toMillis(time);
}
@Override
public long getDelay(TimeUnit unit) {
return time - System.currentTimeMillis();
}
@Override
public int compareTo(Delayed o) {
long diff = this.time - ((Test)o).time;
if(diff <= 0){
return -1;
}else{
return 1;
}
}
}
public class TestDemo {
public static void main(String[] args) {
Test test1 = new Test(5, TimeUnit.SECONDS);
Test test2 = new Test(10, TimeUnit.SECONDS);
Test test3 = new Test(15, TimeUnit.SECONDS);
DelayQueue<Test> queue = new DelayQueue<>();
queue.put(test1);
queue.put(test2);
queue.put(test3);
System.out.println("begin time "+ LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
for(int i=0; i<3; i++){
try {
Test test = queue.take();
System.out.println("current time "+LocalDateTime.now().format(DateTimeFormatter.ISO_LOCAL_DATE_TIME));
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
SynchronousQueue
SynchronousQueue
是一个不存储元素的阻塞队列。每一个put
操作必须等待一个take
操作,否则不能继续添加元素。它支持公平访问队列。默认情况下线程采用非公平性策略访问队列。使用以下构造方法可以创建公平性访问的SynchronousQueue
,如果设置为true
,则等待的线程会采用先进先出的顺序访问队列。
SynchronousQueue不存储元素的队列,每一个put操作必须等待一个take操作,都则不能继续添加元素。(天然实现了生产者消费者模型,负责额生产者线程处理的数据直接传递给消费者线程,队列本身不存放任何元素)
SynchronousQueue<E> queue = new SynchronousQueue<E>(true);
- 构造函数:
/**
* Creates a {@code SynchronousQueue} with nonfair access policy.
*/
public SynchronousQueue() {
this(false);
}
/**
* Creates a {@code SynchronousQueue} with the specified fairness policy.
*
* @param fair if true, waiting threads contend in FIFO order for
* access; otherwise the order is unspecified.
*/
public SynchronousQueue(boolean fair) {
transferer = fair ? new TransferQueue<E>() : new TransferStack<E>();
}
SynchronousQueue
可以看成是一个传球手,负责把生产者线程处理的数据直接传递给消费者线程。队列本身并不存储任何元素,非常适合传递性场景。SynchronousQueue
的吞吐量高于LinkedBlockingQueue
和ArrayBlockingQueue
。
public class TestDemo {
private static final SynchronousQueue<Integer> queue = new SynchronousQueue<>();
public static void main(String[] args) {
Thread putThread = new Thread("putThread"){
@Override
public void run() {
System.out.println("put start");
try {
queue.put(1);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("put end");
}
};
Thread takeThread = new Thread("takeThread"){
@Override
public void run() {
System.out.println("take start");
try {
System.out.println("take from putThread "+queue.take());
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("take end");
}
};
putThread.start();
try {
TimeUnit.MILLISECONDS.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
takeThread.start();
}
}
LinkedTransferQueue
LinkedTransferQueue
是一个由链表结构组成的无界阻塞TransferQueue
队列。相对于其他阻塞队列,LinkedTransferQueue
多了tryTransfer
和transfer
方法。
- 1、
transfer
方法。
如果当前有消费者正在等待接收元素(消费者使用take()方法或带时间限制的poll()方法时),transfer方法可以把生产者传入的元素立刻transfer(传输)给消费者。如果没有消费者在等待接收元素,transfer方法会将元素存放在队列的tail节点,并等到该元素被消费者消费了才返回。
- 2、
tryTransfer
方法。
tryTransfer方法是用来试探生产者传入的元素是否能直接传给消费者。如果没有消费者等待接收元素,则返回false。和transfer方法的区别是tryTransfer方法无论消费者是否接收,方法立即返回,而transfer方法是必须等到消费者消费了才返回。
对于带有时间限制的tryTransfer(E e,long timeout,TimeUnit unit)
方法,试图把生产者传入的元素直接传给消费者,但是如果没有消费者消费该元素则等待指定的时间再返回,如果超时还没消费元素,则返回false
,如果在超时时间内消费了元素,则返回true
。
LinkedBlockingDeque
LinkedBlockingDeque
是一个由链表结构组成的双向阻塞队列。所谓双向队列指的是可以从队列的两端插入和移出元素。双向队列因为多了一个操作队列的入口,在多线程同时入队时,也就减少了一半的竞争。相比其他的阻塞队列,LinkedBlockingDeque
多了addFirst
、addLast
、offerFirst
、offerLast
、peekFirst
和peekLast
等方法,以First
单词结尾的方法,表示插入、获取(peek)
或移除双端队列的第一个元素。以Last
单词结尾的方法,表示插入、获取或移除双端队列的最后一个元素。另外,插入方法add
等同于addLast
,移除方法remove
等效于removeFirst
。但是take
方法却等同于takeFirst
,不知道是不是JDK的bug,使用时还是用带有First
和Last
后缀的方法更清楚。在初始化LinkedBlockingDeque
时可以设置容量防止其过度膨胀。另外,双向阻塞队列可以运用在“工作窃取”模式中。
阻塞的实现
ReentranLock + Condition
实现队列的阻塞,ReentranLock
是锁,Condition
是条件状态,通过等待/通知机制,来实现线程之间的通信。
ReentranLock + Condition
的等待/通知机制和Object
的wait()
与notify()
是类似的,通过synchronized
,在锁中使用wait()
与notify()
达到线程之间通信,在ReentranLock
的lock()
和unlock()
之间通过类似的await()
和signal()
达到线程之间的通信。
在阻塞队列中A调用put()
方法的时候,如果队列已满,则A线程挂起,等待恢复,如果B线程调用take()
后,消耗一个队列元素后,会通知put()
方法中挂起的A线程,因为这个时候B线程已经消耗一个元素可以在向队列中添加元素。相反如果队列一空 B线程调用tack()
方法会阻塞,当A线程调用put()
调用后通知tack()
方法中的B线程,有元素可以获取。这就是阻塞的实现过程。
下面查看ArrayBlockingQueue
源码来分析这一实现过程:
ArrayBlockingQueue
的成员变量。lock
是锁。notEmpty
和notFull
是表示不为空和不为满的Condition
状态。
/** Main lock guarding all access */
final ReentrantLock lock;
/** Condition for waiting takes */
private final Condition notEmpty;
/** Condition for waiting puts */
private final Condition notFull;
- 构造函数初始化,默认使用非公平锁。
/**
* Creates an {@code ArrayBlockingQueue} with the given (fixed)
* capacity and default access policy.
*
* @param capacity the capacity of this queue
* @throws IllegalArgumentException if {@code capacity < 1}
*/
public ArrayBlockingQueue(int capacity) {
this(capacity, false);
}
/**
* Creates an {@code ArrayBlockingQueue} with the given (fixed)
* capacity and the specified access policy.
*
* @param capacity the capacity of this queue
* @param fair if {@code true} then queue accesses for threads blocked
* on insertion or removal, are processed in FIFO order;
* if {@code false} the access order is unspecified.
* @throws IllegalArgumentException if {@code capacity < 1}
*/
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();
}
- 查看
put()
方法。
/**
* Inserts the specified element at the tail of this queue, waiting
* for space to become available if the queue is full.
*
* @throws InterruptedException {@inheritDoc}
* @throws NullPointerException {@inheritDoc}
*/
public void put(E e) throws InterruptedException {
//非空校验
checkNotNull(e);
final ReentrantLock lock = this.lock;
//加锁
lock.lockInterruptibly();
try {
//循环判断队列是否已满
while (count == items.length)
//获得lock锁的对象挂起
notFull.await();
//队列不满,添加队列
enqueue(e);
} finally {
//释放锁
lock.unlock();
}
}
- 查看
enqueue()
方法。
/**
* Inserts element at current put position, advances, and signals.
* Call only when holding lock.
*/
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++;
/**
* 在take()方法中,如果队列为空,调用notEmpty.await()
* 挂起当前take中的线程,当put()方法中添加元素成功后,
* 调用notEmpty.signal()通知take()方法中挂起的线程
*/
notEmpty.signal();
}
- 查看
take()
方法。
public E take() throws InterruptedException {
final ReentrantLock lock = this.lock;
//加锁
lock.lockInterruptibly();
try {
//循环判断 队列是否为空
while (count == 0)
//挂起线程
notEmpty.await();
//获取队列头部元素
return dequeue();
} finally {
//释放锁
lock.unlock();
}
}
- 查看
dequeue()
方法。
/**
* Extracts element at current take position, advances, and signals.
* Call only when holding lock.
*/
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.await()
notFull.signal();
return x;
}