Atomic classes use compare-and-swap, so counters stay correct under contention without synchronized blocks.
var counter = new AtomicInteger();
var pool = Executors.newFixedThreadPool(8);
for (int i = 0; i < 1000; i++) pool.submit(counter::incrementAndGet);
pool.shutdown();
pool.awaitTermination(10, TimeUnit.SECONDS);
System.out.println("expected 1000, got " + counter.get());
System.out.println("accumulateAndGet(x10) = " + counter.accumulateAndGet(10, (a, b) -> a * b));
expected 1000, got 1000
accumulateAndGet(x10) = 10000
Run this yourself in the Online Java Compiler, spin up a live REST API in the API Sandbox, or practise with Java interview questions.
Published 2026-07-30