16 |
|
import java.util.concurrent.AbstractExecutorService; |
17 |
|
import java.util.concurrent.ArrayBlockingQueue; |
18 |
|
import java.util.concurrent.Callable; |
19 |
+ |
import java.util.concurrent.CancellationException; |
20 |
|
import java.util.concurrent.CountDownLatch; |
21 |
|
import java.util.concurrent.ExecutionException; |
22 |
|
import java.util.concurrent.Executors; |
588 |
|
l.add(new StringTask()); |
589 |
|
l.add(new StringTask()); |
590 |
|
List<Future<String>> futures = |
591 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
591 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
592 |
|
assertEquals(2, futures.size()); |
593 |
|
for (Future<String> future : futures) |
594 |
|
assertSame(TEST_STRING, future.get()); |
600 |
|
/** |
601 |
|
* timed invokeAll cancels tasks not completed by timeout |
602 |
|
*/ |
603 |
< |
public void testTimedInvokeAll6() throws InterruptedException { |
603 |
> |
public void testTimedInvokeAll6() throws Exception { |
604 |
|
ExecutorService e = new DirectExecutorService(); |
605 |
|
try { |
606 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
607 |
< |
l.add(new StringTask()); |
608 |
< |
l.add(Executors.callable(possiblyInterruptedRunnable(2 * SHORT_DELAY_MS), TEST_STRING)); |
609 |
< |
l.add(new StringTask()); |
606 |
> |
long timeout = timeoutMillis(); |
607 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
608 |
> |
tasks.add(new StringTask("0")); |
609 |
> |
tasks.add(Executors.callable(possiblyInterruptedRunnable(timeout), |
610 |
> |
TEST_STRING)); |
611 |
> |
tasks.add(new StringTask("2")); |
612 |
> |
long startTime = System.nanoTime(); |
613 |
|
List<Future<String>> futures = |
614 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
615 |
< |
assertEquals(l.size(), futures.size()); |
614 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
615 |
> |
assertEquals(tasks.size(), futures.size()); |
616 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
617 |
|
for (Future future : futures) |
618 |
|
assertTrue(future.isDone()); |
619 |
< |
assertFalse(futures.get(0).isCancelled()); |
620 |
< |
assertFalse(futures.get(1).isCancelled()); |
619 |
> |
assertEquals("0", futures.get(0).get()); |
620 |
> |
assertEquals(TEST_STRING, futures.get(1).get()); |
621 |
|
assertTrue(futures.get(2).isCancelled()); |
622 |
|
} finally { |
623 |
|
joinPool(e); |