1、概述
LinkedBlockingQueue:
- 线程安全
- 队列可以指定容量大小
- 使用内部类Node作为链表的节点
- FIFO:先进先出队列
- 双锁模式/双等待队列:采用生产者和消费者模式,并且都有对应的锁(ReentrantLock)以及条件等待队列
2、源码分析
public class LinkedBlockingQueue<E> extends AbstractQueue<E>
implements BlockingQueue<E>, java.io.Serializable {
private static final long serialVersionUID = -6903933977591709194L;
// 采用节点的形式来保存数据,保存了当前的数值以及下一个节点的引用
static class Node<E> {
E item;
Node<E> next;
Node(E x) { item = x; }
}
/** 队列的容量 */
private final int capacity;
/** 当前队列的元素个数 */
private final AtomicInteger count = new AtomicInteger();
/** 队列的头结点,一直都为空 */
transient Node<E> head;
/** 队列的尾结点 */
private transient Node<E> last;
/** 取元素的锁 */
private final ReentrantLock takeLock = new ReentrantLock();
/** 取元素的等待队列(消费队列):队列为空,那么取元素的线程就必须到Condition进行等待 */
private final Condition notEmpty = takeLock.newCondition();
/** 放元素的锁 */
private final ReentrantLock putLock = new ReentrantLock();
/** 放元素的等待队列(生产队列):队列元素个数已达到容量限制,那么放元素的线程就必须等待 */
private final Condition notFull = putLock.newCondition();
// 队列的元素不为空,需要唤醒消费队列的线程进行消费
private void signalNotEmpty() {
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
// 唤醒消费队列中的线程
notEmpty.signal();
} finally {
takeLock.unlock();
}
}
// 队列的元素未满,需要唤醒生产队列的线程进行生产
private void signalNotFull() {
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
notFull.signal();
} finally {
putLock.unlock();
}
}
// 入队操作:插入到队列尾部
private void enqueue(Node<E> node) {
last = last.next = node;
}
// 出队操作:头部元素出队
private E dequeue() {
Node<E> h = head;
Node<E> first = h.next;
h.next = h; // help GC
head = first;
E x = first.item;
first.item = null; // 出队之后依旧保持头部节点的item=null
return x;
}
// 同时加锁、同时解锁
void fullyLock() {
putLock.lock();
takeLock.lock();
}
void fullyUnlock() {
takeLock.unlock();
putLock.unlock();
}
// 默认容量为Interger的最大值
public LinkedBlockingQueue() {
this(Integer.MAX_VALUE);
}
// 可以指定容量,队列使用item=null作为头部
public LinkedBlockingQueue(int capacity) {
if (capacity <= 0) throw new IllegalArgumentException();
this.capacity = capacity;
last = head = new Node<E>(null);
}
public LinkedBlockingQueue(Collection<? extends E> c) {
this(Integer.MAX_VALUE);
final ReentrantLock putLock = this.putLock;
putLock.lock(); // Never contended, but necessary for visibility
try {
int n = 0;
for (E e : c) {
if (e == null)
throw new NullPointerException();
if (n == capacity)
throw new IllegalStateException("Queue full");
enqueue(new Node<E>(e));
++n;
}
count.set(n);
} finally {
putLock.unlock();
}
}
// 获取队列中的元素个数
public int size() {
return count.get();
}
// 队列剩余的容量
public int remainingCapacity() {
return capacity - count.get();
}
// 插入元素,当队列已满,再放元素就会一直等待,直到队列不满或者被其它线程打断
public void put(E e) throws InterruptedException {
if (e == null) throw new NullPointerException();
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
final AtomicInteger count = this.count;
// 上锁,如果执行逻辑很久,当前线程可以被其它线程打断
putLock.lockInterruptibly();
try {
// 当前元素个数和容量相等的话,那么生产线程需要等待(循环)
while (count.get() == capacity) {
notFull.await(); // 将当前生产线程扔进等待队列中进行等待
}
enqueue(node); // 入队操作
c = count.getAndIncrement(); // 获取插入元素前队列的元素个数
// c+1表示插入元素后队列当前元素个数,如果小于当前容量,那么唤醒一个生产者进行生产
if (c + 1 < capacity)
notFull.signal();
} finally {
// 解锁
putLock.unlock();
}
/**
* 如果c==0,那么说明,插入元素之前,就表明队列为空,那么消费线程都会被扔到notEmpty队列
* 中进行等待,此时如果不进行如此操作,就会出现生产者生产之后,无消费线程进行消费。
* 如果以后写生产消费模式,无论是生产还是消费动作,一定要确保生产和消费配对出现
*/
if (c == 0)
signalNotEmpty();
}
/**
* 插入元素,当队列已满,会等待指定的时长,等待中途可以被打断。
* 当指定时长过后,如果队列依旧已满,则直接返回false,表示插入元素失败。
* 队列未满,操作和put方法一致
*/
public boolean offer(E e, long timeout, TimeUnit unit) throws InterruptedException {
if (e == null) throw new NullPointerException();
long nanos = unit.toNanos(timeout);
int c = -1;
final ReentrantLock putLock = this.putLock;
final AtomicInteger count = this.count;
putLock.lockInterruptibly();
try {
while (count.get() == capacity) {
if (nanos <= 0)
return false;
nanos = notFull.awaitNanos(nanos);
}
enqueue(new Node<E>(e));
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
return true;
}
// 插入元素的大致逻辑都一致,只不过当前的offer为提供等待时间
public boolean offer(E e) {
if (e == null) throw new NullPointerException();
final AtomicInteger count = this.count;
if (count.get() == capacity)
return false;
int c = -1;
Node<E> node = new Node<E>(e);
final ReentrantLock putLock = this.putLock;
putLock.lock();
try {
if (count.get() < capacity) {
enqueue(node);
c = count.getAndIncrement();
if (c + 1 < capacity)
notFull.signal();
}
} finally {
putLock.unlock();
}
if (c == 0)
signalNotEmpty();
return c >= 0;
}
// 取元素
public E take() throws InterruptedException {
E x;
int c = -1;
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (count.get() == 0) {
notEmpty.await(); // 当队列为空,消费线程就被扔进等待队列
}
x = dequeue(); // 出队操作
c = count.getAndDecrement(); // 获取出队之前的元素个数
// 出队前元素的个数大于1,还可以唤醒消费者线程
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
/**
* 如果c==capacity,消费之前,就表明队列已满,那么生产线程都会被扔到notFull队列
* 中进行等待,此时如果不进行如此操作,就会出现消费之后,无生产线程进行生产。
* 如果以后写生产消费模式,无论是生产还是消费动作,一定要确保生产和消费配对出现
*/
if (c == capacity)
signalNotFull();
return x;
}
// 取元素(指定的时间内),否则插入取失败
public E poll(long timeout, TimeUnit unit) throws InterruptedException {
E x = null;
int c = -1;
long nanos = unit.toNanos(timeout);
final AtomicInteger count = this.count;
final ReentrantLock takeLock = this.takeLock;
takeLock.lockInterruptibly();
try {
while (count.get() == 0) {
if (nanos <= 0)
return null;
nanos = notEmpty.awaitNanos(nanos);
}
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
// 取元素
public E poll() {
final AtomicInteger count = this.count;
if (count.get() == 0)
return null;
E x = null;
int c = -1;
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
if (count.get() > 0) {
x = dequeue();
c = count.getAndDecrement();
if (c > 1)
notEmpty.signal();
}
} finally {
takeLock.unlock();
}
if (c == capacity)
signalNotFull();
return x;
}
// 获取头部元素,但头部元素不会出队
public E peek() {
if (count.get() == 0)
return null;
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
Node<E> first = head.next;
if (first == null)
return null;
else
return first.item;
} finally {
takeLock.unlock();
}
}
/**
* p:待删除的节点
* trail:p节点的前驱节点
*/
void unlink(Node<E> p, Node<E> trail) {
p.item = null; // 置空
trail.next = p.next; // 链表衔接
if (last == p)
last = trail;
// 删除之后要唤醒生产者线程进行生产
if (count.getAndDecrement() == capacity)
notFull.signal();
}
// 删除元素
public boolean remove(Object o) {
if (o == null) return false;
fullyLock();
try {
// 因为头部节点为null,所以从下一个节点开始遍历
for (Node<E> trail = head, p = trail.next; p != null; trail = p, p = p.next){
// 数据首匹配,那么就开始删除数据
if (o.equals(p.item)) {
// 将当前的元素从链表汇总剔除
unlink(p, trail);
return true;
}
}
return false;
} finally {
fullyUnlock();
}
}
// 是否包含元素
public boolean contains(Object o) {
if (o == null) return false;
fullyLock();
try {
for (Node<E> p = head.next; p != null; p = p.next)
if (o.equals(p.item))
return true;
return false;
} finally {
fullyUnlock();
}
}
// 转换为数组
public Object[] toArray() {
fullyLock();
try {
int size = count.get();
Object[] a = new Object[size];
int k = 0;
for (Node<E> p = head.next; p != null; p = p.next)
a[k++] = p.item;
return a;
} finally {
fullyUnlock();
}
}
// 根据类型转为数组
@SuppressWarnings("unchecked")
public <T> T[] toArray(T[] a) {
fullyLock();
try {
int size = count.get();
if (a.length < size)
a = (T[])java.lang.reflect.Array.newInstance
(a.getClass().getComponentType(), size);
int k = 0;
for (Node<E> p = head.next; p != null; p = p.next)
a[k++] = (T)p.item;
if (a.length > k)
a[k] = null;
return a;
} finally {
fullyUnlock();
}
}
public String toString() {
fullyLock();
try {
Node<E> p = head.next;
if (p == null)
return "[]";
StringBuilder sb = new StringBuilder();
sb.append('[');
for (;;) {
E e = p.item;
sb.append(e == this ? "(this Collection)" : e);
p = p.next;
if (p == null)
return sb.append(']').toString();
sb.append(',').append(' ');
}
} finally {
fullyUnlock();
}
}
// 清空队列的元素
public void clear() {
fullyLock();
try {
for (Node<E> p, h = head; (p = h.next) != null; h = p) {
h.next = h;
p.item = null;
}
head = last;
// assert head.item == null && head.next == null;
if (count.getAndSet(0) == capacity)
notFull.signal();
} finally {
fullyUnlock();
}
}
public int drainTo(Collection<? super E> c) {
return drainTo(c, Integer.MAX_VALUE);
}
// 将队列的元素按照maxElements个数已到c集合中,从头部开始
public int drainTo(Collection<? super E> c, int maxElements) {
if (c == null)
throw new NullPointerException();
if (c == this)
throw new IllegalArgumentException();
if (maxElements <= 0)
return 0;
boolean signalNotFull = false;
final ReentrantLock takeLock = this.takeLock;
takeLock.lock();
try {
int n = Math.min(maxElements, count.get());
// count.get provides visibility to first n Nodes
Node<E> h = head;
int i = 0;
try {
while (i < n) {
Node<E> p = h.next;
c.add(p.item);
p.item = null;
h.next = h;
h = p;
++i;
}
return n;
} finally {
// Restore invariants even if c.add() threw
if (i > 0) {
// assert h.item == null;
head = h;
signalNotFull = (count.getAndAdd(-i) == capacity);
}
}
} finally {
takeLock.unlock();
if (signalNotFull)
signalNotFull();
}
}
public Iterator<E> iterator() {
return new Itr();
}
// 迭代器
private class Itr implements Iterator<E> {
/*
* Basic weakly-consistent iterator. At all times hold the next
* item to hand out so that if hasNext() reports true, we will
* still have it to return even if lost race with a take etc.
*/
private Node<E> current; //当前的节点引用
private Node<E> lastRet; // 上一个节点引用
private E currentElement; // 当前节点数据
Itr() {
fullyLock();
try {
current = head.next;
if (current != null)
currentElement = current.item;
} finally {
fullyUnlock();
}
}
public boolean hasNext() {
return current != null;
}
/**
* Returns the next live successor of p, or null if no such.
*
* Unlike other traversal methods, iterators need to handle both:
* - dequeued nodes (p.next == p)
* - (possibly multiple) interior removed nodes (p.item == null)
*/
private Node<E> nextNode(Node<E> p) {
for (;;) {
Node<E> s = p.next;
if (s == p)
return head.next;
if (s == null || s.item != null)
return s;
p = s;
}
}
public E next() {
fullyLock();
try {
if (current == null)
throw new NoSuchElementException();
E x = currentElement;
lastRet = current;
current = nextNode(current);
currentElement = (current == null) ? null : current.item;
return x;
} finally {
fullyUnlock();
}
}
public void remove() {
if (lastRet == null)
throw new IllegalStateException();
fullyLock();
try {
Node<E> node = lastRet;
lastRet = null;
for (Node<E> trail = head, p = trail.next;
p != null;
trail = p, p = p.next) {
if (p == node) {
unlink(p, trail);
break;
}
}
} finally {
fullyUnlock();
}
}
}
/** 可以分割的迭代器,迎合并行流处理设计,每次分割都是上一批次的量再加一 */
static final class LBQSpliterator<E> implements Spliterator<E> {
static final int MAX_BATCH = 1 << 25; // max batch array size;
final LinkedBlockingQueue<E> queue;
Node<E> current; // current node; null until initialized
int batch; // 被分割了多少次批次
boolean exhausted; // true when no more nodes
long est; // 元素的总个数
LBQSpliterator(LinkedBlockingQueue<E> queue) {
this.queue = queue;
this.est = queue.size();
}
// 当前批次中剩余的元素个数
public long estimateSize() { return est; }
public Spliterator<E> trySplit() {
Node<E> h;
final LinkedBlockingQueue<E> q = this.queue;
int b = batch;
// 一开始的批次量是1,后面的批次量都是再上一次量再加1
int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
if (!exhausted &&
((h = current) != null || (h = q.head.next) != null) &&
h.next != null) {
Object[] a = new Object[n];
int i = 0;
Node<E> p = current;
q.fullyLock();
try {
if (p != null || (p = q.head.next) != null) {
do {
if ((a[i] = p.item) != null)
++i;
} while ((p = p.next) != null && i < n);
}
} finally {
q.fullyUnlock();
}
if ((current = p) == null) {
est = 0L;
exhausted = true;
}
else if ((est -= i) < 0L)
est = 0L;
if (i > 0) {
batch = i;
return Spliterators.spliterator
(a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
Spliterator.CONCURRENT);
}
}
return null;
}
// 对剩余元素做相关操作
public void forEachRemaining(Consumer<? super E> action) {
if (action == null) throw new NullPointerException();
final LinkedBlockingQueue<E> q = this.queue;
if (!exhausted) {
exhausted = true;
Node<E> p = current;
do {
E e = null;
q.fullyLock();
try {
if (p == null)
p = q.head.next;
while (p != null) {
e = p.item;
p = p.next;
if (e != null)
break;
}
} finally {
q.fullyUnlock();
}
if (e != null)
action.accept(e);
} while (p != null);
}
}
public boolean tryAdvance(Consumer<? super E> action) {
if (action == null) throw new NullPointerException();
final LinkedBlockingQueue<E> q = this.queue;
if (!exhausted) {
E e = null;
q.fullyLock();
try {
if (current == null)
current = q.head.next;
while (current != null) {
e = current.item;
current = current.next;
if (e != null)
break;
}
} finally {
q.fullyUnlock();
}
if (current == null)
exhausted = true;
if (e != null) {
action.accept(e);
return true;
}
}
return false;
}
public int characteristics() {
return Spliterator.ORDERED | Spliterator.NONNULL |
Spliterator.CONCURRENT;
}
}
public Spliterator<E> spliterator() {
return new LBQSpliterator<E>(this);
}
/**
* Saves this queue to a stream (that is, serializes it).
*
* @param s the stream
* @throws java.io.IOException if an I/O error occurs
* @serialData The capacity is emitted (int), followed by all of
* its elements (each an {@code Object}) in the proper order,
* followed by a null
*/
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {
fullyLock();
try {
// Write out any hidden stuff, plus capacity
s.defaultWriteObject();
// Write out all elements in the proper order.
for (Node<E> p = head.next; p != null; p = p.next)
s.writeObject(p.item);
// Use trailing null as sentinel
s.writeObject(null);
} finally {
fullyUnlock();
}
}
/**
* Reconstitutes this queue from a stream (that is, deserializes it).
* @param s the stream
* @throws ClassNotFoundException if the class of a serialized object
* could not be found
* @throws java.io.IOException if an I/O error occurs
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
// Read in capacity, and any hidden stuff
s.defaultReadObject();
count.set(0);
last = head = new Node<E>(null);
// Read in all elements and place in queue
for (;;) {
@SuppressWarnings("unchecked")
E item = (E)s.readObject();
if (item == null)
break;
add(item);
}
}
}