14 |
|
import java.util.concurrent.ArrayBlockingQueue; |
15 |
|
import java.util.concurrent.BlockingQueue; |
16 |
|
import java.util.concurrent.Callable; |
17 |
+ |
import java.util.concurrent.CancellationException; |
18 |
|
import java.util.concurrent.CountDownLatch; |
19 |
|
import java.util.concurrent.ExecutionException; |
20 |
|
import java.util.concurrent.Executors; |
1738 |
|
public void testTimedInvokeAll6() throws Exception { |
1739 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1740 |
|
try { |
1741 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1742 |
< |
l.add(new StringTask()); |
1743 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1744 |
< |
l.add(new StringTask()); |
1745 |
< |
List<Future<String>> futures = |
1746 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1747 |
< |
assertEquals(l.size(), futures.size()); |
1748 |
< |
for (Future future : futures) |
1749 |
< |
assertTrue(future.isDone()); |
1750 |
< |
assertFalse(futures.get(0).isCancelled()); |
1751 |
< |
assertTrue(futures.get(1).isCancelled()); |
1741 |
> |
for (long timeout = timeoutMillis();;) { |
1742 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1743 |
> |
tasks.add(new StringTask("0")); |
1744 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1745 |
> |
tasks.add(new StringTask("2")); |
1746 |
> |
long startTime = System.nanoTime(); |
1747 |
> |
List<Future<String>> futures = |
1748 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1749 |
> |
assertEquals(tasks.size(), futures.size()); |
1750 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1751 |
> |
for (Future future : futures) |
1752 |
> |
assertTrue(future.isDone()); |
1753 |
> |
assertTrue(futures.get(1).isCancelled()); |
1754 |
> |
try { |
1755 |
> |
assertEquals("0", futures.get(0).get()); |
1756 |
> |
assertEquals("2", futures.get(2).get()); |
1757 |
> |
break; |
1758 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1759 |
> |
timeout *= 2; |
1760 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1761 |
> |
fail("expected exactly one task to be cancelled"); |
1762 |
> |
} |
1763 |
> |
} |
1764 |
|
} finally { |
1765 |
|
joinPool(e); |
1766 |
|
} |
1803 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1804 |
|
*/ |
1805 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1806 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1806 |
> |
long keepAliveTime = timeoutMillis(); |
1807 |
|
final ThreadPoolExecutor p = |
1808 |
|
new CustomTPE(2, 10, |
1809 |
< |
coreThreadTimeOut, MILLISECONDS, |
1809 |
> |
keepAliveTime, MILLISECONDS, |
1810 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1811 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1812 |
|
try { |
1813 |
|
p.allowCoreThreadTimeOut(true); |
1814 |
|
p.execute(new CheckedRunnable() { |
1815 |
< |
public void realRun() throws InterruptedException { |
1815 |
> |
public void realRun() { |
1816 |
|
threadStarted.countDown(); |
1817 |
|
assertEquals(1, p.getPoolSize()); |
1818 |
|
}}); |
1819 |
|
await(threadStarted); |
1820 |
< |
delay(coreThreadTimeOut); |
1820 |
> |
delay(keepAliveTime); |
1821 |
|
long startTime = System.nanoTime(); |
1822 |
|
while (p.getPoolSize() > 0 |
1823 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1833 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1834 |
|
*/ |
1835 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1836 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1836 |
> |
long keepAliveTime = timeoutMillis(); |
1837 |
|
final ThreadPoolExecutor p = |
1838 |
|
new CustomTPE(2, 10, |
1839 |
< |
coreThreadTimeOut, MILLISECONDS, |
1839 |
> |
keepAliveTime, MILLISECONDS, |
1840 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1841 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1842 |
|
try { |
1846 |
|
threadStarted.countDown(); |
1847 |
|
assertTrue(p.getPoolSize() >= 1); |
1848 |
|
}}); |
1849 |
< |
delay(2 * coreThreadTimeOut); |
1849 |
> |
delay(2 * keepAliveTime); |
1850 |
|
assertTrue(p.getPoolSize() >= 1); |
1851 |
|
} finally { |
1852 |
|
joinPool(p); |