76 |
|
* compareAndSet in one thread enables another waiting for value |
77 |
|
* to succeed |
78 |
|
*/ |
79 |
< |
public void testCompareAndSetInMultipleThreads() { |
79 |
> |
public void testCompareAndSetInMultipleThreads() throws Exception { |
80 |
|
final AtomicReference ai = new AtomicReference(one); |
81 |
|
Thread t = new Thread(new Runnable() { |
82 |
|
public void run() { |
83 |
|
while (!ai.compareAndSet(two, three)) Thread.yield(); |
84 |
|
}}); |
85 |
< |
try { |
86 |
< |
t.start(); |
87 |
< |
assertTrue(ai.compareAndSet(one, two)); |
88 |
< |
t.join(LONG_DELAY_MS); |
89 |
< |
assertFalse(t.isAlive()); |
90 |
< |
assertEquals(ai.get(), three); |
91 |
< |
} |
92 |
< |
catch (Exception e) { |
93 |
< |
unexpectedException(); |
94 |
< |
} |
85 |
> |
|
86 |
> |
t.start(); |
87 |
> |
assertTrue(ai.compareAndSet(one, two)); |
88 |
> |
t.join(LONG_DELAY_MS); |
89 |
> |
assertFalse(t.isAlive()); |
90 |
> |
assertEquals(ai.get(), three); |
91 |
|
} |
92 |
|
|
93 |
|
/** |
116 |
|
/** |
117 |
|
* a deserialized serialized atomic holds same value |
118 |
|
*/ |
119 |
< |
public void testSerialization() { |
119 |
> |
public void testSerialization() throws Exception { |
120 |
|
AtomicReference l = new AtomicReference(); |
121 |
|
|
122 |
< |
try { |
123 |
< |
l.set(one); |
124 |
< |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
125 |
< |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
126 |
< |
out.writeObject(l); |
127 |
< |
out.close(); |
128 |
< |
|
129 |
< |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
130 |
< |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
131 |
< |
AtomicReference r = (AtomicReference) in.readObject(); |
136 |
< |
assertEquals(l.get(), r.get()); |
137 |
< |
} catch (Exception e) { |
138 |
< |
unexpectedException(); |
139 |
< |
} |
122 |
> |
l.set(one); |
123 |
> |
ByteArrayOutputStream bout = new ByteArrayOutputStream(10000); |
124 |
> |
ObjectOutputStream out = new ObjectOutputStream(new BufferedOutputStream(bout)); |
125 |
> |
out.writeObject(l); |
126 |
> |
out.close(); |
127 |
> |
|
128 |
> |
ByteArrayInputStream bin = new ByteArrayInputStream(bout.toByteArray()); |
129 |
> |
ObjectInputStream in = new ObjectInputStream(new BufferedInputStream(bin)); |
130 |
> |
AtomicReference r = (AtomicReference) in.readObject(); |
131 |
> |
assertEquals(l.get(), r.get()); |
132 |
|
} |
133 |
|
|
134 |
|
/** |