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