2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 * This code is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License version 2 only, as
6 * published by the Free Software Foundation. Oracle designates this
7 * particular file as subject to the "Classpath" exception as provided
8 * by Oracle in the LICENSE file that accompanied this code.
10 * This code is distributed in the hope that it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 * version 2 for more details (a copy is included in the LICENSE file that
14 * accompanied this code).
16 * You should have received a copy of the GNU General Public License version
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.
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
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
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/
36 package java.util.concurrent.atomic;
37 import sun.misc.Unsafe;
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.
52 public class AtomicInteger extends Number implements java.io.Serializable {
53 private static final long serialVersionUID = 6214790243416807050L;
55 // setup to use Unsafe.compareAndSwapInt for updates
56 private static final Unsafe unsafe = Unsafe.getUnsafe();
57 private static final long valueOffset;
61 valueOffset = unsafe.objectFieldOffset
62 (AtomicInteger.class.getDeclaredField("value"));
63 } catch (Exception ex) { throw new Error(ex); }
66 private volatile int value;
69 * Creates a new AtomicInteger with the given initial value.
71 * @param initialValue the initial value
73 public AtomicInteger(int initialValue) {
78 * Creates a new AtomicInteger with initial value {@code 0}.
80 public AtomicInteger() {
84 * Gets the current value.
86 * @return the current value
88 public final int get() {
93 * Sets to the given value.
95 * @param newValue the new value
97 public final void set(int newValue) {
102 * Eventually sets to the given value.
104 * @param newValue the new value
107 public final void lazySet(int newValue) {
108 unsafe.putOrderedInt(this, valueOffset, newValue);
112 * Atomically sets to the given value and returns the old value.
114 * @param newValue the new value
115 * @return the previous value
117 public final int getAndSet(int newValue) {
120 if (compareAndSet(current, newValue))
126 * Atomically sets the value to the given updated value
127 * if the current value {@code ==} the expected value.
129 * @param expect the expected value
130 * @param update the new value
131 * @return true if successful. False return indicates that
132 * the actual value was not equal to the expected value.
134 public final boolean compareAndSet(int expect, int update) {
135 return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
139 * Atomically sets the value to the given updated value
140 * if the current value {@code ==} the expected value.
142 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
143 * and does not provide ordering guarantees, so is only rarely an
144 * appropriate alternative to {@code compareAndSet}.
146 * @param expect the expected value
147 * @param update the new value
148 * @return true if successful.
150 public final boolean weakCompareAndSet(int expect, int update) {
151 return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
155 * Atomically increments by one the current value.
157 * @return the previous value
159 public final int getAndIncrement() {
162 int next = current + 1;
163 if (compareAndSet(current, next))
169 * Atomically decrements by one the current value.
171 * @return the previous value
173 public final int getAndDecrement() {
176 int next = current - 1;
177 if (compareAndSet(current, next))
183 * Atomically adds the given value to the current value.
185 * @param delta the value to add
186 * @return the previous value
188 public final int getAndAdd(int delta) {
191 int next = current + delta;
192 if (compareAndSet(current, next))
198 * Atomically increments by one the current value.
200 * @return the updated value
202 public final int incrementAndGet() {
205 int next = current + 1;
206 if (compareAndSet(current, next))
212 * Atomically decrements by one the current value.
214 * @return the updated value
216 public final int decrementAndGet() {
219 int next = current - 1;
220 if (compareAndSet(current, next))
226 * Atomically adds the given value to the current value.
228 * @param delta the value to add
229 * @return the updated value
231 public final int addAndGet(int delta) {
234 int next = current + delta;
235 if (compareAndSet(current, next))
241 * Returns the String representation of the current value.
242 * @return the String representation of the current value.
244 public String toString() {
245 return Integer.toString(get());
249 public int intValue() {
253 public long longValue() {
257 public float floatValue() {
261 public double doubleValue() {
262 return (double)get();