public class CountTest { int count = 0; private final AtomicLong atomicLong = new AtomicLong(); public int testCount() { return ++count; } public void manyCountThread() throws InterruptedException { ExecutorService service = Executors.newFixedThreadPool(10); CountDownLatch countDownLatch = new CountDownLatch(1000) { @Override public void await() throws InterruptedException { super.await(); System.out.println(count); System.out.println(atomicLong); } }; Object object = new Object(); for (int i = 0; i < 1000; i++) { Future<?> submit = service.submit(() -> { synchronized (object) { count++; } atomicLong.incrementAndGet(); countDownLatch.countDown(); }); } countDownLatch.await(); } public static void main(String[] args) throws InterruptedException { CountTest countTest = new CountTest(); countTest.testCount(); countTest.manyCountThread(); }
关于count++的一点思考
最新推荐文章于 2021-09-13 18:01:57 发布