src/share/classes/java/util/concurrent/atomic/AtomicIntegerArray.java
Print this page
@@ -143,17 +143,12 @@
* @param i the index
* @param newValue the new value
* @return the previous value
*/
public final int getAndSet(int i, int newValue) {
- long offset = checkedByteOffset(i);
- while (true) {
- int current = getRaw(offset);
- if (compareAndSetRaw(offset, current, newValue))
- return current;
+ return unsafe.getAndSetInt(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.
*
@@ -180,11 +175,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, int expect, int update) {
return compareAndSet(i, expect, update);
}
@@ -214,36 +209,31 @@
* @param i the index
* @param delta the value to add
* @return the previous value
*/
public final int getAndAdd(int i, int delta) {
- long offset = checkedByteOffset(i);
- while (true) {
- int current = getRaw(offset);
- if (compareAndSetRaw(offset, current, current + delta))
- return current;
+ return unsafe.getAndAddInt(array, checkedByteOffset(i), delta);
}
- }
/**
* Atomically increments by one the element at index {@code i}.
*
* @param i the index
* @return the updated value
*/
public final int 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 int decrementAndGet(int i) {
- return addAndGet(i, -1);
+ return getAndAdd(i, -1) - 1;
}
/**
* Atomically adds the given value to the element at index {@code i}.
*
@@ -250,18 +240,12 @@
* @param i the index
* @param delta the value to add
* @return the updated value
*/
public final int addAndGet(int i, int delta) {
- long offset = checkedByteOffset(i);
- while (true) {
- int current = getRaw(offset);
- int 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
*/