Class SharedBooleanArray

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

public class SharedBooleanArray extends Object
Class SharedBooleanArray provides an array reduction variable with elements of type boolean.

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

Note: Class SharedBooleanArray is implemented using class java.util.concurrent.atomic.AtomicIntegerArray. Each Boolean array element is stored as an int whose values are restricted to the range of type boolean (0 = false, 1 = true).

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

    Constructors
    Constructor
    Description
    SharedBooleanArray(boolean[] array)
    Construct a new Boolean array reduction variable whose elements are copied from the given array.
    Construct a new Boolean array reduction variable with the given length.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    compareAndSet(int i, boolean expect, boolean update)
    Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value.
    boolean
    get(int i)
    Returns this array reduction variable's current value at the given index.
    boolean
    getAndSet(int i, boolean value)
    Set this array reduction variable at the given index to the given value and return the previous value.
    int
    Returns this array reduction variable's length.
    void
    reduce(boolean[] src, BooleanOp op)
    Combine this array reduction variable with the given array using the given operation.
    void
    reduce(int dstoff, boolean[] src, int srcoff, int len, BooleanOp op)
    Combine a portion of this array reduction variable with a portion of the given array using the given operation.
    boolean
    reduce(int i, boolean value, BooleanOp op)
    Combine this array reduction variable at the given index with the given value using the given operation.
    void
    set(int i, boolean value)
    Set this array reduction variable at the given index to the given value.
    Returns a string version of this array reduction variable.
    boolean
    weakCompareAndSet(int i, boolean expect, boolean update)
    Atomically set this array reduction variable at the given index to the given updated value if the current value equals the expected value.

    Methods inherited from class java.lang.Object

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

    • SharedBooleanArray

      public SharedBooleanArray(int len)
      Construct a new Boolean array reduction variable with the given length. Each array element is initially false.
      Parameters:
      len - Length.
      Throws:
      NegativeArraySizeException - (unchecked exception) Thrown if len < 0.
    • SharedBooleanArray

      public SharedBooleanArray(boolean[] array)
      Construct a new Boolean array reduction variable whose elements are copied from the given array.
      Parameters:
      array - Array to copy.
      Throws:
      NullPointerException - (unchecked exception) Thrown if array is null.
  • Method Details

    • length

      public int length()
      Returns this array reduction variable's length.
      Returns:
      Length.
    • get

      public boolean get(int i)
      Returns this array reduction variable's current value at the given index.
      Parameters:
      i - Index.
      Returns:
      Current value.
    • set

      public void set(int i, boolean value)
      Set this array reduction variable at the given index to the given value.
      Parameters:
      i - Index.
      value - New value.
    • getAndSet

      public boolean getAndSet(int i, boolean value)
      Set this array reduction variable at the given index to the given value and return the previous value.
      Parameters:
      i - Index.
      value - New value.
      Returns:
      Previous value.
    • compareAndSet

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

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

      public boolean reduce(int i, boolean value, BooleanOp op)
      Combine this array reduction variable at the given index with the given value using the given operation. (This array [i]) is set to (this array [i]) op (value), then (this array [i]) is returned.
      Parameters:
      i - Index.
      value - Value.
      op - Binary operation.
      Returns:
      (This array [i]) op (value).
    • reduce

      public void reduce(boolean[] src, BooleanOp op)
      Combine this array reduction variable with the given array using the given operation. For each index i from 0 to this array's length-1, (this array [i]) is set to (this array [i]) op (src[i]).

      The reduce() method is multiple thread safe on a per-element basis. Each individual array element is updated atomically, but the array as a whole is not updated atomically.

      Parameters:
      src - Source array.
      op - Binary operation.
      Throws:
      NullPointerException - (unchecked exception) Thrown if src is null. Thrown if op is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if any array index would be out of bounds.
    • reduce

      public void reduce(int dstoff, boolean[] src, int srcoff, int len, BooleanOp op)
      Combine a portion of this array reduction variable with a portion of the given array using the given operation. For each index i from 0 to len-1, (this array [dstoff+i]) is set to (this array [dstoff+i]) op (src[srcoff+i]).

      The reduce() method is multiple thread safe on a per-element basis. Each individual array element is updated atomically, but the array as a whole is not updated atomically.

      Parameters:
      dstoff - Index of first element to update in this array.
      src - Source array.
      srcoff - Index of first element to update from in the source array.
      len - Number of array elements to update.
      op - Binary operation.
      Throws:
      NullPointerException - (unchecked exception) Thrown if src is null. Thrown if op is null.
      IndexOutOfBoundsException - (unchecked exception) Thrown if len < 0. Thrown if any array index would be out of bounds.
    • toString

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