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;
39 * An {@code int} value that may be updated atomically. See the
40 * {@link java.util.concurrent.atomic} package specification for
41 * description of the properties of atomic variables. An
42 * {@code AtomicInteger} is used in applications such as atomically
43 * incremented counters, and cannot be used as a replacement for an
44 * {@link java.lang.Integer}. However, this class does extend
45 * {@code Number} to allow uniform access by tools and utilities that
46 * deal with numerically-based classes.
51 public class AtomicInteger extends Number implements java.io.Serializable {
52 private static final long serialVersionUID = 6214790243416807050L;
54 private volatile int value;
57 * Creates a new AtomicInteger with the given initial value.
59 * @param initialValue the initial value
61 public AtomicInteger(int initialValue) {
66 * Creates a new AtomicInteger with initial value {@code 0}.
68 public AtomicInteger() {
72 * Gets the current value.
74 * @return the current value
76 public final int get() {
81 * Sets to the given value.
83 * @param newValue the new value
85 public final void set(int newValue) {
90 * Eventually sets to the given value.
92 * @param newValue the new value
95 public final void lazySet(int newValue) {
100 * Atomically sets to the given value and returns the old value.
102 * @param newValue the new value
103 * @return the previous value
105 public final int getAndSet(int newValue) {
108 if (compareAndSet(current, newValue))
114 * Atomically sets the value to the given updated value
115 * if the current value {@code ==} the expected value.
117 * @param expect the expected value
118 * @param update the new value
119 * @return true if successful. False return indicates that
120 * the actual value was not equal to the expected value.
122 public final boolean compareAndSet(int expect, int update) {
123 if (value == expect) {
132 * Atomically sets the value to the given updated value
133 * if the current value {@code ==} the expected value.
135 * <p>May <a href="package-summary.html#Spurious">fail spuriously</a>
136 * and does not provide ordering guarantees, so is only rarely an
137 * appropriate alternative to {@code compareAndSet}.
139 * @param expect the expected value
140 * @param update the new value
141 * @return true if successful.
143 public final boolean weakCompareAndSet(int expect, int update) {
144 return compareAndSet(expect, update);
148 * Atomically increments by one the current value.
150 * @return the previous value
152 public final int getAndIncrement() {
155 int next = current + 1;
156 if (compareAndSet(current, next))
162 * Atomically decrements by one the current value.
164 * @return the previous value
166 public final int getAndDecrement() {
169 int next = current - 1;
170 if (compareAndSet(current, next))
176 * Atomically adds the given value to the current value.
178 * @param delta the value to add
179 * @return the previous value
181 public final int getAndAdd(int delta) {
184 int next = current + delta;
185 if (compareAndSet(current, next))
191 * Atomically increments by one the current value.
193 * @return the updated value
195 public final int incrementAndGet() {
198 int next = current + 1;
199 if (compareAndSet(current, next))
205 * Atomically decrements by one the current value.
207 * @return the updated value
209 public final int decrementAndGet() {
212 int next = current - 1;
213 if (compareAndSet(current, next))
219 * Atomically adds the given value to the current value.
221 * @param delta the value to add
222 * @return the updated value
224 public final int addAndGet(int delta) {
227 int next = current + delta;
228 if (compareAndSet(current, next))
234 * Returns the String representation of the current value.
235 * @return the String representation of the current value.
237 public String toString() {
238 return Integer.toString(get());
242 public int intValue() {
246 public long longValue() {
250 public float floatValue() {
254 public double doubleValue() {
255 return (double)get();