AtomicReference 和 ConcurrentHashMap 线程安全下的性能区别
分别在两个配置相同的线程池中,循环执行一千万次给AtomicReference封装的Map和保证原子性的ConcurrentHashMap中key为amount的值递增赋值
@Service
public class AtomicReferenceExampleImpl implements AtomicReferenceExample {
@Resource
@Qualifier("taskExecutor1")
private ThreadPoolTaskExecutor taskExecutor1;
@Resource
@Qualifier("taskExecutor2")
private ThreadPoolTaskExecutor taskExecutor2;
private static final AtomicReference<Map<String, Integer>> DEBIT_CARD_REF
= new AtomicReference<>(Map.of("amount", 0));
private final Map<String, AtomicInteger> DEBIT_CARD_REF2 = new ConcurrentHashMap<>();
private final int cycle = 10000000;
@Override
public void atomicReferenceExample() {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
// cycle个线程,每个线程累加1元
List<Future<?>> futures = IntStream.range(0, cycle)
.mapToObj(i -> taskExecutor1.submit(() -> {
var map = DEBIT_CARD_REF.get();
Map<String, Integer> newMap = new HashMap<>();
newMap.put("amount", map.get("amount") + 1);
while (!DEBIT_CARD_REF.compareAndSet(map, newMap)) {
map = DEBIT_CARD_REF.get();
newMap.put("amount", map.get("amount") + 1);
}
}))
.collect(Collectors.toList());
// 等待所有任务完成
futures.forEach(AtomicReferenceExampleImpl::accept);
stopWatch.stop();
System.out.println("task1 cost time -- " + stopWatch.getTotalTimeMillis() + "ms");
// 打印最终结果
Integer amount = DEBIT_CARD_REF.get().get("amount");
System.out.println("final amount -- " + amount);
// cycle个线程,每个线程累加1元
StopWatch stopWatch2 = new StopWatch();
stopWatch2.start();
// 初始化
DEBIT_CARD_REF2.put("amount", new AtomicInteger(0));
List<Future<?>> futures2 = IntStream.range(0, cycle)
.mapToObj(i -> taskExecutor2.submit(() -> {
DEBIT_CARD_REF2.get("amount").getAndAdd(1);
}))
.collect(Collectors.toList());
futures2.forEach(AtomicReferenceExampleImpl::accept);
stopWatch2.stop();
System.out.println("task2 cost time -- " + stopWatch2.getTotalTimeMillis() + "ms");
// 打印最终结果
AtomicInteger amount2 = DEBIT_CARD_REF2.get("amount");
System.out.println("final amount2 -- " + amount2);
taskExecutor2.shutdown();
taskExecutor1.shutdown();
}
private static void accept(Future<?> future) {
try {
future.get();
} catch (Exception e) {
e.printStackTrace();
}
}
}
执行结果
结果显示速度基本一致,ConcurrentHashMap略快于AtomicReference