package statick; import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; public class Stack { private static class Node { private final T data; private volatile Node next; public Node(T data, Node next) { super(); this.data = data; this.next = next; } public Node getNext() { return next; } public void setNext(Node next) { this.next = next; } public T getData() { return data; } } private volatile Node top; private static final AtomicReferenceFieldUpdater updater = AtomicReferenceFieldUpdater.newUpdater(Stack.class, Node.class, "top"); public Stack() { this.top = null; } public T pop() throws Exception { Node node; do { node = this.top; if (node == null) { throw new Exception(); } } while (!updater.compareAndSet(this, node, node.getNext())); return node.getData(); } public void push(T data) { Node node = new Node(data, null); do { node.setNext(this.top); } while (!updater.compareAndSet(this, node.getNext(), node)); } }