src/share/classes/java/util/concurrent/atomic/AtomicInteger.java

Print this page




  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * Written by Doug Lea with assistance from members of JCP JSR-166
  32  * Expert Group and released to the public domain, as explained at
  33  * http://creativecommons.org/publicdomain/zero/1.0/
  34  */
  35 
  36 package java.util.concurrent.atomic;


  37 import sun.misc.Unsafe;
  38 
  39 /**
  40  * An {@code int} value that may be updated atomically.  See the
  41  * {@link java.util.concurrent.atomic} package specification for
  42  * description of the properties of atomic variables. An
  43  * {@code AtomicInteger} is used in applications such as atomically
  44  * incremented counters, and cannot be used as a replacement for an
  45  * {@link java.lang.Integer}. However, this class does extend
  46  * {@code Number} to allow uniform access by tools and utilities that
  47  * deal with numerically-based classes.
  48  *
  49  * @since 1.5
  50  * @author Doug Lea
  51 */
  52 public class AtomicInteger extends Number implements java.io.Serializable {
  53     private static final long serialVersionUID = 6214790243416807050L;
  54 
  55     // setup to use Unsafe.compareAndSwapInt for updates
  56     private static final Unsafe unsafe = Unsafe.getUnsafe();


 186 
 187     /**
 188      * Atomically decrements by one the current value.
 189      *
 190      * @return the updated value
 191      */
 192     public final int decrementAndGet() {
 193         return getAndAdd(-1) - 1;
 194     }
 195 
 196     /**
 197      * Atomically adds the given value to the current value.
 198      *
 199      * @param delta the value to add
 200      * @return the updated value
 201      */
 202     public final int addAndGet(int delta) {
 203         return getAndAdd(delta) + delta;
 204     }
 205 




















































































 206     /**
 207      * Returns the String representation of the current value.
 208      * @return the String representation of the current value
 209      */
 210     public String toString() {
 211         return Integer.toString(get());
 212     }
 213 
 214     /**
 215      * Returns the value of this {@code AtomicInteger} as an {@code int}.
 216      */
 217     public int intValue() {
 218         return get();
 219     }
 220 
 221     /**
 222      * Returns the value of this {@code AtomicInteger} as a {@code long}
 223      * after a widening primitive conversion.
 224      * @jls 5.1.2 Widening Primitive Conversions
 225      */




  17  * 2 along with this work; if not, write to the Free Software Foundation,
  18  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  19  *
  20  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  21  * or visit www.oracle.com if you need additional information or have any
  22  * questions.
  23  */
  24 
  25 /*
  26  * This file is available under and governed by the GNU General Public
  27  * License version 2 only, as published by the Free Software Foundation.
  28  * However, the following notice accompanied the original version of this
  29  * file:
  30  *
  31  * Written by Doug Lea with assistance from members of JCP JSR-166
  32  * Expert Group and released to the public domain, as explained at
  33  * http://creativecommons.org/publicdomain/zero/1.0/
  34  */
  35 
  36 package java.util.concurrent.atomic;
  37 import java.util.function.IntUnaryOperator;
  38 import java.util.function.IntBinaryOperator;
  39 import sun.misc.Unsafe;
  40 
  41 /**
  42  * An {@code int} value that may be updated atomically.  See the
  43  * {@link java.util.concurrent.atomic} package specification for
  44  * description of the properties of atomic variables. An
  45  * {@code AtomicInteger} is used in applications such as atomically
  46  * incremented counters, and cannot be used as a replacement for an
  47  * {@link java.lang.Integer}. However, this class does extend
  48  * {@code Number} to allow uniform access by tools and utilities that
  49  * deal with numerically-based classes.
  50  *
  51  * @since 1.5
  52  * @author Doug Lea
  53 */
  54 public class AtomicInteger extends Number implements java.io.Serializable {
  55     private static final long serialVersionUID = 6214790243416807050L;
  56 
  57     // setup to use Unsafe.compareAndSwapInt for updates
  58     private static final Unsafe unsafe = Unsafe.getUnsafe();


 188 
 189     /**
 190      * Atomically decrements by one the current value.
 191      *
 192      * @return the updated value
 193      */
 194     public final int decrementAndGet() {
 195         return getAndAdd(-1) - 1;
 196     }
 197 
 198     /**
 199      * Atomically adds the given value to the current value.
 200      *
 201      * @param delta the value to add
 202      * @return the updated value
 203      */
 204     public final int addAndGet(int delta) {
 205         return getAndAdd(delta) + delta;
 206     }
 207 
 208     /**
 209      * Atomically updates the current value with the results of
 210      * applying the given function. The function should be
 211      * side-effect-free, since it may be re-applied when attempted
 212      * updates fail due to contention among threads.
 213      *
 214      * @param updateFunction a side-effect-free function
 215      * @return the previous value
 216      * @since 1.8
 217      */
 218     public final int getAndUpdate(IntUnaryOperator updateFunction) {
 219         int prev, next;
 220         do {
 221             prev = get();
 222             next = updateFunction.operateAsInt(prev);
 223         } while (!compareAndSet(prev, next));
 224         return prev;
 225     }
 226 
 227     /**
 228      * Atomically updates the current value with the results of
 229      * applying the given function. The function should be
 230      * side-effect-free, since it may be re-applied when attempted
 231      * updates fail due to contention among threads.
 232      *
 233      * @param updateFunction a side-effect-free function
 234      * @return the updated value
 235      * @since 1.8
 236      */
 237     public final int updateAndGet(IntUnaryOperator updateFunction) {
 238         int prev, next;
 239         do {
 240             prev = get();
 241             next = updateFunction.operateAsInt(prev);
 242         } while (!compareAndSet(prev, next));
 243         return next;
 244     }
 245 
 246     /**
 247      * Atomically updates the current value with the results of
 248      * applying the given function to the current and given values.
 249      * The function should be side-effect-free, since it may be
 250      * re-applied when attempted updates fail due to contention among
 251      * threads.  The function is applied with the current value as its
 252      * first argument, and the given update as the second argument.
 253      *
 254      * @param x the update value
 255      * @param accumulatorFunction a side-effect-free function of two arguments
 256      * @return the previous value
 257      * @since 1.8
 258      */
 259     public final int getAndAccumulate(int x,
 260                                       IntBinaryOperator accumulatorFunction) {
 261         int prev, next;
 262         do {
 263             prev = get();
 264             next = accumulatorFunction.operateAsInt(prev, x);
 265         } while (!compareAndSet(prev, next));
 266         return prev;
 267     }
 268 
 269     /**
 270      * Atomically updates the current value with the results of
 271      * applying the given function to the current and given values.
 272      * The function should be side-effect-free, since it may be
 273      * re-applied when attempted updates fail due to contention among
 274      * threads.  The function is applied with the current value as its
 275      * first argument, and the given update as the second argument.
 276      *
 277      * @param x the update value
 278      * @param accumulatorFunction a side-effect-free function of two arguments
 279      * @return the updated value
 280      * @since 1.8
 281      */
 282     public final int accumulateAndGet(int x,
 283                                       IntBinaryOperator accumulatorFunction) {
 284         int prev, next;
 285         do {
 286             prev = get();
 287             next = accumulatorFunction.operateAsInt(prev, x);
 288         } while (!compareAndSet(prev, next));
 289         return next;
 290     }
 291 
 292     /**
 293      * Returns the String representation of the current value.
 294      * @return the String representation of the current value
 295      */
 296     public String toString() {
 297         return Integer.toString(get());
 298     }
 299 
 300     /**
 301      * Returns the value of this {@code AtomicInteger} as an {@code int}.
 302      */
 303     public int intValue() {
 304         return get();
 305     }
 306 
 307     /**
 308      * Returns the value of this {@code AtomicInteger} as a {@code long}
 309      * after a widening primitive conversion.
 310      * @jls 5.1.2 Widening Primitive Conversions
 311      */