Module org.elasticsearch.security
Class LockingAtomicCounter
java.lang.Object
org.elasticsearch.xpack.security.support.LockingAtomicCounter
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 Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionbooleancompareAndRun(long count, Runnable runnable) Execute the given runnable if the internal counter matches the given count.longget()voidIncrement the internal counter in the writeLock so it will be blocked if any invocation ofcompareAndRun(long, java.lang.Runnable)is already underway.
-
Constructor Details
-
LockingAtomicCounter
public LockingAtomicCounter()
-
-
Method Details
-
get
public long get() -
compareAndRun
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 toincrement(). This method is in concept similar toAtomicLong.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 ofcompareAndRun(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 ofcompareAndRun(long, java.lang.Runnable)is already underway.
-