在对ConcurrentLinkedQueue的解释中,我们说它是非阻塞的,这只是一个概念上的说明,并不能让大家真正理解其非阻塞的原因。这里需要我们深入到源码中去讨论,找到ConcurrentLinkedQueue使用时的原理。同时我们还可以在其构造函数中,为大家验证其链表结构的代码分享。
1.源码详解
private static class Node<E> { volatile E item; volatile Node<E> next; 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); }
2.构造函数
public ConcurrentLinkedQueue() { head = tail = new Node<E>(null); }
当创建对象时,头尾节点都是指向一个空节点。
以上就是关于ConcurrentLinkedQueue在java的原理探究,本篇我们从ConcurrentLinkedQueue的源码和构造函数进行分析,相信现在大家已经对其概念和用法有了很好的理解了。