public class LongAdder extends Number implements Serializable
long总和。
当更新(方法add(long) )跨线程竞争时,变量集可以动态增长以减少争用。
方法sum() (或等效地, longValue() )返回保持总和的整个变量组合的当前总和。
这个类是通常优选AtomicLong当多个线程更新时使用,用于诸如收集统计信息,不用于细粒度同步控制的共同总和。 在低更新争议下,这两类具有相似的特征。 但是,在高度争议的情况下,这一类的预期吞吐量明显高于牺牲更高的空间消耗。
LongAdders可以使用ConcurrentHashMap来维护可扩展的频率映射(一种直方图或多集)。 例如,要向ConcurrentHashMap<String,LongAdder> freqs添加一个计数,如果尚未存在,则可以使用freqs.computeIfAbsent(k -> new LongAdder()).increment();
该类扩展Number ,但不定义诸如方法equals , hashCode和compareTo ,因为实例预计将发生突变,所以不如收集钥匙有用。
| Constructor and Description |
|---|
LongAdder()
创建一个新的加法器,初始和为零。
|
| Modifier and Type | Method and Description |
|---|---|
void |
add(long x)
添加给定值。
|
void |
decrement()
相当于
add(-1) 。
|
double |
doubleValue()
返回
sum()为
double一个宽元转换后。
|
float |
floatValue()
返回
sum()为
float一个宽元转换后。
|
void |
increment()
相当于
add(1) 。
|
int |
intValue()
|
long |
longValue()
相当于
sum() 。
|
void |
reset()
将保持总和的变量重置为零。
|
long |
sum()
返回当前总和。
|
long |
sumThenReset()
|
String |
toString()
返回
sum()的String表示
形式 。
|
byteValue, shortValuepublic void add(long x)
x - 要添加的值
public void increment()
add(1) 。
public void decrement()
add(-1) 。
public long sum()
public void reset()
public long sumThenReset()
public float floatValue()
sum()为
float一个宽元转换后。
floatValue在类别
Number
float之后表示的
float 。
public double doubleValue()
sum()为
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.