这段代码是AQS框架中将当前节点入队的操作。
Node pred = tail;
if (pred != null) {
node.prev = pred;
if (compareAndSetTail(pred, node)) {
pred.next = node;
return node;
}
}
上面代码中pred被赋值为尾节点,node为当前节点。我理解的将新节点插入链表尾处的逻辑应当如下:
node.prev = pred; node节点的前驱指向尾节点
pred.next = node; 将尾节点的后继设置为当前节点
tail = node; 将node节点设置为尾节点
对于上面代码我的疑问如下:
如果尾节点不为空,node节点的前驱会指向尾节点,然后调用CAS交换pred和node的值。
此时pred(即tail)的值应该已经是当前节点node的值了,再执行pred.next=node是什么意思呢,这是否存在逻辑问题?
TZ may have a wrong understanding of compareAndSetTail.
Compare the value of pred with the expected node value at the tailOffset position of AbstractQueuedSynchronizer. If they are the same, update the value of the tailOffset position.
compareAndSetTail(pred, node) After the execution of this code is completed, only the value of tailOffset of the object AbstractQueuedSynchronizer is modified, that is, the value of the member variable tail, which has no effect on the value of pred. As for the doubly linked list, there is no logical problem with tail insertion.
This design is a
双向链表
.B.prev == A
A.next == B
compareAndSetTail
设置成功只是将tail
更新为当前node
.pred.next
是将上一个尾部节点的next
设置为当前node
There is no problem with this logic.