V
- The type of object referred to by this referenceSerializable
public class AtomicReference<V> extends Object implements Serializable
VarHandle
specification for descriptions of the properties of atomic accesses.Constructor | Description |
---|---|
AtomicReference() |
Creates a new AtomicReference with null initial value. |
AtomicReference |
Creates a new AtomicReference with the given initial value. |
Modifier and Type | Method | Description |
---|---|---|
final V |
accumulateAndGet |
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.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 |
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(java.lang.Object...) . |
final V |
compareAndExchangeAcquire |
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(java.lang.Object...) . |
final V |
compareAndExchangeRelease |
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(java.lang.Object...) . |
final boolean |
compareAndSet |
Atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...) . |
final V |
get() |
Returns the current value, with memory effects as specified by VarHandle.getVolatile(java.lang.Object...) . |
final V |
getAcquire() |
Returns the current value, with memory effects as specified by VarHandle.getAcquire(java.lang.Object...) . |
final V |
getAndAccumulate |
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.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 |
Atomically sets the value to newValue and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...) . |
final V |
getAndUpdate |
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.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(java.lang.Object...) . |
final V |
getPlain() |
Returns the current value, with memory semantics of reading as if the variable was declared non- volatile . |
final void |
lazySet |
Sets the value to newValue , with memory effects as specified by VarHandle.setRelease(java.lang.Object...) . |
final void |
set |
Sets the value to newValue , with memory effects as specified by VarHandle.setVolatile(java.lang.Object...) . |
final void |
setOpaque |
Sets the value to newValue , with memory effects as specified by VarHandle.setOpaque(java.lang.Object...) . |
final void |
setPlain |
Sets the value to newValue , with memory semantics of setting as if the variable was declared non-volatile and non-final . |
final void |
setRelease |
Sets the value to newValue , with memory effects as specified by VarHandle.setRelease(java.lang.Object...) . |
String |
toString() |
Returns the String representation of the current value. |
final V |
updateAndGet |
Atomically updates (with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...) ) the current value with the results of applying the given function, returning the updated value. |
final boolean |
weakCompareAndSet |
Deprecated. |
final boolean |
weakCompareAndSetAcquire |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object...) . |
final boolean |
weakCompareAndSetPlain |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...) . |
final boolean |
weakCompareAndSetRelease |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object...) . |
final boolean |
weakCompareAndSetVolatile |
Possibly atomically sets the value to newValue if the current value == expectedValue , with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object...) . |
public AtomicReference(V initialValue)
initialValue
- the initial valuepublic AtomicReference()
public final V get()
VarHandle.getVolatile(java.lang.Object...)
.public final void set(V newValue)
newValue
, with memory effects as specified by VarHandle.setVolatile(java.lang.Object...)
.newValue
- the new valuepublic final void lazySet(V newValue)
newValue
, with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final boolean compareAndSet(V expectedValue, V newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.compareAndSet(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successful. False return indicates that the actual value was not equal to the expected value.@Deprecated(since="9") public final boolean weakCompareAndSet(V expectedValue, V newValue)
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.newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final boolean weakCompareAndSetPlain(V expectedValue, V newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetPlain(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final V getAndSet(V newValue)
newValue
and returns the old value, with memory effects as specified by VarHandle.getAndSet(java.lang.Object...)
.newValue
- the new valuepublic final V getAndUpdate(UnaryOperator<V> updateFunction)
VarHandle.compareAndSet(java.lang.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.updateFunction
- a side-effect-free functionpublic final V updateAndGet(UnaryOperator<V> updateFunction)
VarHandle.compareAndSet(java.lang.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.updateFunction
- a side-effect-free functionpublic final V getAndAccumulate(V x, BinaryOperator<V> accumulatorFunction)
VarHandle.compareAndSet(java.lang.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.x
- the update valueaccumulatorFunction
- a side-effect-free function of two argumentspublic final V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction)
VarHandle.compareAndSet(java.lang.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.x
- the update valueaccumulatorFunction
- a side-effect-free function of two argumentspublic String toString()
public final V getPlain()
volatile
.public final void setPlain(V newValue)
newValue
, with memory semantics of setting as if the variable was declared non-volatile
and non-final
.newValue
- the new valuepublic final V getOpaque()
VarHandle.getOpaque(java.lang.Object...)
.public final void setOpaque(V newValue)
newValue
, with memory effects as specified by VarHandle.setOpaque(java.lang.Object...)
.newValue
- the new valuepublic final V getAcquire()
VarHandle.getAcquire(java.lang.Object...)
.public final void setRelease(V newValue)
newValue
, with memory effects as specified by VarHandle.setRelease(java.lang.Object...)
.newValue
- the new valuepublic final V compareAndExchange(V expectedValue, V newValue)
newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchange(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuepublic final V compareAndExchangeAcquire(V expectedValue, V newValue)
newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchangeAcquire(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuepublic final V compareAndExchangeRelease(V expectedValue, V newValue)
newValue
if the current value, referred to as the witness value, == expectedValue
, with memory effects as specified by VarHandle.compareAndExchangeRelease(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuepublic final boolean weakCompareAndSetVolatile(V expectedValue, V newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSet(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final boolean weakCompareAndSetAcquire(V expectedValue, V newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetAcquire(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successfulpublic final boolean weakCompareAndSetRelease(V expectedValue, V newValue)
newValue
if the current value == expectedValue
, with memory effects as specified by VarHandle.weakCompareAndSetRelease(java.lang.Object...)
.expectedValue
- the expected valuenewValue
- the new valuetrue
if successful
© 1993, 2023, Oracle and/or its affiliates. All rights reserved.
Documentation extracted from Debian's OpenJDK Development Kit package.
Licensed under the GNU General Public License, version 2, with the Classpath Exception.
Various third party code in OpenJDK is licensed under different licenses (see Debian package).
Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/util/concurrent/atomic/AtomicReference.html
compareAndExchange(V, V)
andcompareAndSet(V, V)
).