< prev index next >

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

Print this page
8225490: Miscellaneous changes imported from jsr166 CVS 2019-09
Reviewed-by: martin, alanb


  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 
  38 import java.lang.invoke.VarHandle;
  39 import java.util.function.IntBinaryOperator;
  40 import java.util.function.IntUnaryOperator;

  41 
  42 /**
  43  * An {@code int} value that may be updated atomically.  See the
  44  * {@link VarHandle} specification for descriptions of the properties
  45  * of atomic accesses. An {@code AtomicInteger} is used in
  46  * applications such as atomically incremented counters, and cannot be
  47  * used as a replacement for an {@link java.lang.Integer}. However,
  48  * this class does extend {@code Number} to allow uniform access by
  49  * tools and utilities that 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     /*
  58      * This class intended to be implemented using VarHandles, but there
  59      * are unresolved cyclic startup dependencies.
  60      */
  61     private static final jdk.internal.misc.Unsafe U = jdk.internal.misc.Unsafe.getUnsafe();
  62     private static final long VALUE = U.objectFieldOffset(AtomicInteger.class, "value");

  63 
  64     private volatile int value;
  65 
  66     /**
  67      * Creates a new AtomicInteger with the given initial value.
  68      *
  69      * @param initialValue the initial value
  70      */
  71     public AtomicInteger(int initialValue) {
  72         value = initialValue;
  73     }
  74 
  75     /**
  76      * Creates a new AtomicInteger with initial value {@code 0}.
  77      */
  78     public AtomicInteger() {
  79     }
  80 
  81     /**
  82      * Returns the current value,




  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 
  38 import java.lang.invoke.VarHandle;
  39 import java.util.function.IntBinaryOperator;
  40 import java.util.function.IntUnaryOperator;
  41 import jdk.internal.misc.Unsafe;
  42 
  43 /**
  44  * An {@code int} value that may be updated atomically.  See the
  45  * {@link VarHandle} specification for descriptions of the properties
  46  * of atomic accesses. An {@code AtomicInteger} is used in
  47  * applications such as atomically incremented counters, and cannot be
  48  * used as a replacement for an {@link java.lang.Integer}. However,
  49  * this class does extend {@code Number} to allow uniform access by
  50  * tools and utilities that deal with numerically-based classes.
  51  *
  52  * @since 1.5
  53  * @author Doug Lea
  54  */
  55 public class AtomicInteger extends Number implements java.io.Serializable {
  56     private static final long serialVersionUID = 6214790243416807050L;
  57 
  58     /*
  59      * This class intended to be implemented using VarHandles, but there
  60      * are unresolved cyclic startup dependencies.
  61      */
  62     private static final Unsafe U = Unsafe.getUnsafe();
  63     private static final long VALUE
  64         = U.objectFieldOffset(AtomicInteger.class, "value");
  65 
  66     private volatile int value;
  67 
  68     /**
  69      * Creates a new AtomicInteger with the given initial value.
  70      *
  71      * @param initialValue the initial value
  72      */
  73     public AtomicInteger(int initialValue) {
  74         value = initialValue;
  75     }
  76 
  77     /**
  78      * Creates a new AtomicInteger with initial value {@code 0}.
  79      */
  80     public AtomicInteger() {
  81     }
  82 
  83     /**
  84      * Returns the current value,


< prev index next >