APIdia Launch Partner
Top Description Inners Fields Constructors Methods
java.util.concurrent.atomic

public Class DoubleAdder

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 double sum. When updates (method add) are contended across threads, the set of variables may grow dynamically to reduce contention. Method sum (or, equivalently doubleValue) returns the current total combined across the variables maintaining the sum. The order of accumulation within or across threads is not guaranteed. Thus, this class may not be applicable if numerical stability is required, especially when combining values of substantially different orders of magnitude.

This class is usually preferable to alternatives when multiple threads update a common value that is used for purposes such as summary statistics that are frequently updated but less frequently read.

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

Creates a new adder with initial sum of zero.

Method Summary

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

Adds the given value.

public double

Returns:

the sum
doubleValue
()

Implements abstract java.lang.Number.doubleValue.

Equivalent to sum.

public float
floatValue()

Implements abstract java.lang.Number.floatValue.

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

public int
intValue()

Implements abstract java.lang.Number.intValue.

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

public long
longValue()

Implements abstract java.lang.Number.longValue.

Returns the sum as a long after a narrowing primitive conversion.

private void
public void
reset()

Resets variables maintaining the sum to zero.

public double

Returns:

the sum
sum
()

Returns the current sum.

public double

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

DoubleAdderback to summary
public DoubleAdder()

Creates a new adder with initial sum of zero.

Method Detail

addback to summary
public void add(double x)

Adds the given value.

Parameters
x:double

the value to add

doubleValueback to summary
public double doubleValue()

Implements abstract java.lang.Number.doubleValue.

Equivalent to sum.

Returns:double

the sum

floatValueback to summary
public float floatValue()

Implements abstract java.lang.Number.floatValue.

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

intValueback to summary
public int intValue()

Implements abstract java.lang.Number.intValue.

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

longValueback to summary
public long longValue()

Implements abstract java.lang.Number.longValue.

Returns the sum as a long after a narrowing primitive conversion.

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 double 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. Also, because floating-point arithmetic is not strictly associative, the returned result need not be identical to the value that would be obtained in a sequential series of updates to a single variable.

Returns:double

the sum

sumThenResetback to summary
public double 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:double

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 DoubleAdder.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 double
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 DoubleAdder object with initial state held by this proxy
readResolve
()

Returns a DoubleAdder 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 double value

The current value returned by sum().

Constructor Detail

SerializationProxyback to summary
pack-priv SerializationProxy(DoubleAdder a)

Method Detail

readResolveback to summary
private Object readResolve()

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

Returns:Object

a DoubleAdder object with initial state held by this proxy