77 |
|
* compareAndSet in one thread enables another waiting for value |
78 |
|
* to succeed |
79 |
|
*/ |
80 |
< |
public void testCompareAndSetInMultipleThreads() { |
80 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
81 |
|
final AtomicInteger ai = new AtomicInteger(1); |
82 |
< |
Thread t = new Thread(new Runnable() { |
83 |
< |
public void run() { |
84 |
< |
while (!ai.compareAndSet(2, 3)) Thread.yield(); |
85 |
< |
}}); |
86 |
< |
try { |
87 |
< |
t.start(); |
88 |
< |
assertTrue(ai.compareAndSet(1, 2)); |
89 |
< |
t.join(LONG_DELAY_MS); |
90 |
< |
assertFalse(t.isAlive()); |
91 |
< |
assertEquals(ai.get(), 3); |
92 |
< |
} |
93 |
< |
catch (Exception e) { |
94 |
< |
unexpectedException(); |
95 |
< |
} |
82 |
> |
Thread t = new Thread(new CheckedRunnable() { |
83 |
> |
public void realRun() { |
84 |
> |
while (!ai.compareAndSet(2, 3)) |
85 |
> |
Thread.yield(); |
86 |
> |
}}); |
87 |
> |
|
88 |
> |
t.start(); |
89 |
> |
assertTrue(ai.compareAndSet(1, 2)); |
90 |
> |
t.join(LONG_DELAY_MS); |
91 |
> |
assertFalse(t.isAlive()); |
92 |
> |
assertEquals(ai.get(), 3); |
93 |
|
} |
94 |
|
|
95 |
|
/** |
189 |
|
/** |
190 |
|
* a deserialized serialized atomic holds same value |
191 |
|
*/ |
192 |
< |
public void testSerialization() { |
192 |
> |
public void testSerialization() throws Exception { |
193 |
|
AtomicInteger l = new AtomicInteger(); |
194 |
|
|
195 |
< |
try { |
196 |
< |
l.set(22); |
197 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
198 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
199 |
< |
out.writeObject(l); |
200 |
< |
out.close(); |
201 |
< |
|
202 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
203 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
204 |
< |
AtomicInteger r = (AtomicInteger) in.readObject(); |
208 |
< |
assertEquals(l.get(), r.get()); |
209 |
< |
} catch (Exception e) { |
210 |
< |
unexpectedException(); |
211 |
< |
} |
195 |
> |
l.set(22); |
196 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
197 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
198 |
> |
out.writeObject(l); |
199 |
> |
out.close(); |
200 |
> |
|
201 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
202 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
203 |
> |
AtomicInteger r = (AtomicInteger) in.readObject(); |
204 |
> |
assertEquals(l.get(), r.get()); |
205 |
|
} |
206 |
|
|
207 |
|
/** |