15 |
|
import java.util.concurrent.ArrayBlockingQueue; |
16 |
|
import java.util.concurrent.BlockingQueue; |
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; |
216 |
|
new ThreadPoolExecutor(2, 2, |
217 |
|
1000, MILLISECONDS, |
218 |
|
new ArrayBlockingQueue<Runnable>(10)); |
219 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
219 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
220 |
|
joinPool(p); |
221 |
|
} |
222 |
|
|
991 |
|
public void testInterruptedSubmit() throws InterruptedException { |
992 |
|
final ThreadPoolExecutor p = |
993 |
|
new ThreadPoolExecutor(1, 1, |
994 |
< |
60, TimeUnit.SECONDS, |
994 |
> |
60, SECONDS, |
995 |
|
new ArrayBlockingQueue<Runnable>(10)); |
996 |
|
|
997 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1332 |
|
} |
1333 |
|
|
1334 |
|
/** |
1335 |
+ |
* Configuration changes that allow core pool size greater than |
1336 |
+ |
* max pool size result in IllegalArgumentException. |
1337 |
+ |
*/ |
1338 |
+ |
public void testPoolSizeInvariants() { |
1339 |
+ |
ThreadPoolExecutor p = |
1340 |
+ |
new ThreadPoolExecutor(1, 1, |
1341 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1342 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1343 |
+ |
for (int s = 1; s < 5; s++) { |
1344 |
+ |
p.setMaximumPoolSize(s); |
1345 |
+ |
p.setCorePoolSize(s); |
1346 |
+ |
try { |
1347 |
+ |
p.setMaximumPoolSize(s - 1); |
1348 |
+ |
shouldThrow(); |
1349 |
+ |
} catch (IllegalArgumentException success) {} |
1350 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1351 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1352 |
+ |
try { |
1353 |
+ |
p.setCorePoolSize(s + 1); |
1354 |
+ |
shouldThrow(); |
1355 |
+ |
} catch (IllegalArgumentException success) {} |
1356 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1357 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1358 |
+ |
} |
1359 |
+ |
joinPool(p); |
1360 |
+ |
} |
1361 |
+ |
|
1362 |
+ |
/** |
1363 |
|
* setKeepAliveTime throws IllegalArgumentException |
1364 |
|
* when given a negative value |
1365 |
|
*/ |
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("0")); |
1899 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1900 |
> |
tasks.add(new StringTask("2")); |
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("0", futures.get(0).get()); |
1911 |
> |
assertEquals("2", 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 |
|
} |
1961 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1962 |
|
*/ |
1963 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1964 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1964 |
> |
long keepAliveTime = timeoutMillis(); |
1965 |
|
final ThreadPoolExecutor p = |
1966 |
|
new ThreadPoolExecutor(2, 10, |
1967 |
< |
coreThreadTimeOut, MILLISECONDS, |
1967 |
> |
keepAliveTime, MILLISECONDS, |
1968 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1969 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1970 |
|
try { |
1975 |
|
assertEquals(1, p.getPoolSize()); |
1976 |
|
}}); |
1977 |
|
await(threadStarted); |
1978 |
< |
delay(coreThreadTimeOut); |
1978 |
> |
delay(keepAliveTime); |
1979 |
|
long startTime = System.nanoTime(); |
1980 |
|
while (p.getPoolSize() > 0 |
1981 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1991 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1992 |
|
*/ |
1993 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1994 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1994 |
> |
long keepAliveTime = timeoutMillis(); |
1995 |
|
final ThreadPoolExecutor p = |
1996 |
|
new ThreadPoolExecutor(2, 10, |
1997 |
< |
coreThreadTimeOut, MILLISECONDS, |
1997 |
> |
keepAliveTime, MILLISECONDS, |
1998 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1999 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2000 |
|
try { |
2004 |
|
threadStarted.countDown(); |
2005 |
|
assertTrue(p.getPoolSize() >= 1); |
2006 |
|
}}); |
2007 |
< |
delay(2 * coreThreadTimeOut); |
2007 |
> |
delay(2 * keepAliveTime); |
2008 |
|
assertTrue(p.getPoolSize() >= 1); |
2009 |
|
} finally { |
2010 |
|
joinPool(p); |
2023 |
|
done.countDown(); |
2024 |
|
}}; |
2025 |
|
final ThreadPoolExecutor p = |
2026 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2026 |
> |
new ThreadPoolExecutor(1, 30, |
2027 |
> |
60, SECONDS, |
2028 |
|
new ArrayBlockingQueue(30)); |
2029 |
|
try { |
2030 |
|
for (int i = 0; i < nTasks; ++i) { |