14 |
|
import java.util.List; |
15 |
|
import java.util.concurrent.ArrayBlockingQueue; |
16 |
|
import java.util.concurrent.BlockingQueue; |
17 |
– |
import java.util.concurrent.CancellationException; |
17 |
|
import java.util.concurrent.Callable; |
18 |
+ |
import java.util.concurrent.CancellationException; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.ExecutionException; |
21 |
|
import java.util.concurrent.Executors; |
624 |
|
} |
625 |
|
|
626 |
|
/** |
627 |
< |
* shutdownNow returns a list containing tasks that were not run |
627 |
> |
* shutdownNow returns a list containing tasks that were not run, |
628 |
> |
* and those tasks are drained from the queue |
629 |
|
*/ |
630 |
|
public void testShutdownNow() { |
631 |
|
final ThreadPoolExecutor p = |
643 |
|
} catch (SecurityException ok) { return; } |
644 |
|
} |
645 |
|
assertTrue(p.isShutdown()); |
646 |
+ |
assertTrue(p.getQueue().isEmpty()); |
647 |
|
assertTrue(l.size() <= 4); |
648 |
|
} |
649 |
|
|
1897 |
|
try { |
1898 |
|
for (long timeout = timeoutMillis();;) { |
1899 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1900 |
< |
tasks.add(new StringTask()); |
1900 |
> |
tasks.add(new StringTask("0")); |
1901 |
|
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1902 |
< |
tasks.add(new StringTask()); |
1902 |
> |
tasks.add(new StringTask("2")); |
1903 |
|
long startTime = System.nanoTime(); |
1904 |
|
List<Future<String>> futures = |
1905 |
|
e.invokeAll(tasks, timeout, MILLISECONDS); |
1909 |
|
assertTrue(future.isDone()); |
1910 |
|
assertTrue(futures.get(1).isCancelled()); |
1911 |
|
try { |
1912 |
< |
assertEquals(TEST_STRING, futures.get(0).get()); |
1913 |
< |
assertEquals(TEST_STRING, futures.get(2).get()); |
1912 |
> |
assertEquals("0", futures.get(0).get()); |
1913 |
> |
assertEquals("2", futures.get(2).get()); |
1914 |
|
break; |
1915 |
|
} catch (CancellationException retryWithLongerTimeout) { |
1916 |
|
timeout *= 2; |
1963 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1964 |
|
*/ |
1965 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1966 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1966 |
> |
long keepAliveTime = timeoutMillis(); |
1967 |
|
final ThreadPoolExecutor p = |
1968 |
|
new ThreadPoolExecutor(2, 10, |
1969 |
< |
coreThreadTimeOut, MILLISECONDS, |
1969 |
> |
keepAliveTime, MILLISECONDS, |
1970 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1971 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1972 |
|
try { |
1977 |
|
assertEquals(1, p.getPoolSize()); |
1978 |
|
}}); |
1979 |
|
await(threadStarted); |
1980 |
< |
delay(coreThreadTimeOut); |
1980 |
> |
delay(keepAliveTime); |
1981 |
|
long startTime = System.nanoTime(); |
1982 |
|
while (p.getPoolSize() > 0 |
1983 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1993 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1994 |
|
*/ |
1995 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1996 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1996 |
> |
long keepAliveTime = timeoutMillis(); |
1997 |
|
final ThreadPoolExecutor p = |
1998 |
|
new ThreadPoolExecutor(2, 10, |
1999 |
< |
coreThreadTimeOut, MILLISECONDS, |
1999 |
> |
keepAliveTime, MILLISECONDS, |
2000 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2001 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2002 |
|
try { |
2006 |
|
threadStarted.countDown(); |
2007 |
|
assertTrue(p.getPoolSize() >= 1); |
2008 |
|
}}); |
2009 |
< |
delay(2 * coreThreadTimeOut); |
2009 |
> |
delay(2 * keepAliveTime); |
2010 |
|
assertTrue(p.getPoolSize() >= 1); |
2011 |
|
} finally { |
2012 |
|
joinPool(p); |