一、简述
ConcurrentLinkedQueue是jdk提供的一个非阻塞式的链表结构的并发队列,ConcurrentLinkedQueue通过CAS方法基于Unsafe类直接对对象进行反射修改,从而保证其并发性。
ConcurrentLinkedQueue的元素不能为null。
ConcurrentLinkedQueue的思想使用的是Michael & Scott 算法
二、Node
Node是ConcurrentLinkedQueue的静态内部类,用来表示其链表结果中的节点,不过与其它链表不同的是,Node对象的所有修改都基于Unsafe完成。
volatile E item;
volatile Node<E> next;
三、属性
ConcurrentLinkedQueue只有两个成员变量:head和tail,分别表示头结点和尾节点。
private transient volatile Node<E> head;
private transient volatile Node<E> tail;
1. head
表示链表的头结点,但不一定是链表的第一个节点。
A node from which the first live (non-deleted) node (if any)
can be reached in O(1) time.
Invariants:
- all live nodes are reachable from head via succ()
- head != null
- (tmp = head).next != tmp || tmp != head
Non-invariants:
- 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!、
(1)不变性
- 队列里的所有节点都可以由head节点通过succ()方法到达
- head不能为null
- head 的next属性不能指向自身
(2)可变性
- head的item属性可以为null,也可能不为null
- 运行tail滞后于head,也就是说tail对于head可以是不可达的
2. tail
表示链表的尾节点,但不一定是队列的最后一个节点
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.
Invariants:
- the last node is always reachable from tail via succ()
- tail != null
Non-invariants:
- 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.
(1)不变性
- 队列里的最后一个节点都可以由tail节点通过succ()方法到达
- tail不能为null
(2)可变性
- tail的item属性可以为null