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()
Creates a new AtomicReference with null initial value. |
|
AtomicReference(V initialValue)
Creates a new AtomicReference with the given initial value. |
Public methods | |
---|---|
final
V
|
accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction)
Atomically updates (with memory effects as specified by |
final
V
|
compareAndExchange(V expectedValue, V newValue)
Atomically sets the value to |
final
V
|
compareAndExchangeAcquire(V expectedValue, V newValue)
Atomically sets the value to |
final
V
|
compareAndExchangeRelease(V expectedValue, V newValue)
Atomically sets the value to |
final
boolean
|
compareAndSet(V expectedValue, V newValue)
Atomically sets the value to |
final
V
|
get()
Returns the current value,
with memory effects as specified by |
final
V
|
getAcquire()
Returns the current value,
with memory effects as specified by |
final
V
|
getAndAccumulate(V x, BinaryOperator<V> accumulatorFunction)
Atomically updates (with memory effects as specified by |
final
V
|
getAndSet(V newValue)
Atomically sets the value to |
final
V
|
getAndUpdate(UnaryOperator<V> updateFunction)
Atomically updates (with memory effects as specified by |
final
V
|
getOpaque()
Returns the current value,
with memory effects as specified by |
final
V
|
getPlain()
Returns the current value, with memory semantics of reading as
if the variable was declared non- |
final
void
|
lazySet(V newValue)
Sets the value to |
final
void
|
set(V newValue)
Sets the value to |
final
void
|
setOpaque(V newValue)
Sets the value to |
final
void
|
setPlain(V newValue)
Sets the value to |
final
void
|
setRelease(V newValue)
Sets the value to |
String
|
toString()
Returns the String representation of the current value. |
final
V
|
updateAndGet(UnaryOperator<V> updateFunction)
Atomically updates (with memory effects as specified by |
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
|
final
boolean
|
weakCompareAndSetAcquire(V expectedValue, V newValue)
Possibly atomically sets the value to |
final
boolean
|
weakCompareAndSetPlain(V expectedValue, V newValue)
Possibly atomically sets the value to |
final
boolean
|
weakCompareAndSetRelease(V expectedValue, V newValue)
Possibly atomically sets the value to |
final
boolean
|
weakCompareAndSetVolatile(V expectedValue, V newValue)
Possibly atomically sets the value to |
Inherited methods | |
---|---|
Public constructors
AtomicReference
public AtomicReference ()
Creates a new AtomicReference with null initial value.
AtomicReference
public AtomicReference (V initialValue)
Creates a new AtomicReference with the given initial value.
Parameters | |
---|---|
initialValue |
V : the initial value |
Public methods
accumulateAndGet
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
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
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
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
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
public final V get ()
Returns the current value,
with memory effects as specified by VarHandle.getVolatile
.
Returns | |
---|---|
V |
the current value |
getAcquire
public final V getAcquire ()
Returns the current value,
with memory effects as specified by VarHandle.getAcquire
.
Returns | |
---|---|
V |
the value |
getAndAccumulate
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
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
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
public final V getOpaque ()
Returns the current value,
with memory effects as specified by VarHandle.getOpaque
.
Returns | |
---|---|
V |
the value |
getPlain
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
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
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
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
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
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
public String toString ()
Returns the String representation of the current value.
Returns | |
---|---|
String |
the String representation of the current value |
updateAndGet
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
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 |
See also:
weakCompareAndSetAcquire
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
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
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
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 |