Class SharedLong

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

public class SharedLong extends Number
Class SharedLong provides a reduction variable for a value of type long.

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

Note: Class SharedLong is implemented using class java.util.concurrent.atomic.AtomicLong.

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

    Constructors
    Constructor
    Description
    Construct a new long reduction variable with the initial value 0.
    SharedLong(long initialValue)
    Construct a new long reduction variable with the given initial value.
  • Method Summary Link icon

    Modifier and Type
    Method
    Description
    long
    addAndGet(long value)
    Add the given value to this reduction variable and return the new value.
    boolean
    compareAndSet(long expect, long update)
    Atomically set this reduction variable to the given updated value if the current value equals the expected value.
    long
    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.
    long
    get()
    Returns this reduction variable's current value.
    long
    getAndAdd(long value)
    Add the given value to this reduction variable and return the previous value.
    long
    Subtract one from this reduction variable and return the previous value.
    long
    Add one to this reduction variable and return the previous value.
    long
    getAndSet(long value)
    Set this reduction variable to the given value and return the previous value.
    long
    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.
    long
    reduce(long value, LongOp op)
    Combine this reduction variable with the given value using the given operation.
    void
    set(long value)
    Set this reduction variable to the given value.
    Returns a string version of this reduction variable.
    boolean
    weakCompareAndSet(long expect, long 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 Link icon

    byteValue, shortValue

    Methods inherited from class java.lang.Object Link icon

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details Link icon

    • SharedLong Link icon

      public SharedLong()
      Construct a new long reduction variable with the initial value 0.
    • SharedLong Link icon

      public SharedLong(long initialValue)
      Construct a new long reduction variable with the given initial value.
      Parameters:
      initialValue - Initial value.
  • Method Details Link icon

    • get Link icon

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

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

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

      public boolean compareAndSet(long expect, long 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 Link icon

      public boolean weakCompareAndSet(long expect, long 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 Link icon

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

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

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

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

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

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

      public long reduce(long value, LongOp 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 Link icon

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

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

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

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

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