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 |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import java.util.concurrent.atomic.*; |
14 |
|
volatile int x = 0; |
15 |
|
int w; |
16 |
|
long z; |
17 |
< |
public static void main(String[] args){ |
17 |
> |
public static void main(String[] args) { |
18 |
|
junit.textui.TestRunner.run(suite()); |
19 |
|
} |
20 |
|
public static Test suite() { |
25 |
|
* Construction with non-existent field throws RuntimeException |
26 |
|
*/ |
27 |
|
public void testConstructor() { |
28 |
< |
try{ |
29 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
28 |
> |
try { |
29 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
30 |
|
a = AtomicIntegerFieldUpdater.newUpdater |
31 |
|
(AtomicIntegerFieldUpdaterTest.class, "y"); |
32 |
|
shouldThrow(); |
33 |
< |
} |
34 |
< |
catch (RuntimeException rt) {} |
33 |
> |
} catch (RuntimeException success) {} |
34 |
|
} |
35 |
|
|
36 |
|
/** |
37 |
|
* construction with field not of given type throws RuntimeException |
38 |
|
*/ |
39 |
|
public void testConstructor2() { |
40 |
< |
try{ |
41 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
40 |
> |
try { |
41 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
42 |
|
a = AtomicIntegerFieldUpdater.newUpdater |
43 |
|
(AtomicIntegerFieldUpdaterTest.class, "z"); |
44 |
|
shouldThrow(); |
45 |
< |
} |
47 |
< |
catch (RuntimeException rt) {} |
45 |
> |
} catch (RuntimeException success) {} |
46 |
|
} |
47 |
|
|
48 |
|
/** |
49 |
|
* construction with non-volatile field throws RuntimeException |
50 |
|
*/ |
51 |
|
public void testConstructor3() { |
52 |
< |
try{ |
53 |
< |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
52 |
> |
try { |
53 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> |
54 |
|
a = AtomicIntegerFieldUpdater.newUpdater |
55 |
|
(AtomicIntegerFieldUpdaterTest.class, "w"); |
56 |
|
shouldThrow(); |
57 |
< |
} |
60 |
< |
catch (RuntimeException rt) {} |
57 |
> |
} catch (RuntimeException success) {} |
58 |
|
} |
59 |
|
|
60 |
|
/** |
68 |
|
return; |
69 |
|
} |
70 |
|
x = 1; |
71 |
< |
assertEquals(1,a.get(this)); |
72 |
< |
a.set(this,2); |
73 |
< |
assertEquals(2,a.get(this)); |
74 |
< |
a.set(this,-3); |
75 |
< |
assertEquals(-3,a.get(this)); |
76 |
< |
|
71 |
> |
assertEquals(1,a.get(this)); |
72 |
> |
a.set(this,2); |
73 |
> |
assertEquals(2,a.get(this)); |
74 |
> |
a.set(this,-3); |
75 |
> |
assertEquals(-3,a.get(this)); |
76 |
> |
} |
77 |
> |
|
78 |
> |
/** |
79 |
> |
* get returns the last value lazySet by same thread |
80 |
> |
*/ |
81 |
> |
public void testGetLazySet() { |
82 |
> |
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
83 |
> |
try { |
84 |
> |
a = AtomicIntegerFieldUpdater.newUpdater(AtomicIntegerFieldUpdaterTest.class, "x"); |
85 |
> |
} catch (RuntimeException ok) { |
86 |
> |
return; |
87 |
> |
} |
88 |
> |
x = 1; |
89 |
> |
assertEquals(1,a.get(this)); |
90 |
> |
a.lazySet(this,2); |
91 |
> |
assertEquals(2,a.get(this)); |
92 |
> |
a.lazySet(this,-3); |
93 |
> |
assertEquals(-3,a.get(this)); |
94 |
|
} |
95 |
+ |
|
96 |
|
/** |
97 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
98 |
|
*/ |
104 |
|
return; |
105 |
|
} |
106 |
|
x = 1; |
107 |
< |
assertTrue(a.compareAndSet(this,1,2)); |
108 |
< |
assertTrue(a.compareAndSet(this,2,-4)); |
109 |
< |
assertEquals(-4,a.get(this)); |
110 |
< |
assertFalse(a.compareAndSet(this,-5,7)); |
111 |
< |
assertFalse((7 == a.get(this))); |
112 |
< |
assertTrue(a.compareAndSet(this,-4,7)); |
113 |
< |
assertEquals(7,a.get(this)); |
107 |
> |
assertTrue(a.compareAndSet(this,1,2)); |
108 |
> |
assertTrue(a.compareAndSet(this,2,-4)); |
109 |
> |
assertEquals(-4,a.get(this)); |
110 |
> |
assertFalse(a.compareAndSet(this,-5,7)); |
111 |
> |
assertEquals(-4,a.get(this)); |
112 |
> |
assertTrue(a.compareAndSet(this,-4,7)); |
113 |
> |
assertEquals(7,a.get(this)); |
114 |
|
} |
115 |
|
|
116 |
|
|
118 |
|
* compareAndSet in one thread enables another waiting for value |
119 |
|
* to succeed |
120 |
|
*/ |
121 |
< |
public void testCompareAndSetInMultipleThreads() { |
121 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
122 |
|
x = 1; |
123 |
|
final AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest>a; |
124 |
|
try { |
127 |
|
return; |
128 |
|
} |
129 |
|
|
130 |
< |
Thread t = new Thread(new Runnable() { |
131 |
< |
public void run() { |
132 |
< |
while(!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
133 |
< |
}}); |
134 |
< |
try { |
135 |
< |
t.start(); |
136 |
< |
assertTrue(a.compareAndSet(this, 1, 2)); |
137 |
< |
t.join(LONG_DELAY_MS); |
138 |
< |
assertFalse(t.isAlive()); |
139 |
< |
assertEquals(a.get(this), 3); |
140 |
< |
} |
126 |
< |
catch(Exception e) { |
127 |
< |
unexpectedException(); |
128 |
< |
} |
130 |
> |
Thread t = new Thread(new CheckedRunnable() { |
131 |
> |
public void realRun() { |
132 |
> |
while (!a.compareAndSet(AtomicIntegerFieldUpdaterTest.this, 2, 3)) |
133 |
> |
Thread.yield(); |
134 |
> |
}}); |
135 |
> |
|
136 |
> |
t.start(); |
137 |
> |
assertTrue(a.compareAndSet(this, 1, 2)); |
138 |
> |
t.join(LONG_DELAY_MS); |
139 |
> |
assertFalse(t.isAlive()); |
140 |
> |
assertEquals(a.get(this), 3); |
141 |
|
} |
142 |
|
|
143 |
|
/** |
144 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
145 |
< |
* to expected |
145 |
> |
* to expected |
146 |
|
*/ |
147 |
|
public void testWeakCompareAndSet() { |
148 |
|
AtomicIntegerFieldUpdater<AtomicIntegerFieldUpdaterTest> a; |
152 |
|
return; |
153 |
|
} |
154 |
|
x = 1; |
155 |
< |
while(!a.weakCompareAndSet(this,1,2)); |
156 |
< |
while(!a.weakCompareAndSet(this,2,-4)); |
157 |
< |
assertEquals(-4,a.get(this)); |
158 |
< |
while(!a.weakCompareAndSet(this,-4,7)); |
159 |
< |
assertEquals(7,a.get(this)); |
155 |
> |
while (!a.weakCompareAndSet(this,1,2)); |
156 |
> |
while (!a.weakCompareAndSet(this,2,-4)); |
157 |
> |
assertEquals(-4,a.get(this)); |
158 |
> |
while (!a.weakCompareAndSet(this,-4,7)); |
159 |
> |
assertEquals(7,a.get(this)); |
160 |
|
} |
161 |
|
|
162 |
|
/** |
170 |
|
return; |
171 |
|
} |
172 |
|
x = 1; |
173 |
< |
assertEquals(1,a.getAndSet(this, 0)); |
174 |
< |
assertEquals(0,a.getAndSet(this,-10)); |
175 |
< |
assertEquals(-10,a.getAndSet(this,1)); |
173 |
> |
assertEquals(1,a.getAndSet(this, 0)); |
174 |
> |
assertEquals(0,a.getAndSet(this,-10)); |
175 |
> |
assertEquals(-10,a.getAndSet(this,1)); |
176 |
|
} |
177 |
|
|
178 |
|
/** |
186 |
|
return; |
187 |
|
} |
188 |
|
x = 1; |
189 |
< |
assertEquals(1,a.getAndAdd(this,2)); |
190 |
< |
assertEquals(3,a.get(this)); |
191 |
< |
assertEquals(3,a.getAndAdd(this,-4)); |
192 |
< |
assertEquals(-1,a.get(this)); |
189 |
> |
assertEquals(1,a.getAndAdd(this,2)); |
190 |
> |
assertEquals(3,a.get(this)); |
191 |
> |
assertEquals(3,a.getAndAdd(this,-4)); |
192 |
> |
assertEquals(-1,a.get(this)); |
193 |
|
} |
194 |
|
|
195 |
|
/** |
203 |
|
return; |
204 |
|
} |
205 |
|
x = 1; |
206 |
< |
assertEquals(1,a.getAndDecrement(this)); |
207 |
< |
assertEquals(0,a.getAndDecrement(this)); |
208 |
< |
assertEquals(-1,a.getAndDecrement(this)); |
206 |
> |
assertEquals(1,a.getAndDecrement(this)); |
207 |
> |
assertEquals(0,a.getAndDecrement(this)); |
208 |
> |
assertEquals(-1,a.getAndDecrement(this)); |
209 |
|
} |
210 |
|
|
211 |
|
/** |
219 |
|
return; |
220 |
|
} |
221 |
|
x = 1; |
222 |
< |
assertEquals(1,a.getAndIncrement(this)); |
223 |
< |
assertEquals(2,a.get(this)); |
224 |
< |
a.set(this,-2); |
225 |
< |
assertEquals(-2,a.getAndIncrement(this)); |
226 |
< |
assertEquals(-1,a.getAndIncrement(this)); |
227 |
< |
assertEquals(0,a.getAndIncrement(this)); |
228 |
< |
assertEquals(1,a.get(this)); |
222 |
> |
assertEquals(1,a.getAndIncrement(this)); |
223 |
> |
assertEquals(2,a.get(this)); |
224 |
> |
a.set(this,-2); |
225 |
> |
assertEquals(-2,a.getAndIncrement(this)); |
226 |
> |
assertEquals(-1,a.getAndIncrement(this)); |
227 |
> |
assertEquals(0,a.getAndIncrement(this)); |
228 |
> |
assertEquals(1,a.get(this)); |
229 |
|
} |
230 |
|
|
231 |
|
/** |
239 |
|
return; |
240 |
|
} |
241 |
|
x = 1; |
242 |
< |
assertEquals(3,a.addAndGet(this,2)); |
243 |
< |
assertEquals(3,a.get(this)); |
244 |
< |
assertEquals(-1,a.addAndGet(this,-4)); |
245 |
< |
assertEquals(-1,a.get(this)); |
242 |
> |
assertEquals(3,a.addAndGet(this,2)); |
243 |
> |
assertEquals(3,a.get(this)); |
244 |
> |
assertEquals(-1,a.addAndGet(this,-4)); |
245 |
> |
assertEquals(-1,a.get(this)); |
246 |
|
} |
247 |
|
|
248 |
|
/** |
256 |
|
return; |
257 |
|
} |
258 |
|
x = 1; |
259 |
< |
assertEquals(0,a.decrementAndGet(this)); |
260 |
< |
assertEquals(-1,a.decrementAndGet(this)); |
261 |
< |
assertEquals(-2,a.decrementAndGet(this)); |
262 |
< |
assertEquals(-2,a.get(this)); |
259 |
> |
assertEquals(0,a.decrementAndGet(this)); |
260 |
> |
assertEquals(-1,a.decrementAndGet(this)); |
261 |
> |
assertEquals(-2,a.decrementAndGet(this)); |
262 |
> |
assertEquals(-2,a.get(this)); |
263 |
|
} |
264 |
|
|
265 |
|
/** |
273 |
|
return; |
274 |
|
} |
275 |
|
x = 1; |
276 |
< |
assertEquals(2,a.incrementAndGet(this)); |
277 |
< |
assertEquals(2,a.get(this)); |
278 |
< |
a.set(this,-2); |
279 |
< |
assertEquals(-1,a.incrementAndGet(this)); |
280 |
< |
assertEquals(0,a.incrementAndGet(this)); |
281 |
< |
assertEquals(1,a.incrementAndGet(this)); |
282 |
< |
assertEquals(1,a.get(this)); |
276 |
> |
assertEquals(2,a.incrementAndGet(this)); |
277 |
> |
assertEquals(2,a.get(this)); |
278 |
> |
a.set(this,-2); |
279 |
> |
assertEquals(-1,a.incrementAndGet(this)); |
280 |
> |
assertEquals(0,a.incrementAndGet(this)); |
281 |
> |
assertEquals(1,a.incrementAndGet(this)); |
282 |
> |
assertEquals(1,a.get(this)); |
283 |
|
} |
284 |
|
|
285 |
|
} |