在ConcurrentLinkedQueue的源代码中,在offer方法中:
public boolean offer(E e) {
checkNotNull(e);
final Node<E> newNode = new Node<E>(e);
for (Node<E> t = tail, p = t;;) {
Node<E> q = p.next;
if (q == null) {
// p is last node
if (p.casNext(null, newNode)) {
// Successful CAS is the linearization point
// for e to become an element of this queue,
// and for newNode to become "live".
if (p != t) // hop two nodes at a time
casTail(t, newNode); // Failure is OK.
return true;
}
// Lost CAS race to another thread; re-read next
}
else if (p == q)
// We have fallen off list. If tail is unchanged, it
// will also be off-list, in which case we need to
// jump to head, from which all live nodes are always
// reachable. Else the new tail is a better bet.
p = (t != (t = tail)) ? t : head;
else
// Check for tail updates after two hops.
p = (p != t && t != (t = tail)) ? t : q;
}
}
Run Code Online (Sandbox Code Playgroud)
在352行,有这样的条件:
p = (p != t && t != (t = tail)) ? t : q;
Run Code Online (Sandbox Code Playgroud)
我知道代码是将p放到尾部,但为什么要使用如此复杂的代码呢?什么(p != t && t != (t = tail))意思?t!=(t=tail))和之间有什么不同t!=t?应该总是假的?
是否有任何材料可以清楚地解释ConcurrentLinkedQueue?
Abd*_*had -2
所以我认为另一个线程可能会在操作之间进行更新
t != (t = tail))
Run Code Online (Sandbox Code Playgroud)
正在检查,并且正在对此进行测试。它必须以某种方式依赖原子才有用
编辑:
回复 Yassine Badache 的,投反对票,这似乎是正确的
t = tail
Run Code Online (Sandbox Code Playgroud)
是一个赋值运算符
另外,
if
Run Code Online (Sandbox Code Playgroud)
语句用于根据条件分支代码,并且
(x = y)
Run Code Online (Sandbox Code Playgroud)
返回对 x 的引用,如(对于曾经使用过 c 的任何人来说,通过检查显而易见)
if ((p = fopen(f)) == NULL)
Run Code Online (Sandbox Code Playgroud)
我认为OP是关于Java(或任何人)并发功能的内部实现
编辑:
我认为这是 Java 实现中的一个错误和/或愚蠢
| 归档时间: |
|
| 查看次数: |
116 次 |
| 最近记录: |