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

public abstract Class AtomicReferenceFieldUpdater<T, V>

extends Object
Class Inheritance
Known Direct Subclasses
java.util.concurrent.atomic.AtomicReferenceFieldUpdater.AtomicReferenceFieldUpdaterImpl
Type Parameters
<T>
The type of the object holding the updatable field
<V>
The type of the field
Imports
java.lang.reflect.Field, .Modifier, java.security.AccessController, .PrivilegedActionException, .PrivilegedExceptionAction, java.util.function.BinaryOperator, .UnaryOperator, jdk.internal.misc.Unsafe, jdk.internal.reflect.CallerSensitive, .Reflection, java.lang.invoke.VarHandle

A reflection-based utility that enables atomic updates to designated volatile reference fields of designated classes. This class is designed for use in atomic data structures in which several reference fields of the same node are independently subject to atomic updates. For example, a tree node might be declared as
 class Node {
  private volatile Node left, right;

  private static final AtomicReferenceFieldUpdater<Node, Node> leftUpdater =
    AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "left");
  private static final AtomicReferenceFieldUpdater<Node, Node> rightUpdater =
    AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "right");

  Node getLeft() { return left; }
  boolean compareAndSetLeft(Node expect, Node update) {
    return leftUpdater.compareAndSet(this, expect, update);
  }
  // ... and so on
}

Note that the guarantees of the compareAndSet method in this class are weaker than in other atomic classes. Because this class cannot ensure that all uses of the field are appropriate for purposes of atomic access, it can guarantee atomicity only with respect to other invocations of compareAndSet and set on the same updater.

Object arguments for parameters of type T that are not instances of the class passed to newUpdater will result in a ClassCastException being thrown.

Author
Doug Lea
Since
1.5

Nested and Inner Type Summary

Modifier and TypeClass and Description
private static class

Constructor Summary

AccessConstructor and Description
protected
AtomicReferenceFieldUpdater()

Protected do-nothing constructor for use by subclasses.

Method Summary

Modifier and TypeMethod and Description
public final V

Returns:

the updated value
accumulateAndGet
(T
An object whose field to get and set
obj
,
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 field of the given object managed by this updater with the results of applying the given function to the current and given values, returning the updated value.

public abstract boolean

Returns:

true if successful
compareAndSet
(T
An object whose field to conditionally set
obj
,
V
the expected value
expect
,
V
the new value
update
)

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value.

public abstract V

Returns:

the current value
get
(T
An object whose field to get
obj
)

Returns the current value held in the field of the given object managed by this updater.

public final V

Returns:

the previous value
getAndAccumulate
(T
An object whose field to get and set
obj
,
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 field of the given object managed by this updater with the results of applying the given function to the current and given values, returning the previous value.

public V

Returns:

the previous value
getAndSet
(T
An object whose field to get and set
obj
,
V
the new value
newValue
)

Atomically sets the field of the given object managed by this updater to the given value and returns the old value.

public final V

Returns:

the previous value
getAndUpdate
(T
An object whose field to get and set
obj
,
UnaryOperator<V>
a side-effect-free function
updateFunction
)

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

public abstract void
lazySet(T
An object whose field to set
obj
,
V
the new value
newValue
)

Eventually sets the field of the given object managed by this updater to the given updated value.

public static <
the type of instances of tclass
U
,
the type of instances of vclass
W
>
AtomicReferenceFieldUpdater<U, W>

Returns:

the updater
newUpdater
(Class<U>
the class of the objects holding the field
tclass
,
Class<W>
the class of the field
vclass
,
String
the name of the field to be updated
fieldName
)

Creates and returns an updater for objects with the given field.

public abstract void
set(T
An object whose field to set
obj
,
V
the new value
newValue
)

Sets the field of the given object managed by this updater to the given updated value.

public final V

Returns:

the updated value
updateAndGet
(T
An object whose field to get and set
obj
,
UnaryOperator<V>
a side-effect-free function
updateFunction
)

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

public abstract boolean

Returns:

true if successful
weakCompareAndSet
(T
An object whose field to conditionally set
obj
,
V
the expected value
expect
,
V
the new value
update
)

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Constructor Detail

AtomicReferenceFieldUpdaterback to summary
protected AtomicReferenceFieldUpdater()

Protected do-nothing constructor for use by subclasses.

Method Detail

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

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater 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
obj:T

An object whose field to get and set

x:V

the update value

accumulatorFunction:BinaryOperator<V>

a side-effect-free function of two arguments

Returns:V

the updated value

Since
1.8
compareAndSetback to summary
public abstract boolean compareAndSet(T obj, V expect, V update)

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value. This method is guaranteed to be atomic with respect to other calls to compareAndSet and set, but not necessarily with respect to other changes in the field.

Parameters
obj:T

An object whose field to conditionally set

expect:V

the expected value

update:V

the new value

Returns:boolean

true if successful

getback to summary
public abstract V get(T obj)

Returns the current value held in the field of the given object managed by this updater.

Parameters
obj:T

An object whose field to get

Returns:V

the current value

getAndAccumulateback to summary
public final V getAndAccumulate(T obj, V x, BinaryOperator<V> accumulatorFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater 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
obj:T

An object whose field to get and set

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 V getAndSet(T obj, V newValue)

Atomically sets the field of the given object managed by this updater to the given value and returns the old value.

Parameters
obj:T

An object whose field to get and set

newValue:V

the new value

Returns:V

the previous value

getAndUpdateback to summary
public final V getAndUpdate(T obj, UnaryOperator<V> updateFunction)

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater 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
obj:T

An object whose field to get and set

updateFunction:UnaryOperator<V>

a side-effect-free function

Returns:V

the previous value

Since
1.8
lazySetback to summary
public abstract void lazySet(T obj, V newValue)

Eventually sets the field of the given object managed by this updater to the given updated value.

Parameters
obj:T

An object whose field to set

newValue:V

the new value

Since
1.6
newUpdaterback to summary
public static <U, W> AtomicReferenceFieldUpdater<U, W> newUpdater(Class<U> tclass, Class<W> vclass, String fieldName)

Creates and returns an updater for objects with the given field. The Class arguments are needed to check that reflective types and generic types match.

Parameters
<U>
the type of instances of tclass
<W>
the type of instances of vclass
tclass:Class<U>

the class of the objects holding the field

vclass:Class<W>

the class of the field

fieldName:String

the name of the field to be updated

Returns:AtomicReferenceFieldUpdater<U, W>

the updater

Annotations
@CallerSensitive
Exceptions
ClassCastException:
if the field is of the wrong type
IllegalArgumentException:
if the field is not volatile
RuntimeException:
with a nested reflection-based exception if the class does not hold field or is the wrong type, or the field is inaccessible to the caller according to Java language access control
setback to summary
public abstract void set(T obj, V newValue)

Sets the field of the given object managed by this updater to the given updated value. This operation is guaranteed to act as a volatile store with respect to subsequent invocations of compareAndSet.

Parameters
obj:T

An object whose field to set

newValue:V

the new value

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

Atomically updates (with memory effects as specified by VarHandle#compareAndSet) the field of the given object managed by this updater 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
obj:T

An object whose field to get and set

updateFunction:UnaryOperator<V>

a side-effect-free function

Returns:V

the updated value

Since
1.8
weakCompareAndSetback to summary
public abstract boolean weakCompareAndSet(T obj, V expect, V update)

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value. This method is guaranteed to be atomic with respect to other calls to compareAndSet and set, but not necessarily with respect to other changes in the field.

This operation may fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.

Parameters
obj:T

An object whose field to conditionally set

expect:V

the expected value

update:V

the new value

Returns:boolean

true if successful

java.util.concurrent.atomic back to summary

private final Class AtomicReferenceFieldUpdater.AtomicReferenceFieldUpdaterImpl<T, V>

extends AtomicReferenceFieldUpdater<T, V>
Class Inheritance

Field Summary

Modifier and TypeField and Description
private final Class<?>
cclass

if field is protected, the subclass constructing updater, else the same as tclass

private final long
private final Class<T>
tclass

class holding the field

private static final Unsafe
U

private final Class<V>
vclass

field value type

Constructor Summary

AccessConstructor and Description
pack-priv
AtomicReferenceFieldUpdaterImpl(final Class<T> tclass, final Class<V> vclass, final String fieldName, final Class<?> caller)

Method Summary

Modifier and TypeMethod and Description
private final void
accessCheck(T obj)

Checks that target argument is instance of cclass.

public final boolean
compareAndSet(T
An object whose field to conditionally set
obj
,
V
the expected value
expect
,
V
the new value
update
)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.compareAndSet.

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value.

public final V
get(T
An object whose field to get
obj
)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.get.

Returns the current value held in the field of the given object managed by this updater.

public final V
getAndSet(T
An object whose field to get and set
obj
,
V
the new value
newValue
)

Overrides java.util.concurrent.atomic.AtomicReferenceFieldUpdater.getAndSet.

Atomically sets the field of the given object managed by this updater to the given value and returns the old value.

private static boolean
isAncestor(ClassLoader first, ClassLoader second)

Returns true if the second classloader can be found in the first classloader's delegation chain.

private static boolean
isSamePackage(Class<?> class1, Class<?> class2)

Returns true if the two classes have the same class loader and package qualifier

public final void
lazySet(T
An object whose field to set
obj
,
V
the new value
newValue
)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.lazySet.

Eventually sets the field of the given object managed by this updater to the given updated value.

public final void
set(T
An object whose field to set
obj
,
V
the new value
newValue
)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.set.

Sets the field of the given object managed by this updater to the given updated value.

private final void
throwAccessCheckException(T obj)

Throws access exception if accessCheck failed due to protected access, else ClassCastException.

pack-priv static void
private final void
public final boolean
weakCompareAndSet(T
An object whose field to conditionally set
obj
,
V
the expected value
expect
,
V
the new value
update
)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.weakCompareAndSet.

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value.

Inherited from java.util.concurrent.atomic.AtomicReferenceFieldUpdater:
accumulateAndGetgetAndAccumulategetAndUpdatenewUpdaterupdateAndGet

Field Detail

cclassback to summary
private final Class<?> cclass

if field is protected, the subclass constructing updater, else the same as tclass

offsetback to summary
private final long offset
tclassback to summary
private final Class<T> tclass

class holding the field

Uback to summary
private static final Unsafe U
vclassback to summary
private final Class<V> vclass

field value type

Constructor Detail

AtomicReferenceFieldUpdaterImplback to summary
pack-priv AtomicReferenceFieldUpdaterImpl(final Class<T> tclass, final Class<V> vclass, final String fieldName, final Class<?> caller)
Annotations
@SuppressWarnings:removal

Method Detail

accessCheckback to summary
private final void accessCheck(T obj)

Checks that target argument is instance of cclass. On failure, throws cause.

compareAndSetback to summary
public final boolean compareAndSet(T obj, V expect, V update)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.compareAndSet.

Doc from java.util.concurrent.atomic.AtomicReferenceFieldUpdater.compareAndSet.

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value. This method is guaranteed to be atomic with respect to other calls to compareAndSet and set, but not necessarily with respect to other changes in the field.

Parameters
obj:T

An object whose field to conditionally set

expect:V

the expected value

update:V

the new value

Returns:boolean

true if successful

getback to summary
public final V get(T obj)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.get.

Doc from java.util.concurrent.atomic.AtomicReferenceFieldUpdater.get.

Returns the current value held in the field of the given object managed by this updater.

Parameters
obj:T

An object whose field to get

Returns:V

the current value

Annotations
@SuppressWarnings:unchecked
getAndSetback to summary
public final V getAndSet(T obj, V newValue)

Overrides java.util.concurrent.atomic.AtomicReferenceFieldUpdater.getAndSet.

Doc from java.util.concurrent.atomic.AtomicReferenceFieldUpdater.getAndSet.

Atomically sets the field of the given object managed by this updater to the given value and returns the old value.

Parameters
obj:T

An object whose field to get and set

newValue:V

the new value

Returns:V

the previous value

Annotations
@SuppressWarnings:unchecked
isAncestorback to summary
private static boolean isAncestor(ClassLoader first, ClassLoader second)

Returns true if the second classloader can be found in the first classloader's delegation chain. Equivalent to the inaccessible: first.isAncestor(second).

isSamePackageback to summary
private static boolean isSamePackage(Class<?> class1, Class<?> class2)

Returns true if the two classes have the same class loader and package qualifier

lazySetback to summary
public final void lazySet(T obj, V newValue)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.lazySet.

Doc from java.util.concurrent.atomic.AtomicReferenceFieldUpdater.lazySet.

Eventually sets the field of the given object managed by this updater to the given updated value.

Parameters
obj:T

An object whose field to set

newValue:V

the new value

setback to summary
public final void set(T obj, V newValue)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.set.

Doc from java.util.concurrent.atomic.AtomicReferenceFieldUpdater.set.

Sets the field of the given object managed by this updater to the given updated value. This operation is guaranteed to act as a volatile store with respect to subsequent invocations of compareAndSet.

Parameters
obj:T

An object whose field to set

newValue:V

the new value

throwAccessCheckExceptionback to summary
private final void throwAccessCheckException(T obj)

Throws access exception if accessCheck failed due to protected access, else ClassCastException.

throwCCEback to summary
pack-priv static void throwCCE()
valueCheckback to summary
private final void valueCheck(V v)
weakCompareAndSetback to summary
public final boolean weakCompareAndSet(T obj, V expect, V update)

Implements abstract java.util.concurrent.atomic.AtomicReferenceFieldUpdater.weakCompareAndSet.

Doc from java.util.concurrent.atomic.AtomicReferenceFieldUpdater.weakCompareAndSet.

Atomically sets the field of the given object managed by this updater to the given updated value if the current value == the expected value. This method is guaranteed to be atomic with respect to other calls to compareAndSet and set, but not necessarily with respect to other changes in the field.

This operation may fail spuriously and does not provide ordering guarantees, so is only rarely an appropriate alternative to compareAndSet.

Parameters
obj:T

An object whose field to conditionally set

expect:V

the expected value

update:V

the new value

Returns:boolean

true if successful