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; |
29 |
|
import java.util.concurrent.ThreadFactory; |
30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
|
import java.util.concurrent.TimeUnit; |
32 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
33 |
|
|
34 |
|
import junit.framework.Test; |
35 |
|
import junit.framework.TestSuite; |
132 |
|
*/ |
133 |
|
public void testPrestartCoreThread() { |
134 |
|
final ThreadPoolExecutor p = |
135 |
< |
new ThreadPoolExecutor(2, 2, |
135 |
> |
new ThreadPoolExecutor(2, 6, |
136 |
|
LONG_DELAY_MS, MILLISECONDS, |
137 |
|
new ArrayBlockingQueue<Runnable>(10)); |
138 |
< |
assertEquals(0, p.getPoolSize()); |
139 |
< |
assertTrue(p.prestartCoreThread()); |
140 |
< |
assertEquals(1, p.getPoolSize()); |
141 |
< |
assertTrue(p.prestartCoreThread()); |
142 |
< |
assertEquals(2, p.getPoolSize()); |
143 |
< |
assertFalse(p.prestartCoreThread()); |
144 |
< |
assertEquals(2, p.getPoolSize()); |
145 |
< |
joinPool(p); |
138 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
139 |
> |
assertEquals(0, p.getPoolSize()); |
140 |
> |
assertTrue(p.prestartCoreThread()); |
141 |
> |
assertEquals(1, p.getPoolSize()); |
142 |
> |
assertTrue(p.prestartCoreThread()); |
143 |
> |
assertEquals(2, p.getPoolSize()); |
144 |
> |
assertFalse(p.prestartCoreThread()); |
145 |
> |
assertEquals(2, p.getPoolSize()); |
146 |
> |
p.setCorePoolSize(4); |
147 |
> |
assertTrue(p.prestartCoreThread()); |
148 |
> |
assertEquals(3, p.getPoolSize()); |
149 |
> |
assertTrue(p.prestartCoreThread()); |
150 |
> |
assertEquals(4, p.getPoolSize()); |
151 |
> |
assertFalse(p.prestartCoreThread()); |
152 |
> |
assertEquals(4, p.getPoolSize()); |
153 |
> |
} |
154 |
|
} |
155 |
|
|
156 |
|
/** |
158 |
|
*/ |
159 |
|
public void testPrestartAllCoreThreads() { |
160 |
|
final ThreadPoolExecutor p = |
161 |
< |
new ThreadPoolExecutor(2, 2, |
161 |
> |
new ThreadPoolExecutor(2, 6, |
162 |
|
LONG_DELAY_MS, MILLISECONDS, |
163 |
|
new ArrayBlockingQueue<Runnable>(10)); |
164 |
< |
assertEquals(0, p.getPoolSize()); |
165 |
< |
p.prestartAllCoreThreads(); |
166 |
< |
assertEquals(2, p.getPoolSize()); |
167 |
< |
p.prestartAllCoreThreads(); |
168 |
< |
assertEquals(2, p.getPoolSize()); |
169 |
< |
joinPool(p); |
164 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
165 |
> |
assertEquals(0, p.getPoolSize()); |
166 |
> |
p.prestartAllCoreThreads(); |
167 |
> |
assertEquals(2, p.getPoolSize()); |
168 |
> |
p.prestartAllCoreThreads(); |
169 |
> |
assertEquals(2, p.getPoolSize()); |
170 |
> |
p.setCorePoolSize(4); |
171 |
> |
p.prestartAllCoreThreads(); |
172 |
> |
assertEquals(4, p.getPoolSize()); |
173 |
> |
p.prestartAllCoreThreads(); |
174 |
> |
assertEquals(4, p.getPoolSize()); |
175 |
> |
} |
176 |
|
} |
177 |
|
|
178 |
|
/** |
184 |
|
new ThreadPoolExecutor(2, 2, |
185 |
|
LONG_DELAY_MS, MILLISECONDS, |
186 |
|
new ArrayBlockingQueue<Runnable>(10)); |
187 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
188 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
189 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
190 |
< |
try { |
187 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
188 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
189 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
190 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
191 |
|
assertEquals(0, p.getCompletedTaskCount()); |
192 |
|
p.execute(new CheckedRunnable() { |
193 |
|
public void realRun() throws InterruptedException { |
206 |
|
fail("timed out"); |
207 |
|
Thread.yield(); |
208 |
|
} |
193 |
– |
} finally { |
194 |
– |
joinPool(p); |
209 |
|
} |
210 |
|
} |
211 |
|
|
217 |
|
new ThreadPoolExecutor(1, 1, |
218 |
|
LONG_DELAY_MS, MILLISECONDS, |
219 |
|
new ArrayBlockingQueue<Runnable>(10)); |
220 |
< |
assertEquals(1, p.getCorePoolSize()); |
221 |
< |
joinPool(p); |
220 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
221 |
> |
assertEquals(1, p.getCorePoolSize()); |
222 |
> |
} |
223 |
|
} |
224 |
|
|
225 |
|
/** |
230 |
|
new ThreadPoolExecutor(2, 2, |
231 |
|
1000, MILLISECONDS, |
232 |
|
new ArrayBlockingQueue<Runnable>(10)); |
233 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
234 |
< |
joinPool(p); |
233 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
234 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
235 |
> |
} |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
|
* getThreadFactory returns factory in constructor if not set |
240 |
|
*/ |
241 |
|
public void testGetThreadFactory() { |
242 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
242 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
243 |
|
final ThreadPoolExecutor p = |
244 |
|
new ThreadPoolExecutor(1, 2, |
245 |
|
LONG_DELAY_MS, MILLISECONDS, |
246 |
|
new ArrayBlockingQueue<Runnable>(10), |
247 |
< |
tf, |
247 |
> |
threadFactory, |
248 |
|
new NoOpREHandler()); |
249 |
< |
assertSame(tf, p.getThreadFactory()); |
250 |
< |
joinPool(p); |
249 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
250 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
251 |
> |
} |
252 |
|
} |
253 |
|
|
254 |
|
/** |
259 |
|
new ThreadPoolExecutor(1, 2, |
260 |
|
LONG_DELAY_MS, MILLISECONDS, |
261 |
|
new ArrayBlockingQueue<Runnable>(10)); |
262 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
263 |
< |
p.setThreadFactory(tf); |
264 |
< |
assertSame(tf, p.getThreadFactory()); |
265 |
< |
joinPool(p); |
262 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
263 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
264 |
> |
p.setThreadFactory(threadFactory); |
265 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
266 |
> |
} |
267 |
|
} |
268 |
|
|
269 |
|
/** |
641 |
|
} |
642 |
|
|
643 |
|
/** |
644 |
< |
* shutdownNow returns a list containing tasks that were not run |
644 |
> |
* shutdownNow returns a list containing tasks that were not run, |
645 |
> |
* and those tasks are drained from the queue |
646 |
|
*/ |
647 |
< |
public void testShutdownNow() { |
647 |
> |
public void testShutdownNow() throws InterruptedException { |
648 |
> |
final int poolSize = 2; |
649 |
> |
final int count = 5; |
650 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
651 |
|
final ThreadPoolExecutor p = |
652 |
< |
new ThreadPoolExecutor(1, 1, |
652 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
653 |
|
LONG_DELAY_MS, MILLISECONDS, |
654 |
|
new ArrayBlockingQueue<Runnable>(10)); |
655 |
< |
List l; |
656 |
< |
try { |
657 |
< |
for (int i = 0; i < 5; i++) |
636 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
637 |
< |
} |
638 |
< |
finally { |
655 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
656 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
657 |
> |
threadsStarted.countDown(); |
658 |
|
try { |
659 |
< |
l = p.shutdownNow(); |
660 |
< |
} catch (SecurityException ok) { return; } |
659 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
660 |
> |
} catch (InterruptedException success) {} |
661 |
> |
ran.getAndIncrement(); |
662 |
> |
}}; |
663 |
> |
for (int i = 0; i < count; i++) |
664 |
> |
p.execute(waiter); |
665 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
666 |
> |
assertEquals(poolSize, p.getActiveCount()); |
667 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
668 |
> |
final List<Runnable> queuedTasks; |
669 |
> |
try { |
670 |
> |
queuedTasks = p.shutdownNow(); |
671 |
> |
} catch (SecurityException ok) { |
672 |
> |
return; // Allowed in case test doesn't have privs |
673 |
|
} |
674 |
|
assertTrue(p.isShutdown()); |
675 |
< |
assertTrue(l.size() <= 4); |
675 |
> |
assertTrue(p.getQueue().isEmpty()); |
676 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
677 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
678 |
> |
assertTrue(p.isTerminated()); |
679 |
> |
assertEquals(poolSize, ran.get()); |
680 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
681 |
|
} |
682 |
|
|
683 |
|
// Exception Tests |
1026 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1027 |
|
final ThreadPoolExecutor p = |
1028 |
|
new ThreadPoolExecutor(1, 1, |
1029 |
< |
60, TimeUnit.SECONDS, |
1029 |
> |
60, SECONDS, |
1030 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1031 |
|
|
1032 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1367 |
|
} |
1368 |
|
|
1369 |
|
/** |
1370 |
+ |
* Configuration changes that allow core pool size greater than |
1371 |
+ |
* max pool size result in IllegalArgumentException. |
1372 |
+ |
*/ |
1373 |
+ |
public void testPoolSizeInvariants() { |
1374 |
+ |
ThreadPoolExecutor p = |
1375 |
+ |
new ThreadPoolExecutor(1, 1, |
1376 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1377 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1378 |
+ |
for (int s = 1; s < 5; s++) { |
1379 |
+ |
p.setMaximumPoolSize(s); |
1380 |
+ |
p.setCorePoolSize(s); |
1381 |
+ |
try { |
1382 |
+ |
p.setMaximumPoolSize(s - 1); |
1383 |
+ |
shouldThrow(); |
1384 |
+ |
} catch (IllegalArgumentException success) {} |
1385 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1386 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1387 |
+ |
try { |
1388 |
+ |
p.setCorePoolSize(s + 1); |
1389 |
+ |
shouldThrow(); |
1390 |
+ |
} catch (IllegalArgumentException success) {} |
1391 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1392 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1393 |
+ |
} |
1394 |
+ |
joinPool(p); |
1395 |
+ |
} |
1396 |
+ |
|
1397 |
+ |
/** |
1398 |
|
* setKeepAliveTime throws IllegalArgumentException |
1399 |
|
* when given a negative value |
1400 |
|
*/ |
1910 |
|
l.add(new StringTask()); |
1911 |
|
l.add(new StringTask()); |
1912 |
|
List<Future<String>> futures = |
1913 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1913 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1914 |
|
assertEquals(2, futures.size()); |
1915 |
|
for (Future<String> future : futures) |
1916 |
|
assertSame(TEST_STRING, future.get()); |
1928 |
|
LONG_DELAY_MS, MILLISECONDS, |
1929 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1930 |
|
try { |
1931 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1932 |
< |
l.add(new StringTask()); |
1933 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1934 |
< |
l.add(new StringTask()); |
1935 |
< |
List<Future<String>> futures = |
1936 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1937 |
< |
assertEquals(l.size(), futures.size()); |
1938 |
< |
for (Future future : futures) |
1939 |
< |
assertTrue(future.isDone()); |
1940 |
< |
assertFalse(futures.get(0).isCancelled()); |
1941 |
< |
assertTrue(futures.get(1).isCancelled()); |
1931 |
> |
for (long timeout = timeoutMillis();;) { |
1932 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1933 |
> |
tasks.add(new StringTask("0")); |
1934 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1935 |
> |
tasks.add(new StringTask("2")); |
1936 |
> |
long startTime = System.nanoTime(); |
1937 |
> |
List<Future<String>> futures = |
1938 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1939 |
> |
assertEquals(tasks.size(), futures.size()); |
1940 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1941 |
> |
for (Future future : futures) |
1942 |
> |
assertTrue(future.isDone()); |
1943 |
> |
assertTrue(futures.get(1).isCancelled()); |
1944 |
> |
try { |
1945 |
> |
assertEquals("0", futures.get(0).get()); |
1946 |
> |
assertEquals("2", futures.get(2).get()); |
1947 |
> |
break; |
1948 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1949 |
> |
timeout *= 2; |
1950 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1951 |
> |
fail("expected exactly one task to be cancelled"); |
1952 |
> |
} |
1953 |
> |
} |
1954 |
|
} finally { |
1955 |
|
joinPool(e); |
1956 |
|
} |
1996 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1997 |
|
*/ |
1998 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1999 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1999 |
> |
long keepAliveTime = timeoutMillis(); |
2000 |
|
final ThreadPoolExecutor p = |
2001 |
|
new ThreadPoolExecutor(2, 10, |
2002 |
< |
coreThreadTimeOut, MILLISECONDS, |
2002 |
> |
keepAliveTime, MILLISECONDS, |
2003 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2004 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2005 |
|
try { |
2010 |
|
assertEquals(1, p.getPoolSize()); |
2011 |
|
}}); |
2012 |
|
await(threadStarted); |
2013 |
< |
delay(coreThreadTimeOut); |
2013 |
> |
delay(keepAliveTime); |
2014 |
|
long startTime = System.nanoTime(); |
2015 |
|
while (p.getPoolSize() > 0 |
2016 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
2026 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
2027 |
|
*/ |
2028 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
2029 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
2029 |
> |
long keepAliveTime = timeoutMillis(); |
2030 |
|
final ThreadPoolExecutor p = |
2031 |
|
new ThreadPoolExecutor(2, 10, |
2032 |
< |
coreThreadTimeOut, MILLISECONDS, |
2032 |
> |
keepAliveTime, MILLISECONDS, |
2033 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2034 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2035 |
|
try { |
2039 |
|
threadStarted.countDown(); |
2040 |
|
assertTrue(p.getPoolSize() >= 1); |
2041 |
|
}}); |
2042 |
< |
delay(2 * coreThreadTimeOut); |
2042 |
> |
delay(2 * keepAliveTime); |
2043 |
|
assertTrue(p.getPoolSize() >= 1); |
2044 |
|
} finally { |
2045 |
|
joinPool(p); |
2058 |
|
done.countDown(); |
2059 |
|
}}; |
2060 |
|
final ThreadPoolExecutor p = |
2061 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2061 |
> |
new ThreadPoolExecutor(1, 30, |
2062 |
> |
60, SECONDS, |
2063 |
|
new ArrayBlockingQueue(30)); |
2064 |
|
try { |
2065 |
|
for (int i = 0; i < nTasks; ++i) { |
2078 |
|
} |
2079 |
|
} |
2080 |
|
|
2081 |
+ |
/** |
2082 |
+ |
* get(cancelled task) throws CancellationException |
2083 |
+ |
*/ |
2084 |
+ |
public void testGet_cancelled() throws Exception { |
2085 |
+ |
final ExecutorService e = |
2086 |
+ |
new ThreadPoolExecutor(1, 1, |
2087 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
2088 |
+ |
new LinkedBlockingQueue<Runnable>()); |
2089 |
+ |
try { |
2090 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2091 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2092 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
2093 |
+ |
for (int i = 0; i < 2; i++) { |
2094 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
2095 |
+ |
throws Throwable { |
2096 |
+ |
blockerStarted.countDown(); |
2097 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2098 |
+ |
}}; |
2099 |
+ |
futures.add(e.submit(r)); |
2100 |
+ |
} |
2101 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2102 |
+ |
for (Future<?> future : futures) future.cancel(false); |
2103 |
+ |
for (Future<?> future : futures) { |
2104 |
+ |
try { |
2105 |
+ |
future.get(); |
2106 |
+ |
shouldThrow(); |
2107 |
+ |
} catch (CancellationException success) {} |
2108 |
+ |
try { |
2109 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2110 |
+ |
shouldThrow(); |
2111 |
+ |
} catch (CancellationException success) {} |
2112 |
+ |
assertTrue(future.isCancelled()); |
2113 |
+ |
assertTrue(future.isDone()); |
2114 |
+ |
} |
2115 |
+ |
done.countDown(); |
2116 |
+ |
} finally { |
2117 |
+ |
joinPool(e); |
2118 |
+ |
} |
2119 |
+ |
} |
2120 |
+ |
|
2121 |
|
} |