public class AtomicDouble
extends java.lang.Number
implements java.io.Serializable
double value that may be updated atomically. See the java.util.concurrent.atomic package specification for description of the properties of atomic
variables. An AtomicDouble is used in applications such as atomic accumulation, and
cannot be used as a replacement for a Double. However, this class does extend Number to allow uniform access by tools and utilities that deal with numerically-based classes.
This class compares primitive double values in methods such as
compareAndSet(double, double) by comparing their bitwise representation using Double.doubleToRawLongBits(double), which differs from both the primitive double == operator and
from Double.equals(java.lang.Object), as if implemented by:
static boolean bitEquals(double x, double y) {
long xBits = Double.doubleToRawLongBits(x);
long yBits = Double.doubleToRawLongBits(y);
return xBits == yBits;
}
It is possible to write a more scalable updater, at the cost of giving up strict atomicity. See for example DoubleAdder.
| Modifier and Type | Field and Description |
|---|---|
private static long |
serialVersionUID |
private static java.util.concurrent.atomic.AtomicLongFieldUpdater<AtomicDouble> |
updater |
private long |
value |
| Constructor and Description |
|---|
AtomicDouble()
Creates a new
AtomicDouble with initial value 0.0. |
AtomicDouble(double initialValue)
Creates a new
AtomicDouble with the given initial value. |
| Modifier and Type | Method and Description |
|---|---|
double |
accumulateAndGet(double x,
java.util.function.DoubleBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the
current and given values.
|
double |
addAndGet(double delta)
Atomically adds the given value to the current value.
|
boolean |
compareAndSet(double expect,
double update)
Atomically sets the value to the given updated value if the current value is bitwise equal to the expected value.
|
double |
doubleValue()
Returns the value of this
AtomicDouble as a double. |
float |
floatValue()
Returns the value of this
AtomicDouble as a float after a narrowing primitive
conversion. |
double |
get()
Gets the current value.
|
double |
getAndAccumulate(double x,
java.util.function.DoubleBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the
current and given values.
|
double |
getAndAdd(double delta)
Atomically adds the given value to the current value.
|
double |
getAndSet(double newValue)
Atomically sets to the given value and returns the old value.
|
double |
getAndUpdate(java.util.function.DoubleUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function.
|
int |
intValue()
Returns the value of this
AtomicDouble as an int after a narrowing primitive
conversion. |
void |
lazySet(double newValue)
Eventually sets to the given value.
|
long |
longValue()
Returns the value of this
AtomicDouble as a long after a narrowing primitive
conversion. |
private void |
readObject(java.io.ObjectInputStream s)
Reconstitutes the instance from a stream (that is, deserializes it).
|
void |
set(double newValue)
Sets to the given value.
|
java.lang.String |
toString()
Returns the String representation of the current value.
|
double |
updateAndGet(java.util.function.DoubleUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function.
|
boolean |
weakCompareAndSet(double expect,
double update)
Atomically sets the value to the given updated value if the current value is bitwise equal to the expected value.
|
private void |
writeObject(java.io.ObjectOutputStream s)
Saves the state to a stream (that is, serializes it).
|
private static final long serialVersionUID
private transient volatile long value
private static final java.util.concurrent.atomic.AtomicLongFieldUpdater<AtomicDouble> updater
public AtomicDouble(double initialValue)
AtomicDouble with the given initial value.initialValue - the initial valuepublic AtomicDouble()
AtomicDouble with initial value 0.0.public final double get()
public final void set(double newValue)
newValue - the new valuepublic final void lazySet(double newValue)
newValue - the new valuepublic final double getAndSet(double newValue)
newValue - the new valuepublic final boolean compareAndSet(double expect,
double update)
expect - the expected valueupdate - the new valuetrue if successful. False return indicates that the actual value was not
bitwise equal to the expected value.public final boolean weakCompareAndSet(double expect,
double update)
May
fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate
alternative to compareAndSet.
expect - the expected valueupdate - the new valuetrue if successfulpublic final double getAndAdd(double delta)
delta - the value to addpublic final double addAndGet(double delta)
delta - the value to addpublic final double getAndAccumulate(double x,
java.util.function.DoubleBinaryOperator accumulatorFunction)
x - the update valueaccumulatorFunction - the accumulator functionpublic final double accumulateAndGet(double x,
java.util.function.DoubleBinaryOperator accumulatorFunction)
x - the update valueaccumulatorFunction - the accumulator functionpublic final double getAndUpdate(java.util.function.DoubleUnaryOperator updateFunction)
updateFunction - the update functionpublic final double updateAndGet(java.util.function.DoubleUnaryOperator updateFunction)
updateFunction - the update functionpublic java.lang.String toString()
toString in class java.lang.Objectpublic int intValue()
AtomicDouble as an int after a narrowing primitive
conversion.intValue in class java.lang.Numberpublic long longValue()
AtomicDouble as a long after a narrowing primitive
conversion.longValue in class java.lang.Numberpublic float floatValue()
AtomicDouble as a float after a narrowing primitive
conversion.floatValue in class java.lang.Numberpublic double doubleValue()
AtomicDouble as a double.doubleValue in class java.lang.Numberprivate void writeObject(java.io.ObjectOutputStream s)
throws java.io.IOException
java.io.IOExceptionprivate void readObject(java.io.ObjectInputStream s)
throws java.io.IOException,
java.lang.ClassNotFoundException
java.io.IOExceptionjava.lang.ClassNotFoundException