源码基于Java8

无界非阻塞队列,底层使用单向链表实现,对于出队和入队使用CAS来实现线程安全。

ConcurrentLinkedQueue 源码分析 - 图1

看一下ConcurrentLinkedQueue的UML类图结构

AbstractQueue 实现了Queue接口的基本方法

Node 静态内部类,表示队列的节点

内部类

Node

队列节点对象,ConcurrentLinkedQueue的静态内部类

  1. private static class Node<E> {
  2. //节点值
  3. volatile E item;
  4. //指向下一个节点
  5. volatile Node<E> next;
  6. /**
  7. * node构造函数,将item值写入内存,宽松写入,因为只有在casNext成功后才可见
  8. */
  9. Node(E item) {
  10. UNSAFE.putObject(this, itemOffset, item);
  11. }
  12. //cas更新item值
  13. boolean casItem(E cmp, E val) {
  14. return UNSAFE.compareAndSwapObject(this, itemOffset, cmp, val);
  15. }
  16. //防重排序更新next值
  17. void lazySetNext(Node<E> val) {
  18. UNSAFE.putOrderedObject(this, nextOffset, val);
  19. }
  20. //cas更新next值
  21. boolean casNext(Node<E> cmp, Node<E> val) {
  22. return UNSAFE.compareAndSwapObject(this, nextOffset, cmp, val);
  23. }
  24. private static final sun.misc.Unsafe UNSAFE;
  25. //item属性的偏移量
  26. private static final long itemOffset
  27. //next属性的偏移量
  28. private static final long nextOffset;
  29. static {
  30. try {
  31. UNSAFE = sun.misc.Unsafe.getUnsafe();
  32. Class<?> k = Node.class;
  33. itemOffset = UNSAFE.objectFieldOffset
  34. (k.getDeclaredField("item"));
  35. nextOffset = UNSAFE.objectFieldOffset
  36. (k.getDeclaredField("next"));
  37. } catch (Exception e) {
  38. throw new Error(e);
  39. }
  40. }
  41. }

CLQSpliterator

可拆分迭代器

  1. static final class CLQSpliterator<E> implements Spliterator<E> {
  2. static final int MAX_BATCH = 1 << 25; //最大批处理大小
  3. final ConcurrentLinkedQueue<E> queue;
  4. Node<E> current; //当前节点,初始化之前都为null
  5. int batch; //分割批次的大小
  6. boolean exhausted; //如果没有元素了,为true
  7. CLQSpliterator(ConcurrentLinkedQueue<E> queue) {
  8. this.queue = queue;
  9. }
  10. //分割队列,返回的是ArraySpliterator
  11. public Spliterator<E> trySplit() {
  12. Node<E> p;
  13. final ConcurrentLinkedQueue<E> q = this.queue;
  14. int b = batch;
  15. //n表示数组长度
  16. //分割批次长度<=0,数组长度为1;
  17. //分割批次长度>=最大批处理大小,数组长度为最大批处理大小;否则为分割批次长度+1
  18. int n = (b <= 0) ? 1 : (b >= MAX_BATCH) ? MAX_BATCH : b + 1;
  19. //!exhausted 为true表示队列还有元素
  20. //((p = current) != null || (p = q.first()) != null) 如果前部分为true表示已经运行过一次,后部分为true表 //示是第一次运行,将队列的头节点赋值给p
  21. //p.next != null 表示队列后面还有元素,p表示的节点不是最后一个
  22. if (!exhausted &&
  23. ((p = current) != null || (p = q.first()) != null) &&
  24. p.next != null) {
  25. //临时存储需分割的元素
  26. Object[] a = new Object[n];
  27. //已分割的元素计数,只计数item不为null的元素
  28. //对于ConcurrentLinkedQueue来说,这里不会有item值为null的情况
  29. int i = 0;
  30. do {
  31. //节点item值赋值临时数组
  32. if ((a[i] = p.item) != null)
  33. //计数
  34. ++i;
  35. //当next也指向本身p的时候为true,也就是自引用,重新找头节点
  36. if (p == (p = p.next))
  37. p = q.first();
  38. } while (p != null && i < n); //直到取完需要的元素数量
  39. //如果p等于null了就说明没有元素了
  40. if ((current = p) == null)
  41. exhausted = true;
  42. //有不为null的元素
  43. if (i > 0) {
  44. batch = i;
  45. //创建Spliterator,数组从0-i不包括i,指定了迭代器的特性
  46. return Spliterators.spliterator
  47. (a, 0, i, Spliterator.ORDERED | Spliterator.NONNULL |
  48. Spliterator.CONCURRENT);
  49. }
  50. }
  51. return null;
  52. }
  53. //循环每个节点并执行指定函数
  54. public void forEachRemaining(Consumer<? super E> action) {
  55. Node<E> p;
  56. if (action == null) throw new NullPointerException();
  57. final ConcurrentLinkedQueue<E> q = this.queue;
  58. if (!exhausted &&
  59. ((p = current) != null || (p = q.first()) != null)) {
  60. exhausted = true;
  61. do {
  62. E e = p.item;
  63. if (p == (p = p.next))
  64. p = q.first();
  65. if (e != null)
  66. action.accept(e);
  67. } while (p != null); //循环每个节点,直到没有节点
  68. }
  69. }
  70. //获取一个有效的节点,执行指定函数
  71. public boolean tryAdvance(Consumer<? super E> action) {
  72. Node<E> p;
  73. if (action == null) throw new NullPointerException();
  74. final ConcurrentLinkedQueue<E> q = this.queue;
  75. if (!exhausted &&
  76. ((p = current) != null || (p = q.first()) != null)) {
  77. E e;
  78. //通过循环的方式直到节点item值不为null
  79. //item值为null时通过p.next继续寻找
  80. do {
  81. e = p.item;
  82. //同样如果p是自引用就重新寻找头节点
  83. if (p == (p = p.next))
  84. p = q.first();
  85. } while (e == null && p != null);
  86. if ((current = p) == null)
  87. exhausted = true;
  88. if (e != null) {
  89. action.accept(e);
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. //返回估计的元素数量,因为ConcurrentLinkedQueue是无限队列,返回MAX_VALUE
  96. public long estimateSize() { return Long.MAX_VALUE; }
  97. //返回特征值
  98. public int characteristics() {
  99. return Spliterator.ORDERED | Spliterator.NONNULL |
  100. Spliterator.CONCURRENT;
  101. }
  102. }

Itr

迭代器,从头节点到尾节点进行迭代,迭代器是弱一致性的,只是某一时刻的快照,因为同时有可能其他线程还在对队列进行修改

  1. private class Itr implements Iterator<E> {
  2. /**
  3. * 返回下个项目的节点
  4. */
  5. private Node<E> nextNode;
  6. /**
  7. * 保存下一个节点的item字段
  8. */
  9. private E nextItem;
  10. /**
  11. * 最近一个返回的项目节点,支持删除
  12. */
  13. private Node<E> lastRet;
  14. Itr() {
  15. advance();
  16. }
  17. /**
  18. * 移动到下一个有效节点,通过next()返回item或者null
  19. */
  20. private E advance() {
  21. lastRet = nextNode;
  22. E x = nextItem;
  23. Node<E> pred, p;
  24. //nextNode为null是说明是第一次运行
  25. if (nextNode == null) {
  26. //获取头节点
  27. p = first();
  28. pred = null;
  29. } else {
  30. //将上次获取的节点保存为此次的上一个节点
  31. pred = nextNode;
  32. //获取下一个节点
  33. p = succ(nextNode);
  34. }
  35. for (;;) {
  36. //p == null 的情况,可能是哨兵节点时,p=first()返回null
  37. //也可能是 p=succ(nextNode)到最后一个节点时,返回null
  38. if (p == null) {
  39. nextNode = null;
  40. nextItem = null;
  41. return x;
  42. }
  43. E item = p.item;
  44. if (item != null) {
  45. nextNode = p;
  46. nextItem = item;
  47. return x;
  48. } else {
  49. // 如果item=null时,即节点已被其他线程删除,那么继续获取下一个节点
  50. Node<E> next = succ(p);
  51. if (pred != null && next != null)
  52. //跳过null节点,将上一个节点的next指向新的节点
  53. pred.casNext(p, next);
  54. //将p赋值新节点,继续循环
  55. p = next;
  56. }
  57. }
  58. }
  59. //通过下个项目节点不为null来判断是否还有下一个
  60. public boolean hasNext() {
  61. return nextNode != null;
  62. }
  63. //下一个项目
  64. public E next() {
  65. if (nextNode == null) throw new NoSuchElementException();
  66. return advance();
  67. }
  68. public void remove() {
  69. Node<E> l = lastRet;
  70. if (l == null) throw new IllegalStateException();
  71. //将最近一个项目节点设置为null,在下一次执行advance时,该节点将被重新连接
  72. l.item = null;
  73. lastRet = null;
  74. }
  75. }

属性

先看下ConcurrentLinkedQueue有哪些基本属性

  1. /**
  2. * 头节点(未删除的),到达时间复杂度O(1)
  3. * 不变性:
  4. * - 所有有效节点能够从头节点通过succ()方法到达
  5. * - head != null
  6. * - (tmp = head).next != tmp || tmp != head,就是头节点不会自引用
  7. * 可变性:
  8. * - head.item 可能是null也可能不是null
  9. * - 允许tail滞后于head,也就是不能从头节点通过succ()到达
  10. *
  11. */
  12. private transient volatile Node<E> head;
  13. /**
  14. * 尾节点,唯一一个node.next==null的节点,到达时间复杂度O(1)
  15. * 不变性:
  16. * - tail 节点通过succ()方法一定到达队列中的最后一个节点
  17. * - tail != null
  18. * 可变性:
  19. * - tail.item 可能是null也可能不是null.
  20. * - 允许tail滞后于head,也就是不能从头节点通过succ()到达
  21. * - tail.next 可以指向自己,也可以不指向自己
  22. *
  23. */
  24. private transient volatile Node<E> tail;

Unsafe相关

  1. private static final sun.misc.Unsafe UNSAFE;
  2. //头节点相对于对象的偏移量
  3. private static final long headOffset;
  4. //尾节点相对于对象的偏移量
  5. private static final long tailOffset;
  6. static {
  7. try {
  8. UNSAFE = sun.misc.Unsafe.getUnsafe();
  9. Class<?> k = ConcurrentLinkedQueue.class;
  10. //使用Unsafe获取head属性相对于ConcurrentLinkedQueue对象的偏移量,可以使用该偏移量获取head属性的值
  11. headOffset = UNSAFE.objectFieldOffset
  12. (k.getDeclaredField("head"));
  13. tailOffset = UNSAFE.objectFieldOffset
  14. (k.getDeclaredField("tail"));
  15. } catch (Exception e) {
  16. throw new Error(e);
  17. }
  18. }

构造函数

  1. /**
  2. * 创建一个item,next都为null的Node节点
  3. * head tail都指向该节点
  4. */
  5. public ConcurrentLinkedQueue() {
  6. head = tail = new Node<E>(null);
  7. }
  8. /**
  9. * 根据给定的集合顺序创建队列对象
  10. *
  11. * @param c 给定集合
  12. * @throws 如果给定的集合为null 或者其中任何一个元素为null将抛出NullPointerException
  13. */
  14. public ConcurrentLinkedQueue(Collection<? extends E> c) {
  15. //定义头尾节点局部变量,h头节点变量,t尾节点变量
  16. Node<E> h = null, t = null;
  17. for (E e : c) {
  18. //检查集合元素是否是null,否则将抛出NPE
  19. checkNotNull(e);
  20. //构造节点对象
  21. Node<E> newNode = new Node<E>(e);
  22. //如果t指向null,则h,t都指向第一个创建的node节点
  23. if (h == null)
  24. h = t = newNode;
  25. //否则说明已经存在头节点
  26. else {
  27. //①设置原node节点的next变量
  28. //lazySetNext使用了UNSAFE.putOrderedObject能够防止重排序
  29. t.lazySetNext(newNode);
  30. //②将t指向最后一个node 节点
  31. t = newNode;
  32. }
  33. }
  34. //当传入的集合为空集合时,局部变量h依然是null,将h,t指向item,next都为null的节点
  35. if (h == null)
  36. h = t = new Node<E>(null);
  37. //更新全局头节点变量,尾节点变量
  38. head = h;
  39. tail = t;
  40. }

无参构造函数创建对象后的节点图

ConcurrentLinkedQueue 源码分析 - 图2

根据给定的集合顺序创建队列对象,给定集合不是空集合

ConcurrentLinkedQueue 源码分析 - 图3

add

在尾部插入元素,实际调用offer()

offer

在尾部插入元素

  1. public boolean offer(E e) {
  2. //检查插入值不能为null
  3. checkNotNull(e);
  4. //构造node对象
  5. final Node<E> newNode = new Node<E>(e);
  6. for (Node<E> t = tail, p = t;;) {
  7. Node<E> q = p.next;
  8. //q->p.next,也就是说p已经是最后一个节点,p.next指向null
  9. if (q == null) {
  10. //cas设置,如果cas失败,说明有其他线程竞争,等待下次循环再次尝试cas设置
  11. if (p.casNext(null, newNode)) {
  12. //设置成功
  13. //尾节点与当前节点相隔两个,设置尾节点tail指向最后一个节点,即newNode
  14. if (p != t)
  15. //允许失败,下次会继续处理
  16. casTail(t, newNode);
  17. return true;
  18. }
  19. }
  20. //❓这里我在debug时有个奇怪问题
  21. //测试用例只有简单的add()方法,有时候能够进入该代码内,有时候执行的else部分代码
  22. //这个问题,见https://my.oschina.net/u/4311881/blog/3264783/print
  23. else if (p == q)
  24. //这种情况会发生在节点已经被删除的时候,即p.next -> p 本身
  25. //如果tail没有发生变化,需要跳转到头节点head,从head节点能够到达任何存活节点
  26. //否则说明tail已经重新指向,用新的tail
  27. p = (t != (t = tail)) ? t : head;
  28. else
  29. //(p != t) 说明执行过 p = q 操作(向后遍历操作)
  30. //(t != (t = tail))) 说明尾节点在其他的线程发生变化
  31. //添加2次检查一次
  32. p = (p != t && t != (t = tail)) ? t : q;
  33. }
  34. }

remove

add、remove发生的bug

  1. public static void main(String[] args) {
  2. ConcurrentLinkedQueue<Object> queue = new ConcurrentLinkedQueue<Object>();
  3. queue.add(new Object());
  4. Object object = new Object();
  5. int loops = 0;
  6. Runtime rt = Runtime.getRuntime();
  7. long last = System.currentTimeMillis();
  8. while (true) {
  9. if (loops % 10000 == 0) {
  10. long now = System.currentTimeMillis();
  11. long duration = now - last;
  12. last = now;
  13. System.out.printf("duration=%d q.size=%d memory max=%d free=%d total=%d%n",
  14. duration,queue.size(),rt.maxMemory(),rt.freeMemory(),rt.totalMemory());
  15. }
  16. queue.add(object);
  17. queue.remove(object);
  18. ++loops;
  19. }
  20. }

多次add,remove操作后,会有很多已经删除的null节点,本应该GC回收,但因为互相连接无法回收,导致长时间运行后内存泄漏。

ConcurrentLinkedQueue 源码分析 - 图4

这种情况是jdk的一个bug,可见文章:https://mp.weixin.qq.com/s/ppZlpL5Ip7DHG1GG09uT6g

看看remove代码

  1. public boolean remove(Object o) {
  2. //删除元素为null直接返回false
  3. if (o != null) {
  4. //pred上一个节点,next下一个节点
  5. Node<E> next, pred = null;
  6. for (Node<E> p = first(); p != null; pred = p, p = next) {
  7. boolean removed = false;
  8. E item = p.item;
  9. //元素item值不为null,item=null都是被删除的节点
  10. if (item != null) {
  11. //当前节点的item与希望删除值不相同,继续下一个节点
  12. //通过equals判断值是否相同
  13. if (!o.equals(item)) {
  14. //获取下一个节点
  15. next = succ(p);
  16. //下一次循环
  17. continue;
  18. }
  19. //相同将item置为null,cas设置成功返回true
  20. removed = p.casItem(item, null);
  21. }
  22. //如果item=null,获取下一个节点,用于下次循环
  23. //如果节点casItem成功,判断下了节点是否有节点
  24. next = succ(p);
  25. //当上一个节点,和下一个节点都存在时
  26. //将上一个节点的next跳过当前节点,关联到下一个节点
  27. //这就是产生上述bug的地方
  28. if (pred != null && next != null) // unlink
  29. pred.casNext(p, next);
  30. if (removed)
  31. return true;
  32. }
  33. }
  34. return false;
  35. }

poll

弹出头节点,会删除头节点,返回节点item值

  1. public E poll() {
  2. restartFromHead:
  3. //这层循环能够起到在continue restartFromHead后,重新初始化第二个for
  4. for (;;) {
  5. for (Node<E> h = head, p = h, q;;) {
  6. E item = p.item;
  7. //头节点item不为null,那么设置头节点的item为null
  8. if (item != null && p.casItem(item, null)) {
  9. //cas设置成功后,由于h指向初始的head,p为当前节点,需要更新头节点head
  10. if (p != h)
  11. //(q = p.next) != null 为false时,即p为尾节点,那么head还是指向当前节点p
  12. //否则将head节点更新为p.next
  13. updateHead(h, ((q = p.next) != null) ? q : p);
  14. //返回
  15. return item;
  16. }
  17. //(q = p.next) == null 为true,p.item=null
  18. //空队列,有可能当前节点要被删除(多线程下),或者是一个哨兵节点
  19. else if ((q = p.next) == null) {
  20. //更新头节点
  21. updateHead(h, p);
  22. return null;
  23. }
  24. //自引用节点,说明当前节点已被删除,跳到开头重新开始
  25. else if (p == q)
  26. continue restartFromHead;
  27. else
  28. // q -> p进行下个节点判断
  29. p = q;
  30. }
  31. }
  32. }

peek

获取头节点元素,不删除元素,与poll()类似

  1. public E peek() {
  2. restartFromHead:
  3. for (;;) {
  4. for (Node<E> h = head, p = h, q;;) {
  5. E item = p.item;
  6. //(q = p.next) == null 为true 说明是空队列
  7. if (item != null || (q = p.next) == null) {
  8. updateHead(h, p);
  9. return item;
  10. }
  11. else if (p == q)
  12. continue restartFromHead;
  13. else
  14. p = q;
  15. }
  16. }
  17. }

isEmpty

判断队列是否为空,通过头节点是否为null判断

  1. public boolean isEmpty() {
  2. return first() == null;
  3. }

size

返回队列中有效元素(item != null)长度

public int size() {
    int count = 0;
    //从头节点开始循环
    for (Node<E> p = first(); p != null; p = succ(p))
        if (p.item != null)
            // 如果超过最大值,提前结束
            if (++count == Integer.MAX_VALUE)
                break;
    return count;
}

contains

判断队列中是否包含指定值的节点

public boolean contains(Object o) {
    //由于队列中不包含null的节点(哨兵节点不算),直接返回false
    if (o == null) return false;
    for (Node<E> p = first(); p != null; p = succ(p)) {
        E item = p.item;
        //通过equals判断
        if (item != null && o.equals(item))
            return true;
    }
    return false;
}

addAll

将给定集合添加到队列中,返回是否成功

public boolean addAll(Collection<? extends E> c) {
    if (c == this)
        //不能自己添加自己
        throw new IllegalArgumentException();

    //循环设置队列
    Node<E> beginningOfTheEnd = null, last = null;
    for (E e : c) {
        checkNotNull(e);
        Node<E> newNode = new Node<E>(e);
        //beginningOfTheEnd = null 表示第一次循环,将beginningOfTheEnd,last都指向第一个节点
        if (beginningOfTheEnd == null)
            beginningOfTheEnd = last = newNode;
        else {
            //beginningOfTheEnd != null 设置后续节点
            last.lazySetNext(newNode);
            last = newNode;
        }
    }
    //为ture说明c是一个空集合
    if (beginningOfTheEnd == null)
        return false;

    //更新head,和tail变量
    for (Node<E> t = tail, p = t;;) {
        //一直获取下一个节点
        Node<E> q = p.next;
        //直到最后一个
        if (q == null) {
            //beginningOfTheEnd已经在前面设置完成,形成了链表 这里通过cas将新链表链接到原来链表的最后一个节点
            if (p.casNext(null, beginningOfTheEnd)) {
                // 更新tail指向链表最后一个节点
                if (!casTail(t, last)) {
                    // 如果失败了,说明有竞争,再次尝试
                    t = tail;
                    if (last.next == null)
                        casTail(t, last);
                }
                return true;
            }
        }
        else if (p == q)
            //在多线程的情况下会发生节点被删除的情况,需要重新赋值头部,并重新寻找最后的节点
            p = (t != (t = tail)) ? t : head;
        else
            //检查tail节点
            p = (p != t && t != (t = tail)) ? t : q;
    }
}

toArray

//将队列元素循环赋值给ArrayList,再输出数组
//由于返回新数组,所以对数组的修改不会影响原队列,但如果元素是对象,对对象的修改,会影响到原队列中的元素
public Object[] toArray() {
    ArrayList<E> al = new ArrayList<E>();
    for (Node<E> p = first(); p != null; p = succ(p)) {
        E item = p.item;
        if (item != null)
            al.add(item);
    }
    return al.toArray();
}

例子:

@Test
public void test1 {
    concurrentLinkedQueue2.add(new Person());
    concurrentLinkedQueue2.add(new Person());
    concurrentLinkedQueue2.add(new Person());

    //对数组中的对象进行修改
    Object[] objects = concurrentLinkedQueue2.toArray();
    Person person = (Person) objects[0];
    Person person1 = concurrentLinkedQueue2.peek();
    assert  person == person1;
}
//将队列元素输出到指定的数组中
public <T> T[] toArray(T[] a) {
    //将元素输出到指定数组中
    int k = 0;
    Node<E> p;
    for (p = first(); p != null && k < a.length; p = succ(p)) {
        E item = p.item;
        if (item != null)
            a[k++] = (T)item;
    }
    //p == null 为true 表示队列后面没有节点了
    //为false 表示后面还有节点,将采用ArrayList方法输出数组
    //所以始终会将队列中的所有有效元素输出
    if (p == null) {
        //k < a.length为true 表明给定数组长度比较大
        if (k < a.length)
            //为什么要设值一下呢?
            a[k] = null;
        return a;
    }

    //使用ArrayList输出
    ArrayList<E> al = new ArrayList<E>();
    for (Node<E> q = first(); q != null; q = succ(q)) {
        E item = q.item;
        if (item != null)
            al.add(item);
    }
    return al.toArray(a);
}

iterator、spliterator

//创建迭代器
public Iterator<E> iterator() {
        return new Itr();
}

//创建可拆分迭代器
public Spliterator<E> spliterator() {
        return new CLQSpliterator<E>(this);
}

updateHead

更新头节点head,主要作用就是将h节点变为自引用,并更新head头节点,变为自引用的节点将被GC回收

//更新头节点head
final void updateHead(Node<E> h, Node<E> p) {
    if (h != p && casHead(h, p))
        //将h节点变为自引用
        h.lazySetNext(h);
}

succ

//获取下一个节点
final Node<E> succ(Node<E> p) {
    Node<E> next = p.next;
    //p == next 表示p是自引用节点,需要从head头开始,返回头节点
    return (p == next) ? head : next;
}

first

//获取队列的第一个元素
Node<E> first() {
    restartFromHead:
    for (;;) {
        for (Node<E> h = head, p = h, q;;) {
            boolean hasItem = (p.item != null);
            //hasItem 为true,就返回
            //为false 执行 q=p.next 获取下个节点,当下个节点也不存在时,说明该节点p已经被删除
              //此时就相当于初始化时候的哨兵节点item = null, next = null
            if (hasItem || (q = p.next) == null) {
                //更新头
                updateHead(h, p);
                return hasItem ? p : null;
            }
            else if (p == q)
                continue restartFromHead;
            else
                p = q;
        }
    }
}

writeObject、readObject

//将队列写入输出流中
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);
}

//从输入流中读取并转换为队列
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;
    }

参考文章:

https://www.jianshu.com/p/08e8b0c424c0


瓦雀