35 |
|
try { |
36 |
|
long[] a = null; |
37 |
|
AtomicLongArray ai = new AtomicLongArray(a); |
38 |
< |
} catch (NullPointerException success) { |
39 |
< |
} catch (Exception ex) { |
40 |
< |
unexpectedException(); |
41 |
< |
} |
38 |
> |
shouldThrow(); |
39 |
> |
} catch (NullPointerException success) {} |
40 |
|
} |
41 |
|
|
42 |
|
/** |
57 |
|
AtomicLongArray ai = new AtomicLongArray(SIZE); |
58 |
|
try { |
59 |
|
ai.get(SIZE); |
60 |
+ |
shouldThrow(); |
61 |
|
} catch (IndexOutOfBoundsException success) { |
62 |
|
} |
63 |
|
try { |
64 |
|
ai.get(-1); |
65 |
+ |
shouldThrow(); |
66 |
|
} catch (IndexOutOfBoundsException success) { |
67 |
|
} |
68 |
|
try { |
69 |
|
ai.set(SIZE, 0); |
70 |
+ |
shouldThrow(); |
71 |
|
} catch (IndexOutOfBoundsException success) { |
72 |
|
} |
73 |
|
try { |
74 |
|
ai.set(-1, 0); |
75 |
+ |
shouldThrow(); |
76 |
|
} catch (IndexOutOfBoundsException success) { |
77 |
|
} |
78 |
|
} |
128 |
|
* compareAndSet in one thread enables another waiting for value |
129 |
|
* to succeed |
130 |
|
*/ |
131 |
< |
public void testCompareAndSetInMultipleThreads() { |
131 |
> |
public void testCompareAndSetInMultipleThreads() throws InterruptedException { |
132 |
|
final AtomicLongArray a = new AtomicLongArray(1); |
133 |
|
a.set(0, 1); |
134 |
|
Thread t = new Thread(new Runnable() { |
135 |
|
public void run() { |
136 |
|
while (!a.compareAndSet(0, 2, 3)) Thread.yield(); |
137 |
|
}}); |
138 |
< |
try { |
139 |
< |
t.start(); |
140 |
< |
assertTrue(a.compareAndSet(0, 1, 2)); |
141 |
< |
t.join(LONG_DELAY_MS); |
142 |
< |
assertFalse(t.isAlive()); |
143 |
< |
assertEquals(a.get(0), 3); |
142 |
< |
} |
143 |
< |
catch (Exception e) { |
144 |
< |
unexpectedException(); |
145 |
< |
} |
138 |
> |
|
139 |
> |
t.start(); |
140 |
> |
assertTrue(a.compareAndSet(0, 1, 2)); |
141 |
> |
t.join(LONG_DELAY_MS); |
142 |
> |
assertFalse(t.isAlive()); |
143 |
> |
assertEquals(a.get(0), 3); |
144 |
|
} |
145 |
|
|
146 |
|
/** |
289 |
|
* Multiple threads using same array of counters successfully |
290 |
|
* update a number of times equal to total count |
291 |
|
*/ |
292 |
< |
public void testCountingInMultipleThreads() { |
293 |
< |
try { |
294 |
< |
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
295 |
< |
for (int i = 0; i < SIZE; ++i) |
296 |
< |
ai.set(i, COUNTDOWN); |
297 |
< |
Counter c1 = new Counter(ai); |
298 |
< |
Counter c2 = new Counter(ai); |
299 |
< |
Thread t1 = new Thread(c1); |
300 |
< |
Thread t2 = new Thread(c2); |
301 |
< |
t1.start(); |
302 |
< |
t2.start(); |
303 |
< |
t1.join(); |
304 |
< |
t2.join(); |
307 |
< |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
308 |
< |
} |
309 |
< |
catch (InterruptedException ie) { |
310 |
< |
unexpectedException(); |
311 |
< |
} |
292 |
> |
public void testCountingInMultipleThreads() throws InterruptedException { |
293 |
> |
final AtomicLongArray ai = new AtomicLongArray(SIZE); |
294 |
> |
for (int i = 0; i < SIZE; ++i) |
295 |
> |
ai.set(i, COUNTDOWN); |
296 |
> |
Counter c1 = new Counter(ai); |
297 |
> |
Counter c2 = new Counter(ai); |
298 |
> |
Thread t1 = new Thread(c1); |
299 |
> |
Thread t2 = new Thread(c2); |
300 |
> |
t1.start(); |
301 |
> |
t2.start(); |
302 |
> |
t1.join(); |
303 |
> |
t2.join(); |
304 |
> |
assertEquals(c1.counts+c2.counts, SIZE * COUNTDOWN); |
305 |
|
} |
306 |
|
|
307 |
|
/** |
308 |
|
* a deserialized serialized array holds same values |
309 |
|
*/ |
310 |
< |
public void testSerialization() { |
310 |
> |
public void testSerialization() throws Exception { |
311 |
|
AtomicLongArray l = new AtomicLongArray(SIZE); |
312 |
|
for (int i = 0; i < SIZE; ++i) |
313 |
|
l.set(i, -i); |
314 |
|
|
315 |
< |
try { |
316 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
317 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
318 |
< |
out.writeObject(l); |
319 |
< |
out.close(); |
320 |
< |
|
321 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
322 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
323 |
< |
AtomicLongArray r = (AtomicLongArray) in.readObject(); |
324 |
< |
for (int i = 0; i < SIZE; ++i) { |
332 |
< |
assertEquals(l.get(i), r.get(i)); |
333 |
< |
} |
334 |
< |
} catch (Exception e) { |
335 |
< |
unexpectedException(); |
315 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
316 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
317 |
> |
out.writeObject(l); |
318 |
> |
out.close(); |
319 |
> |
|
320 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
321 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
322 |
> |
AtomicLongArray r = (AtomicLongArray) in.readObject(); |
323 |
> |
for (int i = 0; i < SIZE; ++i) { |
324 |
> |
assertEquals(l.get(i), r.get(i)); |
325 |
|
} |
326 |
|
} |
327 |
|
|