DoubleAdder
Stay organized with collections
Save and categorize content based on your preferences.
One or more variables that together maintain an initially zero
double
sum. When updates (method add(double)
) 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.
Public Constructor Summary
|
DoubleAdder()
Creates a new adder with initial sum of zero.
|
Public Method Summary
void
|
add(double x)
Adds the given value.
|
double
|
|
float
|
floatValue()
Returns the sum() as a float
after a narrowing primitive conversion.
|
int
|
intValue()
Returns the sum() as an int after a
narrowing primitive conversion.
|
long
|
longValue()
Returns the sum() as a long after a
narrowing primitive conversion.
|
void
|
reset()
Resets variables maintaining the sum to zero.
|
double
|
sum()
Returns the current sum.
|
double
|
|
String
|
|
Inherited Method Summary
From class
java.lang.Number
byte
|
byteValue()
Returns the value of the specified number as a byte ,
which may involve rounding or truncation.
|
abstract
double
|
doubleValue()
Returns the value of the specified number as a double ,
which may involve rounding.
|
abstract
float
|
floatValue()
Returns the value of the specified number as a float ,
which may involve rounding.
|
abstract
int
|
intValue()
Returns the value of the specified number as an int ,
which may involve rounding or truncation.
|
abstract
long
|
longValue()
Returns the value of the specified number as a long ,
which may involve rounding or truncation.
|
short
|
shortValue()
Returns the value of the specified number as a short ,
which may involve rounding or truncation.
|
From class
java.lang.Object
Object
|
clone()
Creates and returns a copy of this Object .
|
boolean
|
equals( Object obj)
Compares this instance with the specified object and indicates if they
are equal.
|
void
|
finalize()
Invoked when the garbage collector has detected that this instance is no longer reachable.
|
final
Class<?>
|
getClass()
Returns the unique instance of Class that represents this
object's class.
|
int
|
hashCode()
Returns an integer hash code for this object.
|
final
void
|
notify()
Causes a thread which is waiting on this object's monitor (by means of
calling one of the wait() methods) to be woken up.
|
final
void
|
notifyAll()
Causes all threads which are waiting on this object's monitor (by means
of calling one of the wait() methods) to be woken up.
|
String
|
toString()
Returns a string containing a concise, human-readable description of this
object.
|
final
void
|
wait(long timeout, int nanos)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait(long timeout)
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object or until the
specified timeout expires.
|
final
void
|
wait()
Causes the calling thread to wait until another thread calls the notify() or notifyAll() method of this object.
|
Public Constructors
public
DoubleAdder
()
Creates a new adder with initial sum of zero.
Public Methods
public
void
add
(double x)
public
double
doubleValue
()
public
float
floatValue
()
Returns the sum()
as a float
after a narrowing primitive conversion.
Returns
- the numeric value represented by this object after conversion
to type
float
.
public
int
intValue
()
Returns the sum()
as an int
after a
narrowing primitive conversion.
Returns
- the numeric value represented by this object after conversion
to type
int
.
public
long
longValue
()
Returns the sum()
as a long
after a
narrowing primitive conversion.
Returns
- the numeric value represented by this object after conversion
to type
long
.
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.
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.
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.
public
String
toString
()
Returns the String representation of the sum()
.
Returns
- the String representation of the
sum()
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2024-07-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2024-07-10 UTC."],[[["`DoubleAdder` provides a way to efficiently sum `double` values across multiple threads, minimizing contention."],["It's suitable for statistics collection and scenarios where frequent updates are more common than reads."],["Although it extends `Number`, it lacks methods like `equals`, `hashCode`, and `compareTo` due to its mutable nature, rendering it unsuitable as collection keys."],["While `sum()` returns the current total, it doesn't offer an atomic snapshot and may not reflect all concurrent updates."],["For a thread-safe reset, use `reset()` only when no other threads are concurrently updating the `DoubleAdder`."]]],["`DoubleAdder` maintains a `double` sum, starting at zero. It dynamically adjusts internal variables to handle contention when multiple threads update the sum using `add(double)`. `sum()` or `doubleValue()` returns the combined total. The class offers `floatValue()`, `intValue()`, and `longValue()` for conversions, and `reset()` to zero the sum. `sumThenReset()` combines summing and resetting. Results are not guaranteed to be numerically stable. `toString()` return string representation of the sum.\n"]]