Top Description Fields Constructors Methods
java.util.concurrent.atomic

public Class AtomicReference<V>

extends Object
implements Serializable
Class Inheritance
All Implemented Interfaces
java.io.Serializable
Type Parameters
<V>
The type of object referred to by this reference
Imports
java.lang.invoke.MethodHandles, .VarHandle, java.util.function.BinaryOperator, .UnaryOperator

An object reference that may be updated atomically. See the VarHandle specification for descriptions of the properties of atomic accesses.
Author
Doug Lea
Since
1.5

Field Summary

Modifier and TypeField and Description
private static final long
private static final VarHandle
private volatile V

Constructor Summary

AccessConstructor and Description
public
AtomicReference(V
the initial value
initialValue
)

Creates a new AtomicReference with the given initial value.

public
AtomicReference()

Creates a new AtomicReference with null initial value.

Method Summary

Modifier and TypeMethod and Description
public final V

Returns:

the updated value
accumulateAndGet
(V
the update value
x
,
BinaryOperator<V>
a side-effect-free function of two arguments
accumulatorFunction
)

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

public final V

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchange
(V
the expected value
expectedValue
,
V
the new value
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.

public final V

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeAcquire
(V
the expected value
expectedValue
,
V
the new value
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.

public final V

Returns:

the witness value, which will be the same as the expected value if successful
compareAndExchangeRelease
(V
the expected value
expectedValue
,
V
the new value
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.

public final boolean

Returns:

true if successful. False return indicates that the actual value was not equal to the expected value.
compareAndSet
(V
the expected value
expectedValue
,
V
the new value
newValue
)

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

public final V

Returns:

the current value
get
()

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

public final V

Returns:

the value
getAcquire
()

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

public final V

Returns:

the previous value
getAndAccumulate
(V
the update value
x
,
BinaryOperator<V>
a side-effect-free function of two arguments
accumulatorFunction
)

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

public final V

Returns:

the previous value
getAndSet
(V
the new value
newValue
)

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

public final V

Returns:

the previous value
getAndUpdate
(UnaryOperator<V>
a side-effect-free function
updateFunction
)

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

public final V

Returns:

the value
getOpaque
()

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

public final V

Returns:

the value
getPlain
()

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

public final void
lazySet(V
the new value
newValue
)

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

public final void
set(V
the new value
newValue
)

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

public final void
setOpaque(V
the new value
newValue
)

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

public final void
setPlain(V
the new value
newValue
)

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

public final void
setRelease(V
the new value
newValue
)

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

public String

Returns:

the String representation of the current value
toString
()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.
public final V

Returns:

the updated value
updateAndGet
(UnaryOperator<V>
a side-effect-free function
updateFunction
)

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

public final boolean

Returns:

true if successful
weakCompareAndSet
(V
the expected value
expectedValue
,
V
the new value
newValue
)

Deprecated since 9. This method has plain memory effects but the method name implies volatile memory effects (see methods such as compareAndExchange and compareAndSet).
Possibly atomically sets the value to newValue if the current value == expectedValue, with memory effects as specified by VarHandle#weakCompareAndSetPlain.
public final boolean

Returns:

true if successful
weakCompareAndSetAcquire
(V
the expected value
expectedValue
,
V
the new value
newValue
)

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

public final boolean

Returns:

true if successful
weakCompareAndSetPlain
(V
the expected value
expectedValue
,
V
the new value
newValue
)

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

public final boolean

Returns:

true if successful
weakCompareAndSetRelease
(V
the expected value
expectedValue
,
V
the new value
newValue
)

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

public final boolean

Returns:

true if successful
weakCompareAndSetVolatile
(V
the expected value
expectedValue
,
V
the new value
newValue
)

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

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAllwaitwaitwait

Field Detail

serialVersionUIDback to summary
private static final long serialVersionUID
VALUEback to summary
private static final VarHandle VALUE
valueback to summary
private volatile V value
Annotations
@SuppressWarnings:serial

Constructor Detail

AtomicReferenceback to summary
public AtomicReference(V initialValue)

Creates a new AtomicReference with the given initial value.

Parameters
initialValue:V

the initial value

AtomicReferenceback to summary
public AtomicReference()

Creates a new AtomicReference with null initial value.

Method Detail

accumulateAndGetback to summary
public final V accumulateAndGet(V x, BinaryOperator<V> accumulatorFunction)

Atomically updates (with memory effects as specified by 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

Returns:V

the updated value

Since
1.8
compareAndExchangeback to summary
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

Since
9
compareAndExchangeAcquireback to summary
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

Since
9
compareAndExchangeReleaseback to summary
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

Since
9
compareAndSetback to summary
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.

getback to summary
public final V get()

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

Returns:V

the current value

getAcquireback to summary
public final V getAcquire()

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

Returns:V

the value

Since
9
getAndAccumulateback to summary
public final V getAndAccumulate(V x, BinaryOperator<V> accumulatorFunction)

Atomically updates (with memory effects as specified by 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

Returns:V

the previous value

Since
1.8
getAndSetback to summary
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

Annotations
@SuppressWarnings:unchecked
getAndUpdateback to summary
public final V getAndUpdate(UnaryOperator<V> updateFunction)

Atomically updates (with memory effects as specified by 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

Returns:V

the previous value

Since
1.8
getOpaqueback to summary
public final V getOpaque()

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

Returns:V

the value

Since
9
getPlainback to summary
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

Since
9
lazySetback to summary
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

Since
1.6
setback to summary
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

setOpaqueback to summary
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

Since
9
setPlainback to summary
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

Since
9
setReleaseback to summary
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

Since
9
toStringback to summary
public String toString()

Overrides java.lang.Object.toString.

Returns the String representation of the current value.

Returns:String

the String representation of the current value

updateAndGetback to summary
public final V updateAndGet(UnaryOperator<V> updateFunction)

Atomically updates (with memory effects as specified by 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

Returns:V

the updated value

Since
1.8
weakCompareAndSetback to summary
public final boolean weakCompareAndSet(V expectedValue, V newValue)

Deprecated

since 9.

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

Returns:boolean

true if successful

Annotations
@Deprecated
since:9
See Also
weakCompareAndSetPlain
weakCompareAndSetAcquireback to summary
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

Since
9
weakCompareAndSetPlainback to summary
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

Since
9
weakCompareAndSetReleaseback to summary
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

Since
9
weakCompareAndSetVolatileback to summary
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

Since
9