Class AtomicDouble
- java.lang.Object
-
- java.lang.Number
-
- com.google.common.util.concurrent.AtomicDouble
-
- All Implemented Interfaces:
java.io.Serializable
@GwtIncompatible @ReflectionSupport(FULL) public class AtomicDouble extends java.lang.Number implements java.io.Serializable
Adoublevalue that may be updated atomically. See thejava.util.concurrent.atomicpackage specification for description of the properties of atomic variables. AnAtomicDoubleis used in applications such as atomic accumulation, and cannot be used as a replacement for aDouble. However, this class does extendNumberto allow uniform access by tools and utilities that deal with numerically-based classes.This class compares primitive
doublevalues in methods such ascompareAndSet(double, double)by comparing their bitwise representation usingDouble.doubleToRawLongBits(double), which differs from both the primitive double==operator and fromDouble.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.
- Since:
- 11.0
- Author:
- Doug Lea, Martin Buchholz
- See Also:
- Serialized Form
-
-
Constructor Summary
Constructors Constructor Description AtomicDouble()Creates a newAtomicDoublewith initial value0.0.AtomicDouble(double initialValue)Creates a newAtomicDoublewith the given initial value.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description doubleaccumulateAndGet(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.doubleaddAndGet(double delta)Atomically adds the given value to the current value.booleancompareAndSet(double expect, double update)Atomically sets the value to the given updated value if the current value is bitwise equal to the expected value.doubledoubleValue()Returns the value of thisAtomicDoubleas adouble.floatfloatValue()Returns the value of thisAtomicDoubleas afloatafter a narrowing primitive conversion.doubleget()Gets the current value.doublegetAndAccumulate(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.doublegetAndAdd(double delta)Atomically adds the given value to the current value.doublegetAndSet(double newValue)Atomically sets to the given value and returns the old value.doublegetAndUpdate(java.util.function.DoubleUnaryOperator updateFunction)Atomically updates the current value with the results of applying the given function.intintValue()Returns the value of thisAtomicDoubleas anintafter a narrowing primitive conversion.voidlazySet(double newValue)Eventually sets to the given value.longlongValue()Returns the value of thisAtomicDoubleas alongafter a narrowing primitive conversion.voidset(double newValue)Sets to the given value.java.lang.StringtoString()Returns the String representation of the current value.doubleupdateAndGet(java.util.function.DoubleUnaryOperator updateFunction)Atomically updates the current value with the results of applying the given function.booleanweakCompareAndSet(double expect, double update)Atomically sets the value to the given updated value if the current value is bitwise equal to the expected value.
-
-
-
Constructor Detail
-
AtomicDouble
public AtomicDouble(double initialValue)
Creates a newAtomicDoublewith the given initial value.- Parameters:
initialValue- the initial value
-
AtomicDouble
public AtomicDouble()
Creates a newAtomicDoublewith initial value0.0.
-
-
Method Detail
-
get
public final double get()
Gets the current value.- Returns:
- the current value
-
set
public final void set(double newValue)
Sets to the given value.- Parameters:
newValue- the new value
-
lazySet
public final void lazySet(double newValue)
Eventually sets to the given value.- Parameters:
newValue- the new value
-
getAndSet
public final double getAndSet(double newValue)
Atomically sets to the given value and returns the old value.- Parameters:
newValue- the new value- Returns:
- the previous value
-
compareAndSet
public final 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.- Parameters:
expect- the expected valueupdate- the new value- Returns:
trueif successful. False return indicates that the actual value was not bitwise equal to the expected value.
-
weakCompareAndSet
public final 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.May fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to
compareAndSet.- Parameters:
expect- the expected valueupdate- the new value- Returns:
trueif successful
-
getAndAdd
@CanIgnoreReturnValue public final double getAndAdd(double delta)
Atomically adds the given value to the current value.- Parameters:
delta- the value to add- Returns:
- the previous value
-
addAndGet
@CanIgnoreReturnValue public final double addAndGet(double delta)
Atomically adds the given value to the current value.- Parameters:
delta- the value to add- Returns:
- the updated value
-
getAndAccumulate
@CanIgnoreReturnValue public final 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.- Parameters:
x- the update valueaccumulatorFunction- the accumulator function- Returns:
- the previous value
- Since:
- 31.1
-
accumulateAndGet
@CanIgnoreReturnValue public final 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.- Parameters:
x- the update valueaccumulatorFunction- the accumulator function- Returns:
- the updated value
- Since:
- 31.1
-
getAndUpdate
@CanIgnoreReturnValue public final double getAndUpdate(java.util.function.DoubleUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function.- Parameters:
updateFunction- the update function- Returns:
- the previous value
- Since:
- 31.1
-
updateAndGet
@CanIgnoreReturnValue public final double updateAndGet(java.util.function.DoubleUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function.- Parameters:
updateFunction- the update function- Returns:
- the updated value
- Since:
- 31.1
-
toString
public java.lang.String toString()
Returns the String representation of the current value.- Overrides:
toStringin classjava.lang.Object- Returns:
- the String representation of the current value
-
intValue
public int intValue()
Returns the value of thisAtomicDoubleas anintafter a narrowing primitive conversion.- Specified by:
intValuein classjava.lang.Number
-
longValue
public long longValue()
Returns the value of thisAtomicDoubleas alongafter a narrowing primitive conversion.- Specified by:
longValuein classjava.lang.Number
-
floatValue
public float floatValue()
Returns the value of thisAtomicDoubleas afloatafter a narrowing primitive conversion.- Specified by:
floatValuein classjava.lang.Number
-
doubleValue
public double doubleValue()
Returns the value of thisAtomicDoubleas adouble.- Specified by:
doubleValuein classjava.lang.Number
-
-