Interface AsyncAtomicLong
- All Known Implementing Classes:
AsyncLocalAtomicLong
,AsyncRemoteAtomicLong
long
value that may be updated atomically.
Unlike AtomicLong
, each method from this interface is non-blocking,
which allows asynchronous invocation and consumption of the return value
via CompletableFuture
API. This is particularly useful when using
remote
implementation, because of relatively
high latency associated with an inevitable network call, but we do provide a
local
implementation as well.
An AsyncAtomicLong
is used in applications
such as atomically incremented sequence numbers, and cannot be used
as a replacement for a Long
.
- Since:
- 21.12
- Author:
- Aleks Seovic 2020.12.03
-
Method Summary
Modifier and TypeMethodDescriptionaccumulateAndGet
(long lUpdate, Remote.LongBinaryOperator accumulatorFunction) Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.accumulateAndGet
(long lUpdate, LongBinaryOperator accumulatorFunction) Atomically updates the current value with the results of applying the given function to the current and given values, returning the updated value.addAndGet
(long lDelta) Atomically adds the given value to the current value.Returns the value of the specified number as abyte
.compareAndExchange
(long lExpectedValue, long lNewValue) Atomically sets the value tonewValue
if the current value, referred to as the witness value,== expectedValue
.compareAndSet
(long lExpectedValue, long lNewValue) Atomically sets the value tonewValue
if the current value== expectedValue
.Atomically decrements the current value.Returns the current value of thisDistributedAtomicLong
as adouble
after a widening primitive conversion.Returns the current value of thisDistributedAtomicLong
as afloat
after a widening primitive conversion.get()
Returns the current value.getAndAccumulate
(long lUpdate, Remote.LongBinaryOperator accumulatorFunction) Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.getAndAccumulate
(long lUpdate, LongBinaryOperator accumulatorFunction) Atomically updates the current value with the results of applying the given function to the current and given values, returning the previous value.getAndAdd
(long lDelta) Atomically adds the given value to the current value.Atomically decrements the current value.Atomically increments the current value.getAndSet
(long lNewValue) Atomically sets the value tonewValue
and returns the old value.getAndUpdate
(Remote.LongUnaryOperator updateFunction) Atomically updates the current value with the results of applying the given function, returning the previous value.getAndUpdate
(LongUnaryOperator updateFunction) Atomically updates the current value with the results of applying the given function, returning the previous value.Atomically increments the current value.intValue()
Returns the current value of thisDistributedAtomicLong
as anint
after a narrowing primitive conversion.Returns the current value of thisDistributedAtomicLong
as along
.set
(long lNewValue) Sets the value tonewValue
.Returns the value of the specified number as ashort
.updateAndGet
(Remote.LongUnaryOperator updateFunction) Atomically updates the current value with the results of applying the given function, returning the updated value.updateAndGet
(LongUnaryOperator updateFunction) Atomically updates the current value with the results of applying the given function, returning the updated value.
-
Method Details
-
get
CompletableFuture<Long> get()Returns the current value.- Returns:
- the current value
-
set
Sets the value tonewValue
.- Parameters:
lNewValue
- the new value- Returns:
- a
CompletableFuture
that can be used to determine whether the operation completed
-
getAndSet
Atomically sets the value tonewValue
and returns the old value.- Parameters:
lNewValue
- the new value- Returns:
- the previous value
-
compareAndSet
Atomically sets the value tonewValue
if the current value== expectedValue
.- Parameters:
lExpectedValue
- the expected valuelNewValue
- the new value- Returns:
true
if successful. False return indicates that the actual value was not equal to the expected value.
-
getAndIncrement
CompletableFuture<Long> getAndIncrement()Atomically increments the current value.Equivalent to
getAndAdd(1)
.- Returns:
- the previous value
-
getAndDecrement
CompletableFuture<Long> getAndDecrement()Atomically decrements the current value.Equivalent to
getAndAdd(-1)
.- Returns:
- the previous value
-
getAndAdd
Atomically adds the given value to the current value.- Parameters:
lDelta
- the value to add- Returns:
- the previous value
-
incrementAndGet
CompletableFuture<Long> incrementAndGet()Atomically increments the current value.Equivalent to
addAndGet(1)
.- Returns:
- the updated value
-
decrementAndGet
CompletableFuture<Long> decrementAndGet()Atomically decrements the current value.Equivalent to
addAndGet(-1)
.- Returns:
- the updated value
-
addAndGet
Atomically adds the given value to the current value.- Parameters:
lDelta
- the value to add- Returns:
- the updated value
-
getAndUpdate
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
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
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
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<Long> getAndAccumulate(long lUpdate, Remote.LongBinaryOperator 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:
lUpdate
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the previous value
-
getAndAccumulate
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:
lUpdate
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the previous value
-
accumulateAndGet
CompletableFuture<Long> accumulateAndGet(long lUpdate, Remote.LongBinaryOperator 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:
lUpdate
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the updated value
-
accumulateAndGet
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:
lUpdate
- the update valueaccumulatorFunction
- a side-effect-free function of two arguments- Returns:
- the updated value
-
compareAndExchange
Atomically sets the value tonewValue
if the current value, referred to as the witness value,== expectedValue
.- Parameters:
lExpectedValue
- the expected valuelNewValue
- 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 thisDistributedAtomicLong
as anint
after a narrowing primitive conversion.- Returns:
- the current value of this
DistributedAtomicLong
as anint
after a narrowing primitive conversion
-
longValue
CompletableFuture<Long> longValue()- Returns:
- the current value of this
DistributedAtomicLong
as along
-
floatValue
CompletableFuture<Float> floatValue()Returns the current value of thisDistributedAtomicLong
as afloat
after a widening primitive conversion.- Returns:
- the current value of this
DistributedAtomicLong
as afloat
after a widening primitive conversion.
-
doubleValue
CompletableFuture<Double> doubleValue()Returns the current value of thisDistributedAtomicLong
as adouble
after a widening primitive conversion.- Returns:
- the current value of this
DistributedAtomicLong
as adouble
after a widening primitive conversion
-
byteValue
CompletableFuture<Byte> byteValue()Returns the value of the specified number as abyte
.This implementation returns the result of
intValue()
cast to abyte
.- Returns:
- the numeric value represented by this object after conversion
to type
byte
.
-
shortValue
CompletableFuture<Short> shortValue()Returns the value of the specified number as ashort
.This implementation returns the result of
intValue()
cast to ashort
.- Returns:
- the numeric value represented by this object after conversion
to type
short
.
-