rt/emul/compact/src/main/java/java/util/concurrent/atomic/AtomicBoolean.java
author Jaroslav Tulach <jaroslav.tulach@apidesign.org>
Fri, 04 Oct 2013 10:52:01 +0200
changeset 1338 aa70afac4eca
parent 1334 588d5bf7a560
permissions -rw-r--r--
Concurrency utilities
     1 /*
     2  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  *
     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.
     9  *
    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).
    15  *
    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.
    19  *
    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
    22  * questions.
    23  */
    24 
    25 /*
    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
    29  * file:
    30  *
    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/
    34  */
    35 
    36 package java.util.concurrent.atomic;
    37 
    38 /**
    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}.
    45  *
    46  * @since 1.5
    47  * @author Doug Lea
    48  */
    49 public class AtomicBoolean implements java.io.Serializable {
    50     private static final long serialVersionUID = 4654671469794556979L;
    51 
    52     private volatile int value;
    53 
    54     /**
    55      * Creates a new {@code AtomicBoolean} with the given initial value.
    56      *
    57      * @param initialValue the initial value
    58      */
    59     public AtomicBoolean(boolean initialValue) {
    60         value = initialValue ? 1 : 0;
    61     }
    62 
    63     /**
    64      * Creates a new {@code AtomicBoolean} with initial value {@code false}.
    65      */
    66     public AtomicBoolean() {
    67     }
    68 
    69     /**
    70      * Returns the current value.
    71      *
    72      * @return the current value
    73      */
    74     public final boolean get() {
    75         return value != 0;
    76     }
    77 
    78     /**
    79      * Atomically sets the value to the given updated value
    80      * if the current value {@code ==} the expected value.
    81      *
    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.
    86      */
    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) {
    91             this.value = u;
    92             return true;
    93         } else {
    94             return false;
    95         }
    96     }
    97 
    98     /**
    99      * Atomically sets the value to the given updated value
   100      * if the current value {@code ==} the expected value.
   101      *
   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}.
   105      *
   106      * @param expect the expected value
   107      * @param update the new value
   108      * @return true if successful.
   109      */
   110     public boolean weakCompareAndSet(boolean expect, boolean update) {
   111         return compareAndSet(expect, update);
   112     }
   113 
   114     /**
   115      * Unconditionally sets to the given value.
   116      *
   117      * @param newValue the new value
   118      */
   119     public final void set(boolean newValue) {
   120         value = newValue ? 1 : 0;
   121     }
   122 
   123     /**
   124      * Eventually sets to the given value.
   125      *
   126      * @param newValue the new value
   127      * @since 1.6
   128      */
   129     public final void lazySet(boolean newValue) {
   130         set(newValue);
   131     }
   132 
   133     /**
   134      * Atomically sets to the given value and returns the previous value.
   135      *
   136      * @param newValue the new value
   137      * @return the previous value
   138      */
   139     public final boolean getAndSet(boolean newValue) {
   140         for (;;) {
   141             boolean current = get();
   142             if (compareAndSet(current, newValue))
   143                 return current;
   144         }
   145     }
   146 
   147     /**
   148      * Returns the String representation of the current value.
   149      * @return the String representation of the current value.
   150      */
   151     public String toString() {
   152         return Boolean.toString(get());
   153     }
   154 
   155 }