首页 > 文章列表 > ConcurrentLinkedQueue在java的原理探究

ConcurrentLinkedQueue在java的原理探究

java ConcurrentLinkedQueue原理
410 2022-08-06

本教程操作环境:windows7系统、java10版,DELL G3电脑。

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);
}

当创建对象时,头尾节点都是指向一个空节点。