Class SharedBoolean

java.lang.Object
edu.rit.pj.reduction.SharedBoolean

public class SharedBoolean extends Object
Class SharedBoolean provides a reduction variable for a value of type boolean.

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

Note: Class SharedBoolean is implemented using class java.util.concurrent.atomic.AtomicBoolean. The Boolean value is stored as an int with 0 = false and 1 = true.

Version:
06-Jun-2007
Author:
Alan Kaminsky
  • Constructor Summary Link icon

    Constructors
    Constructor
    Description
    Construct a new Boolean reduction variable with the initial value false.
    SharedBoolean(boolean initialValue)
    Construct a new Boolean reduction variable with the given initial value.
  • Method Summary Link icon

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

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

    • SharedBoolean Link icon

      public SharedBoolean()
      Construct a new Boolean reduction variable with the initial value false.
    • SharedBoolean Link icon

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

    • get Link icon

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

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

      public boolean getAndSet(boolean 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(boolean expect, boolean 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(boolean expect, boolean 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.
    • reduce Link icon

      public boolean reduce(boolean value, BooleanOp 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.