src/share/classes/java/util/concurrent/atomic/AtomicReference.java
Print this page
@@ -122,11 +122,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 final boolean weakCompareAndSet(V expect, V update) {
return unsafe.compareAndSwapObject(this, valueOffset, expect, update);
}
@@ -134,21 +134,18 @@
* Atomically sets to the given value and returns the old value.
*
* @param newValue the new value
* @return the previous value
*/
+ @SuppressWarnings("unchecked")
public final V getAndSet(V newValue) {
- while (true) {
- V x = get();
- if (compareAndSet(x, newValue))
- return x;
+ return (V)unsafe.getAndSetObject(this, valueOffset, newValue);
}
- }
/**
* 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 String.valueOf(get());
}