long
sum. When updates (method add
) are contended
across threads, the set of variables may grow dynamically to reduce
contention. Method sum
(or, equivalently, longValue
) returns the current total combined across the
variables maintaining the sum.
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.
LongAdders can be used with a java.
to maintain a scalable
frequency map (a form of histogram or multiset). For example, to
add a count to a ConcurrentHashMap<String,LongAdder> freqs
,
initializing if not already present, you can use freqs.computeIfAbsent(key, k -> new LongAdder()).increment();
This class extends Number
, but does not define
methods such as equals
, hashCode
and compareTo
because instances are expected to be mutated, and so are
not useful as collection keys.
Modifier and Type | Class and Description |
---|---|
private static class | LongAdder.
Serialization proxy, used to avoid reference to the non-public Striped64 superclass in serialized forms. |
Modifier and Type | Field and Description |
---|---|
private static final long |
Access | Constructor and Description |
---|---|
public |
Modifier and Type | Method and Description |
---|---|
public void | |
public void | |
public double | doubleValue()
Implements abstract java. Returns the |
public float | floatValue()
Implements abstract java. Returns the |
public void | |
public int | intValue()
Implements abstract java. Returns the |
public long | |
private void | |
public void | |
public long | |
public long | |
public String | Returns: the String representation of thesum Overrides java. Returns the String representation of the |
private Object | Returns: aSerializationProxy
representing the state of this instanceReturns a SerializationProxy representing the state of this instance. |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID |
LongAdder | back to summary |
---|---|
public LongAdder() Creates a new adder with initial sum of zero. |
add | back to summary |
---|---|
public void add(long x) Adds the given value.
|
decrement | back to summary |
---|---|
public void decrement() Equivalent to |
doubleValue | back to summary |
---|---|
public double doubleValue() Implements abstract java. Returns the
|
floatValue | back to summary |
---|---|
public float floatValue() Implements abstract java. Returns the
|
increment | back to summary |
---|---|
public void increment() Equivalent to |
intValue | back to summary |
---|---|
public int intValue() Implements abstract java. Returns the
|
longValue | back to summary |
---|---|
public long longValue() Implements abstract java. Equivalent to
|
readObject | back to summary |
---|---|
private void readObject(ObjectInputStream s) throws InvalidObjectException
|
reset | back to summary |
---|---|
public void reset() Resets variables maintaining the sum to zero. This method may be a useful alternative to creating a new adder, but is only effective if there are no concurrent updates. Because this method is intrinsically racy, it should only be used when it is known that no threads are concurrently updating. |
sum | back to summary |
---|---|
public long sum() Returns the current sum. The returned value is NOT an atomic snapshot; invocation in the absence of concurrent updates returns an accurate result, but concurrent updates that occur while the sum is being calculated might not be incorporated.
|
sumThenReset | back to summary |
---|---|
public long sumThenReset() Equivalent in effect to
|
toString | back to summary |
---|---|
public String toString() Overrides java. Returns the String representation of the |
writeReplace | back to summary |
---|---|
private Object writeReplace() Returns a SerializationProxy representing the state of this instance.
|
Modifier and Type | Field and Description |
---|---|
private static final long | |
private final long | value
The current value returned by sum(). |
Access | Constructor and Description |
---|---|
pack-priv |
Modifier and Type | Method and Description |
---|---|
private Object | Returns: aLongAdder object with initial state
held by this proxyReturns a |
serialVersionUID | back to summary |
---|---|
private static final long serialVersionUID |
value | back to summary |
---|---|
private final long value The current value returned by sum(). |
SerializationProxy | back to summary |
---|---|
pack-priv SerializationProxy(LongAdder a) |
readResolve | back to summary |
---|---|
private Object readResolve() Returns a
|