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