Class LockingAtomicCounter

java.lang.Object
org.elasticsearch.xpack.security.support.LockingAtomicCounter

public class LockingAtomicCounter extends Object
An utility class that keeps an internal counter to ensure given runnable is only executed when the counter matches the expected value. It also ensures that the counter value do not change while the runnable is executing. This is done by applying a RW lock when reading and writing (increment) to the counter.
  • Constructor Details

    • LockingAtomicCounter

      public LockingAtomicCounter()
  • Method Details

    • get

      public long get()
    • compareAndRun

      public boolean compareAndRun(long count, Runnable runnable)
      Execute the given runnable if the internal counter matches the given count. The counter check is performed inside a read-locking block to prevent the counter value from changing (i.e. block the call to increment(). This method is in concept similar to AtomicLong.compareAndSet(long, long). Instead of a single "setValue" operation, this method takes any Runnable and ensure that the counter value do not change while the runnable is executing. Because it uses a readLock, it does *not* block other invocations of compareAndRun(long, java.lang.Runnable).
      Returns:
      true if the runnable is executed, other false.
    • increment

      public void increment()
      Increment the internal counter in the writeLock so it will be blocked if any invocation of compareAndRun(long, java.lang.Runnable) is already underway.