多线程实现一个累加(可以是具体业务)操作
普通做法 在这个直接累加100 public static void main(String[] args) throws Exception { long start = System.currentTimeMillis(); Integer count = 100; Integer sum = 0; for (int a = 0; a < count; a++) { sum += a; Thread.sleep(100); } System.out.println(String.format("结果:%d\n耗时:%d", sum, System.currentTimeMillis() - start)); } 执行结果: 结果:4950 耗时:10054 可以看到耗时10秒; 优化方案 通过线程异步执行实现累加 public static void main(String[] args) throws Exception { long start = System.currentTimeMillis(); Integer count = 100; CountDownLatch countDownLatch = new CountDownLatch(count); AtomicInteger sum = new AtomicInteger(0); for (int a = 0;