import com.chilldev.commons.concurrent.MutableAtomicInteger; public class ThreadSafeExample { private static final MutableAtomicInteger counter = new MutableAtomicInteger(0); public static void main(String[] args) { int numOfThreads = 10; for (int i = 0; i < numOfThreads; i++) { new Thread(() -> { for (int j = 0; j < 1000; j++) { counter.incrementAndGet(); } }).start(); } while (Thread.activeCount() > 1) { Thread.yield(); } System.out.println("Counter: " + counter.get()); } }


上一篇:
下一篇:
切换中文