ConcurrentLinkedQueue学习
来源:互联网 发布:淘宝号申请日期怎么看 编辑:程序博客网 时间:2024/06/05 19:33
队列的概念
- 队列是一种先进先出的基于结点的数据结构,进入队列时间长的元素称之为队头,即head。进入队列时间最晚的元素称之为队尾,即tail。每次插入新的元素都是从队列的尾部插入。每次从队列中获取元素都是从头元素获取。
- 在JAVA中普通的队列Queue是线程非安全的数据结构,每次只有一个线程进行操作时才是安全的。然而,ConcurrentLinkedQueue属于线程安全的队列,支持多个线程并发操作。ConcurrentLinkedQueue如同其他并发框架一样,其中元素不能为空。
- ConcurrentLinkedQueue是一种非阻塞队列,采用CAS算法实现。
Queue
- Queue是队列的父接口,继承Collection接口。定义了Queue的各种行为:
public interface Collection<E> extends Iterable<E> { /** **往队列中添加一个元素,如果队列没有容量限制时,一直返回true **/ boolean add(E e); /** **往队列中添加一个元素,如果队列没有容量限制时,一直返回true. **如果队列容量有限制,建议使用add方法;当对列满时,抛出异常 **/ boolean offer(E e); /** **获取队列中的head元素并从队列中删除head元素;如果队列为空,则抛出异常 **/ E remove(); /** **获取队列中的head元素并从队列中删除head元素;如果队列为空,则返回NULL **/ E poll(); /** **返回队列中的head元素,如果队列为空则抛出异常 **/ E element(); /** **返回队列中的head元素,如果队列为空则返回Null **/ E peek();}
内部数据结构
ConcurrentLinkedQueue内部定义了一个内部类Node,使用Node表示底层的链表结构。其中Node表示链表节点。程序中使用的共享变量都存放在节点的元素中。分析源码更有说服力:
private static class Node<E> { // 对象存储在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); } boolean casItem(E cmp, E val) { return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val); } void lazySetNext(Node<E> val) { UNSAFE.putOrderedObject(this, nextOffset, val); } 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); } } }
ConcurrentLinkedQueue
并发队列中定义了头节点head,尾节点tail。在实例化ConcurrentLinkedQueue时,头节点head和尾节点tail中的元素都是空的。并且指向同一个节点。
public ConcurrentLinkedQueue() { head = tail = new Node<E>(null); }
offer添加元素过程
- ConcurrentLinkedQueue队列添加元素的过程,可以根据下图的过程来解释。
- 初始化ConcurrentLinkedQueue:head节点和tail节点都为空节点,指向同一个空节点。即节点中没有存储任何对象。
- 添加元素1(node1):往链表中添加元素,首先要定位尾节点,第一次添加元素时tail节点即是尾节点。直接将Node1添加到尾节点即可。此时tail节点仍然指向最开始的空节点,没有指向Node1节点。此时,尾节点是node1节点。
- 添加元素2(node2):每次往队列中添加元素都要首先定位尾节点。添加元素1时,尾节点是node1,而不是tail节点。先将node2添加到node1节点的后面,由于tail节点距离尾节点距离大于1个节点的步长,需要将tail节点设置成尾节点。(分析源码可以得到,后面分析)。此时tail节点的引用指向node2节点对象,tail节点不是空节点。
- 添加元素3(node3):此时tail节点是尾节点,直接将node3节点加入到node2节点后面,现在变成node3节点是尾节点,tail节点还是指向node2节点。
- 添加元素4(node4):添加元素4的过程跟添加元素2过程一样。都是先定位到尾节点node3,将Node4节点添加到尾部,此时tail节点与尾节点步长大于1个节点,需要将tail节点设置为尾节点。
- offer源码分析
public boolean offer(E e) { checkNotNull(e); // 添加一个新元素,实例化一个新节点newNode final Node<E> newNode = new Node<E>(e); // 每次添加都是先定位tail节点是否是尾节点 // 新加入元素都要加入到原尾节点的后面 for (Node<E> t = tail, p = t;;) { Node<E> q = p.next; // 如果tail是尾节点,则q必定为空 if (q == null) { // 将newNode设置为当前链表的尾节点 if (p.casNext(null, newNode)) { // 根据上面分析,只要tail节点与尾节点步长大于1,则 // 需要将tail节点设置当前队列尾节点。 // 失败了,说明其他线程操作成功。 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. p = (t != (t = tail)) ? t : head; else // 定位尾节点:当tail节点不是尾节点时,程序会执行到这儿。将p重新 // 指向尾节点q。即p=p.next。因为tail与尾节点步长最大为1. p = (p != t && t != (t = tail)) ? t : q; } }
- 从整个添加元素过程分析可以看出,头节点head中一直是空节点,并没有存储任何元素。
poll获取元素过程
- 源码分析
public E poll() { restartFromHead: for (;;) { for (Node<E> h = head, p = h, q;;) { // 第一次获取元素时,头节点元素为空。后面获取时也会发生为空的情况 E item = p.item; // 如果不为空,则castItem将当前已经获取到元素的节点内容设置为空 if (item != null && p.casItem(item, null)) { // 头节点为空,从下一个节点成功获取到元素后,需要 // 更新头节点指向新的链表中下一个首节点。 // 头节点不为空的情况下,不需要更新头节点的指向, if (p != h) // hop two nodes at a time updateHead(h, ((q = p.next) != null) ? q : p); return item; } // 头节点元素为空,判断头节点的next节点是否为空。 else if ((q = p.next) == null) { updateHead(h, p); return null; } else if (p == q) continue restartFromHead; // 头节点为空,重新定位获取元素的指针位置 else p = q; } } }
阅读全文
0 0
- ConcurrentLinkedQueue学习
- ConcurrentLinkedQueue学习记
- ConcurrentLinkedQueue学习(一)
- ConcurrentLinkedQueue学习(二)
- ConcurrentLinkedQueue学习(三)
- ConcurrentLinkedQueue
- ConcurrentLinkedQueue
- ConcurrentLinkedQueue
- ConcurrentLinkedQueue
- ConcurrentLinkedQueue
- ConcurrentLinkedQueue
- ConcurrentLinkedQueue
- ConcurrentLinkedQueue
- ConcurrentLinkedQueue
- ConcurrentLinkedQueue
- conCurrentLinkedQueue
- 学习笔记之ConcurrentLinkedQueue源码分析整理
- [JAVA学习笔记-92]concurrentLinkedQueue的特性
- TCP协议如何保证传输的可靠性
- Java产生死锁的一个简单例子
- spring 事务的总结
- STL学习&&数据结构
- 常见字符串函数的学习总结
- ConcurrentLinkedQueue学习
- 学大数据为什么要学Java
- navicat premium 64位 ,管理MYSQL,SQLite3的好工具
- Temporary failure in name resolution
- eclipse日常使用
- 用Dockerfile构建java web环境(2)
- sqlservler 插入与批量插入
- 小V画了一幅图
- numpy.std