src/share/classes/java/util/concurrent/atomic/AtomicBoolean.java
Print this page
@@ -109,11 +109,11 @@
* and does not provide ordering guarantees, so is only rarely an
* appropriate alternative to {@code compareAndSet}.
*
* @param expect the expected value
* @param update the new value
- * @return true if successful.
+ * @return true if successful
*/
public boolean weakCompareAndSet(boolean expect, boolean update) {
int e = expect ? 1 : 0;
int u = update ? 1 : 0;
return unsafe.compareAndSwapInt(this, valueOffset, e, u);
@@ -144,20 +144,20 @@
*
* @param newValue the new value
* @return the previous value
*/
public final boolean getAndSet(boolean newValue) {
- for (;;) {
- boolean current = get();
- if (compareAndSet(current, newValue))
- return current;
+ boolean prev;
+ do {
+ prev = get();
+ } while (!compareAndSet(prev, newValue));
+ return prev;
}
- }
/**
* Returns the String representation of the current value.
- * @return the String representation of the current value.
+ * @return the String representation of the current value
*/
public String toString() {
return Boolean.toString(get());
}