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

Print this page
rev 12972 : 8140606: Update library code to use internal Unsafe
Reviewed-by: duke


  32  * Expert Group and released to the public domain, as explained at
  33  * http://creativecommons.org/publicdomain/zero/1.0/
  34  */
  35 
  36 package java.util.concurrent.atomic;
  37 
  38 import java.util.function.BinaryOperator;
  39 import java.util.function.UnaryOperator;
  40 
  41 /**
  42  * An object reference that may be updated atomically. See the {@link
  43  * java.util.concurrent.atomic} package specification for description
  44  * of the properties of atomic variables.
  45  * @since 1.5
  46  * @author Doug Lea
  47  * @param <V> The type of object referred to by this reference
  48  */
  49 public class AtomicReference<V> implements java.io.Serializable {
  50     private static final long serialVersionUID = -1848883965231344442L;
  51 
  52     private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe();
  53     private static final long VALUE;
  54 
  55     static {
  56         try {
  57             VALUE = U.objectFieldOffset
  58                 (AtomicReference.class.getDeclaredField("value"));
  59         } catch (ReflectiveOperationException e) {
  60             throw new Error(e);
  61         }
  62     }
  63 
  64     private volatile V value;
  65 
  66     /**
  67      * Creates a new AtomicReference with the given initial value.
  68      *
  69      * @param initialValue the initial value
  70      */
  71     public AtomicReference(V initialValue) {
  72         value = initialValue;




  32  * Expert Group and released to the public domain, as explained at
  33  * http://creativecommons.org/publicdomain/zero/1.0/
  34  */
  35 
  36 package java.util.concurrent.atomic;
  37 
  38 import java.util.function.BinaryOperator;
  39 import java.util.function.UnaryOperator;
  40 
  41 /**
  42  * An object reference that may be updated atomically. See the {@link
  43  * java.util.concurrent.atomic} package specification for description
  44  * of the properties of atomic variables.
  45  * @since 1.5
  46  * @author Doug Lea
  47  * @param <V> The type of object referred to by this reference
  48  */
  49 public class AtomicReference<V> implements java.io.Serializable {
  50     private static final long serialVersionUID = -1848883965231344442L;
  51 
  52     private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
  53     private static final long VALUE;
  54 
  55     static {
  56         try {
  57             VALUE = U.objectFieldOffset
  58                 (AtomicReference.class.getDeclaredField("value"));
  59         } catch (ReflectiveOperationException e) {
  60             throw new Error(e);
  61         }
  62     }
  63 
  64     private volatile V value;
  65 
  66     /**
  67      * Creates a new AtomicReference with the given initial value.
  68      *
  69      * @param initialValue the initial value
  70      */
  71     public AtomicReference(V initialValue) {
  72         value = initialValue;