< prev index next >

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

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


  51 public class AtomicBoolean implements java.io.Serializable {
  52     private static final long serialVersionUID = 4654671469794556979L;
  53     private static final VarHandle VALUE;
  54     static {
  55         try {
  56             MethodHandles.Lookup l = MethodHandles.lookup();
  57             VALUE = l.findVarHandle(AtomicBoolean.class, "value", int.class);
  58         } catch (ReflectiveOperationException e) {
  59             throw new ExceptionInInitializerError(e);
  60         }
  61     }
  62 
  63     private volatile int value;
  64 
  65     /**
  66      * Creates a new {@code AtomicBoolean} with the given initial value.
  67      *
  68      * @param initialValue the initial value
  69      */
  70     public AtomicBoolean(boolean initialValue) {
  71         if (initialValue) {
  72             value = 1;
  73         }
  74     }
  75 
  76     /**
  77      * Creates a new {@code AtomicBoolean} with initial value {@code false}.
  78      */
  79     public AtomicBoolean() {
  80     }
  81 
  82     /**
  83      * Returns the current value,
  84      * with memory effects as specified by {@link VarHandle#getVolatile}.
  85      *
  86      * @return the current value
  87      */
  88     public final boolean get() {
  89         return value != 0;
  90     }
  91 
  92     /**
  93      * Atomically sets the value to {@code newValue}




  51 public class AtomicBoolean implements java.io.Serializable {
  52     private static final long serialVersionUID = 4654671469794556979L;
  53     private static final VarHandle VALUE;
  54     static {
  55         try {
  56             MethodHandles.Lookup l = MethodHandles.lookup();
  57             VALUE = l.findVarHandle(AtomicBoolean.class, "value", int.class);
  58         } catch (ReflectiveOperationException e) {
  59             throw new ExceptionInInitializerError(e);
  60         }
  61     }
  62 
  63     private volatile int value;
  64 
  65     /**
  66      * Creates a new {@code AtomicBoolean} with the given initial value.
  67      *
  68      * @param initialValue the initial value
  69      */
  70     public AtomicBoolean(boolean initialValue) {
  71         if (initialValue)
  72             value = 1;

  73     }
  74 
  75     /**
  76      * Creates a new {@code AtomicBoolean} with initial value {@code false}.
  77      */
  78     public AtomicBoolean() {
  79     }
  80 
  81     /**
  82      * Returns the current value,
  83      * with memory effects as specified by {@link VarHandle#getVolatile}.
  84      *
  85      * @return the current value
  86      */
  87     public final boolean get() {
  88         return value != 0;
  89     }
  90 
  91     /**
  92      * Atomically sets the value to {@code newValue}


< prev index next >