Class AtomicDouble
java.lang.Object
java.lang.Number
com.google.common.util.concurrent.AtomicDouble
- All Implemented Interfaces:
- Serializable
A 
 
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.
- Since:
- 11.0
- Author:
- Doug Lea, Martin Buchholz
- See Also:
- 
Constructor SummaryConstructorsConstructorDescriptionCreates a newAtomicDoublewith initial value0.0.AtomicDouble(double initialValue) Creates a newAtomicDoublewith the given initial value.
- 
Method SummaryModifier and TypeMethodDescriptionfinal doubleaddAndGet(double delta) Atomically adds the given value to the current value.final 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.doubleReturns the value of thisAtomicDoubleas adouble.floatReturns the value of thisAtomicDoubleas afloatafter a narrowing primitive conversion.final doubleget()Gets the current value.final doublegetAndAdd(double delta) Atomically adds the given value to the current value.final doublegetAndSet(double newValue) Atomically sets to the given value and returns the old value.intintValue()Returns the value of thisAtomicDoubleas anintafter a narrowing primitive conversion.final voidlazySet(double newValue) Eventually sets to the given value.longReturns the value of thisAtomicDoubleas alongafter a narrowing primitive conversion.final voidset(double newValue) Sets to the given value.toString()Returns the String representation of the current value.final 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.Methods inherited from class java.lang.NumberbyteValue, shortValue
- 
Constructor Details- 
AtomicDoublepublic AtomicDouble(double initialValue) Creates a newAtomicDoublewith the given initial value.- Parameters:
- initialValue- the initial value
 
- 
AtomicDoublepublic AtomicDouble()Creates a newAtomicDoublewith initial value0.0.
 
- 
- 
Method Details- 
getpublic final double get()Gets the current value.- Returns:
- the current value
 
- 
setpublic final void set(double newValue) Sets to the given value.- Parameters:
- newValue- the new value
 
- 
lazySetpublic final void lazySet(double newValue) Eventually sets to the given value.- Parameters:
- newValue- the new value
 
- 
getAndSetpublic 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
 
- 
compareAndSetpublic 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 value
- update- the new value
- Returns:
- trueif successful. False return indicates that the actual value was not bitwise equal to the expected value.
 
- 
weakCompareAndSetpublic 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 value
- update- the new value
- Returns:
- trueif successful
 
- 
getAndAddAtomically adds the given value to the current value.- Parameters:
- delta- the value to add
- Returns:
- the previous value
 
- 
addAndGetAtomically adds the given value to the current value.- Parameters:
- delta- the value to add
- Returns:
- the updated value
- Since:
- 31.1
 
- 
toString
- 
intValue
- 
longValue
- 
floatValuepublic float floatValue()Returns the value of thisAtomicDoubleas afloatafter a narrowing primitive conversion.- Specified by:
- floatValuein class- Number
 
- 
doubleValuepublic double doubleValue()Returns the value of thisAtomicDoubleas adouble.- Specified by:
- doubleValuein class- Number
 
 
-