### Question:
AtomicCycle { int incrementAndGet() { return atomicInteger.accumulateAndGet(maxIndex, (current, max) -> (current + 1) % max); } AtomicCycle(int maxIndex);   }### Answer:
@Test void testSingleThread() { AtomicCycle cycle = new AtomicCycle(2); assertThat(cycle.incrementAndGet(), is(0)); assertThat(cycle.incrementAndGet(), is(1)); assertThat(cycle.incrementAndGet(), is(2)); assertThat(cycle.incrementAndGet(), is(0)); assertThat(cycle.incrementAndGet(), is(1)); assertThat(cycle.incrementAndGet(), is(2)); assertThat(cycle.incrementAndGet(), is(0)); }
@Test void testParallel() throws InterruptedException { int numberCount = 5; int threadCount = 8; int inThreadCycleCount = 10; AtomicCycle cycle = new AtomicCycle(numberCount - 1); Thread[] threads = new Thread[threadCount]; int[] results = new int[numberCount * threadCount * inThreadCycleCount]; for (int i = 0; i < threads.length; i++) { int finalIndex = i; threads[i] = new Thread(() -> { int firstIndex = finalIndex * numberCount * inThreadCycleCount; int cycles = inThreadCycleCount * numberCount; for (int j = 0; j < cycles; j++) { results[firstIndex + j] = cycle.incrementAndGet(); } }); } for (Thread thread : threads) { thread.start(); } for (Thread thread : threads) { thread.join(); } long eachNumberCount = threadCount * inThreadCycleCount; Arrays.sort(results); for (int i = 0; i < numberCount; i++) { long count = count(results, i); assertThat("There should be a number of " + i + "s equal to number of threads", count, is(eachNumberCount)); } }