import java.util.concurrent.*; public class TestThreadUnsafe { public static void main(String[] args) throws Exception { int threads = 1000; ExecutorService executor = Executors.newCachedThreadPool(); CountDownLatch cd = new CountDownLatch(threads); Counter counter = new Counter(); for (int i = 0; i < threads; i++) { executor.execute(() -> { counter.add(); cd.countDown(); }); } cd.await(); executor.shutdown(); System.out.println(counter.get()); } } class Counter { private int count = 0; void add() { count++; } int get() { return count; } }