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; |
699 |
|
} |
700 |
|
|
701 |
|
/** |
702 |
< |
* shutdownNow returns a list containing tasks that were not run |
702 |
> |
* shutdownNow returns a list containing tasks that were not run, |
703 |
> |
* and those tasks are drained from the queue |
704 |
|
*/ |
705 |
|
public void testShutdownNow() { |
706 |
|
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
715 |
|
} catch (SecurityException ok) { return; } |
716 |
|
} |
717 |
|
assertTrue(p.isShutdown()); |
718 |
+ |
assertTrue(p.getQueue().isEmpty()); |
719 |
|
assertTrue(l.size() <= 4); |
720 |
|
} |
721 |
|
|
1740 |
|
public void testTimedInvokeAll6() throws Exception { |
1741 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1742 |
|
try { |
1743 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1744 |
< |
l.add(new StringTask()); |
1745 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1746 |
< |
l.add(new StringTask()); |
1747 |
< |
List<Future<String>> futures = |
1748 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1749 |
< |
assertEquals(l.size(), futures.size()); |
1750 |
< |
for (Future future : futures) |
1751 |
< |
assertTrue(future.isDone()); |
1752 |
< |
assertFalse(futures.get(0).isCancelled()); |
1753 |
< |
assertTrue(futures.get(1).isCancelled()); |
1743 |
> |
for (long timeout = timeoutMillis();;) { |
1744 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1745 |
> |
tasks.add(new StringTask("0")); |
1746 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1747 |
> |
tasks.add(new StringTask("2")); |
1748 |
> |
long startTime = System.nanoTime(); |
1749 |
> |
List<Future<String>> futures = |
1750 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1751 |
> |
assertEquals(tasks.size(), futures.size()); |
1752 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1753 |
> |
for (Future future : futures) |
1754 |
> |
assertTrue(future.isDone()); |
1755 |
> |
assertTrue(futures.get(1).isCancelled()); |
1756 |
> |
try { |
1757 |
> |
assertEquals("0", futures.get(0).get()); |
1758 |
> |
assertEquals("2", futures.get(2).get()); |
1759 |
> |
break; |
1760 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1761 |
> |
timeout *= 2; |
1762 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1763 |
> |
fail("expected exactly one task to be cancelled"); |
1764 |
> |
} |
1765 |
> |
} |
1766 |
|
} finally { |
1767 |
|
joinPool(e); |
1768 |
|
} |
1805 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1806 |
|
*/ |
1807 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1808 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1808 |
> |
long keepAliveTime = timeoutMillis(); |
1809 |
|
final ThreadPoolExecutor p = |
1810 |
|
new CustomTPE(2, 10, |
1811 |
< |
coreThreadTimeOut, MILLISECONDS, |
1811 |
> |
keepAliveTime, MILLISECONDS, |
1812 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1813 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1814 |
|
try { |
1815 |
|
p.allowCoreThreadTimeOut(true); |
1816 |
|
p.execute(new CheckedRunnable() { |
1817 |
< |
public void realRun() throws InterruptedException { |
1817 |
> |
public void realRun() { |
1818 |
|
threadStarted.countDown(); |
1819 |
|
assertEquals(1, p.getPoolSize()); |
1820 |
|
}}); |
1821 |
|
await(threadStarted); |
1822 |
< |
delay(coreThreadTimeOut); |
1822 |
> |
delay(keepAliveTime); |
1823 |
|
long startTime = System.nanoTime(); |
1824 |
|
while (p.getPoolSize() > 0 |
1825 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1835 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1836 |
|
*/ |
1837 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1838 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1838 |
> |
long keepAliveTime = timeoutMillis(); |
1839 |
|
final ThreadPoolExecutor p = |
1840 |
|
new CustomTPE(2, 10, |
1841 |
< |
coreThreadTimeOut, MILLISECONDS, |
1841 |
> |
keepAliveTime, MILLISECONDS, |
1842 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1843 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1844 |
|
try { |
1848 |
|
threadStarted.countDown(); |
1849 |
|
assertTrue(p.getPoolSize() >= 1); |
1850 |
|
}}); |
1851 |
< |
delay(2 * coreThreadTimeOut); |
1851 |
> |
delay(2 * keepAliveTime); |
1852 |
|
assertTrue(p.getPoolSize() >= 1); |
1853 |
|
} finally { |
1854 |
|
joinPool(p); |