src/share/classes/java/util/concurrent/atomic/AtomicLongArray.java
Print this page
@@ -142,17 +142,12 @@
* @param i the index
* @param newValue the new value
* @return the previous value
*/
public final long getAndSet(int i, long newValue) {
- long offset = checkedByteOffset(i);
- while (true) {
- long current = getRaw(offset);
- if (compareAndSetRaw(offset, current, newValue))
- return current;
+ return unsafe.getAndSetLong(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.
*
@@ -179,11 +174,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, long expect, long update) {
return compareAndSet(i, expect, update);
}
@@ -213,36 +208,31 @@
* @param i the index
* @param delta the value to add
* @return the previous value
*/
public final long getAndAdd(int i, long delta) {
- long offset = checkedByteOffset(i);
- while (true) {
- long current = getRaw(offset);
- if (compareAndSetRaw(offset, current, current + delta))
- return current;
+ return unsafe.getAndAddLong(array, checkedByteOffset(i), delta);
}
- }
/**
* Atomically increments by one the element at index {@code i}.
*
* @param i the index
* @return the updated value
*/
public final long incrementAndGet(int i) {
- return addAndGet(i, 1);
+ return getAndAdd(i, 1) + 1;
}
/**
* Atomically decrements by one the element at index {@code i}.
*
* @param i the index
* @return the updated value
*/
public final long decrementAndGet(int i) {
- return addAndGet(i, -1);
+ return getAndAdd(i, -1) - 1;
}
/**
* Atomically adds the given value to the element at index {@code i}.
*
@@ -249,18 +239,12 @@
* @param i the index
* @param delta the value to add
* @return the updated value
*/
public long addAndGet(int i, long delta) {
- long offset = checkedByteOffset(i);
- while (true) {
- long current = getRaw(offset);
- long next = current + delta;
- if (compareAndSetRaw(offset, current, next))
- return next;
+ return getAndAdd(i, delta) + delta;
}
- }
/**
* Returns the String representation of the current values of array.
* @return the String representation of the current values of array
*/