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.
Modifier and Type | Class and Description |
---|---|
private static class |
Access | Constructor and Description |
---|---|
protected |
Modifier and Type | Method and Description |
---|---|
public final V | Returns: the updated valueAn 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 |
public abstract boolean | Returns: true if successfulAn 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 |
public abstract V | Returns: the current valueAn 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 valueAn 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 |
public V | Returns: the previous valueAn 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 valueAn object whose field to get and set obj, UnaryOperator<V> a side-effect-free function updateFunction)Atomically updates (with memory effects as specified by |
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 | Returns: the updaterthe 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 valueAn object whose field to get and set obj, UnaryOperator<V> a side-effect-free function updateFunction)Atomically updates (with memory effects as specified by |
public abstract boolean | Returns: true if successfulAn 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 |
AtomicReferenceFieldUpdater | back to summary |
---|---|
protected AtomicReferenceFieldUpdater() Protected do-nothing constructor for use by subclasses. |
accumulateAndGet | back to summary |
---|---|
public final V accumulateAndGet(T obj, V x, BinaryOperator<V> accumulatorFunction) Atomically updates (with memory effects as specified by
|
compareAndSet | back 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
|
get | back to summary |
---|---|
public abstract V get(T obj) Returns the current value held in the field of the given object managed by this updater.
|
getAndAccumulate | back to summary |
---|---|
public final V getAndAccumulate(T obj, V x, BinaryOperator<V> accumulatorFunction) Atomically updates (with memory effects as specified by
|
getAndSet | back 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.
|
getAndUpdate | back to summary |
---|---|
public final V getAndUpdate(T obj, UnaryOperator<V> updateFunction) Atomically updates (with memory effects as specified by
|
lazySet | back 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.
|
newUpdater | back to summary |
---|---|
public static <U, W> AtomicReferenceFieldUpdater 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.
|
set | back 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
|
updateAndGet | back to summary |
---|---|
public final V updateAndGet(T obj, UnaryOperator<V> updateFunction) Atomically updates (with memory effects as specified by
|
weakCompareAndSet | back 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 This operation may fail spuriously and does not provide
ordering guarantees, so is only rarely an appropriate
alternative to
|
Modifier and Type | Field 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 | tclass
class holding the field |
private static final Unsafe | |
private final Class | vclass
field value type |
Access | Constructor and Description |
---|---|
pack-priv | AtomicReferenceFieldUpdaterImpl(final Class<T> tclass, final Class<V> vclass, final String fieldName, final Class<?> caller)
|
Modifier and Type | Method and Description |
---|---|
private final void | |
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. Atomically sets the field of the given object managed by this updater
to the given updated value if the current value |
public final V | get(T
An object whose field to get obj)Implements abstract java. 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. 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. 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. 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. Atomically sets the field of the given object managed by this updater
to the given updated value if the current value |
cclass | back to summary |
---|---|
private final Class<?> cclass if field is protected, the subclass constructing updater, else the same as tclass |
offset | back to summary |
---|---|
private final long offset |
tclass | back to summary |
---|---|
private final Class<T> tclass class holding the field |
U | back to summary |
---|---|
private static final Unsafe U |
vclass | back to summary |
---|---|
private final Class<V> vclass field value type |
AtomicReferenceFieldUpdaterImpl | back to summary |
---|---|
pack-priv AtomicReferenceFieldUpdaterImpl(final Class<T> tclass, final Class<V> vclass, final String fieldName, final Class<?> caller)
|
accessCheck | back to summary |
---|---|
private final void accessCheck(T obj) Checks that target argument is instance of cclass. On failure, throws cause. |
compareAndSet | back to summary |
---|---|
public final boolean compareAndSet(T obj, V expect, V update) Implements abstract java. Doc from java. Atomically sets the field of the given object managed by this updater
to the given updated value if the current value
|
get | back to summary |
---|---|
public final V get(T obj) Implements abstract java. Doc from java. Returns the current value held in the field of the given object managed by this updater.
|
getAndSet | back to summary |
---|---|
public final V getAndSet(T obj, V newValue) Overrides java. Doc from java. Atomically sets the field of the given object managed by this updater to the given value and returns the old value.
|
isAncestor | back 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). |
isSamePackage | back to summary |
---|---|
private static boolean isSamePackage(Class<?> class1, Class<?> class2) Returns true if the two classes have the same class loader and package qualifier |
lazySet | back to summary |
---|---|
public final void lazySet(T obj, V newValue) Implements abstract java. Doc from java. Eventually sets the field of the given object managed by this updater to the given updated value.
|
set | back to summary |
---|---|
public final void set(T obj, V newValue) Implements abstract java. Doc from java. 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
|
throwAccessCheckException | back to summary |
---|---|
private final void throwAccessCheckException(T obj) Throws access exception if accessCheck failed due to protected access, else ClassCastException. |
throwCCE | back to summary |
---|---|
pack-priv static void throwCCE() |
valueCheck | back to summary |
---|---|
private final void valueCheck(V v) |
weakCompareAndSet | back to summary |
---|---|
public final boolean weakCompareAndSet(T obj, V expect, V update) Implements abstract java. Doc from java. Atomically sets the field of the given object managed by this updater
to the given updated value if the current value This operation may fail spuriously and does not provide
ordering guarantees, so is only rarely an appropriate
alternative to
|