Java Can';无法在ConcurrentLinkedQueue源代码中获取此条件
在ConcurrentLinkedQueue的源代码中,在Java Can';无法在ConcurrentLinkedQueue源代码中获取此条件,java,concurrency,Java,Concurrency,在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 n
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;
}
}
我知道代码是为了把p放在后面,但是为什么要使用这么复杂的代码呢?那么(p!=t&&t!=(t=tail))是什么意思?t之间有什么不同=(t=tail))
和t=t
?它应该永远是假的吗
是否有任何资料可以清楚地解释ConcurrentLinkedQueue?因此我认为在执行以下操作之间可能会有另一个线程进行更新:
t != (t = tail))
正在检查,并且正在测试。它必须以某种方式依赖原子才能发挥作用
编辑:
回答亚辛·巴达奇的问题,他投了反对票,这似乎是正确的
是赋值运算符
此外,
if
语句用于根据条件对代码进行分支,并且
(x = y)
返回对x的引用,如中所示(任何使用过c的人都可以通过检查发现)
我认为OP是关于Java(或任何人)并发功能的内部实现的
编辑:
我认为这是Java和/或folly实现中的一个bug,这是一个有趣的问题,我得到了一些答案。从我能读到的关于这个话题的内容来看:
t!=(t=tail)
只是一种奇怪的书写方式:
if (t != tail)
t = tail;
简而言之,您正在将t
的值与右侧t
的赋值进行比较,此处为tail
(所有学分都用于他对主题和答案的理解)
所以要完全回答你的问题:
- 我不知道他们为什么用这么复杂的代码
(p!=t&&t!=(t=tail))
表示如果p!=t如果t!=tail t取tail值
什么是
t
和tail
?tail
和t
是源代码中的节点类型。这并不能回答问题。OP问这是什么意思,这和t!=你的答案可能更适用于C++,我认为java比较引用,可能没有什么其他的。问题是并发性。您断言比较的值是完全错误的。您需要实现并使用和isEqual()method@YassineBadache另外,您的代码是:if(t!=tail)t=tail;是向后的,完全错误我认为第一步分配t=tail然后做t!=T我说得对吗?结果总是falseTry to runint t=1;int-tail=2;如果(t!=(t=tail))System.out.println(“不相等”)代码>正如一些人已经推荐的那样。你会发现顺序与这个答案相反。
(x = y)
if ((p = fopen(f)) == NULL)
if (t != tail)
t = tail;