@@ -0,0 +1,3446 @@
+package ua.yandex.lockfree;%0A%0Aimport ua.yandex.utils.Logger;%0A%0Aimport java.math.BigInteger;%0Aimport java.util.Collections;%0Aimport java.util.Iterator;%0Aimport java.util.LinkedList;%0Aimport java.util.List;%0Aimport java.util.ListIterator;%0Aimport java.util.Random;%0Aimport java.util.concurrent.LinkedBlockingQueue;%0Aimport java.util.concurrent.atomic.AtomicReference;%0A%0A/**%0A * Created by lionell on 4/5/16.%0A *%0A * @author Ruslan Sakevych%0A */%0Apublic class SquareIterator implements Iterator%3CBigInteger%3E %7B%0A    private static final Random random = new Random(17);%0A    private static final BigInteger BIG_TWO = BigInteger.valueOf(2);%0A    private static final LinkedBlockingQueue%3CBigInteger%3E generatedValues =%0A            new LinkedBlockingQueue%3C%3E();%0A    private final AtomicReference%3CBigInteger%3E value =%0A            new AtomicReference%3C%3E(BigInteger.ONE);%0A%0A    public static void main(String%5B%5D args) %7B%0A        SquareIterator iterator = new SquareIterator();%0A%0A        Thread%5B%5D threadPool = new Thread%5B10%5D;%0A        for (int i = 0; i %3C threadPool.length; i++) %7B%0A            threadPool%5Bi%5D = new DummyCounter(iterator);%0A        %7D%0A%0A        for (Thread thread : threadPool) %7B%0A            thread.start();%0A        %7D%0A%0A        try %7B%0A            Thread.sleep(1000);%0A        %7D catch (InterruptedException ignored) %7B%0A        %7D%0A%0A        for (Thread thread : threadPool) %7B%0A            thread.interrupt();%0A        %7D%0A%0A        for (Thread thread : threadPool) %7B%0A            try %7B%0A                thread.join();%0A            %7D catch (InterruptedException ignored) %7B%0A            %7D%0A        %7D%0A%0A        assert checkConsistency() : %22Some values are incorrect!%22;%0A    %7D%0A%0A    private static boolean checkConsistency() %7B%0A        List%3CBigInteger%3E values = new LinkedList%3C%3E();%0A        generatedValues.drainTo(values);%0A        Collections.sort(values);%0A%0A        ListIterator%3CBigInteger%3E listIterator = values.listIterator();%0A        listIterator.next();%0A        listIterator.previous();%0A%0A        BigInteger old = listIterator.next();%0A        if (!old.equals(BigInteger.ONE)) %7B%0A            return false;%0A        %7D%0A%0A        while (listIterator.hasNext()) %7B%0A            old = old.multiply(BIG_TWO);%0A            if (!old.equals(listIterator.next())) %7B%0A                Logger.log(old.toString());%0A                return false;%0A            %7D%0A        %7D%0A%0A        return true;%0A    %7D%0A%0A    private static class DummyCounter extends Thread %7B%0A        private static int nextId = 0;%0A        private final SquareIterator iterator;%0A%0A        private DummyCounter(SquareIterator iterator) %7B%0A            super(%22dummyCounter-%22 + nextId);%0A            this.iterator = iterator;%0A            nextId++;%0A        %7D%0A%0A        @Override%0A        public void run() %7B%0A            while (!isInterrupted()) %7B%0A                BigInteger value = iterator.next();%0A                generatedValues.add(value);%0A                Logger.log(value.toString());%0A%0A                try %7B%0A                    Thread.sleep(random.nextInt(100));%0A                %7D catch (InterruptedException ignored) %7B%0A                    break;%0A                %7D%0A            %7D%0A        %7D%0A    %7D%0A%0A    @Override%0A    public boolean hasNext() %7B%0A        return true;%0A    %7D%0A%0A    @Override%0A    public BigInteger next() %7B%0A        BigInteger oldValue;%0A        BigInteger newValue;%0A%0A        do %7B%0A            oldValue = value.get();%0A            newValue = oldValue.multiply(BIG_TWO);%0A        %7D while (!value.compareAndSet(oldValue, newValue));%0A%0A        return oldValue;%0A    %7D%0A%7D%0A

Implement SquareIterator.