Added in API level 1

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
AtomicReference(initialValue: V)

Creates a new AtomicReference with the given initial value.

Creates a new AtomicReference with null initial value.

Public methods
V
accumulateAndGet(x: V, accumulatorFunction: BinaryOperator<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.

V
compareAndExchange(expectedValue: V, newValue: 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.

V
compareAndExchangeAcquire(expectedValue: V, newValue: 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.

V
compareAndExchangeRelease(expectedValue: V, newValue: 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.

Boolean
compareAndSet(expectedValue: V, newValue: V)

Atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#compareAndSet.

V
get()

Returns the current value, with memory effects as specified by VarHandle#getVolatile.

V

Returns the current value, with memory effects as specified by VarHandle#getAcquire.

V
getAndAccumulate(x: V, accumulatorFunction: BinaryOperator<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.

V
getAndSet(newValue: V)

Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle#getAndSet.

V
getAndUpdate(updateFunction: UnaryOperator<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.

V

Returns the current value, with memory effects as specified by VarHandle#getOpaque.

V

Returns the current value, with memory semantics of reading as if the variable was declared non-volatile.

Unit
lazySet(newValue: V)

Sets the value to newValue, with memory effects as specified by VarHandle#setRelease.

Unit
set(newValue: V)

Sets the value to newValue, with memory effects as specified by VarHandle#setVolatile.

Unit
setOpaque(newValue: V)

Sets the value to newValue, with memory effects as specified by VarHandle#setOpaque.

Unit
setPlain(newValue: V)

Sets the value to newValue, with memory semantics of setting as if the variable was declared non-volatile and non-final.

Unit
setRelease(newValue: V)

Sets the value to newValue, with memory effects as specified by VarHandle#setRelease.

open String

Returns the String representation of the current value.

V
updateAndGet(updateFunction: UnaryOperator<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.

Boolean
weakCompareAndSet(expectedValue: V, newValue: V)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

Boolean
weakCompareAndSetAcquire(expectedValue: V, newValue: V)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetAcquire.

Boolean
weakCompareAndSetPlain(expectedValue: V, newValue: V)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.

Boolean
weakCompareAndSetRelease(expectedValue: V, newValue: V)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetRelease.

Boolean
weakCompareAndSetVolatile(expectedValue: V, newValue: V)

Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSet.

Public constructors

AtomicReference

Added in API level 1
AtomicReference(initialValue: V)

Creates a new AtomicReference with the given initial value.

Parameters
initialValue V: the initial value

AtomicReference

Added in API level 1
AtomicReference()

Creates a new AtomicReference with null initial value.

Public methods

accumulateAndGet

Added in API level 24
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

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 1
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

Added in API level 1
fun get(): V

Returns the current value, with memory effects as specified by VarHandle#getVolatile.

Return
V the current value

getAcquire

Added in API level 33
fun getAcquire(): V

Returns the current value, with memory effects as specified by VarHandle#getAcquire.

Return
V the value

getAndAccumulate

Added in API level 24
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

Added in API level 1
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

Added in API level 24
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

Added in API level 33
fun getOpaque(): V

Returns the current value, with memory effects as specified by VarHandle#getOpaque.

Return
V the value

getPlain

Added in API level 33
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

Added in API level 9
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

Added in API level 1
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

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 1
open fun toString(): String

Returns the String representation of the current value.

Return
String the String representation of the current value

updateAndGet

Added in API level 24
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

Added in API level 1
Deprecated in API level 33
fun weakCompareAndSet(
    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

weakCompareAndSetAcquire

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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

Added in API level 33
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