1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain. Use, modify, and |
4 |
* redistribute this code in any way without acknowledgement. |
5 |
*/ |
6 |
|
7 |
package java.util.concurrent.atomic; |
8 |
import sun.misc.Unsafe; |
9 |
|
10 |
/** |
11 |
* An AtomicInteger maintains an <tt>int</tt> value that is updated |
12 |
* atomically. |
13 |
* @since 1.5 |
14 |
* @author Doug Lea |
15 |
*/ |
16 |
public final class AtomicInteger implements java.io.Serializable { |
17 |
// setup to use Unsafe.compareAndSwapInt for updates |
18 |
private static final Unsafe unsafe = Unsafe.getUnsafe(); |
19 |
private static final long valueOffset; |
20 |
|
21 |
static { |
22 |
try { |
23 |
valueOffset = unsafe.objectFieldOffset |
24 |
(AtomicInteger.class.getDeclaredField("value")); |
25 |
} |
26 |
catch(Exception ex) { throw new Error(ex); } |
27 |
} |
28 |
|
29 |
private volatile int value; |
30 |
|
31 |
/** |
32 |
* Create a new AtomicInteger with the given initial value; |
33 |
* @param initialValue the initial value |
34 |
*/ |
35 |
public AtomicInteger(int initialValue) { |
36 |
value = initialValue; |
37 |
} |
38 |
|
39 |
/** |
40 |
* Get the current value |
41 |
* @return the current value |
42 |
*/ |
43 |
public int get() { |
44 |
return value; |
45 |
} |
46 |
|
47 |
/** |
48 |
* Set to the given value |
49 |
* @param newValue the new value |
50 |
*/ |
51 |
public void set(int newValue) { |
52 |
value = newValue; |
53 |
} |
54 |
|
55 |
/** |
56 |
* Set to the give value and return the old value |
57 |
* @param newValue the new value |
58 |
* @return the previous value |
59 |
*/ |
60 |
public int getAndSet(int newValue) { |
61 |
for (;;) { |
62 |
int current = get(); |
63 |
if (compareAndSet(current, newValue)) |
64 |
return current; |
65 |
} |
66 |
} |
67 |
|
68 |
/** |
69 |
* Atomically increment the current value. |
70 |
* @return the previous value; |
71 |
*/ |
72 |
public int getAndIncrement() { |
73 |
for (;;) { |
74 |
int current = get(); |
75 |
int next = current + 1; |
76 |
if (compareAndSet(current, next)) |
77 |
return current; |
78 |
} |
79 |
} |
80 |
|
81 |
|
82 |
/** |
83 |
* Atomically decrement the current value. |
84 |
* @return the previous value; |
85 |
*/ |
86 |
public int getAndDecrement() { |
87 |
for (;;) { |
88 |
int current = get(); |
89 |
int next = current - 1; |
90 |
if (compareAndSet(current, next)) |
91 |
return current; |
92 |
} |
93 |
} |
94 |
|
95 |
|
96 |
/** |
97 |
* Atomically add the given value to current value. |
98 |
* @param delta the value to add |
99 |
* @return the previous value; |
100 |
*/ |
101 |
public int getAndAdd(int delta) { |
102 |
for (;;) { |
103 |
int current = get(); |
104 |
int next = current + delta; |
105 |
if (compareAndSet(current, next)) |
106 |
return current; |
107 |
} |
108 |
} |
109 |
|
110 |
|
111 |
/** |
112 |
* Atomically set the value to the given updated value |
113 |
* if the current value <tt>==</tt> the expected value. |
114 |
* @param expect the expected value |
115 |
* @param update the new value |
116 |
* @return true if successful. False return indicates that |
117 |
* the actual value was not equal to the expected value. |
118 |
*/ |
119 |
public boolean compareAndSet(int expect, int update) { |
120 |
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); |
121 |
} |
122 |
|
123 |
/** |
124 |
* Atomically set the value to the given updated value |
125 |
* if the current value <tt>==</tt> the expected value. |
126 |
* May fail spuriously. |
127 |
* @param expect the expected value |
128 |
* @param update the new value |
129 |
* @return true if successful. |
130 |
*/ |
131 |
public boolean weakCompareAndSet(int expect, int update) { |
132 |
return unsafe.compareAndSwapInt(this, valueOffset, expect, update); |
133 |
} |
134 |
|
135 |
} |