java 8:LongAdder和LongAccumulator是AtomicLong的首选吗? [英] java 8 : Are LongAdder and LongAccumulator preferred to AtomicLong?

查看:156
本文介绍了java 8:LongAdder和LongAccumulator是AtomicLong的首选吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

LongAdder 替代 AtomicLong

ExecutorService executor = Executors.newFixedThreadPool(2);    
IntStream.range(0, 1000)
    .forEach(i -> executor.submit(adder::increment));    
stop(executor);    
System.out.println(adder.sumThenReset());   // => 1000

LongAccumulator 是一个更通用的版本 LongAdder

LongAccumulator is a more generalized version of LongAdder

LongBinaryOperator op = (x, y) -> 2 * x + y;
LongAccumulator accumulator = new LongAccumulator(op, 1L);

ExecutorService executor = Executors.newFixedThreadPool(2);
IntStream.range(0, 10)
    .forEach(i -> executor.submit(() -> accumulator.accumulate(i)));
stop(executor);

System.out.println(accumulator.getThenReset());     // => 2539

我有一些疑问。


  1. LongAdder 总是首选AtomicLong?

  2. LongAccumulator 首选LongAdder和AtomicLong?

  1. Is LongAdder always preferred to AtomicLong?
  2. Is LongAccumulator preferred to both LongAdder and AtomicLong?


推荐答案

这些类之间的区别,以及什么时候使用另一个,在Javadoc中提到。来自 LongAdder

The difference between those classes, and when to use one over the other, is mentioned in the Javadoc. From LongAdder:


此类通常优于 AtomicLong 当多个线程更新用于收集统计信息等目的的公共和时,而不是用于细粒度同步控制。在低更新争用下,这两个类具有相似的特征。但在高争用的情况下,这一类的预期吞吐量明显更高,但代价是空间消耗更高。

This class is usually preferable to 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.

来自 LongAccumulator


当多个线程更新用于收集统计信息等目的的公共值时,此类通常优于 AtomicLong ,而不是用于细粒度同步控制。在低更新争用下,这两个类具有相似的特征。但在高争用的情况下,这一类的预期吞吐量明显更高,但代价是空间消耗更高。

This class is usually preferable to AtomicLong when multiple threads update a common value 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.

[...]

Class LongAdder 提供此类功能的类似物,用于维护计数和总和的常见特殊情况。调用 new LongAdder()相当于 new LongAccumulator((x,y) - > x + y,0L)

Class LongAdder provides analogs of the functionality of this class for the common special case of maintaining counts and sums. The call new LongAdder() is equivalent to new LongAccumulator((x, y) -> x + y, 0L).

因此,使用一个优先于另一个取决于您的应用程序打算做什么。它并不总是严格优先,只有在预期高并发并且您需要保持共同状态时才会优先。

Thus, the use of one over the other depends on what your application intends to do. It is not always strictly prefered, only when high concurrency is expected and you need to maintain a common state.

这篇关于java 8:LongAdder和LongAccumulator是AtomicLong的首选吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆