src/share/classes/java/util/concurrent/atomic/AtomicReferenceArray.java

Print this page

        

@@ -157,18 +157,14 @@
      *
      * @param i the index
      * @param newValue the new value
      * @return the previous value
      */
+    @SuppressWarnings("unchecked")
     public final E getAndSet(int i, E newValue) {
-        long offset = checkedByteOffset(i);
-        while (true) {
-            E current = getRaw(offset);
-            if (compareAndSetRaw(offset, current, newValue))
-                return current;
+        return (E)unsafe.getAndSetObject(array, checkedByteOffset(i), newValue);
         }
-    }
 
     /**
      * Atomically sets the element at position {@code i} to the given
      * updated value if the current value {@code ==} the expected value.
      *

@@ -195,11 +191,11 @@
      * appropriate alternative to {@code compareAndSet}.
      *
      * @param i the index
      * @param expect the expected value
      * @param update the new value
-     * @return true if successful.
+     * @return true if successful
      */
     public final boolean weakCompareAndSet(int i, E expect, E update) {
         return compareAndSet(i, expect, update);
     }