关于非阻塞算法中的链表问题
看到一篇文章,关于非阻塞算法的链表:
其代码如下:
(详细见:http://www-128.ibm.com/developerworks/cn/java/j-jtp04186/)
public class LinkedQueue <E> {
private static class Node <E> {
final E item;
final AtomicReference <Node <E> > next;
Node(E item, Node <E> next) {
this.item = item;
this.next = new AtomicReference <Node <E> > (next);
}
}
private AtomicReference <Node <E> > head
= new AtomicReference <Node <E> > (new Node <E> (null, null));
private AtomicReference <Node <E> > tail = head;
public boolean put(E item) {
Node <E> newNode = new Node <E> (item, null);
while (true) {
Node <E> curTail = tail.get();
Node <E> residue = curTail.next.get();
if (curTail == tail.get()) {
if (residue == null) /* A */ {
if (curTail.next.compareAndSet(null, newNode)) /* C */ {
tail.compareAndSet(curTail, newNode) /* D */ ;
return true;
}
} else {
tail.compareAndSet(curTail, residue) /* B */;
}
}