2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
|
* http://creativecommons.org/licenses/publicdomain |
5 |
< |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
< |
* Pat Fisher, Mike Judd. |
5 |
> |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
> |
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
|
import junit.framework.*; |
13 |
|
public class FutureTaskTest extends JSR166TestCase { |
14 |
|
|
15 |
|
public static void main(String[] args) { |
16 |
< |
junit.textui.TestRunner.run (suite()); |
16 |
> |
junit.textui.TestRunner.run (suite()); |
17 |
|
} |
18 |
|
public static Test suite() { |
19 |
|
return new TestSuite(FutureTaskTest.class); |
167 |
|
} }); |
168 |
|
Thread t = new Thread(task); |
169 |
|
t.start(); |
170 |
< |
|
170 |
> |
|
171 |
|
try { |
172 |
|
Thread.sleep(SHORT_DELAY_MS); |
173 |
|
assertTrue(task.cancel(true)); |
196 |
|
} }); |
197 |
|
Thread t = new Thread(task); |
198 |
|
t.start(); |
199 |
< |
|
199 |
> |
|
200 |
|
try { |
201 |
|
Thread.sleep(SHORT_DELAY_MS); |
202 |
|
assertTrue(task.cancel(false)); |
243 |
|
} catch(InterruptedException e){ |
244 |
|
unexpectedException(); |
245 |
|
|
246 |
< |
} |
246 |
> |
} |
247 |
|
} |
248 |
|
|
249 |
|
/** |
280 |
|
assertFalse(ft.isCancelled()); |
281 |
|
} catch(InterruptedException e){ |
282 |
|
unexpectedException(); |
283 |
< |
|
284 |
< |
} |
283 |
> |
|
284 |
> |
} |
285 |
|
} |
286 |
|
|
287 |
|
/** |
311 |
|
} |
312 |
|
}); |
313 |
|
Thread t2 = new Thread(ft); |
314 |
< |
t1.start(); |
314 |
> |
t1.start(); |
315 |
|
t2.start(); |
316 |
|
Thread.sleep(SHORT_DELAY_MS); |
317 |
|
ft.cancel(true); |
350 |
|
} |
351 |
|
}); |
352 |
|
Thread t2 = new Thread(ft); |
353 |
< |
t1.start(); |
353 |
> |
t1.start(); |
354 |
|
t2.start(); |
355 |
|
Thread.sleep(SHORT_DELAY_MS); |
356 |
|
ft.cancel(true); |
360 |
|
unexpectedException(); |
361 |
|
} |
362 |
|
} |
363 |
< |
|
363 |
> |
|
364 |
|
|
365 |
|
/** |
366 |
|
* A runtime exception in task causes get to throw ExecutionException |
382 |
|
unexpectedException(); |
383 |
|
} |
384 |
|
} |
385 |
< |
|
385 |
> |
|
386 |
|
/** |
387 |
|
* A runtime exception in task causes timed get to throw ExecutionException |
388 |
|
*/ |
397 |
|
ft.run(); |
398 |
|
ft.get(SHORT_DELAY_MS, TimeUnit.MILLISECONDS); |
399 |
|
shouldThrow(); |
400 |
< |
} catch(ExecutionException success) { |
400 |
> |
} catch(ExecutionException success) { |
401 |
|
} catch(TimeoutException success) { } // unlikely but OK |
402 |
|
catch(Exception e){ |
403 |
|
unexpectedException(); |
404 |
|
} |
405 |
|
} |
406 |
< |
|
406 |
> |
|
407 |
|
|
408 |
|
/** |
409 |
|
* Interrupting a waiting get causes it to throw InterruptedException |
411 |
|
public void testGet_InterruptedException() { |
412 |
|
final FutureTask ft = new FutureTask(new NoOpCallable()); |
413 |
|
Thread t = new Thread(new Runnable() { |
414 |
< |
public void run() { |
414 |
> |
public void run() { |
415 |
|
try { |
416 |
|
ft.get(); |
417 |
|
threadShouldThrow(); |
437 |
|
public void testTimedGet_InterruptedException2() { |
438 |
|
final FutureTask ft = new FutureTask(new NoOpCallable()); |
439 |
|
Thread t = new Thread(new Runnable() { |
440 |
< |
public void run() { |
440 |
> |
public void run() { |
441 |
|
try { |
442 |
|
ft.get(LONG_DELAY_MS,TimeUnit.MILLISECONDS); |
443 |
|
threadShouldThrow(); |
456 |
|
unexpectedException(); |
457 |
|
} |
458 |
|
} |
459 |
< |
|
459 |
> |
|
460 |
|
/** |
461 |
|
* A timed out timed get throws TimeoutException |
462 |
|
*/ |
470 |
|
unexpectedException(); |
471 |
|
} |
472 |
|
} |
473 |
< |
|
473 |
> |
|
474 |
|
} |