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

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值