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

  • 13
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
AtomicReferenceConcurrentHashMap 都是Java中常用的线程安全的类。 AtomicReference 是一个基于CAS(Compare and Swap)算法实现的原子类,可以保证多线程并发访问时数据的安全性。它的主要作用是对一个对象进行原子更新,使得多线程环境下对对象的修改具有原子性。例如,在并发情况下,多个线程需要对同一个对象进行操作,如果使用普通的引用类型,会出现多线程操作同一对象的情况,从而导致数据不一致。而使用AtomicReference可以保证多个线程对同一个对象进行原子操作,避免了数据的不一致性。 ConcurrentHashMapJava线程安全的哈希表实现,它提供了高效的并发访问能力。它采用了分段锁的机制来提高并发访问的效率,通过将整个哈希表分成多个小的segment,在每个segment上加锁,不同线程可以同时访问不同的segment,从而提高了并发访问效率。ConcurrentHashMap线程安全的,可以在多线程环境下安全地进行读写操作。 它们两者之间最主要的区别是,AtomicReference 是用于管理单个变量的原子更新,而ConcurrentHashMap则是用于管理多个变量(即key-value键值对)的线程安全哈希表实现。此外,ConcurrentHashMap采用了分段锁技术来保证线程安全性,相比之下更适合处理大量的key-value数据。而AtomicReference则更适合管理单个变量,并且具有更高的性能

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值