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