Top Description Fields Constructors Methods
com.google.common.hash

pack-priv final Class LongAdder

extends Striped64
implements Serializable, LongAddable
Class Inheritance
All Implemented Interfaces
com.google.common.hash.LongAddable, java.io.Serializable
Annotations
@ElementTypesAreNonnullByDefault
Imports
java.io.IOException, .ObjectInputStream, .ObjectOutputStream, .Serializable, java.util.concurrent.atomic.AtomicLong

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.

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.

JSR 166e Note

This class is targeted to be placed in java.util.concurrent.atomic.

Author
Doug Lea
Since
1.8

Field Summary

Modifier and TypeField and Description
private static final long
Inherited from com.google.common.hash.Striped64:
basebusycellsNCPUrngthreadHashCode

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
)

Implements com.google.common.hash.LongAddable.add.

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.
pack-priv final long
fn(long
the current value (of either base or a cell)
v
,
long
the argument from a user update call
x
)

Implements abstract com.google.common.hash.Striped64.fn.

Version of plus for use in retryUpdate
public void
increment()

Implements com.google.common.hash.LongAddable.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
()

Implements com.google.common.hash.LongAddable.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 void
Inherited from com.google.common.hash.Striped64:
casBasecasBusyinternalResetretryUpdate

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)

Implements com.google.common.hash.LongAddable.add.

Adds the given value.

Parameters
x:long

the value to add

Annotations
@Override
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.

Annotations
@Override
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.

Annotations
@Override
fnback to summary
pack-priv final long fn(long v, long x)

Implements abstract com.google.common.hash.Striped64.fn.

Version of plus for use in retryUpdate

Parameters
v:long

Doc from com.google.common.hash.Striped64.fn.

the current value (of either base or a cell)

x:long

Doc from com.google.common.hash.Striped64.fn.

the argument from a user update call

Returns:long

Doc from com.google.common.hash.Striped64.fn.

result of the update function

Annotations
@Override
incrementback to summary
public void increment()

Implements com.google.common.hash.LongAddable.increment.

Equivalent to add(1).

Annotations
@Override
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.

Annotations
@Override
longValueback to summary
public long longValue()

Implements abstract java.lang.Number.longValue.

Equivalent to sum.

Returns:long

the sum

Annotations
@Override
readObjectback to summary
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException
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()

Implements com.google.common.hash.LongAddable.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

Annotations
@Override
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

Annotations
@Override
writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException