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 |
< |
} |
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 |
|
AtomicLong l = new AtomicLong(); |
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 |
< |
AtomicLong r = (AtomicLong) in.readObject(); |
209 |
< |
assertEquals(l.get(), r.get()); |
210 |
< |
} catch (Exception e) { |
211 |
< |
unexpectedException(); |
212 |
< |
} |
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 |
> |
AtomicLong r = (AtomicLong) in.readObject(); |
204 |
> |
assertEquals(l.get(), r.get()); |
205 |
|
} |
206 |
|
|
207 |
|
/** |