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 java.lang.invoke.MethodHandles; |
10 |
import java.lang.invoke.VarHandle; |
11 |
|
12 |
/** |
13 |
* A {@code boolean} value that may be updated atomically. See the |
14 |
* {@link VarHandle} specification for descriptions of the properties |
15 |
* of atomic accesses. An {@code AtomicBoolean} is used in |
16 |
* applications such as atomically updated flags, and cannot be used |
17 |
* as a replacement for a {@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 |
private static final VarHandle VALUE; |
25 |
static { |
26 |
try { |
27 |
MethodHandles.Lookup l = MethodHandles.lookup(); |
28 |
VALUE = l.findVarHandle(AtomicBoolean.class, "value", int.class); |
29 |
} catch (ReflectiveOperationException e) { |
30 |
throw new ExceptionInInitializerError(e); |
31 |
} |
32 |
} |
33 |
|
34 |
private volatile int value; |
35 |
|
36 |
/** |
37 |
* Creates a new {@code AtomicBoolean} with the given initial value. |
38 |
* |
39 |
* @param initialValue the initial value |
40 |
*/ |
41 |
public AtomicBoolean(boolean initialValue) { |
42 |
if (initialValue) |
43 |
value = 1; |
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 |
* with memory effects as specified by {@link VarHandle#getVolatile}. |
55 |
* |
56 |
* @return the current value |
57 |
*/ |
58 |
public final boolean get() { |
59 |
return value != 0; |
60 |
} |
61 |
|
62 |
/** |
63 |
* Atomically sets the value to {@code newValue} |
64 |
* if the current value {@code == expectedValue}, |
65 |
* with memory effects as specified by {@link VarHandle#compareAndSet}. |
66 |
* |
67 |
* @param expectedValue the expected value |
68 |
* @param newValue the new value |
69 |
* @return {@code true} if successful. False return indicates that |
70 |
* the actual value was not equal to the expected value. |
71 |
*/ |
72 |
public final boolean compareAndSet(boolean expectedValue, boolean newValue) { |
73 |
return VALUE.compareAndSet(this, |
74 |
(expectedValue ? 1 : 0), |
75 |
(newValue ? 1 : 0)); |
76 |
} |
77 |
|
78 |
/** |
79 |
* Possibly atomically sets the value to {@code newValue} |
80 |
* if the current value {@code == expectedValue}, |
81 |
* with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}. |
82 |
* |
83 |
* @deprecated This method has plain memory effects but the method |
84 |
* name implies volatile memory effects (see methods such as |
85 |
* {@link #compareAndExchange} and {@link #compareAndSet}). To avoid |
86 |
* confusion over plain or volatile memory effects it is recommended that |
87 |
* the method {@link #weakCompareAndSetPlain} be used instead. |
88 |
* |
89 |
* @param expectedValue the expected value |
90 |
* @param newValue the new value |
91 |
* @return {@code true} if successful |
92 |
* @see #weakCompareAndSetPlain |
93 |
*/ |
94 |
@Deprecated(since="9") |
95 |
public boolean weakCompareAndSet(boolean expectedValue, boolean newValue) { |
96 |
return VALUE.weakCompareAndSetPlain(this, |
97 |
(expectedValue ? 1 : 0), |
98 |
(newValue ? 1 : 0)); |
99 |
} |
100 |
|
101 |
/** |
102 |
* Possibly atomically sets the value to {@code newValue} |
103 |
* if the current value {@code == expectedValue}, |
104 |
* with memory effects as specified by {@link VarHandle#weakCompareAndSetPlain}. |
105 |
* |
106 |
* @param expectedValue the expected value |
107 |
* @param newValue the new value |
108 |
* @return {@code true} if successful |
109 |
* @since 9 |
110 |
*/ |
111 |
public boolean weakCompareAndSetPlain(boolean expectedValue, boolean newValue) { |
112 |
return VALUE.weakCompareAndSetPlain(this, |
113 |
(expectedValue ? 1 : 0), |
114 |
(newValue ? 1 : 0)); |
115 |
} |
116 |
|
117 |
/** |
118 |
* Sets the value to {@code newValue}, |
119 |
* with memory effects as specified by {@link VarHandle#setVolatile}. |
120 |
* |
121 |
* @param newValue the new value |
122 |
*/ |
123 |
public final void set(boolean newValue) { |
124 |
value = newValue ? 1 : 0; |
125 |
} |
126 |
|
127 |
/** |
128 |
* Sets the value to {@code newValue}, |
129 |
* with memory effects as specified by {@link VarHandle#setRelease}. |
130 |
* |
131 |
* @param newValue the new value |
132 |
* @since 1.6 |
133 |
*/ |
134 |
public final void lazySet(boolean newValue) { |
135 |
VALUE.setRelease(this, (newValue ? 1 : 0)); |
136 |
} |
137 |
|
138 |
/** |
139 |
* Atomically sets the value to {@code newValue} and returns the old value, |
140 |
* with memory effects as specified by {@link VarHandle#getAndSet}. |
141 |
* |
142 |
* @param newValue the new value |
143 |
* @return the previous value |
144 |
*/ |
145 |
public final boolean getAndSet(boolean newValue) { |
146 |
return (int)VALUE.getAndSet(this, (newValue ? 1 : 0)) != 0; |
147 |
} |
148 |
|
149 |
/** |
150 |
* Returns the String representation of the current value. |
151 |
* @return the String representation of the current value |
152 |
*/ |
153 |
public String toString() { |
154 |
return Boolean.toString(get()); |
155 |
} |
156 |
|
157 |
// jdk9 |
158 |
|
159 |
/** |
160 |
* Returns the current value, with memory semantics of reading as |
161 |
* if the variable was declared non-{@code volatile}. |
162 |
* |
163 |
* @return the value |
164 |
* @since 9 |
165 |
*/ |
166 |
public final boolean getPlain() { |
167 |
return (int)VALUE.get(this) != 0; |
168 |
} |
169 |
|
170 |
/** |
171 |
* Sets the value to {@code newValue}, with memory semantics |
172 |
* of setting as if the variable was declared non-{@code volatile} |
173 |
* and non-{@code final}. |
174 |
* |
175 |
* @param newValue the new value |
176 |
* @since 9 |
177 |
*/ |
178 |
public final void setPlain(boolean newValue) { |
179 |
VALUE.set(this, newValue ? 1 : 0); |
180 |
} |
181 |
|
182 |
/** |
183 |
* Returns the current value, |
184 |
* with memory effects as specified by {@link VarHandle#getOpaque}. |
185 |
* |
186 |
* @return the value |
187 |
* @since 9 |
188 |
*/ |
189 |
public final boolean getOpaque() { |
190 |
return (int)VALUE.getOpaque(this) != 0; |
191 |
} |
192 |
|
193 |
/** |
194 |
* Sets the value to {@code newValue}, |
195 |
* with memory effects as specified by {@link VarHandle#setOpaque}. |
196 |
* |
197 |
* @param newValue the new value |
198 |
* @since 9 |
199 |
*/ |
200 |
public final void setOpaque(boolean newValue) { |
201 |
VALUE.setOpaque(this, newValue ? 1 : 0); |
202 |
} |
203 |
|
204 |
/** |
205 |
* Returns the current value, |
206 |
* with memory effects as specified by {@link VarHandle#getAcquire}. |
207 |
* |
208 |
* @return the value |
209 |
* @since 9 |
210 |
*/ |
211 |
public final boolean getAcquire() { |
212 |
return (int)VALUE.getAcquire(this) != 0; |
213 |
} |
214 |
|
215 |
/** |
216 |
* Sets the value to {@code newValue}, |
217 |
* with memory effects as specified by {@link VarHandle#setRelease}. |
218 |
* |
219 |
* @param newValue the new value |
220 |
* @since 9 |
221 |
*/ |
222 |
public final void setRelease(boolean newValue) { |
223 |
VALUE.setRelease(this, newValue ? 1 : 0); |
224 |
} |
225 |
|
226 |
/** |
227 |
* Atomically sets the value to {@code newValue} if the current value, |
228 |
* referred to as the <em>witness value</em>, {@code == expectedValue}, |
229 |
* with memory effects as specified by |
230 |
* {@link VarHandle#compareAndExchange}. |
231 |
* |
232 |
* @param expectedValue the expected value |
233 |
* @param newValue the new value |
234 |
* @return the witness value, which will be the same as the |
235 |
* expected value if successful |
236 |
* @since 9 |
237 |
*/ |
238 |
public final boolean compareAndExchange(boolean expectedValue, boolean newValue) { |
239 |
return (int)VALUE.compareAndExchange(this, |
240 |
(expectedValue ? 1 : 0), |
241 |
(newValue ? 1 : 0)) != 0; |
242 |
} |
243 |
|
244 |
/** |
245 |
* Atomically sets the value to {@code newValue} if the current value, |
246 |
* referred to as the <em>witness value</em>, {@code == expectedValue}, |
247 |
* with memory effects as specified by |
248 |
* {@link VarHandle#compareAndExchangeAcquire}. |
249 |
* |
250 |
* @param expectedValue the expected value |
251 |
* @param newValue the new value |
252 |
* @return the witness value, which will be the same as the |
253 |
* expected value if successful |
254 |
* @since 9 |
255 |
*/ |
256 |
public final boolean compareAndExchangeAcquire(boolean expectedValue, boolean newValue) { |
257 |
return (int)VALUE.compareAndExchangeAcquire(this, |
258 |
(expectedValue ? 1 : 0), |
259 |
(newValue ? 1 : 0)) != 0; |
260 |
} |
261 |
|
262 |
/** |
263 |
* Atomically sets the value to {@code newValue} if the current value, |
264 |
* referred to as the <em>witness value</em>, {@code == expectedValue}, |
265 |
* with memory effects as specified by |
266 |
* {@link VarHandle#compareAndExchangeRelease}. |
267 |
* |
268 |
* @param expectedValue the expected value |
269 |
* @param newValue the new value |
270 |
* @return the witness value, which will be the same as the |
271 |
* expected value if successful |
272 |
* @since 9 |
273 |
*/ |
274 |
public final boolean compareAndExchangeRelease(boolean expectedValue, boolean newValue) { |
275 |
return (int)VALUE.compareAndExchangeRelease(this, |
276 |
(expectedValue ? 1 : 0), |
277 |
(newValue ? 1 : 0)) != 0; |
278 |
} |
279 |
|
280 |
/** |
281 |
* Possibly atomically sets the value to {@code newValue} if the current |
282 |
* value {@code == expectedValue}, |
283 |
* with memory effects as specified by |
284 |
* {@link VarHandle#weakCompareAndSet}. |
285 |
* |
286 |
* @param expectedValue the expected value |
287 |
* @param newValue the new value |
288 |
* @return {@code true} if successful |
289 |
* @since 9 |
290 |
*/ |
291 |
public final boolean weakCompareAndSetVolatile(boolean expectedValue, boolean newValue) { |
292 |
return VALUE.weakCompareAndSet(this, |
293 |
(expectedValue ? 1 : 0), |
294 |
(newValue ? 1 : 0)); |
295 |
} |
296 |
|
297 |
/** |
298 |
* Possibly atomically sets the value to {@code newValue} if the current |
299 |
* value {@code == expectedValue}, |
300 |
* with memory effects as specified by |
301 |
* {@link VarHandle#weakCompareAndSetAcquire}. |
302 |
* |
303 |
* @param expectedValue the expected value |
304 |
* @param newValue the new value |
305 |
* @return {@code true} if successful |
306 |
* @since 9 |
307 |
*/ |
308 |
public final boolean weakCompareAndSetAcquire(boolean expectedValue, boolean newValue) { |
309 |
return VALUE.weakCompareAndSetAcquire(this, |
310 |
(expectedValue ? 1 : 0), |
311 |
(newValue ? 1 : 0)); |
312 |
} |
313 |
|
314 |
/** |
315 |
* Possibly atomically sets the value to {@code newValue} if the current |
316 |
* value {@code == expectedValue}, |
317 |
* with memory effects as specified by |
318 |
* {@link VarHandle#weakCompareAndSetRelease}. |
319 |
* |
320 |
* @param expectedValue the expected value |
321 |
* @param newValue the new value |
322 |
* @return {@code true} if successful |
323 |
* @since 9 |
324 |
*/ |
325 |
public final boolean weakCompareAndSetRelease(boolean expectedValue, boolean newValue) { |
326 |
return VALUE.weakCompareAndSetRelease(this, |
327 |
(expectedValue ? 1 : 0), |
328 |
(newValue ? 1 : 0)); |
329 |
} |
330 |
|
331 |
} |