590 |
|
* CancellationException |
591 |
|
*/ |
592 |
|
public void testTimedGet_Cancellation() { |
593 |
< |
for (final boolean mayInterruptIfRunning : |
594 |
< |
new boolean[] { true, false }) { |
595 |
< |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
596 |
< |
final CountDownLatch cancelled = new CountDownLatch(1); |
597 |
< |
final PublicFutureTask task = |
598 |
< |
new PublicFutureTask(new CheckedCallable<Object>() { |
599 |
< |
public Object realCall() throws InterruptedException { |
600 |
< |
pleaseCancel.countDown(); |
601 |
< |
if (mayInterruptIfRunning) { |
602 |
< |
try { |
603 |
< |
delay(2*LONG_DELAY_MS); |
604 |
< |
} catch (InterruptedException success) {} |
605 |
< |
} else { |
606 |
< |
await(cancelled); |
607 |
< |
} |
608 |
< |
return two; |
609 |
< |
}}); |
593 |
> |
testTimedGet_Cancellation(false); |
594 |
> |
} |
595 |
> |
public void testTimedGet_Cancellation_interrupt() { |
596 |
> |
testTimedGet_Cancellation(true); |
597 |
> |
} |
598 |
> |
public void testTimedGet_Cancellation(final boolean mayInterruptIfRunning) { |
599 |
> |
final CountDownLatch pleaseCancel = new CountDownLatch(3); |
600 |
> |
final CountDownLatch cancelled = new CountDownLatch(1); |
601 |
> |
final Callable<Object> callable = |
602 |
> |
new CheckedCallable<Object>() { |
603 |
> |
public Object realCall() throws InterruptedException { |
604 |
> |
pleaseCancel.countDown(); |
605 |
> |
if (mayInterruptIfRunning) { |
606 |
> |
try { |
607 |
> |
delay(2*LONG_DELAY_MS); |
608 |
> |
} catch (InterruptedException success) {} |
609 |
> |
} else { |
610 |
> |
await(cancelled); |
611 |
> |
} |
612 |
> |
return two; |
613 |
> |
}}; |
614 |
> |
final PublicFutureTask task = new PublicFutureTask(callable); |
615 |
|
|
616 |
< |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
616 |
> |
Thread t1 = new ThreadShouldThrow(CancellationException.class) { |
617 |
|
public void realRun() throws Exception { |
618 |
|
pleaseCancel.countDown(); |
619 |
|
task.get(); |
620 |
|
}}; |
621 |
< |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
621 |
> |
Thread t2 = new ThreadShouldThrow(CancellationException.class) { |
622 |
|
public void realRun() throws Exception { |
623 |
|
pleaseCancel.countDown(); |
624 |
|
task.get(2*LONG_DELAY_MS, MILLISECONDS); |
625 |
|
}}; |
626 |
< |
t1.start(); |
627 |
< |
t2.start(); |
628 |
< |
Thread t3 = newStartedThread(task); |
629 |
< |
await(pleaseCancel); |
630 |
< |
checkIsRunning(task); |
631 |
< |
task.cancel(mayInterruptIfRunning); |
632 |
< |
checkCancelled(task); |
633 |
< |
awaitTermination(t1); |
634 |
< |
awaitTermination(t2); |
635 |
< |
cancelled.countDown(); |
636 |
< |
awaitTermination(t3); |
637 |
< |
assertEquals(1, task.runCount()); |
638 |
< |
assertEquals(1, task.setCount()); |
639 |
< |
assertEquals(0, task.setExceptionCount()); |
640 |
< |
tryToConfuseDoneTask(task); |
641 |
< |
checkCancelled(task); |
637 |
< |
} |
626 |
> |
t1.start(); |
627 |
> |
t2.start(); |
628 |
> |
Thread t3 = newStartedThread(task); |
629 |
> |
await(pleaseCancel); |
630 |
> |
checkIsRunning(task); |
631 |
> |
task.cancel(mayInterruptIfRunning); |
632 |
> |
checkCancelled(task); |
633 |
> |
awaitTermination(t1); |
634 |
> |
awaitTermination(t2); |
635 |
> |
cancelled.countDown(); |
636 |
> |
awaitTermination(t3); |
637 |
> |
assertEquals(1, task.runCount()); |
638 |
> |
assertEquals(1, task.setCount()); |
639 |
> |
assertEquals(0, task.setExceptionCount()); |
640 |
> |
tryToConfuseDoneTask(task); |
641 |
> |
checkCancelled(task); |
642 |
|
} |
643 |
|
|
644 |
|
/** |