6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
9 |
|
import java.util.Arrays; |
10 |
|
import java.util.concurrent.atomic.AtomicLongArray; |
11 |
|
|
12 |
+ |
import junit.framework.Test; |
13 |
+ |
import junit.framework.TestSuite; |
14 |
+ |
|
15 |
|
public class AtomicLongArrayTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run(suite()); |
17 |
> |
main(suite(), args); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(AtomicLongArrayTest.class); |
35 |
|
public void testConstructor2NPE() { |
36 |
|
try { |
37 |
|
long[] a = null; |
38 |
< |
AtomicLongArray aa = new AtomicLongArray(a); |
38 |
> |
new AtomicLongArray(a); |
39 |
|
shouldThrow(); |
40 |
|
} catch (NullPointerException success) {} |
41 |
|
} |
163 |
|
AtomicLongArray aa = new AtomicLongArray(SIZE); |
164 |
|
for (int i = 0; i < SIZE; i++) { |
165 |
|
aa.set(i, 1); |
166 |
< |
while (!aa.weakCompareAndSet(i, 1, 2)); |
167 |
< |
while (!aa.weakCompareAndSet(i, 2, -4)); |
166 |
> |
do {} while (!aa.weakCompareAndSet(i, 1, 2)); |
167 |
> |
do {} while (!aa.weakCompareAndSet(i, 2, -4)); |
168 |
|
assertEquals(-4, aa.get(i)); |
169 |
< |
while (!aa.weakCompareAndSet(i, -4, 7)); |
169 |
> |
do {} while (!aa.weakCompareAndSet(i, -4, 7)); |
170 |
|
assertEquals(7, aa.get(i)); |
171 |
|
} |
172 |
|
} |
273 |
|
} |
274 |
|
} |
275 |
|
|
274 |
– |
static final long COUNTDOWN = 100000; |
275 |
– |
|
276 |
|
class Counter extends CheckedRunnable { |
277 |
|
final AtomicLongArray aa; |
278 |
|
volatile long counts; |
285 |
|
assertTrue(v >= 0); |
286 |
|
if (v != 0) { |
287 |
|
done = false; |
288 |
< |
if (aa.compareAndSet(i, v, v-1)) |
288 |
> |
if (aa.compareAndSet(i, v, v - 1)) |
289 |
|
++counts; |
290 |
|
} |
291 |
|
} |
301 |
|
*/ |
302 |
|
public void testCountingInMultipleThreads() throws InterruptedException { |
303 |
|
final AtomicLongArray aa = new AtomicLongArray(SIZE); |
304 |
+ |
long countdown = 10000; |
305 |
|
for (int i = 0; i < SIZE; i++) |
306 |
< |
aa.set(i, COUNTDOWN); |
306 |
> |
aa.set(i, countdown); |
307 |
|
Counter c1 = new Counter(aa); |
308 |
|
Counter c2 = new Counter(aa); |
309 |
|
Thread t1 = new Thread(c1); |
312 |
|
t2.start(); |
313 |
|
t1.join(); |
314 |
|
t2.join(); |
315 |
< |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
315 |
> |
assertEquals(c1.counts+c2.counts, SIZE * countdown); |
316 |
|
} |
317 |
|
|
318 |
|
/** |
323 |
|
for (int i = 0; i < SIZE; i++) |
324 |
|
x.set(i, -i); |
325 |
|
AtomicLongArray y = serialClone(x); |
326 |
< |
assertTrue(x != y); |
326 |
> |
assertNotSame(x, y); |
327 |
|
assertEquals(x.length(), y.length()); |
328 |
|
for (int i = 0; i < SIZE; i++) { |
329 |
|
assertEquals(x.get(i), y.get(i)); |