ConcurrentLinkedQueue解析

Queue接口定义:[url]http://donald-draper.iteye.com/blog/2363491[/url]
AbstractQueue简介:[url]http://donald-draper.iteye.com/blog/2363608[/url]
package java.util.concurrent;

import java.util.AbstractQueue;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Queue;

/**
* An unbounded thread-safe {@linkplain Queue queue} based on linked nodes.
* This queue orders elements FIFO (first-in-first-out).
* The [i]head[/i] of the queue is that element that has been on the
* queue the longest time.
ConcurrentLinkedQueue一个基于链接节点线程安全的无界队列。队列的元素顺序为FIFO。
队列的头部,是在队列上时间最久的元素。
* The [i]tail[/i] of the queue is that element that has been on the
* queue the shortest time. New elements
* are inserted at the tail of the queue, and the queue retrieval
* operations obtain elements at the head of the queue.
* A {@code ConcurrentLinkedQueue} is an appropriate choice when
* many threads will share access to a common collection.
* Like most other concurrent collection implementations, this class
* does not permit the use of {@code null} elements.
*
队列的尾元素是在队列中时间最短的元素。新元素添加到队列的尾部,队列获取元素,从
队列头部获取。ConcurrentLinkedQueue适用于多个线程需要同时访问一个相同集合的场景。
与大多数并发集合一样,不允许插入null元素。
* <p>This implementation employs an efficient "wait-free"
* algorithm based on one described in <a
* href="http://www.cs.rochester.edu/u/michael/PODC96.html"> Simple,
* Fast, and Practical Non-Blocking and Blocking Concurrent Queue
* Algorithms</a> by Maged M. Michael and Michael L. Scott.
*
队列的实现是基于一个有效的wait-free算法,具体参见链接:简单快速实用的并发
阻塞与非阻塞算法
* <p>Iterators are <i>weakly consistent</i>, returning elements
* reflecting the state of the queue at some point at or since the
* creation of the iterator. They do [i]not[/i] throw {@link
* java.util.ConcurrentModificationException}, and may proceed concurrently
* with other operations. Elements contained in the queue since the creation
* of the iterator will be returned exactly once.
*
Iterators是弱一致性,只反映了队列在某一点的状态,比如创建iterator的时间点。
Iterators不会抛出异常,可以处理其他的并发操作。在队列创建Iterators时,队列中的
元素,全都在Iterators中。
* <p>Beware that, unlike in most collections, the {@code size} method
* is [i]NOT[/i] a constant-time operation. Because of the
* asynchronous nature of these queues, determining the current number
* of elements requires a traversal of the elements, and so may report
* inaccurate results if this collection is modified during traversal.
* Additionally, the bulk operations {@code addAll},
* {@code removeAll}, {@code retainAll}, {@code containsAll},
* {@code equals}, and {@code toArray} are [i]not[/i] guaranteed
* to be performed atomically. For example, an iterator operating
* concurrently with an {@code addAll} operation might view only some
* of the added elements.
*
不像大多数的集合,size操作时间复杂度不是一个常量,因为队列异步操作的天性,
决定了遍历队列元素时,有可能其他线程修改队列,导致最终size的数量可能不准确。
另外addAll,removeAll,retainAll,containsAll,equals,toArray都不能保证原子性。
不如遍历操作和addAll操作同时发生,可能会看到一些相同的新增元素。
* <p>This class and its iterator implement all of the [i]optional[/i]
* methods of the {@link Queue} and {@link Iterator} interfaces.
*
ConcurrentLinkedQueue实现了所有Queue和Iterator接口的所有方法。
* <p>Memory consistency effects: As with other concurrent
* collections, actions in a thread prior to placing an object into a
* {@code ConcurrentLinkedQueue}
* [url=package-summary.html#MemoryVisibility]<i>happen-before</i>[/url]
* actions subsequent to the access or removal of that element from
* the {@code ConcurrentLinkedQueue} in another thread.
*
内存一致性:与其他并发线程集合一样,线程添加一个元素到队列发生在其他线程访问
或移除队列元素之前。
* <p>This class is a member of the
* <a href="{@docRoot}/../technotes/guides/collections/index.html">
* Java Collections Framework</a>.
*
* @since 1.5
* @author Doug Lea
* @param <E> the type of elements held in this collection
*
*/
public class ConcurrentLinkedQueue<E> extends AbstractQueue<E>
implements Queue<E>, java.io.Serializable {
private static final long serialVersionUID = 196745693267521676L;

/*
* This is a modification of the Michael & Scott algorithm,
* adapted for a garbage-collected environment, with support for
* interior node deletion (to support remove(Object)). For
* explanation, read the paper.
*
这是一个Michael & Scott 算法的修改,以适应内部节点的删除,引起的垃圾回收。
* Note that like most non-blocking algorithms in this package,
* this implementation relies on the fact that in garbage
* collected systems, there is no possibility of ABA problems due
* to recycled nodes, so there is no need to use "counted
* pointers" or related techniques seen in versions used in
* non-GC'ed settings.
*
像其他非阻塞算法一样,本队列的实现依赖于实际的系统垃圾回收器,
由于会回收节点,所以不可能发生ABA问题,所以不需要用,计数指针和non-GC
设置的相关技术。

* The fundamental invariants are:
* - There is exactly one (last) Node with a null next reference,
* which is CASed when enqueueing. This last Node can be
* reached in O(1) time from tail, but tail is merely an
* optimization - it can always be reached in O(N) time from
* head as well.
基本原理是不变的:当以CAS方式入队列时,队尾的元素的next指针为null。
我们用tail节点可,以常量1的速度到达尾部元素,tail仅仅是一个优化,也可从
对头以时间 O(N) 到达队尾。
* - The elements contained in the queue are the non-null items in
* Nodes that are reachable from head. CASing the item
* reference of a Node to null atomically removes it from the
* queue. Reachability of all elements from head must remain
* true even in the case of concurrent modifications that cause
* head to advance. A dequeued Node may remain in use
* indefinitely due to creation of an Iterator or simply a
* poll() that has lost its time slice.
*
队列中的元素都是非null的,同时可以从队列头到达。CAS操作一个元素为null,
则直接从队列移除元素。队列头到队列中的其他元素,必须可达,以防并发修改,
导致的队列头前移。一个已经出队列的元素可能仍在被用,比如创建Iterator,
或poll操作失去的时间片。
* The above might appear to imply that all Nodes are GC-reachable
* from a predecessor dequeued Node. That would cause two problems:
* - allow a rogue Iterator to cause unbounded memory retention
* - cause cross-generational linking of old Nodes to new Nodes if
* a Node was tenured while live, which generational GCs have a
* hard time dealing with, causing repeated major collections.
* However, only non-deleted Nodes need to be reachable from
* dequeued Nodes, and reachability does not necessarily have to
* be of the kind understood by the GC. We use the trick of
* linking a Node that has just been dequeued to itself. Such a
* self-link implicitly means to advance to head.
*
以上情况的出现,预示着所有节点从前驱的出队列元素都GC可达。这样
可能会引起来个问题:运行一个无赖Iterator引起内存垃圾;当节点存活在
老年代,可能存在旧节点到新节点的交叉代连接,新生和老年的垃圾回收器
很难处理,这样就会引起重复的FULL GC。然而,已删除的节点,可以从出队列
节点达到,可达性不需要到达GC可以理解的那种。为了避免这种情况的发生,
我们出队列的节点只会连接到它自己。自连接意味着促使队列头元素前进。

* Both head and tail are permitted to lag. In fact, failing to
* update them every time one could is a significant optimization
* (fewer CASes). As with LinkedTransferQueue (see the internal
* documentation for that class), we use a slack threshold of two;
* that is, we update head/tail when the current pointer appears
* to be two or more steps away from the first/last node.
*
对头和队尾都允许滞后,最优化的更新队头和队尾可能会失败,不如fewer CASes。
对于LinkedTransferQueue,我们用一个非严谨的临界条件2,我们从当前节点
更新head/tail,至少需要2步以上。
* Since head and tail are updated concurrently and independently,
* it is possible for tail to lag behind head (why not)?
*
由于head and tail是并发独立更新的,队尾的更新可能在队头后面。
* CASing a Node's item reference to null atomically removes the
* element from the queue. Iterators skip over Nodes with null
* items. Prior implementations of this class had a race between
* poll() and remove(Object) where the same element would appear
* to be successfully removed by two concurrent operations. The
* method remove(Object) also lazily unlinks deleted Nodes, but
* this is merely an optimization.
*
CAS操作一个节点引用为null,将自动从队列中删除。Iterators将会跳过
null元素。当poll和remove操作并发时,优先移除元素。remove也为懒操作
去除链接,这样一个优化。
* When constructing a Node (before enqueuing it) we avoid paying
* for a volatile write to item by using Unsafe.putObject instead
* of a normal write. This allows the cost of enqueue to be
* "one-and-a-half" CASes.
*
在节点入队列前,构造节点时,要避免用可见性的Unsafe.putObject,
而是用正常的write。进队列的消耗允许是CAS操作的1.5倍。
* Both head and tail may or may not point to a Node with a
* non-null item. If the queue is empty, all items must of course
* be null. Upon creation, both head and tail refer to a dummy
* Node with null item. Both head and tail are only updated using
* CAS, so they never regress, although again this is merely an
* optimization.
*/
head and tail可能,也可能不指向一个非null元素。当队列为null时,
所有的元素自然为null。当队列创建时,head and tail引用一个null的
傀儡节点。head and tail仅仅用CAS操作更新,所以不会后退,这是一个优化。
//队列节点元素
private static class Node<E> {
volatile E item;节点
volatile Node<E> next;后继

/**
* Constructs a new node. Uses relaxed write because item can
* only be seen after publication via casNext.
*/
//构造节点
Node(E item) {
UNSAFE.putObject(this, itemOffset, item);
}
//比较旧元素,相等则更新,CAS
boolean casItem(E cmp, E val) {
return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val);
}
//懒设置节点后继
void lazySetNext(Node<E> val) {
UNSAFE.putOrderedObject(this, nextOffset, val);
}
//CAS节点的next
boolean casNext(Node<E> cmp, Node<E> val) {
return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
}

// Unsafe mechanics

private static final sun.misc.Unsafe UNSAFE;
private static final long itemOffset;
private static final long nextOffset;

static {
try {
UNSAFE = sun.misc.Unsafe.getUnsafe();
Class k = Node.class;
itemOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("item"));
nextOffset = UNSAFE.objectFieldOffset
(k.getDeclaredField("next"));
} catch (Exception e) {
throw new Error(e);
}
}
}
/**
* A node from which the first live (non-deleted) node (if any)
* can be reached in O(1) time.
队头元素,队列中第一个存活元素,可以,以常量1的速度到达
* Invariants:
不变的是:所以的节点都可以通过succ,到达头部
* - all live nodes are reachable from head via succ()
* - head != null
* - (tmp = head).next != tmp || tmp != head
* Non-invariants:
可变的是,头结点元素可能为null,也可能不为null
* - head.item may or may not be null.
* - it is permitted for tail to lag behind head, that is, for tail
* to not be reachable from head!
*/
允许tail的更新晚于head,意味着队尾到队头不可达。
private transient volatile Node<E> head;

/**
* A node from which the last node on list (that is, the unique
* node with node.next == null) can be reached in O(1) time.
队列中最后一个元素,后继为null,以常量1的速度到达
* Invariants:
不变的是:所以的节点都可以通过succ,从队尾到
* - the last node is always reachable from tail via succ()
* - tail != null
* Non-invariants:
可变的是:允许tail的更新晚于head,意味着队尾到队头不可达。
* - tail.item may or may not be null.
* - it is permitted for tail to lag behind head, that is, for tail
* to not be reachable from head!
* - tail.next may or may not be self-pointing to tail.
*/
tail可能也可能不会指向自己。
private transient volatile Node<E> tail;
/**
* Creates a {@code ConcurrentLinkedQueue} that is initially empty.
*/
构造ConcurrentLinkedQueue
public ConcurrentLinkedQueue() {
head = tail = new Node<E>(null);
}
}

来看添加队列元素:
/**
* Inserts the specified element at the tail of this queue.
* As the queue is unbounded, this method will never throw
* {@link IllegalStateException} or return {@code false}.
*
* @return {@code true} (as specified by {@link Collection#add})
* @throws NullPointerException if the specified element is null
*/
public boolean add(E e) {
委托给Offer
return offer(e);
}

/**
* Inserts the specified element at the tail of this queue.
* As the queue is unbounded, this method will never return {@code false}.
*
* @return {@code true} (as specified by {@link Queue#offer})
* @throws NullPointerException if the specified element is null
*/
public boolean offer(E e) {
//检查是否为null
checkNotNull(e);
//根据元素创建节点
final Node<E> newNode = new Node<E>(e);

for (Node<E> t = tail, p = t;;) {
Node<E> q = p.next;
if (q == null) {
//如果为最后一个元素,且后继为null,则以CAS设置新节点为tail的后继
// p is last node
if (p.casNext(null, newNode)) {
// Successful CAS is the linearization point
// for e to become an element of this queue,
// and for newNode to become "live".
if (p != t) // hop two nodes at a time
//将新节点设为尾元素
casTail(t, newNode); // Failure is OK.
return true;
}
// Lost CAS race to another thread; re-read next
}
else if (p == q)
// We have fallen off list. If tail is unchanged, it
// will also be off-list, in which case we need to
// jump to head, from which all live nodes are always
// reachable. Else the new tail is a better bet.
//当tail指向自己,则不在队列中,则需要调到队头,以使其他节点可达
p = (t != (t = tail)) ? t : head;
else
// Check for tail updates after two hops.在两步之后,检查队尾是否更新
p = (p != t && t != (t = tail)) ? t : q;
}
}

检查是否为null
  /**
* Throws NullPointerException if argument is null.
*
* @param v the element
*/
private static void checkNotNull(Object v) {
if (v == null)
throw new NullPointerException();
}

poll操作:
  public E poll() {
restartFromHead:
for (;;) {
for (Node<E> h = head, p = h, q;;) {
//检查队头节点,如果不为null,则设置为null
E item = p.item;

if (item != null && p.casItem(item, null)) {
// Successful CAS is the linearization point
// for item to be removed from this queue.
if (p != h) // hop two nodes at a time,更新头部为其后继
updateHead(h, ((q = p.next) != null) ? q : p);
return item;
}
else if ((q = p.next) == null) {
//更新头部为p,队列为空
updateHead(h, p);
return null;
}
else if (p == q)
continue restartFromHead;
else
p = q;
}
}
}

更新头节点:
 /**
* Try to CAS head to p. If successful, repoint old head to itself
* as sentinel for succ(), below.
*/
final void updateHead(Node<E> h, Node<E> p) {
//懒设置队头节点为其后继,并将旧的节点指向自己,以便垃圾回收
if (h != p && casHead(h, p))
h.lazySetNext(h);
}

peek操作:
 public E peek() {
restartFromHead:
for (;;) {
for (Node<E> h = head, p = h, q;;) {
E item = p.item;
if (item != null || (q = p.next) == null) {
//返回队头元素,更新头结点
updateHead(h, p);
return item;
}
else if (p == q)
continue restartFromHead;
else
p = q;
}
}
}

//返回队列第一个节点
Node<E> first() {
restartFromHead:
for (;;) {
for (Node<E> h = head, p = h, q;;) {
boolean hasItem = (p.item != null);
if (hasItem || (q = p.next) == null) {
updateHead(h, p);
return hasItem ? p : null;
}
else if (p == q)
continue restartFromHead;
else
p = q;
}
}
}


获取节点的后继节点
 final Node<E> succ(Node<E> p) {
Node<E> next = p.next;
return (p == next) ? head : next;
}

移除元素
public boolean remove(Object o) {
if (o == null) return false;
Node<E> pred = null;
//遍历队列,找到节点数据与o相等的节点,更新节点后继为null,更新前驱的后继
for (Node<E> p = first(); p != null; p = succ(p)) {
E item = p.item;
if (item != null &&
o.equals(item) &&
p.casItem(item, null)) {
Node<E> next = succ(p);
if (pred != null && next != null)
pred.casNext(p, next);
return true;
}
pred = p;
}
return false;
}

//查询队列是否包含某元素
 public boolean contains(Object o) {
if (o == null) return false;
for (Node<E> p = first(); p != null; p = succ(p)) {
E item = p.item;
if (item != null && o.equals(item))
return true;
}
return false;
}

//获取队列size
 public int size() {
int count = 0;
for (Node<E> p = first(); p != null; p = succ(p))
if (p.item != null)
// Collection.size() spec says to max out
if (++count == Integer.MAX_VALUE)
break;
return count;
}

//根据C构造ConcurrentLinkedQueue
 public ConcurrentLinkedQueue(Collection<? extends E> c) {
Node<E> h = null, t = null;
//遍历集合,将元素组装成节点链
for (E e : c) {
checkNotNull(e);
Node<E> newNode = new Node<E>(e);
if (h == null)
h = t = newNode;
else {
t.lazySetNext(newNode);
t = newNode;
}
}
if (h == null)
h = t = new Node<E>(null);
//初始化队列与队尾
head = h;
tail = t;
}

添加集合元素到队列
public boolean addAll(Collection<? extends E> c) {
if (c == this)
// As historically specified in AbstractQueue#addAll
throw new IllegalArgumentException();

// Copy c into a private chain of Nodes
Node<E> beginningOfTheEnd = null, last = null;
//将集合元素组装成节点链
for (E e : c) {
checkNotNull(e);
Node<E> newNode = new Node<E>(e);
if (beginningOfTheEnd == null)
beginningOfTheEnd = last = newNode;
else {
last.lazySetNext(newNode);
last = newNode;
}
}
if (beginningOfTheEnd == null)
return false;

// Atomically append the chain at the tail of this collection
//将节点链,挂到队列尾
for (Node<E> t = tail, p = t;;) {
Node<E> q = p.next;
if (q == null) {
// p is last node
if (p.casNext(null, beginningOfTheEnd)) {
// Successful CAS is the linearization point
// for all elements to be added to this queue.
if (!casTail(t, last)) {
// Try a little harder to update tail,
// since we may be adding many elements.
t = tail;
if (last.next == null)
casTail(t, last);
}
return true;
}
// Lost CAS race to another thread; re-read next
}
else if (p == q)
// We have fallen off list. If tail is unchanged, it
// will also be off-list, in which case we need to
// jump to head, from which all live nodes are always
// reachable. Else the new tail is a better bet.
p = (t != (t = tail)) ? t : head;
else
// Check for tail updates after two hops.
p = (p != t && t != (t = tail)) ? t : q;
}
}

序列化:
private void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException {

// Write out any hidden stuff
s.defaultWriteObject();

// Write out all elements in the proper order.
//序列化所有元素
for (Node<E> p = first(); p != null; p = succ(p)) {
Object item = p.item;
if (item != null)
s.writeObject(item);
}

// Use trailing null as sentinel
s.writeObject(null);
}

/**
* Reconstitutes the instance from a stream (that is, deserializes it).
* @param s the stream
*/
private void readObject(java.io.ObjectInputStream s)
throws java.io.IOException, ClassNotFoundException {
s.defaultReadObject();

// Read in elements until trailing null sentinel found
Node<E> h = null, t = null;
Object item;
//反序列化所有元素到队列
while ((item = s.readObject()) != null) {
@SuppressWarnings("unchecked")
Node<E> newNode = new Node<E>((E) item);
if (h == null)
h = t = newNode;
else {
t.lazySetNext(newNode);
t = newNode;
}
}
if (h == null)
h = t = new Node<E>(null);
head = h;
tail = t;
}

总结:
[color=green]ConcurrentLinkedQueue一个基于链接节点线程安全的单向无界队列。队列的元素顺序为FIFO。队列的头部,是在队列上时间最久的元素。队列的尾元素是在队列中时间最短的元素。新元素添加到队列的尾部,队列获取元素,从队列头部获取ConcurrentLinkedQueue适用于多个线程需要同时访问一个相同集合的场景。与大多数并发集合一样,不允许插入null元素。Iterators是弱一致性,只反映了队列在某一点的状态,比如创建iterator的时间点。
Iterators不会抛出异常,可以处理其他的并发操作。在队列创建Iterators时,队列中的元素,全都在Iterators中。不像大多数的集合,size操作时间复杂度不是一个常量,因为队列异步操作的天性,决定了遍历队列元素时,有可能其他线程修改队列,导致最终size的数量可能不准确。另外addAll,removeAll,retainAll,containsAll,equals,toArray都不能保证原子性。比如遍历操作和addAll操作同时发生,可能会看到一些相同的新增元素。[/color]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值