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