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