Class AtomicDouble

  • All Implemented Interfaces:
    java.io.Serializable

    public final class AtomicDouble
    extends java.lang.Number
    A double value that may be updated atomically. See the VarHandle specification for descriptions of the properties of atomic accesses. The implementation is based on a VarHandle guarding the access to a primitive double field. For double, numeric (e.g. getAndAdd) as well as bitwise (e.g. getOpaque) atomic update method access modes compare values using their bitwise representation (see Double.doubleToRawLongBits(double)).
    See Also:
    Serialized Form
    • Constructor Summary

      Constructors 
      Constructor Description
      AtomicDouble()
      Creates a new AtomicDouble with initial value 0.
      AtomicDouble​(double initialValue)
      Creates a new AtomicDouble with the given initial value.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      double addAndGet​(double delta)
      Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...).
      double compareAndExchange​(double expectedValue, double newValue)
      Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle.compareAndExchange(java.lang.Object...).
      double compareAndExchangeAcquire​(double expectedValue, double newValue)
      Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle.compareAndExchangeAcquire(java.lang.Object...).
      double compareAndExchangeRelease​(double expectedValue, double newValue)
      Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle.compareAndExchangeRelease(java.lang.Object...).
      boolean compareAndSet​(double expectedValue, double newValue)
      Atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...).
      double doubleValue()
      Returns the current value of this AtomicDouble as an long with memory effects as specified by VarHandle.getVolatile(java.lang.Object...).
      float floatValue()
      Returns the current value of this AtomicDouble as an float after a narrowing primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...).
      double get()
      Returns the current value, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...).
      double getAcquire()
      Returns the current value, with memory effects as specified by VarHandle.getAcquire(java.lang.Object...).
      double getAndAdd​(double delta)
      Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...).
      double getAndSet​(double newValue)
      Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...).
      double getAndUpdate​(java.util.function.DoubleUnaryOperator updateFunction)
      Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...)) the current value with the results of applying the given function, returning the previous value.
      double getOpaque()
      Returns the current value, with memory effects as specified by VarHandle.getOpaque(java.lang.Object...).
      double getPlain()
      Returns the current value, with memory semantics of reading as if the variable was declared non-volatile.
      int intValue()
      Returns the current value of this AtomicDouble as an int after a narrowing primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...).
      void lazySet​(long newValue)
      Sets the value to newValue, with memory effects as specified by VarHandle.setRelease(java.lang.Object...).
      long longValue()
      Returns the current value of this AtomicDouble as an long after a narrowing primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...).
      void set​(double newValue)
      Sets the value to newValue, with memory effects as specified by VarHandle.setVolatile(java.lang.Object...).
      void setOpaque​(double newValue)
      Sets the value to newValue, with memory effects as specified by VarHandle.setOpaque(java.lang.Object...).
      void setPlain​(double newValue)
      Sets the value to newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final.
      void setRelease​(double newValue)
      Sets the value to newValue, with memory effects as specified by VarHandle.setRelease(java.lang.Object...).
      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 weakCompareAndSetAcquire​(double expectedValue, double newValue)
      Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object...).
      boolean weakCompareAndSetPlain​(double expectedValue, double newValue)
      Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...).
      boolean weakCompareAndSetRelease​(double expectedValue, double newValue)
      Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object...).
      boolean weakCompareAndSetVolatile​(double expectedValue, double newValue)
      Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object...).
      • Methods inherited from class java.lang.Number

        byteValue, shortValue
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
    • Constructor Detail

      • AtomicDouble

        public AtomicDouble()
        Creates a new AtomicDouble with initial value 0.
      • AtomicDouble

        public AtomicDouble​(double initialValue)
        Creates a new AtomicDouble with the given initial value.
        Parameters:
        initialValue - the initial value
    • Method Detail

      • get

        public double get()
        Returns the current value, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...).
        Returns:
        the current value
      • set

        public void set​(double newValue)
        Sets the value to newValue, with memory effects as specified by VarHandle.setVolatile(java.lang.Object...).
        Parameters:
        newValue - the new value
      • lazySet

        public void lazySet​(long newValue)
        Sets the value to newValue, with memory effects as specified by VarHandle.setRelease(java.lang.Object...).
        Parameters:
        newValue - the new value
      • getAndSet

        public double getAndSet​(double newValue)
        Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...).
        Parameters:
        newValue - the new value
        Returns:
        the previous value
      • getAndAdd

        public double getAndAdd​(double delta)
        Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...).
        Parameters:
        delta - the value to add
        Returns:
        the previous value
      • addAndGet

        public double addAndGet​(double delta)
        Atomically adds the given value to the current value, with memory effects as specified by VarHandle.getAndAdd(java.lang.Object...).
        Parameters:
        delta - the value to add
        Returns:
        the updated value
      • getAndUpdate

        public double getAndUpdate​(java.util.function.DoubleUnaryOperator updateFunction)
        Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...)) the current value with the results of applying the given function, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.
        Parameters:
        updateFunction - a side-effect-free function
        Returns:
        the previous value
      • updateAndGet

        public 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
      • intValue

        public int intValue()
        Returns the current value of this AtomicDouble as an int after a narrowing primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...).
        Specified by:
        intValue in class java.lang.Number
        Returns:
        the current value
      • longValue

        public long longValue()
        Returns the current value of this AtomicDouble as an long after a narrowing primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...).
        Specified by:
        longValue in class java.lang.Number
        Returns:
        the current value
      • floatValue

        public float floatValue()
        Returns the current value of this AtomicDouble as an float after a narrowing primitive conversion, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...).
        Specified by:
        floatValue in class java.lang.Number
        Returns:
        the current value
      • doubleValue

        public double doubleValue()
        Returns the current value of this AtomicDouble as an long with memory effects as specified by VarHandle.getVolatile(java.lang.Object...). Equivalent to get().
        Specified by:
        doubleValue in class java.lang.Number
        Returns:
        the current value
      • getPlain

        public double getPlain()
        Returns the current value, with memory semantics of reading as if the variable was declared non-volatile.
        Returns:
        the current value
      • setPlain

        public void setPlain​(double newValue)
        Sets the value to newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final.
        Parameters:
        newValue - the new value
      • getOpaque

        public double getOpaque()
        Returns the current value, with memory effects as specified by VarHandle.getOpaque(java.lang.Object...).
        Returns:
        the current value
      • setOpaque

        public void setOpaque​(double newValue)
        Sets the value to newValue, with memory effects as specified by VarHandle.setOpaque(java.lang.Object...).
        Parameters:
        newValue - the new value
      • getAcquire

        public double getAcquire()
        Returns the current value, with memory effects as specified by VarHandle.getAcquire(java.lang.Object...).
        Returns:
        the current value
      • setRelease

        public void setRelease​(double newValue)
        Sets the value to newValue, with memory effects as specified by VarHandle.setRelease(java.lang.Object...).
        Parameters:
        newValue - the new value
      • compareAndSet

        public boolean compareAndSet​(double expectedValue,
                                     double newValue)
        Atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...).
        Parameters:
        expectedValue - the expected value
        newValue - the new value
        Returns:
        true if successful. False return indicates that the actual value was not equal to the expected value.
      • compareAndExchange

        public double compareAndExchange​(double expectedValue,
                                         double newValue)
        Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle.compareAndExchange(java.lang.Object...).
        Parameters:
        expectedValue - the expected value
        newValue - the new value
        Returns:
        the witness value, which will be the same as the expected value if successful
      • compareAndExchangeAcquire

        public double compareAndExchangeAcquire​(double expectedValue,
                                                double newValue)
        Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle.compareAndExchangeAcquire(java.lang.Object...).
        Parameters:
        expectedValue - the expected value
        newValue - the new value
        Returns:
        the witness value, which will be the same as the expected value if successful
      • compareAndExchangeRelease

        public double compareAndExchangeRelease​(double expectedValue,
                                                double newValue)
        Atomically sets the value to newValue if the current value, referred to as the witness value, == expectedValue, with memory effects as specified by VarHandle.compareAndExchangeRelease(java.lang.Object...).
        Parameters:
        expectedValue - the expected value
        newValue - the new value
        Returns:
        the witness value, which will be the same as the expected value if successful
      • weakCompareAndSetPlain

        public boolean weakCompareAndSetPlain​(double expectedValue,
                                              double newValue)
        Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...).
        Parameters:
        expectedValue - the expected value
        newValue - the new value
        Returns:
        true if successful
      • weakCompareAndSetVolatile

        public boolean weakCompareAndSetVolatile​(double expectedValue,
                                                 double newValue)
        Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object...).
        Parameters:
        expectedValue - the expected value
        newValue - the new value
        Returns:
        true if successful
      • weakCompareAndSetAcquire

        public boolean weakCompareAndSetAcquire​(double expectedValue,
                                                double newValue)
        Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object...).
        Parameters:
        expectedValue - the expected value
        newValue - the new value
        Returns:
        true if successful
      • weakCompareAndSetRelease

        public boolean weakCompareAndSetRelease​(double expectedValue,
                                                double newValue)
        Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object...).
        Parameters:
        expectedValue - the expected value
        newValue - the new value
        Returns:
        true if successful
      • toString

        public java.lang.String toString()
        Returns the String representation of the current value.
        Overrides:
        toString in class java.lang.Object
        Returns:
        the String representation of the current value