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.*; |
23 |
|
} |
24 |
|
|
25 |
|
/** |
26 |
< |
* Contruction with non-existent field throws RuntimeException |
26 |
> |
* Construction with non-existent field throws RuntimeException |
27 |
|
*/ |
28 |
|
public void testConstructor(){ |
29 |
|
try{ |
30 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
30 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
31 |
|
a = AtomicLongFieldUpdater.newUpdater |
32 |
< |
(getClass(), "y"); |
32 |
> |
(AtomicLongFieldUpdaterTest.class, "y"); |
33 |
|
shouldThrow(); |
34 |
|
} |
35 |
|
catch (RuntimeException rt) {} |
40 |
|
*/ |
41 |
|
public void testConstructor2(){ |
42 |
|
try{ |
43 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
43 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
44 |
|
a = AtomicLongFieldUpdater.newUpdater |
45 |
< |
(getClass(), "z"); |
45 |
> |
(AtomicLongFieldUpdaterTest.class, "z"); |
46 |
|
shouldThrow(); |
47 |
|
} |
48 |
|
catch (RuntimeException rt) {} |
53 |
|
*/ |
54 |
|
public void testConstructor3(){ |
55 |
|
try{ |
56 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
56 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
57 |
|
a = AtomicLongFieldUpdater.newUpdater |
58 |
< |
(getClass(), "w"); |
58 |
> |
(AtomicLongFieldUpdaterTest.class, "w"); |
59 |
|
shouldThrow(); |
60 |
|
} |
61 |
|
|
66 |
|
* get returns the last value set or assigned |
67 |
|
*/ |
68 |
|
public void testGetSet(){ |
69 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
69 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
70 |
> |
try { |
71 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
72 |
> |
} catch (RuntimeException ok) { |
73 |
> |
return; |
74 |
> |
} |
75 |
|
x = 1; |
76 |
|
assertEquals(1,a.get(this)); |
77 |
|
a.set(this,2); |
78 |
|
assertEquals(2,a.get(this)); |
79 |
|
a.set(this,-3); |
80 |
|
assertEquals(-3,a.get(this)); |
75 |
– |
|
81 |
|
} |
82 |
+ |
|
83 |
+ |
/** |
84 |
+ |
* get returns the last value lazySet by same thread |
85 |
+ |
*/ |
86 |
+ |
public void testGetLazySet(){ |
87 |
+ |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
88 |
+ |
try { |
89 |
+ |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
90 |
+ |
} catch (RuntimeException ok) { |
91 |
+ |
return; |
92 |
+ |
} |
93 |
+ |
x = 1; |
94 |
+ |
assertEquals(1,a.get(this)); |
95 |
+ |
a.lazySet(this,2); |
96 |
+ |
assertEquals(2,a.get(this)); |
97 |
+ |
a.lazySet(this,-3); |
98 |
+ |
assertEquals(-3,a.get(this)); |
99 |
+ |
} |
100 |
+ |
|
101 |
+ |
|
102 |
|
/** |
103 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
104 |
|
*/ |
105 |
|
public void testCompareAndSet(){ |
106 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
106 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
107 |
> |
try { |
108 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
109 |
> |
} catch (RuntimeException ok) { |
110 |
> |
return; |
111 |
> |
} |
112 |
|
x = 1; |
113 |
|
assertTrue(a.compareAndSet(this,1,2)); |
114 |
|
assertTrue(a.compareAndSet(this,2,-4)); |
126 |
|
*/ |
127 |
|
public void testCompareAndSetInMultipleThreads() { |
128 |
|
x = 1; |
129 |
< |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
129 |
> |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a; |
130 |
> |
try { |
131 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
132 |
> |
} catch (RuntimeException ok) { |
133 |
> |
return; |
134 |
> |
} |
135 |
|
|
136 |
|
Thread t = new Thread(new Runnable() { |
137 |
|
public void run() { |
151 |
|
|
152 |
|
/** |
153 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
154 |
< |
* to expected |
154 |
> |
* to expected |
155 |
|
*/ |
156 |
|
public void testWeakCompareAndSet(){ |
157 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
157 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
158 |
> |
try { |
159 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
160 |
> |
} catch (RuntimeException ok) { |
161 |
> |
return; |
162 |
> |
} |
163 |
|
x = 1; |
164 |
|
while(!a.weakCompareAndSet(this,1,2)); |
165 |
|
while(!a.weakCompareAndSet(this,2,-4)); |
172 |
|
* getAndSet returns previous value and sets to given value |
173 |
|
*/ |
174 |
|
public void testGetAndSet(){ |
175 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
175 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
176 |
> |
try { |
177 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
178 |
> |
} catch (RuntimeException ok) { |
179 |
> |
return; |
180 |
> |
} |
181 |
|
x = 1; |
182 |
|
assertEquals(1,a.getAndSet(this, 0)); |
183 |
|
assertEquals(0,a.getAndSet(this,-10)); |
188 |
|
* getAndAdd returns previous value and adds given value |
189 |
|
*/ |
190 |
|
public void testGetAndAdd(){ |
191 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
191 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
192 |
> |
try { |
193 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
194 |
> |
} catch (RuntimeException ok) { |
195 |
> |
return; |
196 |
> |
} |
197 |
|
x = 1; |
198 |
|
assertEquals(1,a.getAndAdd(this,2)); |
199 |
|
assertEquals(3,a.get(this)); |
205 |
|
* getAndDecrement returns previous value and decrements |
206 |
|
*/ |
207 |
|
public void testGetAndDecrement(){ |
208 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
208 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
209 |
> |
try { |
210 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
211 |
> |
} catch (RuntimeException ok) { |
212 |
> |
return; |
213 |
> |
} |
214 |
|
x = 1; |
215 |
|
assertEquals(1,a.getAndDecrement(this)); |
216 |
|
assertEquals(0,a.getAndDecrement(this)); |
221 |
|
* getAndIncrement returns previous value and increments |
222 |
|
*/ |
223 |
|
public void testGetAndIncrement(){ |
224 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
224 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
225 |
> |
try { |
226 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
227 |
> |
} catch (RuntimeException ok) { |
228 |
> |
return; |
229 |
> |
} |
230 |
|
x = 1; |
231 |
|
assertEquals(1,a.getAndIncrement(this)); |
232 |
|
assertEquals(2,a.get(this)); |
241 |
|
* addAndGet adds given value to current, and returns current value |
242 |
|
*/ |
243 |
|
public void testAddAndGet(){ |
244 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
244 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
245 |
> |
try { |
246 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
247 |
> |
} catch (RuntimeException ok) { |
248 |
> |
return; |
249 |
> |
} |
250 |
|
x = 1; |
251 |
|
assertEquals(3,a.addAndGet(this,2)); |
252 |
|
assertEquals(3,a.get(this)); |
258 |
|
* decrementAndGet decrements and returns current value |
259 |
|
*/ |
260 |
|
public void testDecrementAndGet(){ |
261 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
261 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
262 |
> |
try { |
263 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
264 |
> |
} catch (RuntimeException ok) { |
265 |
> |
return; |
266 |
> |
} |
267 |
|
x = 1; |
268 |
|
assertEquals(0,a.decrementAndGet(this)); |
269 |
|
assertEquals(-1,a.decrementAndGet(this)); |
275 |
|
* incrementAndGet increments and returns current value |
276 |
|
*/ |
277 |
|
public void testIncrementAndGet(){ |
278 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
278 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
279 |
> |
try { |
280 |
> |
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
281 |
> |
} catch (RuntimeException ok) { |
282 |
> |
return; |
283 |
> |
} |
284 |
|
x = 1; |
285 |
|
assertEquals(2,a.incrementAndGet(this)); |
286 |
|
assertEquals(2,a.get(this)); |