Interface AsyncAtomicReference<V>
-
- Type Parameters:
V
- the type of object referred to by this reference
- All Known Implementing Classes:
AsyncLocalAtomicReference
,AsyncRemoteAtomicReference
public interface AsyncAtomicReference<V>
An object reference that may be updated atomically.Unlike
AtomicReference
, 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 am inevitable network call, but we do provide alocal
implementation as well.- Since:
- 21.12
- Author:
- Aleks Seovic 2020.12.08
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description CompletableFuture<V>
accumulateAndGet(V x, Remote.BinaryOperator<V> 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<V>
accumulateAndGet(V x, BinaryOperator<V> 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<V>
compareAndExchange(V expectedValue, V newValue)
Atomically sets the value tonewValue
if the current value, referred to as the witness value, is equal to theexpectedValue
.CompletableFuture<Boolean>
compareAndSet(V expectedValue, V newValue)
Atomically sets the value tonewValue
if the current value is equal to theexpectedValue
.CompletableFuture<V>
get()
Returns the current value.CompletableFuture<V>
getAndAccumulate(V x, Remote.BinaryOperator<V> 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<V>
getAndAccumulate(V x, BinaryOperator<V> 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<V>
getAndSet(V newValue)
Atomically sets the value tonewValue
and returns the old value.CompletableFuture<V>
getAndUpdate(Remote.UnaryOperator<V> updateFunction)
Atomically updates the current value with the results of applying the given function, returning the previous value.CompletableFuture<V>
getAndUpdate(UnaryOperator<V> updateFunction)
Atomically updates the current value with the results of applying the given function, returning the previous value.CompletableFuture<Void>
set(V newValue)
Sets the value tonewValue
.CompletableFuture<V>
updateAndGet(Remote.UnaryOperator<V> updateFunction)
Atomically updates the current value with the results of applying the given function, returning the updated value.CompletableFuture<V>
updateAndGet(UnaryOperator<V> updateFunction)
Atomically updates the current value with the results of applying the given function, returning the updated value.
-
-
-
Method Detail
-
get
CompletableFuture<V> get()
Returns the current value.- Returns:
- the current value
-
set
CompletableFuture<Void> set(V newValue)
Sets the value tonewValue
.- Parameters:
newValue
- the new value- Returns:
- a
CompletableFuture
that can be used to determine whether the operation completed
-
getAndSet
CompletableFuture<V> getAndSet(V newValue)
Atomically sets the value tonewValue
and returns the old value.- Parameters:
newValue
- the new value- Returns:
- the previous value
-
compareAndSet
CompletableFuture<Boolean> compareAndSet(V expectedValue, V newValue)
Atomically sets the value tonewValue
if the current value is equal to theexpectedValue
.- Parameters:
expectedValue
- the expected valuenewValue
- the new value- Returns:
true
if successful. False return indicates that the actual value was not equal to the expected value.
-
getAndUpdate
CompletableFuture<V> getAndUpdate(Remote.UnaryOperator<V> 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<V> getAndUpdate(UnaryOperator<V> 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<V> updateAndGet(Remote.UnaryOperator<V> 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<V> updateAndGet(UnaryOperator<V> 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<V> getAndAccumulate(V x, Remote.BinaryOperator<V> 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:
x
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the previous value
-
getAndAccumulate
CompletableFuture<V> getAndAccumulate(V x, BinaryOperator<V> 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:
x
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the previous value
-
accumulateAndGet
CompletableFuture<V> accumulateAndGet(V x, Remote.BinaryOperator<V> 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:
x
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the updated value
-
accumulateAndGet
CompletableFuture<V> accumulateAndGet(V x, BinaryOperator<V> 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:
x
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the updated value
-
compareAndExchange
CompletableFuture<V> compareAndExchange(V expectedValue, V newValue)
Atomically sets the value tonewValue
if the current value, referred to as the witness value, is equal to theexpectedValue
.- Parameters:
expectedValue
- the expected valuenewValue
- the new value- Returns:
- the witness value, which will be the same as the expected value if successful
-
-