KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > java > util > concurrent > atomic > AtomicInteger


1 /*
2  * @(#)AtomicInteger.java 1.5 04/01/12
3  *
4  * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
5  * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
6  */

7
8 package java.util.concurrent.atomic;
9 import sun.misc.Unsafe;
10
11 /**
12  * An <tt>int</tt> value that may be updated atomically. See the
13  * {@link java.util.concurrent.atomic} package specification for
14  * description of the properties of atomic variables. An
15  * <tt>AtomicInteger</tt> is used in applications such as atomically
16  * incremented counters, and cannot be used as a replacement for an
17  * {@link java.lang.Integer}. However, this class does extend
18  * <tt>Number</tt> to allow uniform access by tools and utilities that
19  * deal with numerically-based classes.
20  *
21  *
22  * @since 1.5
23  * @author Doug Lea
24 */

25 public class AtomicInteger extends Number JavaDoc implements java.io.Serializable JavaDoc {
26     private static final long serialVersionUID = 6214790243416807050L;
27
28     // setup to use Unsafe.compareAndSwapInt for updates
29
private static final Unsafe unsafe = Unsafe.getUnsafe();
30     private static final long valueOffset;
31
32     static {
33       try {
34         valueOffset = unsafe.objectFieldOffset
35             (AtomicInteger JavaDoc.class.getDeclaredField("value"));
36       } catch(Exception JavaDoc ex) { throw new Error JavaDoc(ex); }
37     }
38
39     private volatile int value;
40
41     /**
42      * Create a new AtomicInteger with the given initial value.
43      *
44      * @param initialValue the initial value
45      */

46     public AtomicInteger(int initialValue) {
47         value = initialValue;
48     }
49
50     /**
51      * Create a new AtomicInteger with initial value <tt>0</tt>.
52      */

53     public AtomicInteger() {
54     }
55
56     /**
57      * Get the current value.
58      *
59      * @return the current value
60      */

61     public final int get() {
62         return value;
63     }
64   
65     /**
66      * Set to the given value.
67      *
68      * @param newValue the new value
69      */

70     public final void set(int newValue) {
71         value = newValue;
72     }
73
74     /**
75      * Set to the give value and return the old value.
76      *
77      * @param newValue the new value
78      * @return the previous value
79      */

80     public final int getAndSet(int newValue) {
81         for (;;) {
82             int current = get();
83             if (compareAndSet(current, newValue))
84                 return current;
85         }
86     }
87   
88   
89     /**
90      * Atomically set the value to the given updated value
91      * if the current value <tt>==</tt> the expected value.
92      * @param expect the expected value
93      * @param update the new value
94      * @return true if successful. False return indicates that
95      * the actual value was not equal to the expected value.
96      */

97     public final boolean compareAndSet(int expect, int update) {
98       return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
99     }
100
101     /**
102      * Atomically set the value to the given updated value
103      * if the current value <tt>==</tt> the expected value.
104      * May fail spuriously.
105      * @param expect the expected value
106      * @param update the new value
107      * @return true if successful.
108      */

109     public final boolean weakCompareAndSet(int expect, int update) {
110       return unsafe.compareAndSwapInt(this, valueOffset, expect, update);
111     }
112
113
114     /**
115      * Atomically increment by one the current value.
116      * @return the previous value
117      */

118     public final int getAndIncrement() {
119         for (;;) {
120             int current = get();
121             int next = current + 1;
122             if (compareAndSet(current, next))
123                 return current;
124         }
125     }
126   
127   
128     /**
129      * Atomically decrement by one the current value.
130      * @return the previous value
131      */

132     public final int getAndDecrement() {
133         for (;;) {
134             int current = get();
135             int next = current - 1;
136             if (compareAndSet(current, next))
137                 return current;
138         }
139     }
140   
141   
142     /**
143      * Atomically add the given value to current value.
144      * @param delta the value to add
145      * @return the previous value
146      */

147     public final int getAndAdd(int delta) {
148         for (;;) {
149             int current = get();
150             int next = current + delta;
151             if (compareAndSet(current, next))
152                 return current;
153         }
154     }
155
156     /**
157      * Atomically increment by one the current value.
158      * @return the updated value
159      */

160     public final int incrementAndGet() {
161         for (;;) {
162             int current = get();
163             int next = current + 1;
164             if (compareAndSet(current, next))
165                 return next;
166         }
167     }
168     
169     /**
170      * Atomically decrement by one the current value.
171      * @return the updated value
172      */

173     public final int decrementAndGet() {
174         for (;;) {
175             int current = get();
176             int next = current - 1;
177             if (compareAndSet(current, next))
178                 return next;
179         }
180     }
181   
182   
183     /**
184      * Atomically add the given value to current value.
185      * @param delta the value to add
186      * @return the updated value
187      */

188     public final int addAndGet(int delta) {
189         for (;;) {
190             int current = get();
191             int next = current + delta;
192             if (compareAndSet(current, next))
193                 return next;
194         }
195     }
196
197     /**
198      * Returns the String representation of the current value.
199      * @return the String representation of the current value.
200      */

201     public String JavaDoc toString() {
202         return Integer.toString(get());
203     }
204
205
206     public int intValue() {
207     return get();
208     }
209
210     public long longValue() {
211     return (long)get();
212     }
213
214     public float floatValue() {
215     return (float)get();
216     }
217
218     public double doubleValue() {
219     return (double)get();
220     }
221
222 }
223
Popular Tags