Queue常用类解析之ConcurrentLinkedQueue

Queue常用类解析之PriorityQueue

一、简述

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
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值