前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >【小家java】AtomicLong可以抛弃了,请使用LongAdder代替(或使用LongAccumulator)

【小家java】AtomicLong可以抛弃了,请使用LongAdder代替(或使用LongAccumulator)

作者头像
YourBatman
发布2019-09-03 14:49:58
3.7K0
发布2019-09-03 14:49:58
举报
文章被收录于专栏:BAT的乌托邦BAT的乌托邦


每篇一句

传播正能量——做一个快乐的程序员

前言

如题,如果你对AtomicLong的使用、运行机制还不了解的话,请移步我上一篇博文:【小家java】原子操作你还在用Synchronized?Atomic、LongAdder你真有必要了解一下了

如果你现在是用的JDK还是停留在JDK7及以下,对JDK8没有太多的了解,那么本文的讲述获取能让你又多一个赶紧升级的理由。

LongAdder这个类也许很多人闻所未闻,虽然已经使用JDK8很久了。那本文就是要扫盲啦

LongAdder

DoubleAccumulator、LongAccumulator、DoubleAdder、LongAdder是JDK1.8新增的部分,是对AtomicLong等类的改进。

LongAccumulator与LongAdder在高并发环境下比AtomicLong更高效。

看看LongAdder类的java doc怎么说?

代码语言:javascript
复制
 * <p>This class is usually preferable to {@link AtomicLong} when
 * multiple threads update a common sum that is used for purposes such
 * as collecting statistics, not for fine-grained synchronization
 * control.  Under low update contention, the two classes have similar
 * characteristics. But under high contention, expected throughput of
 * this class is significantly higher, at the expense of higher space
 * consumption.
 *
 * <p>LongAdders can be used with a {@link
 * java.util.concurrent.ConcurrentHashMap} to maintain a scalable
 * frequency map (a form of histogram or multiset). For example, to
 * add a count to a {@code ConcurrentHashMap<String,LongAdder> freqs},
 * initializing if not already present, you can use {@code
 * freqs.computeIfAbsent(k -> new LongAdder()).increment();}

大概我们能抽取一些关键信息总结如下:

LongAdder中会维护一组(一个或多个)变量,这些变量加起来就是要以原子方式更新的long型变量。当更新方法add(long)在线程间竞争时,该组变量可以动态增长减缓竞争。方法sum()返回当前在维持总和的变量上的总和。 (这种机制特别像分段锁机制)

与AtomicLong相比,LongAdder更多地用于收集统计数据,而不是细粒度的同步控制。在低并发环境下,两者性能很相似。但在高并发环境下,LongAdder有着明显更高的吞吐量,但是有着更高的空间复杂度(缺点就是内存占用偏高点)。

LongAdder是JDK1.8开始出现的,所提供的API基本上可以替换掉原先的AtomicLong。该类的outline如下:

LongAdder的优化思想

LongAdder所使用的思想就是热点分离,这一点可以类比一下ConcurrentHashMap的设计思想。就是将value值分离成一个数组,当多线程访问时,通过hash算法映射到其中的一个数字进行计数。而最终的结果,就是这些数组的求和累加。这样一来,就减小了锁的粒度。如下图所示:

在实现的代码中,LongAdder一开始并不会直接使用Cell[]存储。而是先使用一个long类型的base存储,当casBase()出现失败时,则会创建Cell[]。此时,如果在单个Cell上面出现了cell更新冲突,那么会尝试创建新的Cell,或者将Cell[]扩容为2倍。代码如下:

代码语言:javascript
复制
    public void increment() {
        add(1L);
    }
 
    public void add(long x) {
        Cell[] as; long b, v; int m; Cell a;
        if ((as = cells) != null || !casBase(b = base, b + x)) {// 如cells不为空,直接对cells操作;否则casBase
            boolean uncontended = true;
            if (as == null || (m = as.length - 1) < 0 ||
                (a = as[getProbe() & m]) == null ||
                !(uncontended = a.cas(v = a.value, v + x)))    // CAS cell
                longAccumulate(x, null, uncontended);    // 创建新的Cell或者扩容
        }
    }

与其他原子类一样,LongAdder也是基于CAS实现的。

LongAdder和AtomicLong性能对比测试

说了这么多,出现LongAdder就是为了来提高并发性能的,那么是骡子是马,拉出来遛遛吧:

代码语言:javascript
复制
    //访问的线程总数
    public static final int THREAD_COUNT = 100;
    //循环的总次数
    public static final int LOOP_COUNT = 10000;
    
    static ExecutorService pool = Executors.newFixedThreadPool(THREAD_COUNT);
    static CompletionService<Long> completionService = new ExecutorCompletionService<>(pool);

    //static的共享变量
    static final AtomicLong atomicLong = new AtomicLong(0L);
    static final LongAdder longAdder = new LongAdder();

    public static void main(String[] args) throws InterruptedException, ExecutionException {
        long start = System.currentTimeMillis();
        for (int i = 0; i < THREAD_COUNT; i++) {
            completionService.submit(() -> {
                for (int j = 0; j < 100000; j++) {
                    //对比只需要求欢此方法即可
                    atomicLong.incrementAndGet();
                    //longAdder.increment();
                }
                return 1L;
            });
        }
        for (int i = 0; i < THREAD_COUNT; i++) {
            Future<Long> future = completionService.take();
            future.get();
        }
        System.out.println("耗时:" + (System.currentTimeMillis() - start));
        pool.shutdown();
    }

第一把:线程100个,循环总次数10000次:

代码语言:javascript
复制
LongAdder耗时:300ms
AtomicLong耗时:265ms

第二把:线程100个,循环总次数100000次:

代码语言:javascript
复制
LongAdder耗时:306ms
AtomicLong耗时:439ms

第三把:线程1000个,循环总次数10000次:

代码语言:javascript
复制
LongAdder耗时:613ms
AtomicLong耗时:735ms

第四把:线程1000个,循环总次数1000000次:

代码语言:javascript
复制
LongAdder耗时:5463ms
AtomicLong耗时:37964ms

从上面的性能测试,可以得出结论:

  1. 在并发比较低的时候,LongAdder和AtomicLong的效果非常接近
  2. 但是当并发较高时,两者的差距会越来越大。如上最后一个,当并发大循环次数多的时候,LongAdder的优势非常明显(6倍以上)
LongAccumulator

关于LongAccumulator,可以说是加强版的LongAdder。LongAdder的API相对比较简陋,只有对数值的加减,而LongAccumulator提供了自定义的函数操作,我们可以自己去决定计算方式。

代码语言:javascript
复制
    // accumulatorFunction:需要执行的二元函数(接收2个long作为形参,并返回1个long);identity:初始值
    public LongAccumulator(LongBinaryOperator accumulatorFunction, long identity) {
        this.function = accumulatorFunction;
        base = this.identity = identity;
    }

accumulatorFunction:需要执行的二元函数(接收2个long作为形参,并返回1个long);

identity:初始值。下面看一个Demo:

代码示例:

代码语言:javascript
复制
    public static void main(String[] args) throws InterruptedException {
        //这样就可以很安全的求和操作了
        LongAccumulator accumulator = new LongAccumulator(Long::max, Long.MIN_VALUE);

        Thread[] ts = new Thread[1000];

        for (int i = 0; i < 1000; i++) {
            ts[i] = new Thread(() -> {
                Random random = new Random();
                long value = random.nextLong();

                accumulator.accumulate(value); // 比较value和上一次的比较值,然后存储较大者
            });
            ts[i].start();
        }
        for (int i = 0; i < 1000; i++) {
            ts[i].join();
        }
        System.out.println(accumulator.longValue()); //9207653574451187103
    }

accumulate(value)传入的值会与上一次的比较值对比,然后保留较大者,最后打印出最大值。

LongAdder可以代替AtomicLong吗?

话有说回来啊,JDK8并没有把AtomicLong标记为过期,所以肯定还是很多用武之地的。

从LongAdder的Api可以看出,提供的方法还是挺少的。它更多地用于收集统计数据,而不是细粒度的同步控制。

LongAdder只提供了add(long)和decrement()方法,想要使用CAS更全面的方法还是要选择AtomicLong。

因此如果你只需要做形如count++的操作,推荐使用LongAdder代替AtomicLong吧(阿里开发手册就是这么推荐的)

DoubleAdder和DoubleAccumulator使用方法类似,这里不在介绍

注意,没有提供IntegerAdder哦~

本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2018年12月10日,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 每篇一句
  • 前言
  • LongAdder
  • LongAdder的优化思想
  • LongAdder和AtomicLong性能对比测试
  • LongAccumulator
  • LongAdder可以代替AtomicLong吗?
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档