2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
16 * You should have received a copy of the GNU General Public License version
17 * 2 along with this work; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
21 * or visit www.oracle.com if you need additional information or have any
26 * This file is available under and governed by the GNU General Public
27 * License version 2 only, as published by the Free Software Foundation.
28 * However, the following notice accompanied the original version of this
31 * Written by Doug Lea with assistance from members of JCP JSR-166
32 * Expert Group and released to the public domain, as explained at
33 * http://creativecommons.org/publicdomain/zero/1.0/
36 package java.util.concurrent.atomic;
37 import sun.misc.Unsafe;
40 * A {@code boolean} value that may be updated atomically. See the
41 * {@link java.util.concurrent.atomic} package specification for
42 * description of the properties of atomic variables. An
43 * {@code AtomicBoolean} is used in applications such as atomically
44 * updated flags, and cannot be used as a replacement for a
45 * {@link java.lang.Boolean}.
50 public class AtomicBoolean implements java.io.Serializable {
51 private static final long serialVersionUID = 4654671469794556979L;
52 // setup to use Unsafe.compareAndSwapInt for updates
53 private static final Unsafe unsafe = Unsafe.getUnsafe();
54 private static final long valueOffset;
58 valueOffset = unsafe.objectFieldOffset
59 (AtomicBoolean.class.getDeclaredField("value"));
60 } catch (Exception ex) { throw new Error(ex); }
63 private volatile int value;
66 * Creates a new {@code AtomicBoolean} with the given initial value.
68 * @param initialValue the initial value
70 public AtomicBoolean(boolean initialValue) {
71 value = initialValue ? 1 : 0;
75 * Creates a new {@code AtomicBoolean} with initial value {@code false}.
77 public AtomicBoolean() {
81 * Returns the current value.
83 * @return the current value
85 public final boolean get() {
90 * Atomically sets the value to the given updated value
91 * if the current value {@code ==} the expected value.
93 * @param expect the expected value
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.
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);
105 * Atomically sets the value to the given updated value
106 * if the current value {@code ==} the expected value.
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}.
112 * @param expect the expected value
113 * @param update the new value
114 * @return true if successful.
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);
123 * Unconditionally sets to the given value.
125 * @param newValue the new value
127 public final void set(boolean newValue) {
128 value = newValue ? 1 : 0;
132 * Eventually sets to the given value.
134 * @param newValue the new value
137 public final void lazySet(boolean newValue) {
138 int v = newValue ? 1 : 0;
139 unsafe.putOrderedInt(this, valueOffset, v);
143 * Atomically sets to the given value and returns the previous value.
145 * @param newValue the new value
146 * @return the previous value
148 public final boolean getAndSet(boolean newValue) {
150 boolean current = get();
151 if (compareAndSet(current, newValue))
157 * Returns the String representation of the current value.
158 * @return the String representation of the current value.
160 public String toString() {
161 return Boolean.toString(get());