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;
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}.
49 public class AtomicBoolean implements java.io.Serializable {
50 private static final long serialVersionUID = 4654671469794556979L;
52 private volatile int value;
55 * Creates a new {@code AtomicBoolean} with the given initial value.
57 * @param initialValue the initial value
59 public AtomicBoolean(boolean initialValue) {
60 value = initialValue ? 1 : 0;
64 * Creates a new {@code AtomicBoolean} with initial value {@code false}.
66 public AtomicBoolean() {
70 * Returns the current value.
72 * @return the current value
74 public final boolean get() {
79 * Atomically sets the value to the given updated value
80 * if the current value {@code ==} the expected value.
82 * @param expect the expected value
83 * @param update the new value
84 * @return true if successful. False return indicates that
85 * the actual value was not equal to the expected value.
87 public final boolean compareAndSet(boolean expect, boolean update) {
88 int e = expect ? 1 : 0;
89 int u = update ? 1 : 0;
90 if (this.value == e) {
99 * Atomically sets the value to the given updated value
100 * if the current value {@code ==} the expected value.
102 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
103 * and does not provide ordering guarantees, so is only rarely an
104 * appropriate alternative to {@code compareAndSet}.
106 * @param expect the expected value
107 * @param update the new value
108 * @return true if successful.
110 public boolean weakCompareAndSet(boolean expect, boolean update) {
111 return compareAndSet(expect, update);
115 * Unconditionally sets to the given value.
117 * @param newValue the new value
119 public final void set(boolean newValue) {
120 value = newValue ? 1 : 0;
124 * Eventually sets to the given value.
126 * @param newValue the new value
129 public final void lazySet(boolean newValue) {
134 * Atomically sets to the given value and returns the previous value.
136 * @param newValue the new value
137 * @return the previous value
139 public final boolean getAndSet(boolean newValue) {
141 boolean current = get();
142 if (compareAndSet(current, newValue))
148 * Returns the String representation of the current value.
149 * @return the String representation of the current value.
151 public String toString() {
152 return Boolean.toString(get());