14 |
|
import java.util.List; |
15 |
|
import java.util.concurrent.ArrayBlockingQueue; |
16 |
|
import java.util.concurrent.BlockingQueue; |
17 |
+ |
import java.util.concurrent.CancellationException; |
18 |
|
import java.util.concurrent.Callable; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.ExecutionException; |
1893 |
|
LONG_DELAY_MS, MILLISECONDS, |
1894 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1895 |
|
try { |
1896 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1897 |
< |
l.add(new StringTask()); |
1898 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1899 |
< |
l.add(new StringTask()); |
1900 |
< |
List<Future<String>> futures = |
1901 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1902 |
< |
assertEquals(l.size(), futures.size()); |
1903 |
< |
for (Future future : futures) |
1904 |
< |
assertTrue(future.isDone()); |
1905 |
< |
assertFalse(futures.get(0).isCancelled()); |
1906 |
< |
assertTrue(futures.get(1).isCancelled()); |
1896 |
> |
for (long timeout = timeoutMillis();;) { |
1897 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1898 |
> |
tasks.add(new StringTask()); |
1899 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1900 |
> |
tasks.add(new StringTask()); |
1901 |
> |
long startTime = System.nanoTime(); |
1902 |
> |
List<Future<String>> futures = |
1903 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1904 |
> |
assertEquals(tasks.size(), futures.size()); |
1905 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1906 |
> |
for (Future future : futures) |
1907 |
> |
assertTrue(future.isDone()); |
1908 |
> |
assertTrue(futures.get(1).isCancelled()); |
1909 |
> |
try { |
1910 |
> |
assertEquals(TEST_STRING, futures.get(0).get()); |
1911 |
> |
assertEquals(TEST_STRING, futures.get(2).get()); |
1912 |
> |
break; |
1913 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1914 |
> |
timeout *= 2; |
1915 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1916 |
> |
fail("expected exactly one task to be cancelled"); |
1917 |
> |
} |
1918 |
> |
} |
1919 |
|
} finally { |
1920 |
|
joinPool(e); |
1921 |
|
} |