Interface AsyncAtomicInteger
-
- All Known Implementing Classes:
AsyncLocalAtomicInteger
,AsyncRemoteAtomicInteger
public interface AsyncAtomicInteger
Aint
value that may be updated atomically.Unlike
AtomicInteger
, each method from this interface is non-blocking, which allows asynchronous invocation and consumption of the return value viaCompletableFuture
API. This is particularly useful when usingremote
implementation, because of relatively high latency associated with an inevitable network call, but we do provide alocal
implementation as well.An
AsyncAtomicInteger
is used in applications such as atomically incremented sequence numbers, and cannot be used as a replacement for aInteger
.- Since:
- 21.12
- Author:
- Aleks Seovic 2020.12.07
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description CompletableFuture<Integer>
accumulateAndGet(int nUpdate, Remote.IntBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.CompletableFuture<Integer>
accumulateAndGet(int nUpdate, IntBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.CompletableFuture<Integer>
addAndGet(int nDelta)
Atomically adds the given value to the current value.CompletableFuture<Byte>
byteValue()
Returns the current value of thisAsyncAtomicInteger
as abyte
after a narrowing primitive conversion.CompletableFuture<Integer>
compareAndExchange(int nExpectedValue, int nNewValue)
Atomically sets the value tonewValue
if the current value, referred to as the witness value,== expectedValue
.CompletableFuture<Boolean>
compareAndSet(int nExpectedValue, int nNewValue)
Atomically sets the value tonewValue
if the current value== expectedValue
.CompletableFuture<Integer>
decrementAndGet()
Atomically decrements the current value.CompletableFuture<Double>
doubleValue()
Returns the current value of thisAsyncAtomicInteger
as adouble
after a widening primitive conversion.CompletableFuture<Float>
floatValue()
Returns the current value of thisAsyncAtomicInteger
as afloat
after a widening primitive conversion.CompletableFuture<Integer>
get()
Returns the current value.CompletableFuture<Integer>
getAndAccumulate(int nUpdate, Remote.IntBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.CompletableFuture<Integer>
getAndAccumulate(int nUpdate, IntBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.CompletableFuture<Integer>
getAndAdd(int nDelta)
Atomically adds the given value to the current value.CompletableFuture<Integer>
getAndDecrement()
Atomically decrements the current value.CompletableFuture<Integer>
getAndIncrement()
Atomically increments the current value.CompletableFuture<Integer>
getAndSet(int nNewValue)
Atomically sets the value tonewValue
and returns the old value.CompletableFuture<Integer>
getAndUpdate(Remote.IntUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function, returning the previous value.CompletableFuture<Integer>
getAndUpdate(IntUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function, returning the previous value.CompletableFuture<Integer>
incrementAndGet()
Atomically increments the current value.CompletableFuture<Integer>
intValue()
Returns the current value of thisAsyncAtomicInteger
as anint
.CompletableFuture<Long>
longValue()
Returns the current value of thisAsyncAtomicInteger
as along
after a widening primitive conversion.CompletableFuture<Void>
set(int nNewValue)
Sets the value tonewValue
.CompletableFuture<Short>
shortValue()
Returns the current value of thisAsyncAtomicInteger
as ashort
after a narrowing primitive conversion.CompletableFuture<Integer>
updateAndGet(Remote.IntUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function, returning the updated value.CompletableFuture<Integer>
updateAndGet(IntUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function, returning the updated value.
-
-
-
Method Detail
-
get
CompletableFuture<Integer> get()
Returns the current value.- Returns:
- the current value
-
set
CompletableFuture<Void> set(int nNewValue)
Sets the value tonewValue
.- Parameters:
nNewValue
- the new value- Returns:
- a
CompletableFuture
that can be used to determine whether the operation completed
-
getAndSet
CompletableFuture<Integer> getAndSet(int nNewValue)
Atomically sets the value tonewValue
and returns the old value.- Parameters:
nNewValue
- the new value- Returns:
- the previous value
-
compareAndSet
CompletableFuture<Boolean> compareAndSet(int nExpectedValue, int nNewValue)
Atomically sets the value tonewValue
if the current value== expectedValue
.- Parameters:
nExpectedValue
- the expected valuenNewValue
- the new value- Returns:
true
if successful. False return indicates that the actual value was not equal to the expected value.
-
getAndIncrement
CompletableFuture<Integer> getAndIncrement()
Atomically increments the current value.Equivalent to
getAndAdd(1)
.- Returns:
- the previous value
-
getAndDecrement
CompletableFuture<Integer> getAndDecrement()
Atomically decrements the current value.Equivalent to
getAndAdd(-1)
.- Returns:
- the previous value
-
getAndAdd
CompletableFuture<Integer> getAndAdd(int nDelta)
Atomically adds the given value to the current value.- Parameters:
nDelta
- the value to add- Returns:
- the previous value
-
incrementAndGet
CompletableFuture<Integer> incrementAndGet()
Atomically increments the current value.Equivalent to
addAndGet(1)
.- Returns:
- the updated value
-
decrementAndGet
CompletableFuture<Integer> decrementAndGet()
Atomically decrements the current value.Equivalent to
addAndGet(-1)
.- Returns:
- the updated value
-
addAndGet
CompletableFuture<Integer> addAndGet(int nDelta)
Atomically adds the given value to the current value.- Parameters:
nDelta
- the value to add- Returns:
- the updated value
-
getAndUpdate
CompletableFuture<Integer> getAndUpdate(Remote.IntUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.- Parameters:
updateFunction
- a side-effect-free function- Returns:
- the previous value
-
getAndUpdate
CompletableFuture<Integer> getAndUpdate(IntUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function, returning the previous value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.- Parameters:
updateFunction
- a side-effect-free function- Returns:
- the previous value
-
updateAndGet
CompletableFuture<Integer> updateAndGet(Remote.IntUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.- Parameters:
updateFunction
- a side-effect-free function- Returns:
- the updated value
-
updateAndGet
CompletableFuture<Integer> updateAndGet(IntUnaryOperator updateFunction)
Atomically updates the current value with the results of applying the given function, returning the updated value. The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads.- Parameters:
updateFunction
- a side-effect-free function- Returns:
- the updated value
-
getAndAccumulate
CompletableFuture<Integer> getAndAccumulate(int nUpdate, Remote.IntBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.The function should beside-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
- Parameters:
nUpdate
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the previous value
-
getAndAccumulate
CompletableFuture<Integer> getAndAccumulate(int nUpdate, IntBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.The function should beside-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
- Parameters:
nUpdate
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the previous value
-
accumulateAndGet
CompletableFuture<Integer> accumulateAndGet(int nUpdate, Remote.IntBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
- Parameters:
nUpdate
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the updated value
-
accumulateAndGet
CompletableFuture<Integer> accumulateAndGet(int nUpdate, IntBinaryOperator accumulatorFunction)
Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.The function should be side-effect-free, since it may be re-applied when attempted updates fail due to contention among threads. The function is applied with the current value as its first argument, and the given update as the second argument.
- Parameters:
nUpdate
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the updated value
-
compareAndExchange
CompletableFuture<Integer> compareAndExchange(int nExpectedValue, int nNewValue)
Atomically sets the value tonewValue
if the current value, referred to as the witness value,== expectedValue
.- Parameters:
nExpectedValue
- the expected valuenNewValue
- the new value- Returns:
- the witness value, which will be the same as the expected value if successful
-
intValue
CompletableFuture<Integer> intValue()
Returns the current value of thisAsyncAtomicInteger
as anint
.- Returns:
- the numeric value represented by this object
-
longValue
CompletableFuture<Long> longValue()
Returns the current value of thisAsyncAtomicInteger
as along
after a widening primitive conversion.- Returns:
- the numeric value represented by this object after conversion
to type
long
-
floatValue
CompletableFuture<Float> floatValue()
Returns the current value of thisAsyncAtomicInteger
as afloat
after a widening primitive conversion.- Returns:
- the numeric value represented by this object after conversion
to type
float
-
doubleValue
CompletableFuture<Double> doubleValue()
Returns the current value of thisAsyncAtomicInteger
as adouble
after a widening primitive conversion.- Returns:
- the numeric value represented by this object after conversion
to type
double
-
byteValue
CompletableFuture<Byte> byteValue()
Returns the current value of thisAsyncAtomicInteger
as abyte
after a narrowing primitive conversion.- Returns:
- the numeric value represented by this object after conversion
to type
byte
-
shortValue
CompletableFuture<Short> shortValue()
Returns the current value of thisAsyncAtomicInteger
as ashort
after a narrowing primitive conversion.- Returns:
- the numeric value represented by this object after conversion
to type
short
-
-