ConcurrentLinkedQueue

ConcurrentLinkedQueue 能解决什么问题?什么时候使用 ConcurrentLinkedQueue?

1)ConcurrentLinkedQueue 是基于单向链表实现的线程安全【基于 CAS 实现】的、无界、FIFO、非阻塞队列。
2)ConcurrentLinkedQueue 的 offer 和 poll 操作都是非阻塞的。

如何使用 ConcurrentLinkedQueue?

1)ConcurrentLinkedQueue 并发性能比 LinkedBlockingQueue 高,但是当无元素可用时,
频繁的自旋拉取会导致 CPU 飙升,所以当消费者没有拉取到元素时,建议休眠指定的时间后再重试。

使用 ConcurrentLinkedQueue 有什么风险?

1)由于是无界非阻塞队列,当生产速率持续大于消费速率时,会导致资源耗尽,内存溢出。
2)极高的并发场景下,自旋 CAS 长时间不成功会给 CPU 带来非常大的执行开销。

ConcurrentLinkedQueue 核心操作的实现原理?

创建实例

    static final class Node<E> {
volatile E item;
volatile Node<E> next; /**
* 创建一个持有 item 的新节点
*/
Node(E item) {
ConcurrentLinkedQueue.ITEM.set(this, item);
} /** Constructs a dead dummy node. */
Node() {} // 写入后置节点
void appendRelaxed(Node<E> next) {
ConcurrentLinkedQueue.NEXT.set(this, next);
} // 尝试原子更新 item
boolean casItem(E cmp, E val) {
return ConcurrentLinkedQueue.ITEM.compareAndSet(this, cmp, val);
}
} /**
* 队列的第一个活性节点
* Invariants:
* - all live nodes are reachable from head via succ()
* 所有的节点都可以从 head 开始,通过递归 succ() 到达
* - head != null
* 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!
*/
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.
* Invariants:
* - the last node is always reachable from tail via succ()
* 最后一个节点总是可以从 tail 递归 succ() 方法到达
* - tail != null
* 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-linked.
*/
private transient volatile Node<E> tail; /**
* 创建一个空的 ConcurrentLinkedQueue 实例
*/
public ConcurrentLinkedQueue() {
head = tail = new Node<>();
}

插入元素

    /**
* 将元素 e 插入到队列尾部,由于是无界队列,该操作不会被阻塞 && 返回值永远是 true
*/
@Override
public boolean offer(E e) {
// 元素值不允许为 null
final Node<E> newNode = new Node<>(Objects.requireNonNull(e));
/**
* t:tail
* p:predecessor
* 1)读取尾节点
*/
for (Node<E> t = tail, p = t;;) {
// 读取尾节点的后置节点
final Node<E> q = p.next;
// 1)当前尾节点为最后一个节点
if (q == null) {
// p is last node
// 则尝试将新节点链接到其后面
if (ConcurrentLinkedQueue.NEXT.compareAndSet(p, null, newNode)) {
/**
* 成功的 CAS 操作使得元素 e 成为队列的节点。
*/
if (p != t) {
// 如果其他线程并发修改了 tail 节点,则尝试更新 tail 节点。
ConcurrentLinkedQueue.TAIL.weakCompareAndSet(this, t, newNode);
}
return true;
}
// Lost CAS race to another thread; re-read next
}
// 2)tail 节点和最后一个节点间隔为 2
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.
* 1)如果尾节点未改变,则读取头节点
* 2)否则读取新的尾节点
*/
p = t != (t = tail) ? t : head;
// 3)如果滞后了两个节点,则重新读取 tail 节点
} else {
/**
* Check for tail updates after two hops.
* 1)tail 节点和最后一个节点间隔为 2,并且尾部节点已经更新,则读取新的尾部节点
* 2)否则更新 p 为其后置节点 q
*/
p = p != t && t != (t = tail) ? t : q;
}
}
}

拉取元素

    /**
* 移除并返回队列头部元素,如果队列为空,则返回 null
* created by ZXD at 8 Dec 2018 T 17:12:16
* @return
*/
@Override
public E poll() {
restartFromHead: for (;;) {
// 读取头节点
for (Node<E> h = head, p = h, q;; p = q) {
final E item;
// 1)head 节点元素不为 null,则尝试将其更新为 null,此时的 head 不是傀儡节点。
if ((item = p.item) != null && p.casItem(item, null)) {
/**
* Successful CAS is the linearization point for item to be removed from this queue。
* 成功的 CAS 操作说明我们已经移除了队列头部元素
*/
if (p != h) {
// 头节点是哨兵节点,则一次移动两个位置,新头节点为数据节点
updateHead(h, (q = p.next) != null ? q : p);
}
// 返回目标元素
return item;
}
// 后置节点为 null
else if ((q = p.next) == null) {
// 将头结点 h 更新为 p
updateHead(h, p);
return null;
}
else if (p == q) {
continue restartFromHead;
}
}
}
}

ConcurrentLinkedQueue 源码分析的更多相关文章

  1. 死磕 java集合之ConcurrentLinkedQueue源码分析

    问题 (1)ConcurrentLinkedQueue是阻塞队列吗? (2)ConcurrentLinkedQueue如何保证并发安全? (3)ConcurrentLinkedQueue能用于线程池吗 ...

  2. 多线程高并发编程(11) -- 非阻塞队列ConcurrentLinkedQueue源码分析

    一.背景 要实现对队列的安全访问,有两种方式:阻塞算法和非阻塞算法.阻塞算法的实现是使用一把锁(出队和入队同一把锁ArrayBlockingQueue)和两把锁(出队和入队各一把锁LinkedBloc ...

  3. 【JUC】JDK1.8源码分析之ConcurrentLinkedQueue(五)

    一.前言 接着前面的分析,接下来分析ConcurrentLinkedQueue,ConcurerntLinkedQueue一个基于链接节点的无界线程安全队列.此队列按照 FIFO(先进先出)原则对元素 ...

  4. JUC源码分析-集合篇(三)ConcurrentLinkedQueue

    JUC源码分析-集合篇(三)ConcurrentLinkedQueue 在并发编程中,有时候需要使用线程安全的队列.如果要实现一个线程安全的队列有两种方式:一种是使用阻塞算法,另一种是使用非阻塞算法. ...

  5. MyCat源码分析系列之——BufferPool与缓存机制

    更多MyCat源码分析,请戳MyCat源码分析系列 BufferPool MyCat的缓冲区采用的是java.nio.ByteBuffer,由BufferPool类统一管理,相关的设置在SystemC ...

  6. rxjava源码分析

    RXjava响应式编程 此文作者大暴雨原创,转载请注明出处. 如果线程的知识不是很丰富,请先查看     rxjava源码中的线程知识  一文 rxjava总结就是:异步实现主要是通过扩展观察者模式 ...

  7. Tomcat处理HTTP请求源码分析(下)

    转载:http://www.infoq.com/cn/articles/zh-tomcat-http-request-2 很多开源应用服务器都是集成tomcat作为web container的,而且对 ...

  8. 【Zookeeper】源码分析之服务器(四)

    一.前言 前面分析了LeaderZooKeeperServer,接着分析FollowerZooKeeperServer. 二.FollowerZooKeeperServer源码分析 2.1 类的继承关 ...

  9. Mybatis源码分析-BaseExecutor

    根据前文Mybatis源码分析-SqlSessionTemplate的简单分析,对于SqlSession的CURD操作都需要经过Executor接口的update/query方法,本文将分析下Base ...

随机推荐

  1. BZOJ 1906. 树上的蚂蚁

    传送门 发现蚂蚁不多,所以考虑两两枚举然后判断 那么首先要求出两条链的公共部分,然后根据之间在公共链的时间段和是同向还是反向进行判断 思路简单但是细节很多...... 首先求链的公共部分,设两种蚂蚁为 ...

  2. P1397 [NOI2013]矩阵游戏(递推)

    P1397 [NOI2013]矩阵游戏 一波化式子,$f[1][m]=a^{m-1}+b\sum_{i=0}^{m-2}a^i$,用快速幂+逆元求等比数列可以做到$logm$ 设$v=a^{m-1}, ...

  3. 前端校招知识体系之HTML5

    啥是HTML5?官方说HTML5 是下一代的 HTML... 本文主要介绍HTML5三个方面的知识,继续往下看看吧. 语义化标签 canvas&svg 响应式meta 一.语义化标签 语义化标 ...

  4. netserver启动时报错 "Unable to start netserver with 'IN(6)ADDR_ANY' port '12865' and family AF_UNSPEC'"

    netperf启动netserver时报错 "Unable to start netserver with 'IN(6)ADDR_ANY' port '12865' and family A ...

  5. STM32程序加载与调试

    1.STM32程序的ISP下载,只能使用串口1,其它串口不可以.

  6. 数据结构课后练习题(练习一)1007 Maximum Subsequence Sum (25 分)

    Given a sequence of K integers { N​1​​, N​2​​, ..., N​K​​ }. A continuous subsequence is defined to ...

  7. Zookeeper学习笔记(上)

    Zookeeper学习笔记 本篇主要是一些基本的介绍和API的使用介绍, 有些只是记录了知识点,而没有完全在笔记中详细解释, 需要自行查找资料补充相关概念 主要参考了课程中的内容: Zookeeper ...

  8. unittest----常用属性详解(框架属性详解)

    很久没有写关于测试的随笔了,最近有空学习.整理一下关于unittest框架的知识. unittest单元测试框架,不仅可以适用于单元测试,还可以适用WEB自动化测试用例的开发与执行. unittest ...

  9. CSS——相对定位、绝对定位、固定定位

    相对定位: position:relative 当元素被设置相对定位或是绝对定位后,将自动产生层叠,他们的层叠级别自然的高于文本流,除非设置其z-index值为负值. 并且我们发现当相对定位元素进行位 ...

  10. 【leetcode】1154. Day of the Year

    题目如下: Given a string date representing a Gregorian calendar date formatted as YYYY-MM-DD, return the ...