Class LoomAtomicLong
java.lang.Object
com.loomcache.client.LoomAtomicLong
Client-side distributed atomic long proxy.
Provides atomic operations on a 64-bit long stored in the Consistency Subsystem. All operations are linearizable and atomic across the cluster.
Consistency Guarantees
LoomAtomicLong operations are linearizable. This means all operations appear to execute atomically in a total order, providing strong guarantees suitable for critical sections and consensus protocols.
Thread Safety
This class is thread-safe. Multiple threads may perform operations concurrently; the underlying Consistency Subsystem ensures atomicity and visibility.Usage Example
LoomConsistencySubsystem cp = client.consistencySubsystem();
LoomAtomicLong atomic = cp.getAtomicLong("consensus-counter");
long next = atomic.incrementAndGet(); // 1
long val = atomic.get(); // 1
atomic.set(0); // reset
boolean ok = atomic.compareAndSet(0, 100); // atomic CAS
atomic.getAndIncrement();
- Since:
- 1.4
- See Also:
-
Method Summary
Modifier and TypeMethodDescriptionlongaddAndGet(long delta) Atomically adds delta to the value and returns the new value.booleancompareAndSet(long expectedValue, long newValue) Atomically sets the value to an expected value if the current value equals expected.longAtomically decrements the value by 1 and returns the new value.longget()Gets the current value atomically.longgetAndAdd(long delta) Atomically gets the current value and adds delta.longAtomically gets the current value and decrements by 1.longAtomically gets the current value and increments by 1.longAtomically increments the value by 1 and returns the new value.voidset(long newValue) Sets the value atomically.
-
Method Details
-
get
public long get()Gets the current value atomically.- Returns:
- the current value
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
set
public void set(long newValue) Sets the value atomically.- Parameters:
newValue- the new value- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
incrementAndGet
public long incrementAndGet()Atomically increments the value by 1 and returns the new value.- Returns:
- the incremented value
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
decrementAndGet
public long decrementAndGet()Atomically decrements the value by 1 and returns the new value.- Returns:
- the decremented value
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
getAndIncrement
public long getAndIncrement()Atomically gets the current value and increments by 1.- Returns:
- the previous value
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
getAndDecrement
public long getAndDecrement()Atomically gets the current value and decrements by 1.- Returns:
- the previous value
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
addAndGet
public long addAndGet(long delta) Atomically adds delta to the value and returns the new value.- Parameters:
delta- the amount to add (may be negative)- Returns:
- the new value
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
getAndAdd
public long getAndAdd(long delta) Atomically gets the current value and adds delta.- Parameters:
delta- the amount to add (may be negative)- Returns:
- the previous value
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-
compareAndSet
public boolean compareAndSet(long expectedValue, long newValue) Atomically sets the value to an expected value if the current value equals expected. This is a compare-and-set (CAS) operation.- Parameters:
expectedValue- the expected current valuenewValue- the new value to set if current equals expected- Returns:
- true if the update succeeded; false otherwise
- Throws:
LoomException- if the operation fails or the cluster is unavailable
-