src/java.base/share/classes/java/util/concurrent/atomic/AtomicBoolean.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 /**
  39  * A {@code boolean} value that may be updated atomically. See the
  40  * {@link java.util.concurrent.atomic} package specification for
  41  * description of the properties of atomic variables. An
  42  * {@code AtomicBoolean} is used in applications such as atomically
  43  * updated flags, and cannot be used as a replacement for a
  44  * {@link java.lang.Boolean}.
  45  *
  46  * @since 1.5
  47  * @author Doug Lea
  48  */
  49 public class AtomicBoolean implements java.io.Serializable {
  50     private static final long serialVersionUID = 4654671469794556979L;
  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                 (AtomicBoolean.class.getDeclaredField("value"));
  59         } catch (ReflectiveOperationException e) {
  60             throw new Error(e);
  61         }
  62     }
  63 
  64     private volatile int value;
  65 
  66     /**
  67      * Creates a new {@code AtomicBoolean} with the given initial value.
  68      *
  69      * @param initialValue the initial value
  70      */
  71     public AtomicBoolean(boolean initialValue) {
  72         value = initialValue ? 1 : 0;




  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 /**
  39  * A {@code boolean} value that may be updated atomically. See the
  40  * {@link java.util.concurrent.atomic} package specification for
  41  * description of the properties of atomic variables. An
  42  * {@code AtomicBoolean} is used in applications such as atomically
  43  * updated flags, and cannot be used as a replacement for a
  44  * {@link java.lang.Boolean}.
  45  *
  46  * @since 1.5
  47  * @author Doug Lea
  48  */
  49 public class AtomicBoolean implements java.io.Serializable {
  50     private static final long serialVersionUID = 4654671469794556979L;
  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                 (AtomicBoolean.class.getDeclaredField("value"));
  59         } catch (ReflectiveOperationException e) {
  60             throw new Error(e);
  61         }
  62     }
  63 
  64     private volatile int value;
  65 
  66     /**
  67      * Creates a new {@code AtomicBoolean} with the given initial value.
  68      *
  69      * @param initialValue the initial value
  70      */
  71     public AtomicBoolean(boolean initialValue) {
  72         value = initialValue ? 1 : 0;