@@ -0,0 +1,1585 @@
+package ru.job4j.casQueue;%0A%0Aimport javax.annotation.concurrent.ThreadSafe;%0Aimport java.util.concurrent.atomic.AtomicReference;%0A%0A@ThreadSafe%0Apublic class CASQueue%3CT%3E %7B%0A%0A    private AtomicReference%3CNode%3CT%3E%3E head = new AtomicReference%3C%3E();%0A    private AtomicReference%3CNode%3CT%3E%3E tail = new AtomicReference%3C%3E();%0A%0A    public void push(T value) %7B%0A        Node%3CT%3E temp = new Node%3C%3E(value);%0A        Node%3CT%3E ref;%0A        do %7B%0A            ref = head.get();%0A            temp.next = ref;%0A        %7D while (!head.compareAndSet(ref, temp));%0A    %7D%0A%0A    public T poll() %7B%0A            Node%3CT%3E ref;%0A            Node%3CT%3E temp;%0A            do %7B%0A                ref = head.get();%0A                if (ref == null) %7B%0A                    throw new IllegalStateException(%22Stack is empty%22);%0A                %7D%0A                temp = ref.next;%0A            %7D while (!head.compareAndSet(ref, temp));%0A            ref.next = null;%0A            return ref.value;%0A        %7D%0A%0A%0A    private static class Node%3CT%3E %7B%0A%0A        final T value;%0A        Node%3CT%3E next;%0A%0A        public Node(final T value) %7B%0A            this.value = value;%0A        %7D%0A     %7D%0A%0A     public static void main(String%5B%5D args) %7B%0A        CASQueue%3CInteger%3E casQueue = new CASQueue%3C%3E();%0A%0A        Thread thread1 = new Thread() %7B%0A            @Override%0A            public void run() %7B%0A                casQueue.push(1);%0A            %7D%0A        %7D;%0A%0A         Thread thread2 = new Thread() %7B%0A             @Override%0A             public void run() %7B%0A                 casQueue.push(2);%0A             %7D%0A         %7D;%0A%0A         thread1.start();%0A         thread2.start();%0A%0A     %7D%0A%7D%0A%0A

task #187612