jdk/src/share/classes/java/util/concurrent/atomic/AtomicIntegerArray.java
changeset 15267 c884f548a25f
parent 15020 50394fa17c1b
child 16011 890a7ed97f6c
equal deleted inserted replaced
15266:379788c73130 15267:c884f548a25f
    32  * Expert Group and released to the public domain, as explained at
    32  * Expert Group and released to the public domain, as explained at
    33  * http://creativecommons.org/publicdomain/zero/1.0/
    33  * http://creativecommons.org/publicdomain/zero/1.0/
    34  */
    34  */
    35 
    35 
    36 package java.util.concurrent.atomic;
    36 package java.util.concurrent.atomic;
       
    37 import java.util.function.IntUnaryOperator;
       
    38 import java.util.function.IntBinaryOperator;
    37 import sun.misc.Unsafe;
    39 import sun.misc.Unsafe;
    38 
    40 
    39 /**
    41 /**
    40  * An {@code int} array in which elements may be updated atomically.
    42  * An {@code int} array in which elements may be updated atomically.
    41  * See the {@link java.util.concurrent.atomic} package
    43  * See the {@link java.util.concurrent.atomic} package
   244     public final int addAndGet(int i, int delta) {
   246     public final int addAndGet(int i, int delta) {
   245         return getAndAdd(i, delta) + delta;
   247         return getAndAdd(i, delta) + delta;
   246     }
   248     }
   247 
   249 
   248     /**
   250     /**
       
   251      * Atomically updates the element at index {@code i} with the results
       
   252      * of applying the given function, returning the previous value. The
       
   253      * function should be side-effect-free, since it may be re-applied
       
   254      * when attempted updates fail due to contention among threads.
       
   255      *
       
   256      * @param i the index
       
   257      * @param updateFunction a side-effect-free function
       
   258      * @return the previous value
       
   259      * @since 1.8
       
   260      */
       
   261     public final int getAndUpdate(int i, IntUnaryOperator updateFunction) {
       
   262         long offset = checkedByteOffset(i);
       
   263         int prev, next;
       
   264         do {
       
   265             prev = getRaw(offset);
       
   266             next = updateFunction.operateAsInt(prev);
       
   267         } while (!compareAndSetRaw(offset, prev, next));
       
   268         return prev;
       
   269     }
       
   270 
       
   271     /**
       
   272      * Atomically updates the element at index {@code i} with the results
       
   273      * of applying the given function, returning the updated value. The
       
   274      * function should be side-effect-free, since it may be re-applied
       
   275      * when attempted updates fail due to contention among threads.
       
   276      *
       
   277      * @param i the index
       
   278      * @param updateFunction a side-effect-free function
       
   279      * @return the updated value
       
   280      * @since 1.8
       
   281      */
       
   282     public final int updateAndGet(int i, IntUnaryOperator updateFunction) {
       
   283         long offset = checkedByteOffset(i);
       
   284         int prev, next;
       
   285         do {
       
   286             prev = getRaw(offset);
       
   287             next = updateFunction.operateAsInt(prev);
       
   288         } while (!compareAndSetRaw(offset, prev, next));
       
   289         return next;
       
   290     }
       
   291 
       
   292     /**
       
   293      * Atomically updates the element at index {@code i} with the
       
   294      * results of applying the given function to the current and
       
   295      * given values, returning the previous value. The function should
       
   296      * be side-effect-free, since it may be re-applied when attempted
       
   297      * updates fail due to contention among threads.  The function is
       
   298      * applied with the current value at index {@code i} as its first
       
   299      * argument, and the given update as the second argument.
       
   300      *
       
   301      * @param i the index
       
   302      * @param x the update value
       
   303      * @param accumulatorFunction a side-effect-free function of two arguments
       
   304      * @return the previous value
       
   305      * @since 1.8
       
   306      */
       
   307     public final int getAndAccumulate(int i, int x,
       
   308                                       IntBinaryOperator accumulatorFunction) {
       
   309         long offset = checkedByteOffset(i);
       
   310         int prev, next;
       
   311         do {
       
   312             prev = getRaw(offset);
       
   313             next = accumulatorFunction.operateAsInt(prev, x);
       
   314         } while (!compareAndSetRaw(offset, prev, next));
       
   315         return prev;
       
   316     }
       
   317 
       
   318     /**
       
   319      * Atomically updates the element at index {@code i} with the
       
   320      * results of applying the given function to the current and
       
   321      * given values, returning the updated value. The function should
       
   322      * be side-effect-free, since it may be re-applied when attempted
       
   323      * updates fail due to contention among threads.  The function is
       
   324      * applied with the current value at index {@code i} as its first
       
   325      * argument, and the given update as the second argument.
       
   326      *
       
   327      * @param i the index
       
   328      * @param x the update value
       
   329      * @param accumulatorFunction a side-effect-free function of two arguments
       
   330      * @return the updated value
       
   331      * @since 1.8
       
   332      */
       
   333     public final int accumulateAndGet(int i, int x,
       
   334                                       IntBinaryOperator accumulatorFunction) {
       
   335         long offset = checkedByteOffset(i);
       
   336         int prev, next;
       
   337         do {
       
   338             prev = getRaw(offset);
       
   339             next = accumulatorFunction.operateAsInt(prev, x);
       
   340         } while (!compareAndSetRaw(offset, prev, next));
       
   341         return next;
       
   342     }
       
   343 
       
   344     /**
   249      * Returns the String representation of the current values of array.
   345      * Returns the String representation of the current values of array.
   250      * @return the String representation of the current values of array
   346      * @return the String representation of the current values of array
   251      */
   347      */
   252     public String toString() {
   348     public String toString() {
   253         int iMax = array.length - 1;
   349         int iMax = array.length - 1;