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

Print this page




  94      * @param update the new value
  95      * @return true if successful. False return indicates that
  96      * the actual value was not equal to the expected value.
  97      */
  98     public final boolean compareAndSet(boolean expect, boolean update) {
  99         int e = expect ? 1 : 0;
 100         int u = update ? 1 : 0;
 101         return unsafe.compareAndSwapInt(this, valueOffset, e, u);
 102     }
 103 
 104     /**
 105      * Atomically sets the value to the given updated value
 106      * if the current value {@code ==} the expected value.
 107      *
 108      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 109      * and does not provide ordering guarantees, so is only rarely an
 110      * appropriate alternative to {@code compareAndSet}.
 111      *
 112      * @param expect the expected value
 113      * @param update the new value
 114      * @return true if successful.
 115      */
 116     public boolean weakCompareAndSet(boolean expect, boolean update) {
 117         int e = expect ? 1 : 0;
 118         int u = update ? 1 : 0;
 119         return unsafe.compareAndSwapInt(this, valueOffset, e, u);
 120     }
 121 
 122     /**
 123      * Unconditionally sets to the given value.
 124      *
 125      * @param newValue the new value
 126      */
 127     public final void set(boolean newValue) {
 128         value = newValue ? 1 : 0;
 129     }
 130 
 131     /**
 132      * Eventually sets to the given value.
 133      *
 134      * @param newValue the new value
 135      * @since 1.6
 136      */
 137     public final void lazySet(boolean newValue) {
 138         int v = newValue ? 1 : 0;
 139         unsafe.putOrderedInt(this, valueOffset, v);
 140     }
 141 
 142     /**
 143      * Atomically sets to the given value and returns the previous value.
 144      *
 145      * @param newValue the new value
 146      * @return the previous value
 147      */
 148     public final boolean getAndSet(boolean newValue) {
 149         for (;;) {
 150             boolean current = get();
 151             if (compareAndSet(current, newValue))
 152                 return current;

 153         }
 154     }
 155 
 156     /**
 157      * Returns the String representation of the current value.
 158      * @return the String representation of the current value.
 159      */
 160     public String toString() {
 161         return Boolean.toString(get());
 162     }
 163 
 164 }


  94      * @param update the new value
  95      * @return true if successful. False return indicates that
  96      * the actual value was not equal to the expected value.
  97      */
  98     public final boolean compareAndSet(boolean expect, boolean update) {
  99         int e = expect ? 1 : 0;
 100         int u = update ? 1 : 0;
 101         return unsafe.compareAndSwapInt(this, valueOffset, e, u);
 102     }
 103 
 104     /**
 105      * Atomically sets the value to the given updated value
 106      * if the current value {@code ==} the expected value.
 107      *
 108      * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
 109      * and does not provide ordering guarantees, so is only rarely an
 110      * appropriate alternative to {@code compareAndSet}.
 111      *
 112      * @param expect the expected value
 113      * @param update the new value
 114      * @return true if successful
 115      */
 116     public boolean weakCompareAndSet(boolean expect, boolean update) {
 117         int e = expect ? 1 : 0;
 118         int u = update ? 1 : 0;
 119         return unsafe.compareAndSwapInt(this, valueOffset, e, u);
 120     }
 121 
 122     /**
 123      * Unconditionally sets to the given value.
 124      *
 125      * @param newValue the new value
 126      */
 127     public final void set(boolean newValue) {
 128         value = newValue ? 1 : 0;
 129     }
 130 
 131     /**
 132      * Eventually sets to the given value.
 133      *
 134      * @param newValue the new value
 135      * @since 1.6
 136      */
 137     public final void lazySet(boolean newValue) {
 138         int v = newValue ? 1 : 0;
 139         unsafe.putOrderedInt(this, valueOffset, v);
 140     }
 141 
 142     /**
 143      * Atomically sets to the given value and returns the previous value.
 144      *
 145      * @param newValue the new value
 146      * @return the previous value
 147      */
 148     public final boolean getAndSet(boolean newValue) {
 149         boolean prev;
 150         do {
 151             prev = get();
 152         } while (!compareAndSet(prev, newValue));
 153         return prev;
 154     }

 155 
 156     /**
 157      * Returns the String representation of the current value.
 158      * @return the String representation of the current value
 159      */
 160     public String toString() {
 161         return Boolean.toString(get());
 162     }
 163 
 164 }