Top Description Fields Constructors Methods
com.google.common.util.concurrent

public Class AtomicDouble

extends Number
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Annotations
@GwtIncompatible
@J2ktIncompatible
@ReflectionSupport:FULL
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkNotNull, java.lang.Double.doubleToRawLongBits, .Double.longBitsToDouble

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 by comparing their bitwise representation using Double#doubleToRawLongBits, which differs from both the primitive double == operator and from Double#equals, 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.

Authors
Doug Lea, Martin Buchholz
Since
11.0

Field Summary

Modifier and TypeField and Description
private static final long
private static final AtomicLongFieldUpdater<AtomicDouble>
private transient volatile long

Constructor Summary

AccessConstructor and Description
public
AtomicDouble(double
the initial value
initialValue
)

Creates a new AtomicDouble with the given initial value.

public
AtomicDouble()

Creates a new AtomicDouble with initial value 0.0.

Method Summary

Modifier and TypeMethod and Description
public final double

Returns:

the updated value
accumulateAndGet
(double
the update value
x
,
DoubleBinaryOperator
the accumulator function
accumulatorFunction
)

Atomically updates the current value with the results of applying the given function to the current and given values.

public final double

Returns:

the updated value
addAndGet
(double
the value to add
delta
)

Atomically adds the given value to the current value.

public final boolean

Returns:

true if successful. False return indicates that the actual value was not bitwise equal to the expected value.
compareAndSet
(double
the expected value
expect
,
double
the new value
update
)

Atomically sets the value to the given updated value if the current value is bitwise equal to the expected value.

public double
doubleValue()

Implements abstract java.lang.Number.doubleValue.

Returns the value of this AtomicDouble as a double.
public float
floatValue()

Implements abstract java.lang.Number.floatValue.

Returns the value of this AtomicDouble as a float after a narrowing primitive conversion.
public final double

Returns:

the current value
get
()

Gets the current value.

public final double

Returns:

the previous value
getAndAccumulate
(double
the update value
x
,
DoubleBinaryOperator
the accumulator function
accumulatorFunction
)

Atomically updates the current value with the results of applying the given function to the current and given values.

public final double

Returns:

the previous value
getAndAdd
(double
the value to add
delta
)

Atomically adds the given value to the current value.

public final double

Returns:

the previous value
getAndSet
(double
the new value
newValue
)

Atomically sets to the given value and returns the old value.

public final double

Returns:

the previous value
getAndUpdate
(DoubleUnaryOperator
the update function
updateFunction
)

Atomically updates the current value with the results of applying the given function.

public int
intValue()

Implements abstract java.lang.Number.intValue.

Returns the value of this AtomicDouble as an int after a narrowing primitive conversion.
public final void
lazySet(double
the new value
newValue
)

Eventually sets to the given value.

public long
longValue()

Implements abstract java.lang.Number.longValue.

Returns the value of this AtomicDouble as a long after a narrowing primitive conversion.
private void
readObject(ObjectInputStream s)

Reconstitutes the instance from a stream (that is, deserializes it).

public final void
set(double
the new value
newValue
)

Sets to the given value.

public String

Returns:

the String representation of the current value
toString
()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.
public final double

Returns:

the updated value
updateAndGet
(DoubleUnaryOperator
the update function
updateFunction
)

Atomically updates the current value with the results of applying the given function.

public final boolean

Returns:

true if successful
weakCompareAndSet
(double
the expected value
expect
,
double
the new value
update
)

Atomically sets the value to the given updated value if the current value is bitwise equal to the expected value.

private void
writeObject(ObjectOutputStream s)

Saves the state to a stream (that is, serializes it).

Inherited from java.lang.Number:
byteValueshortValue

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID

Hides java.lang.Number.serialVersionUID.

updaterback to summary
private static final AtomicLongFieldUpdater<AtomicDouble> updater
valueback to summary
private transient volatile long value

Constructor Detail

AtomicDoubleback to summary
public AtomicDouble(double initialValue)

Creates a new AtomicDouble with the given initial value.

Parameters
initialValue:double

the initial value

AtomicDoubleback to summary
public AtomicDouble()

Creates a new AtomicDouble with initial value 0.0.

Method Detail

accumulateAndGetback to summary
public final double accumulateAndGet(double x, DoubleBinaryOperator accumulatorFunction)

Atomically updates the current value with the results of applying the given function to the current and given values.

Parameters
x:double

the update value

accumulatorFunction:DoubleBinaryOperator

the accumulator function

Returns:double

the updated value

Annotations
@CanIgnoreReturnValue
Since
31.1
addAndGetback to summary
public final double addAndGet(double delta)

Atomically adds the given value to the current value.

Parameters
delta:double

the value to add

Returns:double

the updated value

Annotations
@CanIgnoreReturnValue
compareAndSetback to summary
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:double

the expected value

update:double

the new value

Returns:boolean

true if successful. False return indicates that the actual value was not bitwise equal to the expected value.

doubleValueback to summary
public double doubleValue()

Implements abstract java.lang.Number.doubleValue.

Returns the value of this AtomicDouble as a double.

Returns:double

Doc from java.lang.Number.doubleValue.

the numeric value represented by this object after conversion to type double.

Annotations
@Override
floatValueback to summary
public float floatValue()

Implements abstract java.lang.Number.floatValue.

Returns the value of this AtomicDouble as a float after a narrowing primitive conversion.

Returns:float

Doc from java.lang.Number.floatValue.

the numeric value represented by this object after conversion to type float.

Annotations
@Override
getback to summary
public final double get()

Gets the current value.

Returns:double

the current value

getAndAccumulateback to summary
public final double getAndAccumulate(double x, DoubleBinaryOperator accumulatorFunction)

Atomically updates the current value with the results of applying the given function to the current and given values.

Parameters
x:double

the update value

accumulatorFunction:DoubleBinaryOperator

the accumulator function

Returns:double

the previous value

Annotations
@CanIgnoreReturnValue
Since
31.1
getAndAddback to summary
public final double getAndAdd(double delta)

Atomically adds the given value to the current value.

Parameters
delta:double

the value to add

Returns:double

the previous value

Annotations
@CanIgnoreReturnValue
getAndSetback to summary
public final double getAndSet(double newValue)

Atomically sets to the given value and returns the old value.

Parameters
newValue:double

the new value

Returns:double

the previous value

getAndUpdateback to summary
public final double getAndUpdate(DoubleUnaryOperator updateFunction)

Atomically updates the current value with the results of applying the given function.

Parameters
updateFunction:DoubleUnaryOperator

the update function

Returns:double

the previous value

Annotations
@CanIgnoreReturnValue
Since
31.1
intValueback to summary
public int intValue()

Implements abstract java.lang.Number.intValue.

Returns the value of this AtomicDouble as an int after a narrowing primitive conversion.

Returns:int

Doc from java.lang.Number.intValue.

the numeric value represented by this object after conversion to type int.

Annotations
@Override
lazySetback to summary
public final void lazySet(double newValue)

Eventually sets to the given value.

Parameters
newValue:double

the new value

longValueback to summary
public long longValue()

Implements abstract java.lang.Number.longValue.

Returns the value of this AtomicDouble as a long after a narrowing primitive conversion.

Returns:long

Doc from java.lang.Number.longValue.

the numeric value represented by this object after conversion to type long.

Annotations
@Override
readObjectback to summary
private void readObject(ObjectInputStream s) throws IOException, ClassNotFoundException

Reconstitutes the instance from a stream (that is, deserializes it).

setback to summary
public final void set(double newValue)

Sets to the given value.

Parameters
newValue:double

the new value

toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.

Returns:String

the String representation of the current value

Annotations
@Override
updateAndGetback to summary
public final double updateAndGet(DoubleUnaryOperator updateFunction)

Atomically updates the current value with the results of applying the given function.

Parameters
updateFunction:DoubleUnaryOperator

the update function

Returns:double

the updated value

Annotations
@CanIgnoreReturnValue
Since
31.1
weakCompareAndSetback to summary
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:double

the expected value

update:double

the new value

Returns:boolean

true if successful

writeObjectback to summary
private void writeObject(ObjectOutputStream s) throws IOException

Saves the state to a stream (that is, serializes it).

Serial data
The current value is emitted (a double).