< prev index next >

src/java.base/share/classes/java/util/concurrent/atomic/AtomicReferenceFieldUpdater.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2021-01
Reviewed-by: martin


  45 import java.util.function.UnaryOperator;
  46 import jdk.internal.misc.Unsafe;
  47 import jdk.internal.reflect.CallerSensitive;
  48 import jdk.internal.reflect.Reflection;
  49 import java.lang.invoke.VarHandle;
  50 
  51 /**
  52  * A reflection-based utility that enables atomic updates to
  53  * designated {@code volatile} reference fields of designated
  54  * classes.  This class is designed for use in atomic data structures
  55  * in which several reference fields of the same node are
  56  * independently subject to atomic updates. For example, a tree node
  57  * might be declared as
  58  *
  59  * <pre> {@code
  60  * class Node {
  61  *   private volatile Node left, right;
  62  *
  63  *   private static final AtomicReferenceFieldUpdater<Node, Node> leftUpdater =
  64  *     AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "left");
  65  *   private static AtomicReferenceFieldUpdater<Node, Node> rightUpdater =
  66  *     AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "right");
  67  *
  68  *   Node getLeft() { return left; }
  69  *   boolean compareAndSetLeft(Node expect, Node update) {
  70  *     return leftUpdater.compareAndSet(this, expect, update);
  71  *   }
  72  *   // ... and so on
  73  * }}</pre>
  74  *
  75  * <p>Note that the guarantees of the {@code compareAndSet}
  76  * method in this class are weaker than in other atomic classes.
  77  * Because this class cannot ensure that all uses of the field
  78  * are appropriate for purposes of atomic access, it can
  79  * guarantee atomicity only with respect to other invocations of
  80  * {@code compareAndSet} and {@code set} on the same updater.
  81  *
  82  * <p>Object arguments for parameters of type {@code T} that are not
  83  * instances of the class passed to {@link #newUpdater} will result in
  84  * a {@link ClassCastException} being thrown.
  85  *




  45 import java.util.function.UnaryOperator;
  46 import jdk.internal.misc.Unsafe;
  47 import jdk.internal.reflect.CallerSensitive;
  48 import jdk.internal.reflect.Reflection;
  49 import java.lang.invoke.VarHandle;
  50 
  51 /**
  52  * A reflection-based utility that enables atomic updates to
  53  * designated {@code volatile} reference fields of designated
  54  * classes.  This class is designed for use in atomic data structures
  55  * in which several reference fields of the same node are
  56  * independently subject to atomic updates. For example, a tree node
  57  * might be declared as
  58  *
  59  * <pre> {@code
  60  * class Node {
  61  *   private volatile Node left, right;
  62  *
  63  *   private static final AtomicReferenceFieldUpdater<Node, Node> leftUpdater =
  64  *     AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "left");
  65  *   private static final AtomicReferenceFieldUpdater<Node, Node> rightUpdater =
  66  *     AtomicReferenceFieldUpdater.newUpdater(Node.class, Node.class, "right");
  67  *
  68  *   Node getLeft() { return left; }
  69  *   boolean compareAndSetLeft(Node expect, Node update) {
  70  *     return leftUpdater.compareAndSet(this, expect, update);
  71  *   }
  72  *   // ... and so on
  73  * }}</pre>
  74  *
  75  * <p>Note that the guarantees of the {@code compareAndSet}
  76  * method in this class are weaker than in other atomic classes.
  77  * Because this class cannot ensure that all uses of the field
  78  * are appropriate for purposes of atomic access, it can
  79  * guarantee atomicity only with respect to other invocations of
  80  * {@code compareAndSet} and {@code set} on the same updater.
  81  *
  82  * <p>Object arguments for parameters of type {@code T} that are not
  83  * instances of the class passed to {@link #newUpdater} will result in
  84  * a {@link ClassCastException} being thrown.
  85  *


< prev index next >