KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > concurrent > atomic > AtomicBoolean


1 /*
2  * @(#)AtomicBoolean.java 1.7 04/07/12
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.concurrent.atomic;
9 import sun.misc.Unsafe;
10
11 /**
12  * A <tt>boolean</tt> value that may be updated atomically. See the
13  * {@link java.util.concurrent.atomic} package specification for
14  * description of the properties of atomic variables. An
15  * <tt>AtomicBoolean</tt> is used in applications such as atomically
16  * updated flags, and cannot be used as a replacement for a
17  * {@link java.lang.Boolean}.
18  *
19  * @since 1.5
20  * @author Doug Lea
21  */

22 public class AtomicBoolean implements java.io.Serializable JavaDoc {
23     private static final long serialVersionUID = 4654671469794556979L;
24     // setup to use Unsafe.compareAndSwapInt for updates
25
private static final Unsafe unsafe = Unsafe.getUnsafe();
26     private static final long valueOffset;
27
28     static {
29       try {
30         valueOffset = unsafe.objectFieldOffset
31             (AtomicBoolean JavaDoc.class.getDeclaredField("value"));
32       } catch (Exception JavaDoc ex) { throw new Error JavaDoc(ex); }
33     }
34
35     private volatile int value;
36
37     /**
38      * Creates a new <tt>AtomicBoolean</tt> with the given initial value.
39      *
40      * @param initialValue the initial value
41      */

42     public AtomicBoolean(boolean initialValue) {
43         value = initialValue ? 1 : 0;
44     }
45
46     /**
47      * Creates a new <tt>AtomicBoolean</tt> with initial value <tt>false</tt>.
48      */

49     public AtomicBoolean() {
50     }
51
52     /**
53      * Returns the current value.
54      *
55      * @return the current value
56      */

57     public final boolean get() {
58         return value != 0;
59     }
60
61     /**
62      * Atomically set the value to the given updated value
63      * 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.
68      */

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);
73     }
74
75     /**
76      * Atomically set the value to the given updated value
77      * if the current value <tt>==</tt> the expected value.
78      * May fail spuriously.
79      * @param expect the expected value
80      * @param update the new value
81      * @return true if successful.
82      */

83     public boolean weakCompareAndSet(boolean expect, boolean update) {
84         int e = expect ? 1 : 0;
85         int u = update ? 1 : 0;
86         return unsafe.compareAndSwapInt(this, valueOffset, e, u);
87     }
88
89     /**
90      * Unconditionally sets to the given value.
91      *
92      * @param newValue the new value
93      */

94     public final void set(boolean newValue) {
95         value = newValue ? 1 : 0;
96     }
97
98     /**
99      * Sets to the given value and returns the previous value.
100      *
101      * @param newValue the new value
102      * @return the previous value
103      */

104     public final boolean getAndSet(boolean newValue) {
105         for (;;) {
106             boolean current = get();
107             if (compareAndSet(current, newValue))
108                 return current;
109         }
110     }
111
112     /**
113      * Returns the String representation of the current value.
114      * @return the String representation of the current value.
115      */

116     public String JavaDoc toString() {
117         return Boolean.toString(get());
118     }
119
120 }
121
Popular Tags