15 |
|
int z; |
16 |
|
long w; |
17 |
|
|
18 |
< |
public static void main(String[] args){ |
18 |
> |
public static void main(String[] args) { |
19 |
|
junit.textui.TestRunner.run(suite()); |
20 |
|
} |
21 |
|
public static Test suite() { |
25 |
|
/** |
26 |
|
* Construction with non-existent field throws RuntimeException |
27 |
|
*/ |
28 |
< |
public void testConstructor(){ |
29 |
< |
try{ |
28 |
> |
public void testConstructor() { |
29 |
> |
try { |
30 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
31 |
|
a = AtomicLongFieldUpdater.newUpdater |
32 |
|
(AtomicLongFieldUpdaterTest.class, "y"); |
38 |
|
/** |
39 |
|
* construction with field not of given type throws RuntimeException |
40 |
|
*/ |
41 |
< |
public void testConstructor2(){ |
42 |
< |
try{ |
41 |
> |
public void testConstructor2() { |
42 |
> |
try { |
43 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
44 |
|
a = AtomicLongFieldUpdater.newUpdater |
45 |
|
(AtomicLongFieldUpdaterTest.class, "z"); |
51 |
|
/** |
52 |
|
* construction with non-volatile field throws RuntimeException |
53 |
|
*/ |
54 |
< |
public void testConstructor3(){ |
55 |
< |
try{ |
54 |
> |
public void testConstructor3() { |
55 |
> |
try { |
56 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> |
57 |
|
a = AtomicLongFieldUpdater.newUpdater |
58 |
|
(AtomicLongFieldUpdaterTest.class, "w"); |
65 |
|
/** |
66 |
|
* get returns the last value set or assigned |
67 |
|
*/ |
68 |
< |
public void testGetSet(){ |
68 |
> |
public void testGetSet() { |
69 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
70 |
|
try { |
71 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
83 |
|
/** |
84 |
|
* get returns the last value lazySet by same thread |
85 |
|
*/ |
86 |
< |
public void testGetLazySet(){ |
86 |
> |
public void testGetLazySet() { |
87 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
88 |
|
try { |
89 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
102 |
|
/** |
103 |
|
* compareAndSet succeeds in changing value if equal to expected else fails |
104 |
|
*/ |
105 |
< |
public void testCompareAndSet(){ |
105 |
> |
public void testCompareAndSet() { |
106 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
107 |
|
try { |
108 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
124 |
|
* compareAndSet in one thread enables another waiting for value |
125 |
|
* to succeed |
126 |
|
*/ |
127 |
< |
public void testCompareAndSetInMultipleThreads() { |
127 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
128 |
|
x = 1; |
129 |
|
final AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest>a; |
130 |
|
try { |
135 |
|
|
136 |
|
Thread t = new Thread(new Runnable() { |
137 |
|
public void run() { |
138 |
< |
while(!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
138 |
> |
while (!a.compareAndSet(AtomicLongFieldUpdaterTest.this, 2, 3)) Thread.yield(); |
139 |
|
}}); |
140 |
< |
try { |
141 |
< |
t.start(); |
142 |
< |
assertTrue(a.compareAndSet(this, 1, 2)); |
143 |
< |
t.join(LONG_DELAY_MS); |
144 |
< |
assertFalse(t.isAlive()); |
145 |
< |
assertEquals(a.get(this), 3); |
146 |
< |
} |
147 |
< |
catch(Exception e) { |
148 |
< |
unexpectedException(); |
149 |
< |
} |
140 |
> |
|
141 |
> |
t.start(); |
142 |
> |
assertTrue(a.compareAndSet(this, 1, 2)); |
143 |
> |
t.join(LONG_DELAY_MS); |
144 |
> |
assertFalse(t.isAlive()); |
145 |
> |
assertEquals(a.get(this), 3); |
146 |
|
} |
147 |
|
|
148 |
|
/** |
149 |
|
* repeated weakCompareAndSet succeeds in changing value when equal |
150 |
|
* to expected |
151 |
|
*/ |
152 |
< |
public void testWeakCompareAndSet(){ |
152 |
> |
public void testWeakCompareAndSet() { |
153 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
154 |
|
try { |
155 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
157 |
|
return; |
158 |
|
} |
159 |
|
x = 1; |
160 |
< |
while(!a.weakCompareAndSet(this,1,2)); |
161 |
< |
while(!a.weakCompareAndSet(this,2,-4)); |
160 |
> |
while (!a.weakCompareAndSet(this,1,2)); |
161 |
> |
while (!a.weakCompareAndSet(this,2,-4)); |
162 |
|
assertEquals(-4,a.get(this)); |
163 |
< |
while(!a.weakCompareAndSet(this,-4,7)); |
163 |
> |
while (!a.weakCompareAndSet(this,-4,7)); |
164 |
|
assertEquals(7,a.get(this)); |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
|
* getAndSet returns previous value and sets to given value |
169 |
|
*/ |
170 |
< |
public void testGetAndSet(){ |
170 |
> |
public void testGetAndSet() { |
171 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
172 |
|
try { |
173 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
183 |
|
/** |
184 |
|
* getAndAdd returns previous value and adds given value |
185 |
|
*/ |
186 |
< |
public void testGetAndAdd(){ |
186 |
> |
public void testGetAndAdd() { |
187 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
188 |
|
try { |
189 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
200 |
|
/** |
201 |
|
* getAndDecrement returns previous value and decrements |
202 |
|
*/ |
203 |
< |
public void testGetAndDecrement(){ |
203 |
> |
public void testGetAndDecrement() { |
204 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
205 |
|
try { |
206 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
216 |
|
/** |
217 |
|
* getAndIncrement returns previous value and increments |
218 |
|
*/ |
219 |
< |
public void testGetAndIncrement(){ |
219 |
> |
public void testGetAndIncrement() { |
220 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
221 |
|
try { |
222 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
236 |
|
/** |
237 |
|
* addAndGet adds given value to current, and returns current value |
238 |
|
*/ |
239 |
< |
public void testAddAndGet(){ |
239 |
> |
public void testAddAndGet() { |
240 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
241 |
|
try { |
242 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
253 |
|
/** |
254 |
|
* decrementAndGet decrements and returns current value |
255 |
|
*/ |
256 |
< |
public void testDecrementAndGet(){ |
256 |
> |
public void testDecrementAndGet() { |
257 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
258 |
|
try { |
259 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |
270 |
|
/** |
271 |
|
* incrementAndGet increments and returns current value |
272 |
|
*/ |
273 |
< |
public void testIncrementAndGet(){ |
273 |
> |
public void testIncrementAndGet() { |
274 |
|
AtomicLongFieldUpdater<AtomicLongFieldUpdaterTest> a; |
275 |
|
try { |
276 |
|
a = AtomicLongFieldUpdater.newUpdater(AtomicLongFieldUpdaterTest.class, "x"); |