public class LongAccumulator extends Number implements Serializable
long 。
当跨线程争用更新(方法accumulate(long) )时,该变量集可以动态增长以减少争用。
方法get() (或等价于longValue() )返回维护更新的变量的当前值。
当多线程更新用于收集统计信息的公共值时,此类通常优于AtomicLong,而不是细粒度的同步控制。 在低更新争议下,这两类具有相似的特征。 但是,在高度争议的情况下,这一类的预期吞吐量明显高于牺牲更高的空间消耗。
线程内或跨线程的累积顺序不能得到保证,不能依赖,所以此类仅适用于积累顺序无关的功能。 提供的累加器功能应该是无效的,因为尝试的更新由于线程之间的争用而失败时可能会被重新应用。 该函数应用当前值作为其第一个参数,给定的更新作为第二个参数。 例如,为了保持最大值,您可以提供Long::max以及Long.MIN_VALUE作为身份。
LongAdder课程提供了类别功能的类别,用于维护计数和总和的常见特殊情况。 电话new LongAdder()相当于new LongAccumulator((x, y) -> x + y, 0L 。
该类扩展Number ,但不定义诸如方法equals , hashCode和compareTo ,因为实例预计将发生突变,所以不如收集钥匙有用。
| Constructor and Description |
|---|
LongAccumulator(LongBinaryOperator accumulatorFunction, long identity)
使用给定的累加器函数和identity元素创建一个新的实例。
|
| Modifier and Type | Method and Description |
|---|---|
void |
accumulate(long x)
具有给定值的更新。
|
double |
doubleValue()
返回
current value为
double一个宽元转换后。
|
float |
floatValue()
返回
current value为
float一个宽元转换后。
|
long |
get()
返回当前值。
|
long |
getThenReset()
|
int |
intValue()
|
long |
longValue()
相当于
get() 。
|
void |
reset()
重置维持更新到标识值的变量。
|
String |
toString()
返回当前值的String表示形式。
|
byteValue, shortValuepublic LongAccumulator(LongBinaryOperator accumulatorFunction, long identity)
accumulatorFunction - 两个参数的无效副作用
identity - 累加器功能的标识(初始值)
public void accumulate(long x)
x - 值
public long get()
public void reset()
public long getThenReset()
public float floatValue()
float一个宽元转换后。
floatValue在类别
Number
float之后表示的
float 。
public double doubleValue()
double一个宽元转换后。
doubleValue在类别
Number
double之后表示的
double 。
Submit a bug or feature
For further API reference and developer documentation, see Java SE Documentation. That documentation contains more detailed, developer-targeted descriptions, with conceptual overviews, definitions of terms, workarounds, and working code examples.
Copyright © 1993, 2014, Oracle and/or its affiliates. All rights reserved.