1 |
|
/* |
2 |
< |
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
< |
* public domain. Use, modify, and redistribute this code in any way |
4 |
< |
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
< |
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
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/publicdomain/zero/1.0/ |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
8 |
– |
import java.util.concurrent.atomic.*; |
9 |
|
import junit.framework.*; |
10 |
< |
import java.util.*; |
10 |
> |
import java.util.concurrent.atomic.AtomicIntegerFieldUpdater; |
11 |
|
|
12 |
|
public class AtomicIntegerFieldUpdaterTest extends JSR166TestCase { |
13 |
|
volatile int x = 0; |
14 |
|
int w; |
15 |
|
long z; |
16 |
< |
public static void main(String[] args){ |
16 |
> |
public static void main(String[] args) { |
17 |
|
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
21 |
|
} |
22 |
|
|
23 |
|
/** |
24 |
< |
* Contruction with non-existent field throws RuntimeException |
24 |
> |
* Construction with non-existent field throws RuntimeException |
25 |
|
*/ |
26 |
|
public void testConstructor() { |
27 |
< |
try{ |
28 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
27 |
> |
try { |
28 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
29 |
|
a = AtomicIntegerFieldUpdater.newUpdater |
30 |
< |
(getClass(), "y"); |
30 |
> |
(AtomicIntegerFieldUpdaterTest.class, "y"); |
31 |
|
shouldThrow(); |
32 |
< |
} |
33 |
< |
catch (RuntimeException rt) {} |
32 |
> |
} catch (RuntimeException success) {} |
33 |
|
} |
34 |
|
|
35 |
|
/** |
36 |
|
* construction with field not of given type throws RuntimeException |
37 |
|
*/ |
38 |
|
public void testConstructor2() { |
39 |
< |
try{ |
40 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
39 |
> |
try { |
40 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
41 |
|
a = AtomicIntegerFieldUpdater.newUpdater |
42 |
< |
(getClass(), "z"); |
42 |
> |
(AtomicIntegerFieldUpdaterTest.class, "z"); |
43 |
|
shouldThrow(); |
44 |
< |
} |
46 |
< |
catch (RuntimeException rt) {} |
44 |
> |
} catch (RuntimeException success) {} |
45 |
|
} |
46 |
|
|
47 |
|
/** |
48 |
|
* construction with non-volatile field throws RuntimeException |
49 |
|
*/ |
50 |
|
public void testConstructor3() { |
51 |
< |
try{ |
52 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
51 |
> |
try { |
52 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
53 |
|
a = AtomicIntegerFieldUpdater.newUpdater |
54 |
< |
(getClass(), "w"); |
54 |
> |
(AtomicIntegerFieldUpdaterTest.class, "w"); |
55 |
|
shouldThrow(); |
56 |
< |
} |
59 |
< |
catch (RuntimeException rt) {} |
56 |
> |
} catch (RuntimeException success) {} |
57 |
|
} |
58 |
|
|
59 |
|
/** |
60 |
< |
* get returns the last value set or assigned |
60 |
> |
* get returns the last value set or assigned |
61 |
|
*/ |
62 |
|
public void testGetSet() { |
63 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
63 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
64 |
> |
try { |
65 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
66 |
> |
} catch (RuntimeException ok) { |
67 |
> |
return; |
68 |
> |
} |
69 |
> |
x = 1; |
70 |
> |
assertEquals(1, a.get(this)); |
71 |
> |
a.set(this, 2); |
72 |
> |
assertEquals(2, a.get(this)); |
73 |
> |
a.set(this, -3); |
74 |
> |
assertEquals(-3, a.get(this)); |
75 |
> |
} |
76 |
> |
|
77 |
> |
/** |
78 |
> |
* get returns the last value lazySet by same thread |
79 |
> |
*/ |
80 |
> |
public void testGetLazySet() { |
81 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
82 |
> |
try { |
83 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
84 |
> |
} catch (RuntimeException ok) { |
85 |
> |
return; |
86 |
> |
} |
87 |
|
x = 1; |
88 |
< |
assertEquals(1,a.get(this)); |
89 |
< |
a.set(this,2); |
90 |
< |
assertEquals(2,a.get(this)); |
91 |
< |
a.set(this,-3); |
92 |
< |
assertEquals(-3,a.get(this)); |
73 |
< |
|
88 |
> |
assertEquals(1, a.get(this)); |
89 |
> |
a.lazySet(this, 2); |
90 |
> |
assertEquals(2, a.get(this)); |
91 |
> |
a.lazySet(this, -3); |
92 |
> |
assertEquals(-3, a.get(this)); |
93 |
|
} |
94 |
+ |
|
95 |
|
/** |
96 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
97 |
|
*/ |
98 |
|
public void testCompareAndSet() { |
99 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
99 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
100 |
> |
try { |
101 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
102 |
> |
} catch (RuntimeException ok) { |
103 |
> |
return; |
104 |
> |
} |
105 |
|
x = 1; |
106 |
< |
assertTrue(a.compareAndSet(this,1,2)); |
107 |
< |
assertTrue(a.compareAndSet(this,2,-4)); |
108 |
< |
assertEquals(-4,a.get(this)); |
109 |
< |
assertFalse(a.compareAndSet(this,-5,7)); |
110 |
< |
assertFalse((7 == a.get(this))); |
111 |
< |
assertTrue(a.compareAndSet(this,-4,7)); |
112 |
< |
assertEquals(7,a.get(this)); |
106 |
> |
assertTrue(a.compareAndSet(this, 1, 2)); |
107 |
> |
assertTrue(a.compareAndSet(this, 2, -4)); |
108 |
> |
assertEquals(-4, a.get(this)); |
109 |
> |
assertFalse(a.compareAndSet(this, -5, 7)); |
110 |
> |
assertEquals(-4, a.get(this)); |
111 |
> |
assertTrue(a.compareAndSet(this, -4, 7)); |
112 |
> |
assertEquals(7, a.get(this)); |
113 |
|
} |
114 |
|
|
90 |
– |
|
115 |
|
/** |
116 |
|
* compareAndSet in one thread enables another waiting for value |
117 |
|
* to succeed |
118 |
|
*/ |
119 |
< |
public void testCompareAndSetInMultipleThreads() { |
119 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
120 |
|
x = 1; |
121 |
< |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
122 |
< |
|
123 |
< |
Thread t = new Thread(new Runnable() { |
124 |
< |
public void run() { |
125 |
< |
while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
102 |
< |
}}); |
103 |
< |
try { |
104 |
< |
t.start(); |
105 |
< |
assertTrue(a.compareAndSet(this, 1, 2)); |
106 |
< |
t.join(LONG_DELAY_MS); |
107 |
< |
assertFalse(t.isAlive()); |
108 |
< |
assertEquals(a.get(this), 3); |
109 |
< |
} |
110 |
< |
catch(Exception e) { |
111 |
< |
unexpectedException(); |
121 |
> |
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a; |
122 |
> |
try { |
123 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
124 |
> |
} catch (RuntimeException ok) { |
125 |
> |
return; |
126 |
|
} |
127 |
+ |
|
128 |
+ |
Thread t = new Thread(new CheckedRunnable() { |
129 |
+ |
public void realRun() { |
130 |
+ |
while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) |
131 |
+ |
Thread.yield(); |
132 |
+ |
}}); |
133 |
+ |
|
134 |
+ |
t.start(); |
135 |
+ |
assertTrue(a.compareAndSet(this, 1, 2)); |
136 |
+ |
t.join(LONG_DELAY_MS); |
137 |
+ |
assertFalse(t.isAlive()); |
138 |
+ |
assertEquals(a.get(this), 3); |
139 |
|
} |
140 |
|
|
141 |
|
/** |
142 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
143 |
< |
* to expected |
143 |
> |
* to expected |
144 |
|
*/ |
145 |
|
public void testWeakCompareAndSet() { |
146 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
146 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
147 |
> |
try { |
148 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
149 |
> |
} catch (RuntimeException ok) { |
150 |
> |
return; |
151 |
> |
} |
152 |
|
x = 1; |
153 |
< |
while(!a.weakCompareAndSet(this,1,2)); |
154 |
< |
while(!a.weakCompareAndSet(this,2,-4)); |
155 |
< |
assertEquals(-4,a.get(this)); |
156 |
< |
while(!a.weakCompareAndSet(this,-4,7)); |
157 |
< |
assertEquals(7,a.get(this)); |
153 |
> |
while (!a.weakCompareAndSet(this, 1, 2)); |
154 |
> |
while (!a.weakCompareAndSet(this, 2, -4)); |
155 |
> |
assertEquals(-4, a.get(this)); |
156 |
> |
while (!a.weakCompareAndSet(this, -4, 7)); |
157 |
> |
assertEquals(7, a.get(this)); |
158 |
|
} |
159 |
|
|
160 |
|
/** |
161 |
< |
* getAndSet returns previous value and sets to given value |
161 |
> |
* getAndSet returns previous value and sets to given value |
162 |
|
*/ |
163 |
|
public void testGetAndSet() { |
164 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
164 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
165 |
> |
try { |
166 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
167 |
> |
} catch (RuntimeException ok) { |
168 |
> |
return; |
169 |
> |
} |
170 |
|
x = 1; |
171 |
< |
assertEquals(1,a.getAndSet(this, 0)); |
172 |
< |
assertEquals(0,a.getAndSet(this,-10)); |
173 |
< |
assertEquals(-10,a.getAndSet(this,1)); |
171 |
> |
assertEquals(1, a.getAndSet(this, 0)); |
172 |
> |
assertEquals(0, a.getAndSet(this, -10)); |
173 |
> |
assertEquals(-10, a.getAndSet(this, 1)); |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
|
* getAndAdd returns previous value and adds given value |
178 |
|
*/ |
179 |
|
public void testGetAndAdd() { |
180 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
180 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
181 |
> |
try { |
182 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
183 |
> |
} catch (RuntimeException ok) { |
184 |
> |
return; |
185 |
> |
} |
186 |
|
x = 1; |
187 |
< |
assertEquals(1,a.getAndAdd(this,2)); |
188 |
< |
assertEquals(3,a.get(this)); |
189 |
< |
assertEquals(3,a.getAndAdd(this,-4)); |
190 |
< |
assertEquals(-1,a.get(this)); |
187 |
> |
assertEquals(1, a.getAndAdd(this, 2)); |
188 |
> |
assertEquals(3, a.get(this)); |
189 |
> |
assertEquals(3, a.getAndAdd(this, -4)); |
190 |
> |
assertEquals(-1, a.get(this)); |
191 |
|
} |
192 |
|
|
193 |
|
/** |
194 |
|
* getAndDecrement returns previous value and decrements |
195 |
|
*/ |
196 |
|
public void testGetAndDecrement() { |
197 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
197 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
198 |
> |
try { |
199 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
200 |
> |
} catch (RuntimeException ok) { |
201 |
> |
return; |
202 |
> |
} |
203 |
|
x = 1; |
204 |
< |
assertEquals(1,a.getAndDecrement(this)); |
205 |
< |
assertEquals(0,a.getAndDecrement(this)); |
206 |
< |
assertEquals(-1,a.getAndDecrement(this)); |
204 |
> |
assertEquals(1, a.getAndDecrement(this)); |
205 |
> |
assertEquals(0, a.getAndDecrement(this)); |
206 |
> |
assertEquals(-1, a.getAndDecrement(this)); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
|
* getAndIncrement returns previous value and increments |
211 |
|
*/ |
212 |
|
public void testGetAndIncrement() { |
213 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
213 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
214 |
> |
try { |
215 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
216 |
> |
} catch (RuntimeException ok) { |
217 |
> |
return; |
218 |
> |
} |
219 |
|
x = 1; |
220 |
< |
assertEquals(1,a.getAndIncrement(this)); |
221 |
< |
assertEquals(2,a.get(this)); |
222 |
< |
a.set(this,-2); |
223 |
< |
assertEquals(-2,a.getAndIncrement(this)); |
224 |
< |
assertEquals(-1,a.getAndIncrement(this)); |
225 |
< |
assertEquals(0,a.getAndIncrement(this)); |
226 |
< |
assertEquals(1,a.get(this)); |
220 |
> |
assertEquals(1, a.getAndIncrement(this)); |
221 |
> |
assertEquals(2, a.get(this)); |
222 |
> |
a.set(this, -2); |
223 |
> |
assertEquals(-2, a.getAndIncrement(this)); |
224 |
> |
assertEquals(-1, a.getAndIncrement(this)); |
225 |
> |
assertEquals(0, a.getAndIncrement(this)); |
226 |
> |
assertEquals(1, a.get(this)); |
227 |
|
} |
228 |
|
|
229 |
|
/** |
230 |
|
* addAndGet adds given value to current, and returns current value |
231 |
|
*/ |
232 |
|
public void testAddAndGet() { |
233 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
233 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
234 |
> |
try { |
235 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
236 |
> |
} catch (RuntimeException ok) { |
237 |
> |
return; |
238 |
> |
} |
239 |
|
x = 1; |
240 |
< |
assertEquals(3,a.addAndGet(this,2)); |
241 |
< |
assertEquals(3,a.get(this)); |
242 |
< |
assertEquals(-1,a.addAndGet(this,-4)); |
243 |
< |
assertEquals(-1,a.get(this)); |
240 |
> |
assertEquals(3, a.addAndGet(this, 2)); |
241 |
> |
assertEquals(3, a.get(this)); |
242 |
> |
assertEquals(-1, a.addAndGet(this, -4)); |
243 |
> |
assertEquals(-1, a.get(this)); |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
|
* decrementAndGet decrements and returns current value |
248 |
|
*/ |
249 |
|
public void testDecrementAndGet() { |
250 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
250 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
251 |
> |
try { |
252 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
253 |
> |
} catch (RuntimeException ok) { |
254 |
> |
return; |
255 |
> |
} |
256 |
|
x = 1; |
257 |
< |
assertEquals(0,a.decrementAndGet(this)); |
258 |
< |
assertEquals(-1,a.decrementAndGet(this)); |
259 |
< |
assertEquals(-2,a.decrementAndGet(this)); |
260 |
< |
assertEquals(-2,a.get(this)); |
257 |
> |
assertEquals(0, a.decrementAndGet(this)); |
258 |
> |
assertEquals(-1, a.decrementAndGet(this)); |
259 |
> |
assertEquals(-2, a.decrementAndGet(this)); |
260 |
> |
assertEquals(-2, a.get(this)); |
261 |
|
} |
262 |
|
|
263 |
|
/** |
264 |
|
* incrementAndGet increments and returns current value |
265 |
|
*/ |
266 |
|
public void testIncrementAndGet() { |
267 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a = AtomicIntegerFieldUpdater.newUpdater(getClass(), "x"); |
267 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
268 |
> |
try { |
269 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
270 |
> |
} catch (RuntimeException ok) { |
271 |
> |
return; |
272 |
> |
} |
273 |
|
x = 1; |
274 |
< |
assertEquals(2,a.incrementAndGet(this)); |
275 |
< |
assertEquals(2,a.get(this)); |
276 |
< |
a.set(this,-2); |
277 |
< |
assertEquals(-1,a.incrementAndGet(this)); |
278 |
< |
assertEquals(0,a.incrementAndGet(this)); |
279 |
< |
assertEquals(1,a.incrementAndGet(this)); |
280 |
< |
assertEquals(1,a.get(this)); |
274 |
> |
assertEquals(2, a.incrementAndGet(this)); |
275 |
> |
assertEquals(2, a.get(this)); |
276 |
> |
a.set(this, -2); |
277 |
> |
assertEquals(-1, a.incrementAndGet(this)); |
278 |
> |
assertEquals(0, a.incrementAndGet(this)); |
279 |
> |
assertEquals(1, a.incrementAndGet(this)); |
280 |
> |
assertEquals(1, a.get(this)); |
281 |
|
} |
282 |
|
|
283 |
|
} |