Class SharedDouble

java.lang.Object
java.lang.Number
edu.rit.pj.reduction.SharedDouble
All Implemented Interfaces:
Serializable

public class SharedDouble extends Number
Class SharedDouble provides a reduction variable for a value of type double.

Class SharedDouble is multiple thread safe. The methods use lock-free atomic compare-and-set.

Note: Class SharedDouble is implemented using class java.util.concurrent.atomic.AtomicLong. The double value is stored as a long whose bit pattern is the same as the double value.

Version:
07-Jun-2007
Author:
Alan Kaminsky
See Also:
  • Constructor Summary

    Constructors
    Constructor
    Description
    Construct a new double reduction variable with the initial value 0.
    SharedDouble(double initialValue)
    Construct a new double reduction variable with the given initial value.
  • Method Summary

    Modifier and Type
    Method
    Description
    double
    addAndGet(double value)
    Add the given value to this reduction variable and return the new value.
    boolean
    compareAndSet(double expect, double update)
    Atomically set this reduction variable to the given updated value if the current value equals the expected value.
    double
    Subtract one from this reduction variable and return the new value.
    double
    Returns this reduction variable's current value converted to type double.
    float
    Returns this reduction variable's current value converted to type float.
    double
    get()
    Returns this reduction variable's current value.
    double
    getAndAdd(double value)
    Add the given value to this reduction variable and return the previous value.
    double
    Subtract one from this reduction variable and return the previous value.
    double
    Add one to this reduction variable and return the previous value.
    double
    getAndSet(double value)
    Set this reduction variable to the given value and return the previous value.
    double
    Add one to this reduction variable and return the new value.
    int
    Returns this reduction variable's current value converted to type int.
    long
    Returns this reduction variable's current value converted to type long.
    double
    reduce(double value, DoubleOp op)
    Combine this reduction variable with the given value using the given operation.
    void
    set(double value)
    Set this reduction variable to the given value.
    Returns a string version of this reduction variable.
    boolean
    weakCompareAndSet(double expect, double update)
    Atomically set this reduction variable to the given updated value if the current value equals the expected value.

    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 Details

    • SharedDouble

      public SharedDouble()
      Construct a new double reduction variable with the initial value 0.
    • SharedDouble

      public SharedDouble(double initialValue)
      Construct a new double reduction variable with the given initial value.
      Parameters:
      initialValue - Initial value.
  • Method Details

    • get

      public double get()
      Returns this reduction variable's current value.
      Returns:
      Current value.
    • set

      public void set(double value)
      Set this reduction variable to the given value.
      Parameters:
      value - New value.
    • getAndSet

      public double getAndSet(double value)
      Set this reduction variable to the given value and return the previous value.
      Parameters:
      value - New value.
      Returns:
      Previous value.
    • compareAndSet

      public boolean compareAndSet(double expect, double update)
      Atomically set this reduction variable to the given updated value if the current value equals the expected value.
      Parameters:
      expect - Expected value.
      update - Updated value.
      Returns:
      True if the update happened, false otherwise.
    • weakCompareAndSet

      public boolean weakCompareAndSet(double expect, double update)
      Atomically set this reduction variable to the given updated value if the current value equals the expected value. May fail spuriously.
      Parameters:
      expect - Expected value.
      update - Updated value.
      Returns:
      True if the update happened, false otherwise.
    • getAndIncrement

      public double getAndIncrement()
      Add one to this reduction variable and return the previous value.
      Returns:
      Previous value.
    • getAndDecrement

      public double getAndDecrement()
      Subtract one from this reduction variable and return the previous value.
      Returns:
      Previous value.
    • getAndAdd

      public double getAndAdd(double value)
      Add the given value to this reduction variable and return the previous value.
      Parameters:
      value - Value to add.
      Returns:
      Previous value.
    • incrementAndGet

      public double incrementAndGet()
      Add one to this reduction variable and return the new value.
      Returns:
      New value.
    • decrementAndGet

      public double decrementAndGet()
      Subtract one from this reduction variable and return the new value.
      Returns:
      New value.
    • addAndGet

      public double addAndGet(double value)
      Add the given value to this reduction variable and return the new value.
      Parameters:
      value - Value to add.
      Returns:
      New value.
    • reduce

      public double reduce(double value, DoubleOp op)
      Combine this reduction variable with the given value using the given operation. The result is stored back into this reduction variable and is returned.
      Parameters:
      value - Value.
      op - Binary operation.
      Returns:
      (This variable) op (value).
    • toString

      public String toString()
      Returns a string version of this reduction variable.
      Overrides:
      toString in class Object
      Returns:
      String version.
    • intValue

      public int intValue()
      Returns this reduction variable's current value converted to type int.
      Specified by:
      intValue in class Number
      Returns:
      Current value.
    • longValue

      public long longValue()
      Returns this reduction variable's current value converted to type long.
      Specified by:
      longValue in class Number
      Returns:
      Current value.
    • floatValue

      public float floatValue()
      Returns this reduction variable's current value converted to type float.
      Specified by:
      floatValue in class Number
      Returns:
      Current value.
    • doubleValue

      public double doubleValue()
      Returns this reduction variable's current value converted to type double.
      Specified by:
      doubleValue in class Number
      Returns:
      Current value.