Top Description Fields Constructors Methods
net.sf.ehcache.util.concurrent

public Class LongAdder

extends Striped64
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Imports
java.io.IOException, .ObjectInputStream, .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 java.util.concurrent.atomic.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 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 net.sf.ehcache.util.concurrent.Striped64:
baseBASE_UPDATERbusyBUSY_UPDATERcellsNCPUthreadHashCode

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.
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 net.sf.ehcache.util.concurrent.Striped64.fn.

Version of plus for use in retryUpdate
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 void
Inherited from net.sf.ehcache.util.concurrent.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)

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.

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

Implements abstract net.sf.ehcache.util.concurrent.Striped64.fn.

Version of plus for use in retryUpdate

Parameters
v:long

Doc from net.sf.ehcache.util.concurrent.Striped64.fn.

the current value (of either base or a cell)

x:long

Doc from net.sf.ehcache.util.concurrent.Striped64.fn.

the argument from a user update call

Returns:long

Doc from net.sf.ehcache.util.concurrent.Striped64.fn.

result of the update function

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 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()

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

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