栈顶为一序列
[java]
public class ConcurrentStack
AtomicReference
public void push(E item){
Node
Node
do{
oldHead = top.get();
newHead.next = oldHead;
}while(!top.compareAndSet(oldHead, newHead));
}
public E pop(){
Node
Node
do{
oldHead = top.get();
if(oldHead == null){
return null;
}
newHead = oldHead.next;
}while(!top.compareAndSet(oldHead, newHead));
return oldHead.item;
}
private static class Node
public final E item;
public Node
public Node(E item){
this.item = item;
}
}
}
public class ConcurrentStack
AtomicReference
public void push(E item){
Node
Node
do{
oldHead = top.get();
newHead.next = oldHead;
}while(!top.compareAndSet(oldHead, newHead));
}
public E pop(){
Node
Node
do{
oldHead = top.get();
if(oldHead == null){
return null;
}
newHead = oldHead.next;
}while(!top.compareAndSet(oldHead, newHead));
return oldHead.item;
}
private static class Node
public final E item;
public Node
public Node(E item){
this.item = item;
}
}
}
Michael Scott 1996
队尾和尾部节点两个序列
[java]
public class ConcurrentLink
private final Node
private final AtomicReference
new AtomicReference
private final AtomicReference
new AtomicReference
public boolean push(E item){
Node
while(true){
Node
Node
if(curTail == tail.get()){
if(tailNext != null){
tail.compareAndSet(curTail, tailNext);
}else{
if(curTail.next.compareAndSet(null, newNode)){
tail.compareAndSet(curTail, newNode);
return true;
}
}
}
}
}
private static class Node
final E item;
final AtomicReference
public Node(E item, Node
this.item = item;
this.next = new AtomicReference
}
}
}