src/share/classes/java/util/concurrent/atomic/AtomicLong.java
Print this page
*** 127,142 ****
*
* @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;
}
- }
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
*
--- 127,138 ----
*
* @param newValue the new value
* @return the previous value
*/
public final long getAndSet(long newValue) {
! return unsafe.getAndSetLong(this, valueOffset, newValue);
}
/**
* Atomically sets the value to the given updated value
* if the current value {@code ==} the expected value.
*
*** 157,167 ****
* 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.
*/
public final boolean weakCompareAndSet(long expect, long update) {
return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
}
--- 153,163 ----
* 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
*/
public final boolean weakCompareAndSet(long expect, long update) {
return unsafe.compareAndSwapLong(this, valueOffset, expect, update);
}
*** 169,261 ****
* 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;
}
- }
/**
* 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;
}
- }
/**
* 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;
}
- }
/**
* 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;
}
- }
/**
* 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;
}
- }
/**
* 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;
}
- }
/**
* Returns the String representation of the current value.
! * @return the String representation of the current value.
*/
public String toString() {
return Long.toString(get());
}
--- 165,227 ----
* Atomically increments by one the current value.
*
* @return the previous value
*/
public final long getAndIncrement() {
! return getAndAdd(1);
}
/**
* Atomically decrements by one the current value.
*
* @return the previous value
*/
public final long getAndDecrement() {
! 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) {
! return unsafe.getAndAddLong(this, valueOffset, delta);
}
/**
* Atomically increments by one the current value.
*
* @return the updated value
*/
public final long incrementAndGet() {
! return getAndAdd(1) + 1;
}
/**
* Atomically decrements by one the current value.
*
* @return the updated value
*/
public final long decrementAndGet() {
! 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) {
! return getAndAdd(delta) + delta;
}
/**
* Returns the String representation of the current value.
! * @return the String representation of the current value
*/
public String toString() {
return Long.toString(get());
}