ConcurrentLinkedQueue在java的原理探究

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

1.源码详解

privatestaticclassNode<E>{
volatileEitem;
volatileNode<E>next;

Node(Eitem){
UNSAFE.putObject(this,itemOffset,item);
}

booleancasItem(Ecmp,Eval){
returnUNSAFE.compareAndSwapObject(this,itemOffset,cmp,val);
}

voidlazySetNext(Node<E>val){
UNSAFE.putOrderedObject(this,nextOffset,val);
}

booleancasNext(Node<E>cmp,Node<E>val){
returnUNSAFE.compareAndSwapObject(this,nextOffset,cmp,val);
}

2.构造函数

publicConcurrentLinkedQueue(){
head=tail=newNode<E>(null);
}

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

原文来自:https://www.py.cn

© 版权声明
THE END
喜欢就支持一下吧
点赞6 分享