Added in API level 1

AtomicReference

public class AtomicReference
extends Object implements Serializable

java.lang.Object
   ↳ java.util.concurrent.atomic.AtomicReference<V>


An object reference that may be updated atomically. See the VarHandle specification for descriptions of the properties of atomic accesses.

Summary

Public constructors

AtomicReference(V initialValue)

Creates a new AtomicReference with the given initial value.

AtomicReference()

Creates a new AtomicReference with null initial value.

Public methods

final V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function to the current and given values, returning the updated value.

final V compareAndExchange(V expectedValue, V newValue)

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.

final V compareAndExchangeAcquire(V expectedValue, V newValue)

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.

final V compareAndExchangeRelease(V expectedValue, V newValue)

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.

final boolean compareAndSet(V expectedValue, V newValue)

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

final V get()

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

final V getAcquire()

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

final V getAndAccumulate(V x, BinaryOperator<V> accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function to the current and given values, returning the previous value.

final V getAndSet(V newValue)

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

final V getAndUpdate(UnaryOperator<V> updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function, returning the previous value.

final V getOpaque()

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

final V getPlain()

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

final void lazySet(V newValue)

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

final void set(V newValue)

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

final void setOpaque(V newValue)

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

final void setPlain(V newValue)

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

final void setRelease(V newValue)

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

String toString()

Returns the String representation of the current value.

final V updateAndGet(UnaryOperator<V> updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) the current value with the results of applying the given function, returning the updated value.

final boolean weakCompareAndSet(V expectedValue, V newValue)

This method was deprecated in API level 33. This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange(V, V) and compareAndSet(V, V)). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain(V, V) be used instead.

final boolean weakCompareAndSetAcquire(V expectedValue, V newValue)

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

final boolean weakCompareAndSetPlain(V expectedValue, V newValue)

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

final boolean weakCompareAndSetRelease(V expectedValue, V newValue)

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

final boolean weakCompareAndSetVolatile(V expectedValue, V newValue)

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

Inherited methods

Public constructors

AtomicReference

Added in API level 1
public AtomicReference (V initialValue)

Creates a new AtomicReference with the given initial value.

Parameters
initialValue V: the initial value

AtomicReference

Added in API level 1
public AtomicReference ()

Creates a new AtomicReference with null initial value.

Public methods

accumulateAndGet

Added in API level 24
public final V accumulateAndGet (V x, 
                BinaryOperator<V> accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) 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: a side-effect-free function of two arguments

Returns
V the updated value

compareAndExchange

Added in API level 33
public final V compareAndExchange (V expectedValue, 
                V newValue)

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

Returns
V the witness value, which will be the same as the expected value if successful

compareAndExchangeAcquire

Added in API level 33
public final V compareAndExchangeAcquire (V expectedValue, 
                V newValue)

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

Returns
V the witness value, which will be the same as the expected value if successful

compareAndExchangeRelease

Added in API level 33
public final V compareAndExchangeRelease (V expectedValue, 
                V newValue)

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

Returns
V the witness value, which will be the same as the expected value if successful

compareAndSet

Added in API level 1
public final boolean compareAndSet (V expectedValue, 
                V newValue)

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

Returns
boolean true if successful. False return indicates that the actual value was not equal to the expected value.

get

Added in API level 1
public final V get ()

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

Returns
V the current value

getAcquire

Added in API level 33
public final V getAcquire ()

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

Returns
V the value

getAndAccumulate

Added in API level 24
public final V getAndAccumulate (V x, 
                BinaryOperator<V> accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) 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: a side-effect-free function of two arguments

Returns
V the previous value

getAndSet

Added in API level 1
public final V getAndSet (V newValue)

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

Returns
V the previous value

getAndUpdate

Added in API level 24
public final V getAndUpdate (UnaryOperator<V> updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) 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: a side-effect-free function

Returns
V the previous value

getOpaque

Added in API level 33
public final V getOpaque ()

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

Returns
V the value

getPlain

Added in API level 33
public final V getPlain ()

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

Returns
V the value

lazySet

Added in API level 9
public final void lazySet (V newValue)

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
public final void set (V newValue)

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
public final void setOpaque (V newValue)

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
public final void setPlain (V newValue)

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
public final void setRelease (V newValue)

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
public String toString ()

Returns the String representation of the current value.

Returns
String the String representation of the current value

updateAndGet

Added in API level 24
public final V updateAndGet (UnaryOperator<V> updateFunction)

Atomically updates (with memory effects as specified by VarHandle.compareAndSet(Object)) 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: a side-effect-free function

Returns
V the updated value

weakCompareAndSet

Added in API level 1
Deprecated in API level 33
public final boolean weakCompareAndSet (V expectedValue, 
                V newValue)

This method was deprecated in API level 33.
This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange(V, V) and compareAndSet(V, V)). To avoid confusion over plain or volatile memory effects it is recommended that the method weakCompareAndSetPlain(V, V) 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

Returns
boolean true if successful

weakCompareAndSetAcquire

Added in API level 33
public final boolean weakCompareAndSetAcquire (V expectedValue, 
                V newValue)

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

Returns
boolean true if successful

weakCompareAndSetPlain

Added in API level 33
public final boolean weakCompareAndSetPlain (V expectedValue, 
                V newValue)

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

Returns
boolean true if successful

weakCompareAndSetRelease

Added in API level 33
public final boolean weakCompareAndSetRelease (V expectedValue, 
                V newValue)

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

Returns
boolean true if successful

weakCompareAndSetVolatile

Added in API level 33
public final boolean weakCompareAndSetVolatile (V expectedValue, 
                V newValue)

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

Returns
boolean true if successful