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);
80 System.out.println("Unimplemented AtomicInteger.lazySet()!");
84 * Atomically sets to the given value and returns the old value.
86 * @param newValue the new value
87 * @return the previous value
89 public final int getAndSet(int newValue) {
92 if (compareAndSet(current, newValue))
98 * Atomically sets the value to the given updated value
99 * if the current value <tt>==</tt> the expected value.
101 * @param expect the expected value
102 * @param update the new value
103 * @return true if successful. False return indicates that
104 * the actual value was not equal to the expected value.
106 public final boolean compareAndSet(int expect, int update) {
107 System.out.println("Unimplemented AtomicInteger.compareAndSet()!");
108 return false; //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
112 * Atomically sets the value to the given updated value
113 * if the current value <tt>==</tt> the expected value.
114 * May fail spuriously and does not provide ordering guarantees,
115 * so is only rarely an appropriate alternative to <tt>compareAndSet</tt>.
117 * @param expect the expected value
118 * @param update the new value
119 * @return true if successful.
121 public final boolean weakCompareAndSet(int expect, int update) {
122 System.out.println("Unimplemented AtomicInteger.weakCompareAndSet()!");
123 return false; //unsafe.compareAndSwapInt(this, valueOffset, expect, update);
127 * Atomically increments by one the current value.
129 * @return the previous value
131 public final int getAndIncrement() {
134 int next = current + 1;
135 if (compareAndSet(current, next))
141 * Atomically decrements by one the current value.
143 * @return the previous value
145 public final int getAndDecrement() {
148 int next = current - 1;
149 if (compareAndSet(current, next))
155 * Atomically adds the given value to the current value.
157 * @param delta the value to add
158 * @return the previous value
160 public final int getAndAdd(int delta) {
163 int next = current + delta;
164 if (compareAndSet(current, next))
170 * Atomically increments by one the current value.
172 * @return the updated value
174 public final int incrementAndGet() {
177 int next = current + 1;
178 if (compareAndSet(current, next))
184 * Atomically decrements by one the current value.
186 * @return the updated value
188 public final int decrementAndGet() {
191 int next = current - 1;
192 if (compareAndSet(current, next))
198 * Atomically adds the given value to the current value.
200 * @param delta the value to add
201 * @return the updated value
203 public final int addAndGet(int delta) {
206 int next = current + delta;
207 if (compareAndSet(current, next))
213 * Returns the String representation of the current value.
214 * @return the String representation of the current value.
216 /*public String toString() {
217 return Integer.toString(get());
221 public int intValue() {
225 public long longValue() {
229 public float floatValue() {
233 public double doubleValue() {
234 return (double)get();