获得徽章 8
- 看到AQS源码时候,入队采用CAS入队,但CAS操作是在tail节点的设置,那么入队设置指向是如何保证线程安全的呢?
private Node enq(final Node node) {
for (;;) {// set tail and add element to FIFO queue.Hot to ensure concurrent safety
Node t = tail;
if (t == null) { // Must initialize
if (compareAndSetHead(new Node()))//
tail = head;
} else {
node.prev = t;
if (compareAndSetTail(t, node)) {
t.next = node;
return t;
}
}
}
}展开赞过51