--- old/src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java Fri Jan 11 15:08:05 2013 +++ new/src/share/classes/java/util/concurrent/atomic/AtomicIntegerFieldUpdater.java Fri Jan 11 15:08:05 2013 @@ -34,6 +34,8 @@ */ package java.util.concurrent.atomic; +import java.util.function.IntUnaryOperator; +import java.util.function.IntBinaryOperator; import sun.misc.Unsafe; import java.lang.reflect.Field; import java.lang.reflect.Modifier; @@ -262,6 +264,94 @@ next = prev + delta; } while (!compareAndSet(obj, prev, next)); return next; + } + + /** + * Atomically updates the current value with the results of + * applying the given function. The function should be + * side-effect-free, since it may be re-applied when attempted + * updates fail due to contention among threads. + * + * @param obj An object whose field to get and set + * @param updateFunction a side-effect-free function + * @return the previous value + * @since 1.8 + */ + public final int getAndUpdate(T obj, IntUnaryOperator updateFunction) { + int prev, next; + do { + prev = get(obj); + next = updateFunction.operateAsInt(prev); + } while (!compareAndSet(obj, prev, next)); + return prev; + } + + /** + * Atomically updates the current value with the results of + * applying the given function. The function should be + * side-effect-free, since it may be re-applied when attempted + * updates fail due to contention among threads. + * + * @param obj An object whose field to get and set + * @param updateFunction a side-effect-free function + * @return the updated value + * @since 1.8 + */ + public final int updateAndGet(T obj, IntUnaryOperator updateFunction) { + int prev, next; + do { + prev = get(obj); + next = updateFunction.operateAsInt(prev); + } while (!compareAndSet(obj, prev, next)); + return next; + } + + /** + * Atomically updates the current value with the results of + * applying the given function to the current and given values. + * The function should be side-effect-free, since it may be + * re-applied when attempted updates fail due to contention among + * threads. The function is applied with the current value as its + * first argument, and the given update as the second argument. + * + * @param obj An object whose field to get and set + * @param x the update value + * @param accumulatorFunction a side-effect-free function of two arguments + * @return the previous value + * @since 1.8 + */ + public final int getAndAccumulate(T obj, int x, + IntBinaryOperator accumulatorFunction) { + int prev, next; + do { + prev = get(obj); + next = accumulatorFunction.operateAsInt(prev, x); + } while (!compareAndSet(obj, prev, next)); + return prev; + } + + /** + * Atomically updates the current value with the results of + * applying the given function to the current and given values. + * The function should be side-effect-free, since it may be + * re-applied when attempted updates fail due to contention among + * threads. The function is applied with the current value as its + * first argument, and the given update as the second argument. + * + * @param obj An object whose field to get and set + * @param x the update value + * @param accumulatorFunction a side-effect-free function of two arguments + * @return the updated value + * @since 1.8 + */ + public final int accumulateAndGet(T obj, int x, + IntBinaryOperator accumulatorFunction) { + int prev, next; + do { + prev = get(obj); + next = accumulatorFunction.operateAsInt(prev, x); + } while (!compareAndSet(obj, prev, next)); + return next; } /**