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) {
82 //unsafe.putOrderedInt(this, valueOffset, newValue);
86 * Atomically sets to the given value and returns the old value.
88 * @param newValue the new value
89 * @return the previous value
91 public final int getAndSet(int newValue) {
94 if (compareAndSet(current, newValue))
100 * Atomically sets the value to the given updated value
101 * if the current value <tt>==</tt> the expected value.
103 * @param expect the expected value
104 * @param update the new value
105 * @return true if successful. False return indicates that
106 * the actual value was not equal to the expected value.
108 public final boolean compareAndSet(int expect, int update) {
109 synchronized (this) {
110 if(expect == value) {
117 //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
121 * Atomically sets the value to the given updated value
122 * if the current value <tt>==</tt> the expected value.
123 * May fail spuriously and does not provide ordering guarantees,
124 * so is only rarely an appropriate alternative to <tt>compareAndSet</tt>.
126 * @param expect the expected value
127 * @param update the new value
128 * @return true if successful.
130 public final boolean weakCompareAndSet(int expect, int update) {
131 synchronized (this) {
132 if(expect == value) {
139 //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
143 * Atomically increments by one the current value.
145 * @return the previous value
147 public final int getAndIncrement() {
150 int next = current + 1;
151 if (compareAndSet(current, next))
157 * Atomically decrements by one the current value.
159 * @return the previous value
161 public final int getAndDecrement() {
164 int next = current - 1;
165 if (compareAndSet(current, next))
171 * Atomically adds the given value to the current value.
173 * @param delta the value to add
174 * @return the previous value
176 public final int getAndAdd(int delta) {
179 int next = current + delta;
180 if (compareAndSet(current, next))
186 * Atomically increments by one the current value.
188 * @return the updated value
190 public final int incrementAndGet() {
193 int next = current + 1;
194 if (compareAndSet(current, next))
200 * Atomically decrements by one the current value.
202 * @return the updated value
204 public final int decrementAndGet() {
207 int next = current - 1;
208 if (compareAndSet(current, next))
214 * Atomically adds the given value to the current value.
216 * @param delta the value to add
217 * @return the updated value
219 public final int addAndGet(int delta) {
222 int next = current + delta;
223 if (compareAndSet(current, next))
229 * Returns the String representation of the current value.
230 * @return the String representation of the current value.
232 /*public String toString() {
233 return Integer.toString(get());
237 public int intValue() {
241 public long longValue() {
245 public float floatValue() {
249 public double doubleValue() {
250 return (double)get();