1 |
/* |
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/publicdomain/zero/1.0/ |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent.atomic; |
8 |
|
9 |
/** |
10 |
* A {@code boolean} value that may be updated atomically. See the |
11 |
* {@link java.util.concurrent.atomic} package specification for |
12 |
* description of the properties of atomic variables. An |
13 |
* {@code AtomicBoolean} is used in applications such as atomically |
14 |
* updated flags, and cannot be used as a replacement for a |
15 |
* {@link java.lang.Boolean}. |
16 |
* |
17 |
* @since 1.5 |
18 |
* @author Doug Lea |
19 |
*/ |
20 |
public class AtomicBoolean implements java.io.Serializable { |
21 |
private static final long serialVersionUID = 4654671469794556979L; |
22 |
|
23 |
private static final sun.misc.Unsafe U = sun.misc.Unsafe.getUnsafe(); |
24 |
private static final long VALUE; |
25 |
|
26 |
static { |
27 |
try { |
28 |
VALUE = U.objectFieldOffset |
29 |
(AtomicBoolean.class.getDeclaredField("value")); |
30 |
} catch (ReflectiveOperationException e) { |
31 |
throw new Error(e); |
32 |
} |
33 |
} |
34 |
|
35 |
private volatile int value; |
36 |
|
37 |
/** |
38 |
* Creates a new {@code AtomicBoolean} 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 {@code AtomicBoolean} with initial value {@code false}. |
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 sets the value to the given updated value |
63 |
* if the current value {@code ==} the expected value. |
64 |
* |
65 |
* @param expect the expected value |
66 |
* @param update the new value |
67 |
* @return {@code true} if successful. False return indicates that |
68 |
* the actual value was not equal to the expected value. |
69 |
*/ |
70 |
public final boolean compareAndSet(boolean expect, boolean update) { |
71 |
return U.compareAndSwapInt(this, VALUE, |
72 |
(expect ? 1 : 0), |
73 |
(update ? 1 : 0)); |
74 |
} |
75 |
|
76 |
/** |
77 |
* Atomically sets the value to the given updated value |
78 |
* if the current value {@code ==} the expected value. |
79 |
* |
80 |
* <p><a href="package-summary.html#weakCompareAndSet">May fail |
81 |
* spuriously and does not provide ordering guarantees</a>, so is |
82 |
* only rarely an appropriate alternative to {@code compareAndSet}. |
83 |
* |
84 |
* @param expect the expected value |
85 |
* @param update the new value |
86 |
* @return {@code true} if successful |
87 |
*/ |
88 |
public boolean weakCompareAndSet(boolean expect, boolean update) { |
89 |
return U.compareAndSwapInt(this, VALUE, |
90 |
(expect ? 1 : 0), |
91 |
(update ? 1 : 0)); |
92 |
} |
93 |
|
94 |
/** |
95 |
* Unconditionally sets to the given value. |
96 |
* |
97 |
* @param newValue the new value |
98 |
*/ |
99 |
public final void set(boolean newValue) { |
100 |
value = newValue ? 1 : 0; |
101 |
} |
102 |
|
103 |
/** |
104 |
* Eventually sets to the given value. |
105 |
* |
106 |
* @param newValue the new value |
107 |
* @since 1.6 |
108 |
*/ |
109 |
public final void lazySet(boolean newValue) { |
110 |
U.putOrderedInt(this, VALUE, (newValue ? 1 : 0)); |
111 |
} |
112 |
|
113 |
/** |
114 |
* Atomically sets to the given value and returns the previous value. |
115 |
* |
116 |
* @param newValue the new value |
117 |
* @return the previous value |
118 |
*/ |
119 |
public final boolean getAndSet(boolean newValue) { |
120 |
// with only 2 boolean values, a single CAS suffices |
121 |
int newInt = newValue ? 1 : 0; |
122 |
return newValue ^ U.compareAndSwapInt(this, VALUE, newInt ^ 1, newInt); |
123 |
} |
124 |
|
125 |
/** |
126 |
* Returns the String representation of the current value. |
127 |
* @return the String representation of the current value |
128 |
*/ |
129 |
public String toString() { |
130 |
return Boolean.toString(get()); |
131 |
} |
132 |
|
133 |
} |