需求:一个jvm中实现一个计数器功能,需保证多线程情况下数据正确性。
我们来模拟50个线程,每个线程对计数器递增100万次,最终结果应该是5000万。
我们使用4种方式实现,看一下其性能,然后引出为什么需要使用LongAdder、LongAccumulator。
方式一:synchronized方式实现
package com.itsoku.chat32;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.atomic.LongAccumulator;
/**
*
*/
public class Demo1 {
static int count = 0;
public static synchronized void incr() {
count++;
}
public static void main(String[] args) throws ExecutionException, InterruptedException {
for (int i = 0; i < 10; i++) {
count = 0;
m1();
}
}
private static void m1() throws InterruptedException {
long t1 = System.currentTimeMillis();
int threadCount = 50;
CountDownLatch countDownLatch = new CountDownLatch(threadCount);
for (int i = 0; i < threadCount; i++) {
new Thread(() -> {
try {
for (int j = 0; j < 1000000; j++) {
incr();
}
} finally {
countDownLatch.countDown();
}
}).start();
}
countDownLatch.await();
long t2 = System.currentTimeMillis();
System.out.println(String.format("结果:%s,耗时(ms):%s", count, (t2 - t1)));
}
}
输出:
结果:50000000,耗时(ms):1437
结果:50000000,耗时(ms):