Top Description Inners Fields Constructors Methods
java.util.concurrent.atomic

public Class LongAdder

extends Striped64
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.Serializable

One or more variables that together maintain an initially zero 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.util.concurrent.ConcurrentHashMap 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.

Author
Doug Lea
Since
1.8

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class
LongAdder.SerializationProxy

Serialization proxy, used to avoid reference to the non-public Striped64 superclass in serialized forms.

Field Summary

Modifier and TypeField and Description
private static final long
Inherited from java.util.concurrent.atomic.Striped64:
basecellscellsBusyNCPU

Constructor Summary

AccessConstructor and Description
public
LongAdder()

Creates a new adder with initial sum of zero.

Method Summary

Modifier and TypeMethod and Description
public void
add(long
the value to add
x
)

Adds the given value.

public void
decrement()

Equivalent to add(-1).

public double
doubleValue()

Implements abstract java.lang.Number.doubleValue.

Returns the sum as a double after a widening primitive conversion.

public float
floatValue()

Implements abstract java.lang.Number.floatValue.

Returns the sum as a float after a widening primitive conversion.

public void
increment()

Equivalent to add(1).

public int
intValue()

Implements abstract java.lang.Number.intValue.

Returns the sum as an int after a narrowing primitive conversion.

public long

Returns:

the sum
longValue
()

Implements abstract java.lang.Number.longValue.

Equivalent to sum.

private void
public void
reset()

Resets variables maintaining the sum to zero.

public long

Returns:

the sum
sum
()

Returns the current sum.

public long

Returns:

the sum
sumThenReset
()

Equivalent in effect to sum followed by reset.

public String

Returns:

the String representation of the sum
toString
()

Overrides java.lang.Object.toString.

Returns the String representation of the sum.

private Object

Returns:

a SerializationProxy representing the state of this instance
writeReplace
()

Returns a SerializationProxy representing the state of this instance.

Inherited from java.util.concurrent.atomic.Striped64:
advanceProbecasBasecasCellsBusydoubleAccumulategetAndSetBasegetProbelongAccumulate

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Number.serialVersionUID.

Constructor Detail

LongAdderback to summary
public LongAdder()

Creates a new adder with initial sum of zero.

Method Detail

addback to summary
public void add(long x)

Adds the given value.

Parameters
x:long

the value to add

decrementback to summary
public void decrement()

Equivalent to add(-1).

doubleValueback to summary
public double doubleValue()

Implements abstract java.lang.Number.doubleValue.

Returns the sum as a double after a widening primitive conversion.

Returns:double

Doc from java.lang.Number.doubleValue.

the numeric value represented by this object after conversion to type double.

floatValueback to summary
public float floatValue()

Implements abstract java.lang.Number.floatValue.

Returns the sum as a float after a widening primitive conversion.

Returns:float

Doc from java.lang.Number.floatValue.

the numeric value represented by this object after conversion to type float.

incrementback to summary
public void increment()

Equivalent to add(1).

intValueback to summary
public int intValue()

Implements abstract java.lang.Number.intValue.

Returns the sum as an int after a narrowing primitive conversion.

Returns:int

Doc from java.lang.Number.intValue.

the numeric value represented by this object after conversion to type int.

longValueback to summary
public long longValue()

Implements abstract java.lang.Number.longValue.

Equivalent to sum.

Returns:long

the sum

readObjectback to summary
private void readObject(ObjectInputStream s) throws InvalidObjectException
Parameters
s:ObjectInputStream

the stream

Exceptions
InvalidObjectException:
always
resetback 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.

sumback 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.

Returns:long

the sum

sumThenResetback to summary
public long sumThenReset()

Equivalent in effect to sum followed by reset. This method may apply for example during quiescent points between multithreaded computations. If there are updates concurrent with this method, the returned value is not guaranteed to be the final value occurring before the reset.

Returns:long

the sum

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the String representation of the sum.

Returns:String

the String representation of the sum

writeReplaceback to summary
private Object writeReplace()

Returns a SerializationProxy representing the state of this instance.

Returns:Object

a SerializationProxy representing the state of this instance

java.util.concurrent.atomic back to summary

private Class LongAdder.SerializationProxy

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable

Serialization proxy, used to avoid reference to the non-public Striped64 superclass in serialized forms.

Field Summary

Modifier and TypeField and Description
private static final long
private final long
value

The current value returned by sum().

Constructor Summary

AccessConstructor and Description
pack-priv

Method Summary

Modifier and TypeMethod and Description
private Object

Returns:

a LongAdder object with initial state held by this proxy
readResolve
()

Returns a LongAdder object with initial state held by this proxy.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID
valueback to summary
private final long value

The current value returned by sum().

Constructor Detail

SerializationProxyback to summary
pack-priv SerializationProxy(LongAdder a)

Method Detail

readResolveback to summary
private Object readResolve()

Returns a LongAdder object with initial state held by this proxy.

Returns:Object

a LongAdder object with initial state held by this proxy