28 |
|
try{ |
29 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
30 |
|
a = AtomicLongFieldUpdater.newUpdater |
31 |
< |
(getClass(), "y"); |
31 |
> |
(AtomicLongFieldUpdaterTest.class, "y"); |
32 |
|
shouldThrow(); |
33 |
|
} |
34 |
|
catch (RuntimeException rt) {} |
41 |
|
try{ |
42 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
43 |
|
a = AtomicLongFieldUpdater.newUpdater |
44 |
< |
(getClass(), "z"); |
44 |
> |
(AtomicLongFieldUpdaterTest.class, "z"); |
45 |
|
shouldThrow(); |
46 |
|
} |
47 |
|
catch (RuntimeException rt) {} |
54 |
|
try{ |
55 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
56 |
|
a = AtomicLongFieldUpdater.newUpdater |
57 |
< |
(getClass(), "w"); |
57 |
> |
(AtomicLongFieldUpdaterTest.class, "w"); |
58 |
|
shouldThrow(); |
59 |
|
} |
60 |
|
|
65 |
|
* get returns the last value set or assigned |
66 |
|
*/ |
67 |
|
public void testGetSet(){ |
68 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
68 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
69 |
|
x = 1; |
70 |
|
assertEquals(1,a.get(this)); |
71 |
|
a.set(this,2); |
78 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
79 |
|
*/ |
80 |
|
public void testCompareAndSet(){ |
81 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
81 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
82 |
|
x = 1; |
83 |
|
assertTrue(a.compareAndSet(this,1,2)); |
84 |
|
assertTrue(a.compareAndSet(this,2,-4)); |
96 |
|
*/ |
97 |
|
public void testCompareAndSetInMultipleThreads() { |
98 |
|
x = 1; |
99 |
< |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
99 |
> |
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
100 |
|
|
101 |
|
Thread t = new Thread(new Runnable() { |
102 |
|
public void run() { |
119 |
|
* to expected |
120 |
|
*/ |
121 |
|
public void testWeakCompareAndSet(){ |
122 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
122 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
123 |
|
x = 1; |
124 |
|
while(!a.weakCompareAndSet(this,1,2)); |
125 |
|
while(!a.weakCompareAndSet(this,2,-4)); |
132 |
|
* getAndSet returns previous value and sets to given value |
133 |
|
*/ |
134 |
|
public void testGetAndSet(){ |
135 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
135 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
136 |
|
x = 1; |
137 |
|
assertEquals(1,a.getAndSet(this, 0)); |
138 |
|
assertEquals(0,a.getAndSet(this,-10)); |
143 |
|
* getAndAdd returns previous value and adds given value |
144 |
|
*/ |
145 |
|
public void testGetAndAdd(){ |
146 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
146 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
147 |
|
x = 1; |
148 |
|
assertEquals(1,a.getAndAdd(this,2)); |
149 |
|
assertEquals(3,a.get(this)); |
155 |
|
* getAndDecrement returns previous value and decrements |
156 |
|
*/ |
157 |
|
public void testGetAndDecrement(){ |
158 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
158 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
159 |
|
x = 1; |
160 |
|
assertEquals(1,a.getAndDecrement(this)); |
161 |
|
assertEquals(0,a.getAndDecrement(this)); |
166 |
|
* getAndIncrement returns previous value and increments |
167 |
|
*/ |
168 |
|
public void testGetAndIncrement(){ |
169 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
169 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
170 |
|
x = 1; |
171 |
|
assertEquals(1,a.getAndIncrement(this)); |
172 |
|
assertEquals(2,a.get(this)); |
181 |
|
* addAndGet adds given value to current, and returns current value |
182 |
|
*/ |
183 |
|
public void testAddAndGet(){ |
184 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
184 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
185 |
|
x = 1; |
186 |
|
assertEquals(3,a.addAndGet(this,2)); |
187 |
|
assertEquals(3,a.get(this)); |
193 |
|
* decrementAndGet decrements and returns current value |
194 |
|
*/ |
195 |
|
public void testDecrementAndGet(){ |
196 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
196 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
197 |
|
x = 1; |
198 |
|
assertEquals(0,a.decrementAndGet(this)); |
199 |
|
assertEquals(-1,a.decrementAndGet(this)); |
205 |
|
* incrementAndGet increments and returns current value |
206 |
|
*/ |
207 |
|
public void testIncrementAndGet(){ |
208 |
< |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(getClass(), "x"); |
208 |
> |
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
209 |
|
x = 1; |
210 |
|
assertEquals(2,a.incrementAndGet(this)); |
211 |
|
assertEquals(2,a.get(this)); |