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 |
import sun.misc.Unsafe; |
10 |
|
11 |
/** |
12 |
* A {@code boolean} 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 |
* {@code AtomicBoolean} 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 { |
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.class.getDeclaredField("value")); |
32 |
} catch (Exception ex) { throw new Error(ex); } |
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 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 |
int e = expect ? 1 : 0; |
72 |
int u = update ? 1 : 0; |
73 |
return unsafe.compareAndSwapInt(this, valueOffset, e, u); |
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 true if successful |
87 |
*/ |
88 |
public boolean weakCompareAndSet(boolean expect, boolean update) { |
89 |
int e = expect ? 1 : 0; |
90 |
int u = update ? 1 : 0; |
91 |
return unsafe.compareAndSwapInt(this, valueOffset, e, u); |
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 |
int v = newValue ? 1 : 0; |
111 |
unsafe.putOrderedInt(this, valueOffset, v); |
112 |
} |
113 |
|
114 |
/** |
115 |
* Atomically sets to the given value and returns the previous value. |
116 |
* |
117 |
* @param newValue the new value |
118 |
* @return the previous value |
119 |
*/ |
120 |
public final boolean getAndSet(boolean newValue) { |
121 |
for (;;) { |
122 |
boolean current = get(); |
123 |
if (compareAndSet(current, newValue)) |
124 |
return current; |
125 |
} |
126 |
} |
127 |
|
128 |
/** |
129 |
* Returns the String representation of the current value. |
130 |
* @return the String representation of the current value |
131 |
*/ |
132 |
public String toString() { |
133 |
return Boolean.toString(get()); |
134 |
} |
135 |
|
136 |
} |