AtomicReference
open class AtomicReference<V : Any!> : Serializable
kotlin.Any | |
↳ | java.util.concurrent.atomic.AtomicReference |
An object reference that may be updated atomically. See the VarHandle
specification for descriptions of the properties of atomic accesses.
Summary
Public constructors | |
---|---|
Creates a new AtomicReference with null initial value. |
|
AtomicReference(initialValue: V) Creates a new AtomicReference with the given initial value. |
Public methods | |
---|---|
V |
accumulateAndGet(x: V, accumulatorFunction: BinaryOperator<V>!) Atomically updates (with memory effects as specified by |
V |
compareAndExchange(expectedValue: V, newValue: V) Atomically sets the value to |
V |
compareAndExchangeAcquire(expectedValue: V, newValue: V) Atomically sets the value to |
V |
compareAndExchangeRelease(expectedValue: V, newValue: V) Atomically sets the value to |
Boolean |
compareAndSet(expectedValue: V, newValue: V) Atomically sets the value to |
V |
get() Returns the current value, with memory effects as specified by |
V |
Returns the current value, with memory effects as specified by |
V |
getAndAccumulate(x: V, accumulatorFunction: BinaryOperator<V>!) Atomically updates (with memory effects as specified by |
V |
getAndSet(newValue: V) Atomically sets the value to |
V |
getAndUpdate(updateFunction: UnaryOperator<V>!) Atomically updates (with memory effects as specified by |
V |
Returns the current value, with memory effects as specified by |
V |
getPlain() Returns the current value, with memory semantics of reading as if the variable was declared non- |
Unit |
lazySet(newValue: V) Sets the value to |
Unit |
set(newValue: V) Sets the value to |
Unit |
setOpaque(newValue: V) Sets the value to |
Unit |
setPlain(newValue: V) Sets the value to |
Unit |
setRelease(newValue: V) Sets the value to |
open String |
toString() Returns the String representation of the current value. |
V |
updateAndGet(updateFunction: UnaryOperator<V>!) Atomically updates (with memory effects as specified by |
Boolean |
weakCompareAndSet(expectedValue: V, newValue: V) Possibly atomically sets the value to |
Boolean |
weakCompareAndSetAcquire(expectedValue: V, newValue: V) Possibly atomically sets the value to |
Boolean |
weakCompareAndSetPlain(expectedValue: V, newValue: V) Possibly atomically sets the value to |
Boolean |
weakCompareAndSetRelease(expectedValue: V, newValue: V) Possibly atomically sets the value to |
Boolean |
weakCompareAndSetVolatile(expectedValue: V, newValue: V) Possibly atomically sets the value to |
Public constructors
AtomicReference
AtomicReference()
Creates a new AtomicReference with null initial value.
AtomicReference
AtomicReference(initialValue: V)
Creates a new AtomicReference with the given initial value.
Parameters | |
---|---|
initialValue |
V: the initial value |
Public methods
accumulateAndGet
fun accumulateAndGet(
x: V,
accumulatorFunction: BinaryOperator<V>!
): V
Atomically updates (with memory effects as specified by java.lang.invoke.VarHandle#compareAndSet
) 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 |
V: the update value |
accumulatorFunction |
BinaryOperator<V>!: a side-effect-free function of two arguments |
Return | |
---|---|
V |
the updated value |
compareAndExchange
fun compareAndExchange(
expectedValue: V,
newValue: V
): V
Atomically sets the value to newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchange
.
Parameters | |
---|---|
expectedValue |
V: the expected value |
newValue |
V: the new value |
Return | |
---|---|
V |
the witness value, which will be the same as the expected value if successful |
compareAndExchangeAcquire
fun compareAndExchangeAcquire(
expectedValue: V,
newValue: V
): V
Atomically sets the value to newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchangeAcquire
.
Parameters | |
---|---|
expectedValue |
V: the expected value |
newValue |
V: the new value |
Return | |
---|---|
V |
the witness value, which will be the same as the expected value if successful |
compareAndExchangeRelease
fun compareAndExchangeRelease(
expectedValue: V,
newValue: V
): V
Atomically sets the value to newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchangeRelease
.
Parameters | |
---|---|
expectedValue |
V: the expected value |
newValue |
V: the new value |
Return | |
---|---|
V |
the witness value, which will be the same as the expected value if successful |
compareAndSet
fun compareAndSet(
expectedValue: V,
newValue: V
): Boolean
Atomically sets the value to newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.compareAndSet
.
Parameters | |
---|---|
expectedValue |
V: the expected value |
newValue |
V: the new value |
Return | |
---|---|
Boolean |
true if successful. False return indicates that the actual value was not equal to the expected value. |
get
fun get(): V
Returns the current value, with memory effects as specified by VarHandle.getVolatile
.
Return | |
---|---|
V |
the current value |
getAcquire
fun getAcquire(): V
Returns the current value, with memory effects as specified by VarHandle.getAcquire
.
Return | |
---|---|
V |
the value |
getAndAccumulate
fun getAndAccumulate(
x: V,
accumulatorFunction: BinaryOperator<V>!
): V
Atomically updates (with memory effects as specified by java.lang.invoke.VarHandle#compareAndSet
) the current value with the results of applying the given function to the current and given values, 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. The function is applied with the current value as its first argument, and the given update as the second argument.
Parameters | |
---|---|
x |
V: the update value |
accumulatorFunction |
BinaryOperator<V>!: a side-effect-free function of two arguments |
Return | |
---|---|
V |
the previous value |
getAndSet
fun getAndSet(newValue: V): V
Atomically sets the value to newValue
and returns the old value, with memory effects as specified by VarHandle.getAndSet
.
Parameters | |
---|---|
newValue |
V: the new value |
Return | |
---|---|
V |
the previous value |
getAndUpdate
fun getAndUpdate(updateFunction: UnaryOperator<V>!): V
Atomically updates (with memory effects as specified by java.lang.invoke.VarHandle#compareAndSet
) 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 |
UnaryOperator<V>!: a side-effect-free function |
Return | |
---|---|
V |
the previous value |
getOpaque
fun getOpaque(): V
Returns the current value, with memory effects as specified by VarHandle.getOpaque
.
Return | |
---|---|
V |
the value |
getPlain
fun getPlain(): V
Returns the current value, with memory semantics of reading as if the variable was declared non-volatile
.
Return | |
---|---|
V |
the value |
lazySet
fun lazySet(newValue: V): Unit
Sets the value to newValue
, with memory effects as specified by VarHandle.setRelease
.
Parameters | |
---|---|
newValue |
V: the new value |
set
fun set(newValue: V): Unit
Sets the value to newValue
, with memory effects as specified by VarHandle.setVolatile
.
Parameters | |
---|---|
newValue |
V: the new value |
setOpaque
fun setOpaque(newValue: V): Unit
Sets the value to newValue
, with memory effects as specified by VarHandle.setOpaque
.
Parameters | |
---|---|
newValue |
V: the new value |
setPlain
fun setPlain(newValue: V): Unit
Sets the value to newValue
, with memory semantics of setting as if the variable was declared non-volatile
and non-final
.
Parameters | |
---|---|
newValue |
V: the new value |
setRelease
fun setRelease(newValue: V): Unit
Sets the value to newValue
, with memory effects as specified by VarHandle.setRelease
.
Parameters | |
---|---|
newValue |
V: the new value |
toString
open fun toString(): String
Returns the String representation of the current value.
Return | |
---|---|
String |
the String representation of the current value |
updateAndGet
fun updateAndGet(updateFunction: UnaryOperator<V>!): V
Atomically updates (with memory effects as specified by java.lang.invoke.VarHandle#compareAndSet
) 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 |
UnaryOperator<V>!: a side-effect-free function |
Return | |
---|---|
V |
the updated value |
weakCompareAndSet
funweakCompareAndSet(
expectedValue: V,
newValue: V
): Boolean
Deprecated: This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange
and compareAndSet
). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain
be used instead.
Possibly atomically sets the value to newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetPlain
.
Parameters | |
---|---|
expectedValue |
V: the expected value |
newValue |
V: the new value |
Return | |
---|---|
Boolean |
true if successful |
See Also
weakCompareAndSetAcquire
fun weakCompareAndSetAcquire(
expectedValue: V,
newValue: V
): Boolean
Possibly atomically sets the value to newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetAcquire
.
Parameters | |
---|---|
expectedValue |
V: the expected value |
newValue |
V: the new value |
Return | |
---|---|
Boolean |
true if successful |
weakCompareAndSetPlain
fun weakCompareAndSetPlain(
expectedValue: V,
newValue: V
): Boolean
Possibly atomically sets the value to newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetPlain
.
Parameters | |
---|---|
expectedValue |
V: the expected value |
newValue |
V: the new value |
Return | |
---|---|
Boolean |
true if successful |
weakCompareAndSetRelease
fun weakCompareAndSetRelease(
expectedValue: V,
newValue: V
): Boolean
Possibly atomically sets the value to newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetRelease
.
Parameters | |
---|---|
expectedValue |
V: the expected value |
newValue |
V: the new value |
Return | |
---|---|
Boolean |
true if successful |
weakCompareAndSetVolatile
fun weakCompareAndSetVolatile(
expectedValue: V,
newValue: V
): Boolean
Possibly atomically sets the value to newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSet
.
Parameters | |
---|---|
expectedValue |
V: the expected value |
newValue |
V: the new value |
Return | |
---|---|
Boolean |
true if successful |