Class SynchronizedLong

java.lang.Object
org.jppf.utils.concurrent.SynchronizedLong

public class SynchronizedLong extends Object
A long value on which a set of operations can be performed atomically.
Author:
Laurent Cohen
  • Constructor Summary

    Constructors
    Constructor
    Description
    Initialize with a value of 0.
    SynchronizedLong(long value)
    Initialize with the specified value.
  • Method Summary

    Modifier and Type
    Method
    Description
    long
    addAndGet(long update)
    Add the specified update and return the new value.
    boolean
    compareAndRun(ComparisonOperator operator, long expected, Runnable action)
    Compare the value with the expected value, and run the action if they are equal.
    boolean
    compareAndSet(long expected, long update)
    Compare the value witht he expected value, and set it to the update value if they are equal.
    boolean
    compareAndSet(ComparisonOperator operator, long expectedUpdate)
    Compare the value with the expected update value using the specified operator, and set it to this update value if the comparison succeeds.
    boolean
    compareAndSet(ComparisonOperator operator, long expected, long update)
    Compare the value with the expected value using the specified operator, and set it to the update value if the comparison succeeds.
    long
    Increment the value.
    long
    get()
    Get the value.
    long
    getAndSet(long newValue)
    Set a new value and return the old value.
    long
    Increment the value.
    boolean
    runIfBetween(long lower, long upper, Runnable action)
    Run the specified action if the value is within the specified bounds.
    boolean
    runIfNotBetween(long lower, long upper, Runnable action)
    Run the specified action if the value is outside the specified bounds.
    void
    set(long newValue)
    Set to a new value.

    Methods inherited from class java.lang.Object

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

    • SynchronizedLong

      public SynchronizedLong()
      Initialize with a value of 0.
    • SynchronizedLong

      public SynchronizedLong(long value)
      Initialize with the specified value.
      Parameters:
      value - the initial value.
  • Method Details

    • get

      public long get()
      Get the value.
      Returns:
      the current value.
    • set

      public void set(long newValue)
      Set to a new value.
      Parameters:
      newValue - the value to set.
    • incrementAndGet

      public long incrementAndGet()
      Increment the value.
      Returns:
      the new value.
    • decrementAndGet

      public long decrementAndGet()
      Increment the value.
      Returns:
      the new value.
    • addAndGet

      public long addAndGet(long update)
      Add the specified update and return the new value.
      Parameters:
      update - the value to add.
      Returns:
      the new value.
    • getAndSet

      public long getAndSet(long newValue)
      Set a new value and return the old value.
      Parameters:
      newValue - the value to set.
      Returns:
      the value before the set.
    • compareAndSet

      public boolean compareAndSet(long expected, long update)
      Compare the value witht he expected value, and set it to the update value if they are equal.
      Parameters:
      expected - the expected value.
      update - the new value to set to if the comparison succeeds.
      Returns:
      true if the update was performed, false otherwise.
    • compareAndSet

      public boolean compareAndSet(ComparisonOperator operator, long expected, long update)
      Compare the value with the expected value using the specified operator, and set it to the update value if the comparison succeeds.
      Parameters:
      operator - the comparison operator.
      expected - the expected value.
      update - the new value to set to if the comparison succeeds.
      Returns:
      true if the update was performed, false otherwise.
    • compareAndSet

      public boolean compareAndSet(ComparisonOperator operator, long expectedUpdate)
      Compare the value with the expected update value using the specified operator, and set it to this update value if the comparison succeeds.
      Parameters:
      operator - the comparison operator.
      expectedUpdate - the expected value.
      Returns:
      true if the update was performed, false otherwise.
    • compareAndRun

      public boolean compareAndRun(ComparisonOperator operator, long expected, Runnable action)
      Compare the value with the expected value, and run the action if they are equal.
      Parameters:
      operator - the comparison operator.
      expected - the expected value.
      action - the action to run if the comparison succeeds.
      Returns:
      true if the update was performed, false otherwise.
    • runIfBetween

      public boolean runIfBetween(long lower, long upper, Runnable action)
      Run the specified action if the value is within the specified bounds.
      Parameters:
      lower - the lower bound of the between comparison.
      upper - the upper bound of the between comparison.
      action - the action to run if the comparison succeeds.
      Returns:
      true if the update was performed, false otherwise.
    • runIfNotBetween

      public boolean runIfNotBetween(long lower, long upper, Runnable action)
      Run the specified action if the value is outside the specified bounds.
      Parameters:
      lower - the lower bound of the between comparison.
      upper - the upper bound of the between comparison.
      action - the action to run if the comparison succeeds.
      Returns:
      true if the update was performed, false otherwise.