package nonstatick; import java.util.concurrent.atomic.AtomicReference; 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 final AtomicReference> top; public Stack() { super(); this.top = new AtomicReference>(); } public T pop() throws Exception { Node node; do { node = this.top.get(); if (node == null) { throw new Exception(); } } while (!this.top.compareAndSet(node, node.getNext())); return node.getData(); } public void push(T data) { Node node = new Node(data, null); do { node.setNext(this.top.get()); } while (!this.top.compareAndSet(node.getNext(), node)); } }