import java.util.NoSuchElementException; public class Stack { Node top = null; public synchronized void push(T value) { Node node = new Node(value); node.next = top; top = node; } public synchronized T pop() throws NoSuchElementException { if(top == null) { throw new NoSuchElementException(); } else { Node oldTop = top; top = top.next; return oldTop.value; } } }