Java高并发累加器Striped64

原子类

在多线程环境下,常用累加操作方式是使用原子类进行累加,例如AtomicInteger、AtomicLong。但是使用原子类在多线程高竞争的情况下,CAS会经常失败,并发效率会大大降低。
在这里插入图片描述
因为CAS操作失败后要自旋再次进行替换,这样失败的线程就会大量消耗CPU资源。所以在高并发的场景下使用原子类累加器并不是很好的选择。

Striped64

Striped64是一种高并发累加器,有效解决了原子类累加的弊端。Striped64将线程竞争的操作分散开来,每个线程操作一个cell,而sum则等于base和所有cell值的和。

性能比较

开启10个线程,并发执行累加操作,每个线程加10000000。

  1. 使用AtomicLong
public static void main(String[] args) {
        AtomicLong atomicLong = new AtomicLong(0);
        CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
        CountDownLatch countDownLatch = new CountDownLatch(10);
        long l = System.currentTimeMillis();
        for (int i=0;i<10;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    int i=10000000;
                    while(i>0){
                        atomicLong.incrementAndGet();
                        i--;
                    }
                    countDownLatch.countDown();
                }
            }).start();
        }
        try {
            countDownLatch.await();
            long l2 = System.currentTimeMillis();
            System.out.println(l2-l);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(atomicLong.get());
    }

计算结果:

1964ms
100000000
  1. 使用LongAdder
public static void main(String[] args) {
        LongAdder longAdder = new LongAdder();
        CyclicBarrier cyclicBarrier = new CyclicBarrier(10);
        CountDownLatch countDownLatch = new CountDownLatch(10);
        long l = System.currentTimeMillis();
        for (int i=0;i<10;i++){
            new Thread(new Runnable() {
                @Override
                public void run() {
                    try {
                        cyclicBarrier.await();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    } catch (BrokenBarrierException e) {
                        e.printStackTrace();
                    }
                    int i=10000000;
                    while(i>0){
                        longAdder.add(1);
                        i--;
                    }
                    countDownLatch.countDown();
                }
            }).start();
        }
        try {
            countDownLatch.await();
            long l2 = System.currentTimeMillis();
            System.out.println((l2-l)+"ms");
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println(longAdder.sum());
    }

执行结果:

266ms
100000000

比较两者的性能,Striped64比原子类要快约10倍。

LongAdder

在这里插入图片描述
当线程来进行添加操作的,会根据线程ID定位到具体的cell,线程再对cell进行CAS操作,进行累加。这样各个线程就不用产生激烈竞争导致频繁CAS失败。对于JVM中最高并发线程数等与机器可用CPU核数,所以cells数组的长度也不会很长,进行数组求和也很快。
在这里插入图片描述
Striped64的继承类有LongAdder,以LongAdder累加流程为例:
在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

hi wei

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值