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.*; |
29 |
|
try{ |
30 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
31 |
|
a = AtomicLongFieldUpdater.newUpdater |
32 |
< |
(getClass(), "y"); |
32 |
> |
(AtomicLongFieldUpdaterTest.class, "y"); |
33 |
|
shouldThrow(); |
34 |
|
} |
35 |
|
catch (RuntimeException rt) {} |
42 |
|
try{ |
43 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
44 |
|
a = AtomicLongFieldUpdater.newUpdater |
45 |
< |
(getClass(), "z"); |
45 |
> |
(AtomicLongFieldUpdaterTest.class, "z"); |
46 |
|
shouldThrow(); |
47 |
|
} |
48 |
|
catch (RuntimeException rt) {} |
55 |
|
try{ |
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 = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
70 |
|
x = 1; |
71 |
|
assertEquals(1,a.get(this)); |
72 |
|
a.set(this,2); |
79 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
80 |
|
*/ |
81 |
|
public void testCompareAndSet(){ |
82 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
82 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
83 |
|
x = 1; |
84 |
|
assertTrue(a.compareAndSet(this,1,2)); |
85 |
|
assertTrue(a.compareAndSet(this,2,-4)); |
97 |
|
*/ |
98 |
|
public void testCompareAndSetInMultipleThreads() { |
99 |
|
x = 1; |
100 |
< |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
100 |
> |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
101 |
|
|
102 |
|
Thread t = new Thread(new Runnable() { |
103 |
|
public void run() { |
120 |
|
* to expected |
121 |
|
*/ |
122 |
|
public void testWeakCompareAndSet(){ |
123 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
123 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
124 |
|
x = 1; |
125 |
|
while(!a.weakCompareAndSet(this,1,2)); |
126 |
|
while(!a.weakCompareAndSet(this,2,-4)); |
133 |
|
* getAndSet returns previous value and sets to given value |
134 |
|
*/ |
135 |
|
public void testGetAndSet(){ |
136 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
136 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
137 |
|
x = 1; |
138 |
|
assertEquals(1,a.getAndSet(this, 0)); |
139 |
|
assertEquals(0,a.getAndSet(this,-10)); |
144 |
|
* getAndAdd returns previous value and adds given value |
145 |
|
*/ |
146 |
|
public void testGetAndAdd(){ |
147 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
147 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
148 |
|
x = 1; |
149 |
|
assertEquals(1,a.getAndAdd(this,2)); |
150 |
|
assertEquals(3,a.get(this)); |
156 |
|
* getAndDecrement returns previous value and decrements |
157 |
|
*/ |
158 |
|
public void testGetAndDecrement(){ |
159 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
159 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
160 |
|
x = 1; |
161 |
|
assertEquals(1,a.getAndDecrement(this)); |
162 |
|
assertEquals(0,a.getAndDecrement(this)); |
167 |
|
* getAndIncrement returns previous value and increments |
168 |
|
*/ |
169 |
|
public void testGetAndIncrement(){ |
170 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
170 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
171 |
|
x = 1; |
172 |
|
assertEquals(1,a.getAndIncrement(this)); |
173 |
|
assertEquals(2,a.get(this)); |
182 |
|
* addAndGet adds given value to current, and returns current value |
183 |
|
*/ |
184 |
|
public void testAddAndGet(){ |
185 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
185 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
186 |
|
x = 1; |
187 |
|
assertEquals(3,a.addAndGet(this,2)); |
188 |
|
assertEquals(3,a.get(this)); |
194 |
|
* decrementAndGet decrements and returns current value |
195 |
|
*/ |
196 |
|
public void testDecrementAndGet(){ |
197 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
197 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
198 |
|
x = 1; |
199 |
|
assertEquals(0,a.decrementAndGet(this)); |
200 |
|
assertEquals(-1,a.decrementAndGet(this)); |
206 |
|
* incrementAndGet increments and returns current value |
207 |
|
*/ |
208 |
|
public void testIncrementAndGet(){ |
209 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
209 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
210 |
|
x = 1; |
211 |
|
assertEquals(2,a.incrementAndGet(this)); |
212 |
|
assertEquals(2,a.get(this)); |