2 * Written by Doug Lea with assistance from members of JCP JSR-166
3 * Expert Group and released to the public domain, as explained at
4 * http://creativecommons.org/licenses/publicdomain
7 package java.util.concurrent.atomic;
8 import sun.misc.Unsafe;
11 * An <tt>int</tt> value that may be updated atomically. See the
12 * {@link java.util.concurrent.atomic} package specification for
13 * description of the properties of atomic variables. An
14 * <tt>AtomicInteger</tt> is used in applications such as atomically
15 * incremented counters, and cannot be used as a replacement for an
16 * {@link java.lang.Integer}. However, this class does extend
17 * <tt>Number</tt> to allow uniform access by tools and utilities that
18 * deal with numerically-based classes.
23 public class AtomicInteger extends Number implements java.io.Serializable {
24 private static final long serialVersionUID = 6214790243416807050L;
26 // setup to use Unsafe.compareAndSwapInt for updates
27 private static final Unsafe unsafe = Unsafe.getUnsafe();
28 private static final long valueOffset;
32 valueOffset = unsafe.objectFieldOffset
33 (AtomicInteger.class.getDeclaredField("value"));
34 } catch (Exception ex) { throw new Error(ex); }
37 private volatile int value;
40 * Creates a new AtomicInteger with the given initial value.
42 * @param initialValue the initial value
44 public AtomicInteger(int initialValue) {
49 * Creates a new AtomicInteger with initial value <tt>0</tt>.
51 public AtomicInteger() {
55 * Gets the current value.
57 * @return the current value
59 public final int get() {
64 * Sets to the given value.
66 * @param newValue the new value
68 public final void set(int newValue) {
73 * Eventually sets to the given value.
75 * @param newValue the new value
78 public final void lazySet(int newValue) {
79 unsafe.putOrderedInt(this, valueOffset, newValue);
83 * Atomically sets to the given value and returns the old value.
85 * @param newValue the new value
86 * @return the previous value
88 public final int getAndSet(int newValue) {
91 if (compareAndSet(current, newValue))
97 * Atomically sets the value to the given updated value
98 * if the current value <tt>==</tt> the expected value.
100 * @param expect the expected value
101 * @param update the new value
102 * @return true if successful. False return indicates that
103 * the actual value was not equal to the expected value.
105 public final boolean compareAndSet(int expect, int update) {
106 return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
110 * Atomically sets the value to the given updated value
111 * if the current value <tt>==</tt> the expected value.
112 * May fail spuriously and does not provide ordering guarantees,
113 * so is only rarely an appropriate alternative to <tt>compareAndSet</tt>.
115 * @param expect the expected value
116 * @param update the new value
117 * @return true if successful.
119 public final boolean weakCompareAndSet(int expect, int update) {
120 return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
124 * Atomically increments by one the current value.
126 * @return the previous value
128 public final int getAndIncrement() {
131 int next = current + 1;
132 if (compareAndSet(current, next))
138 * Atomically decrements by one the current value.
140 * @return the previous value
142 public final int getAndDecrement() {
145 int next = current - 1;
146 if (compareAndSet(current, next))
152 * Atomically adds the given value to the current value.
154 * @param delta the value to add
155 * @return the previous value
157 public final int getAndAdd(int delta) {
160 int next = current + delta;
161 if (compareAndSet(current, next))
167 * Atomically increments by one the current value.
169 * @return the updated value
171 public final int incrementAndGet() {
174 int next = current + 1;
175 if (compareAndSet(current, next))
181 * Atomically decrements by one the current value.
183 * @return the updated value
185 public final int decrementAndGet() {
188 int next = current - 1;
189 if (compareAndSet(current, next))
195 * Atomically adds the given value to the current value.
197 * @param delta the value to add
198 * @return the updated value
200 public final int addAndGet(int delta) {
203 int next = current + delta;
204 if (compareAndSet(current, next))
210 * Returns the String representation of the current value.
211 * @return the String representation of the current value.
213 public String toString() {
214 return Integer.toString(get());
218 public int intValue() {
222 public long longValue() {
226 public float floatValue() {
230 public double doubleValue() {
231 return (double)get();