java的AQS中enp没有同步代码块为啥是原子操作
先看一看enq()的代码 private Node enq( final Node node) { for (;;) { 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; } } } } 看到这段代码稍微有点疑惑: node.prev = t; if (compareAndSetTail(t, node)) { t.next = node; return t; } 为啥是原子操作? compareAndSetTail(pred, node)是CAS原子操作我理解,那为啥if后面的语句也是原子的呢?pred.next会不会指向错误的节点? 然后画了一下图就理解了,自己还是太菜了,这种简单的逻辑还思考了一段时间 来源: oschina 链接: https://my.oschina.net/u/4392850/blog/4283908