2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
7 package java.util.concurrent.atomic;
8 import sun.misc.Unsafe;
11 * A <tt>boolean</tt> value that may be updated atomically. See the
12 * {@link java.util.concurrent.atomic} package specification for
13 * description of the properties of atomic variables. An
14 * <tt>AtomicBoolean</tt> is used in applications such as atomically
15 * updated flags, and cannot be used as a replacement for a
16 * {@link java.lang.Boolean}.
21 public class AtomicBoolean implements java.io.Serializable {
22 private static final long serialVersionUID = 4654671469794556979L;
23 // setup to use Unsafe.compareAndSwapInt for updates
24 private static final Unsafe unsafe = Unsafe.getUnsafe();
25 private static final long valueOffset;
29 valueOffset = unsafe.objectFieldOffset
30 (AtomicBoolean.class.getDeclaredField("value"));
31 } catch (Exception ex) { throw new Error(ex); }
34 private volatile int value;
37 * Creates a new <tt>AtomicBoolean</tt> with the given initial value.
39 * @param initialValue the initial value
41 public AtomicBoolean(boolean initialValue) {
42 value = initialValue ? 1 : 0;
46 * Creates a new <tt>AtomicBoolean</tt> with initial value <tt>false</tt>.
48 public AtomicBoolean() {
52 * Returns the current value.
54 * @return the current value
56 public final boolean get() {
61 * Atomically sets the value to the given updated value
62 * if the current value <tt>==</tt> the expected value.
64 * @param expect the expected value
65 * @param update the new value
66 * @return true if successful. False return indicates that
67 * the actual value was not equal to the expected value.
69 public final boolean compareAndSet(boolean expect, boolean update) {
70 int e = expect ? 1 : 0;
71 int u = update ? 1 : 0;
72 return unsafe.compareAndSwapInt(this, valueOffset, e, u);
76 * Atomically sets the value to the given updated value
77 * if the current value <tt>==</tt> the expected value.
78 * May fail spuriously and does not provide ordering guarantees,
79 * so is only rarely an appropriate alternative to <tt>compareAndSet</tt>.
81 * @param expect the expected value
82 * @param update the new value
83 * @return true if successful.
85 public boolean weakCompareAndSet(boolean expect, boolean update) {
86 int e = expect ? 1 : 0;
87 int u = update ? 1 : 0;
88 return unsafe.compareAndSwapInt(this, valueOffset, e, u);
92 * Unconditionally sets to the given value.
94 * @param newValue the new value
96 public final void set(boolean newValue) {
97 value = newValue ? 1 : 0;
101 * Eventually sets to the given value.
103 * @param newValue the new value
106 public final void lazySet(boolean newValue) {
107 int v = newValue ? 1 : 0;
108 unsafe.putOrderedInt(this, valueOffset, v);
112 * Atomically sets to the given value and returns the previous value.
114 * @param newValue the new value
115 * @return the previous value
117 public final boolean getAndSet(boolean newValue) {
119 boolean current = get();
120 if (compareAndSet(current, newValue))
126 * Returns the String representation of the current value.
127 * @return the String representation of the current value.
129 public String toString() {
130 return Boolean.toString(get());