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

Print this page

        

@@ -127,16 +127,12 @@
      *
      * @param newValue the new value
      * @return the previous value
      */
     public final long getAndSet(long newValue) {
-        while (true) {
-            long current = get();
-            if (compareAndSet(current, newValue))
-                return current;
+        return unsafe.getAndSetLong(this, valueOffset, newValue);
         }
-    }
 
     /**
      * Atomically sets the value to the given updated value
      * if the current value {@code ==} the expected value.
      *

@@ -157,11 +153,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(long expect, long update) {
         return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
     }
 

@@ -169,93 +165,63 @@
      * Atomically increments by one the current value.
      *
      * @return the previous value
      */
     public final long getAndIncrement() {
-        while (true) {
-            long current = get();
-            long next = current + 1;
-            if (compareAndSet(current, next))
-                return current;
+        return getAndAdd(1);
         }
-    }
 
     /**
      * Atomically decrements by one the current value.
      *
      * @return the previous value
      */
     public final long getAndDecrement() {
-        while (true) {
-            long current = get();
-            long next = current - 1;
-            if (compareAndSet(current, next))
-                return current;
+        return getAndAdd(-1);
         }
-    }
 
     /**
      * Atomically adds the given value to the current value.
      *
      * @param delta the value to add
      * @return the previous value
      */
     public final long getAndAdd(long delta) {
-        while (true) {
-            long current = get();
-            long next = current + delta;
-            if (compareAndSet(current, next))
-                return current;
+        return unsafe.getAndAddLong(this, valueOffset, delta);
         }
-    }
 
     /**
      * Atomically increments by one the current value.
      *
      * @return the updated value
      */
     public final long incrementAndGet() {
-        for (;;) {
-            long current = get();
-            long next = current + 1;
-            if (compareAndSet(current, next))
-                return next;
+        return getAndAdd(1) + 1;
         }
-    }
 
     /**
      * Atomically decrements by one the current value.
      *
      * @return the updated value
      */
     public final long decrementAndGet() {
-        for (;;) {
-            long current = get();
-            long next = current - 1;
-            if (compareAndSet(current, next))
-                return next;
+        return getAndAdd(-1) - 1;
         }
-    }
 
     /**
      * Atomically adds the given value to the current value.
      *
      * @param delta the value to add
      * @return the updated value
      */
     public final long addAndGet(long delta) {
-        for (;;) {
-            long current = get();
-            long next = current + delta;
-            if (compareAndSet(current, next))
-                return next;
+        return getAndAdd(delta) + delta;
         }
-    }
 
     /**
      * 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 Long.toString(get());
     }