19 |
|
} |
20 |
|
|
21 |
|
/** |
22 |
< |
* |
22 |
> |
* exchange exchanges objects across two threads |
23 |
|
*/ |
24 |
|
public void testExchange() { |
25 |
|
final Exchanger e = new Exchanger(); |
58 |
|
} |
59 |
|
|
60 |
|
/** |
61 |
< |
* |
61 |
> |
* timed exchange exchanges objects across two threads |
62 |
|
*/ |
63 |
|
public void testTimedExchange() { |
64 |
|
final Exchanger e = new Exchanger(); |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
< |
* |
104 |
> |
* interrupt during wait for exchange throws IE |
105 |
|
*/ |
106 |
|
public void testExchange_InterruptedException(){ |
107 |
|
final Exchanger e = new Exchanger(); |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
< |
* |
128 |
> |
* interrupt during wait for timed exchange throws IE |
129 |
|
*/ |
130 |
|
public void testTimedExchange_InterruptedException(){ |
131 |
|
final Exchanger e = new Exchanger(); |
150 |
|
} |
151 |
|
|
152 |
|
/** |
153 |
< |
* |
153 |
> |
* timeout during wait for timed exchange throws TOE |
154 |
|
*/ |
155 |
|
public void testExchange_TimeOutException(){ |
156 |
|
final Exchanger e = new Exchanger(); |
172 |
|
unexpectedException(); |
173 |
|
} |
174 |
|
} |
175 |
+ |
|
176 |
+ |
/** |
177 |
+ |
* If one exchanging thread is interrupted, another succeeds. |
178 |
+ |
*/ |
179 |
+ |
public void testReplacementAfterExchange() { |
180 |
+ |
final Exchanger e = new Exchanger(); |
181 |
+ |
Thread t1 = new Thread(new Runnable(){ |
182 |
+ |
public void run(){ |
183 |
+ |
try { |
184 |
+ |
Object v = e.exchange(one); |
185 |
+ |
threadAssertEquals(v, two); |
186 |
+ |
Object w = e.exchange(v); |
187 |
+ |
threadShouldThrow(); |
188 |
+ |
} catch(InterruptedException success){ |
189 |
+ |
} |
190 |
+ |
} |
191 |
+ |
}); |
192 |
+ |
Thread t2 = new Thread(new Runnable(){ |
193 |
+ |
public void run(){ |
194 |
+ |
try { |
195 |
+ |
Object v = e.exchange(two); |
196 |
+ |
threadAssertEquals(v, one); |
197 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
198 |
+ |
Object w = e.exchange(v); |
199 |
+ |
threadAssertEquals(w, three); |
200 |
+ |
} catch(InterruptedException e){ |
201 |
+ |
threadUnexpectedException(); |
202 |
+ |
} |
203 |
+ |
} |
204 |
+ |
}); |
205 |
+ |
Thread t3 = new Thread(new Runnable(){ |
206 |
+ |
public void run(){ |
207 |
+ |
try { |
208 |
+ |
Thread.sleep(SMALL_DELAY_MS); |
209 |
+ |
Object w = e.exchange(three); |
210 |
+ |
threadAssertEquals(w, one); |
211 |
+ |
} catch(InterruptedException e){ |
212 |
+ |
threadUnexpectedException(); |
213 |
+ |
} |
214 |
+ |
} |
215 |
+ |
}); |
216 |
+ |
|
217 |
+ |
try { |
218 |
+ |
t1.start(); |
219 |
+ |
t2.start(); |
220 |
+ |
t3.start(); |
221 |
+ |
Thread.sleep(SHORT_DELAY_MS); |
222 |
+ |
t1.interrupt(); |
223 |
+ |
t1.join(); |
224 |
+ |
t2.join(); |
225 |
+ |
t3.join(); |
226 |
+ |
} catch(InterruptedException ex) { |
227 |
+ |
unexpectedException(); |
228 |
+ |
} |
229 |
+ |
} |
230 |
+ |
|
231 |
|
} |