1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
> |
|
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.List; |
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; |
22 |
> |
import java.util.concurrent.ExecutorService; |
23 |
> |
import java.util.concurrent.Future; |
24 |
> |
import java.util.concurrent.FutureTask; |
25 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
26 |
> |
import java.util.concurrent.RejectedExecutionException; |
27 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
28 |
> |
import java.util.concurrent.SynchronousQueue; |
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; |
36 |
|
|
37 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
38 |
|
public static void main(String[] args) { |
39 |
< |
junit.textui.TestRunner.run(suite()); |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
public static Test suite() { |
42 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
43 |
|
} |
44 |
|
|
45 |
|
static class ExtendedTPE extends ThreadPoolExecutor { |
46 |
< |
volatile boolean beforeCalled = false; |
47 |
< |
volatile boolean afterCalled = false; |
48 |
< |
volatile boolean terminatedCalled = false; |
46 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
47 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
48 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
49 |
> |
|
50 |
|
public ExtendedTPE() { |
51 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
52 |
|
} |
53 |
|
protected void beforeExecute(Thread t, Runnable r) { |
54 |
< |
beforeCalled = true; |
54 |
> |
beforeCalled.countDown(); |
55 |
|
} |
56 |
|
protected void afterExecute(Runnable r, Throwable t) { |
57 |
< |
afterCalled = true; |
57 |
> |
afterCalled.countDown(); |
58 |
|
} |
59 |
|
protected void terminated() { |
60 |
< |
terminatedCalled = true; |
60 |
> |
terminatedCalled.countDown(); |
61 |
> |
} |
62 |
> |
|
63 |
> |
public boolean beforeCalled() { |
64 |
> |
return beforeCalled.getCount() == 0; |
65 |
> |
} |
66 |
> |
public boolean afterCalled() { |
67 |
> |
return afterCalled.getCount() == 0; |
68 |
> |
} |
69 |
> |
public boolean terminatedCalled() { |
70 |
> |
return terminatedCalled.getCount() == 0; |
71 |
|
} |
72 |
|
} |
73 |
|
|
79 |
|
} |
80 |
|
} |
81 |
|
|
49 |
– |
|
82 |
|
/** |
83 |
|
* execute successfully executes a runnable |
84 |
|
*/ |
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 { |
196 |
|
threadProceed.await(); |
197 |
|
threadDone.countDown(); |
198 |
|
}}); |
199 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
199 |
> |
await(threadStarted); |
200 |
|
assertEquals(0, p.getCompletedTaskCount()); |
201 |
|
threadProceed.countDown(); |
202 |
|
threadDone.await(); |
203 |
< |
Thread.sleep(SHORT_DELAY_MS); |
204 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
205 |
< |
} finally { |
206 |
< |
joinPool(p); |
203 |
> |
long startTime = System.nanoTime(); |
204 |
> |
while (p.getCompletedTaskCount() != 1) { |
205 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
206 |
> |
fail("timed out"); |
207 |
> |
Thread.yield(); |
208 |
> |
} |
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 |
|
|
188 |
– |
|
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 |
|
|
218 |
– |
|
269 |
|
/** |
270 |
|
* setThreadFactory(null) throws NPE |
271 |
|
*/ |
274 |
|
new ThreadPoolExecutor(1, 2, |
275 |
|
LONG_DELAY_MS, MILLISECONDS, |
276 |
|
new ArrayBlockingQueue<Runnable>(10)); |
277 |
< |
try { |
278 |
< |
p.setThreadFactory(null); |
279 |
< |
shouldThrow(); |
280 |
< |
} catch (NullPointerException success) { |
281 |
< |
} finally { |
232 |
< |
joinPool(p); |
277 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
278 |
> |
try { |
279 |
> |
p.setThreadFactory(null); |
280 |
> |
shouldThrow(); |
281 |
> |
} catch (NullPointerException success) {} |
282 |
|
} |
283 |
|
} |
284 |
|
|
286 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
287 |
|
*/ |
288 |
|
public void testGetRejectedExecutionHandler() { |
289 |
< |
final RejectedExecutionHandler h = new NoOpREHandler(); |
289 |
> |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
290 |
|
final ThreadPoolExecutor p = |
291 |
|
new ThreadPoolExecutor(1, 2, |
292 |
|
LONG_DELAY_MS, MILLISECONDS, |
293 |
|
new ArrayBlockingQueue<Runnable>(10), |
294 |
< |
h); |
295 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
296 |
< |
joinPool(p); |
294 |
> |
handler); |
295 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
296 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
297 |
> |
} |
298 |
|
} |
299 |
|
|
300 |
|
/** |
306 |
|
new ThreadPoolExecutor(1, 2, |
307 |
|
LONG_DELAY_MS, MILLISECONDS, |
308 |
|
new ArrayBlockingQueue<Runnable>(10)); |
309 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
310 |
< |
p.setRejectedExecutionHandler(h); |
311 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
312 |
< |
joinPool(p); |
309 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
310 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
311 |
> |
p.setRejectedExecutionHandler(handler); |
312 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
313 |
> |
} |
314 |
|
} |
315 |
|
|
265 |
– |
|
316 |
|
/** |
317 |
|
* setRejectedExecutionHandler(null) throws NPE |
318 |
|
*/ |
321 |
|
new ThreadPoolExecutor(1, 2, |
322 |
|
LONG_DELAY_MS, MILLISECONDS, |
323 |
|
new ArrayBlockingQueue<Runnable>(10)); |
324 |
< |
try { |
325 |
< |
p.setRejectedExecutionHandler(null); |
326 |
< |
shouldThrow(); |
327 |
< |
} catch (NullPointerException success) { |
328 |
< |
} finally { |
279 |
< |
joinPool(p); |
324 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
325 |
> |
try { |
326 |
> |
p.setRejectedExecutionHandler(null); |
327 |
> |
shouldThrow(); |
328 |
> |
} catch (NullPointerException success) {} |
329 |
|
} |
330 |
|
} |
331 |
|
|
283 |
– |
|
332 |
|
/** |
333 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
334 |
|
* multiple threads active |
339 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
340 |
|
LONG_DELAY_MS, MILLISECONDS, |
341 |
|
new ArrayBlockingQueue<Runnable>(10)); |
342 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
343 |
< |
final CountDownLatch done = new CountDownLatch(1); |
344 |
< |
try { |
342 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
343 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
344 |
> |
final CountDownLatch done = new CountDownLatch(1); |
345 |
|
assertEquals(0, p.getLargestPoolSize()); |
346 |
|
for (int i = 0; i < THREADS; i++) |
347 |
|
p.execute(new CheckedRunnable() { |
350 |
|
done.await(); |
351 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
352 |
|
}}); |
353 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
306 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
307 |
< |
} finally { |
308 |
< |
done.countDown(); |
309 |
< |
joinPool(p); |
353 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
354 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
355 |
+ |
done.countDown(); // release pool |
356 |
|
} |
357 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
358 |
|
} |
359 |
|
|
360 |
|
/** |
366 |
|
new ThreadPoolExecutor(2, 3, |
367 |
|
LONG_DELAY_MS, MILLISECONDS, |
368 |
|
new ArrayBlockingQueue<Runnable>(10)); |
369 |
< |
assertEquals(3, p.getMaximumPoolSize()); |
370 |
< |
joinPool(p); |
369 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
370 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
371 |
> |
p.setMaximumPoolSize(5); |
372 |
> |
assertEquals(5, p.getMaximumPoolSize()); |
373 |
> |
p.setMaximumPoolSize(4); |
374 |
> |
assertEquals(4, p.getMaximumPoolSize()); |
375 |
> |
} |
376 |
|
} |
377 |
|
|
378 |
|
/** |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* isShutDown is false before shutdown, true after |
432 |
> |
* isShutdown is false before shutdown, true after |
433 |
|
*/ |
434 |
|
public void testIsShutdown() { |
435 |
|
final ThreadPoolExecutor p = |
442 |
|
joinPool(p); |
443 |
|
} |
444 |
|
|
445 |
+ |
/** |
446 |
+ |
* awaitTermination on a non-shutdown pool times out |
447 |
+ |
*/ |
448 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
449 |
+ |
final ThreadPoolExecutor p = |
450 |
+ |
new ThreadPoolExecutor(1, 1, |
451 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
452 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
453 |
+ |
assertFalse(p.isTerminated()); |
454 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
455 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
456 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
457 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
458 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
459 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
460 |
+ |
long timeoutNanos = 999999L; |
461 |
+ |
long startTime = System.nanoTime(); |
462 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
463 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
464 |
+ |
assertFalse(p.isTerminated()); |
465 |
+ |
startTime = System.nanoTime(); |
466 |
+ |
long timeoutMillis = timeoutMillis(); |
467 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
468 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
469 |
+ |
assertFalse(p.isTerminated()); |
470 |
+ |
p.shutdown(); |
471 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
472 |
+ |
assertTrue(p.isTerminated()); |
473 |
+ |
} |
474 |
|
|
475 |
|
/** |
476 |
|
* isTerminated is false before termination, true after |
486 |
|
try { |
487 |
|
p.execute(new CheckedRunnable() { |
488 |
|
public void realRun() throws InterruptedException { |
409 |
– |
threadStarted.countDown(); |
489 |
|
assertFalse(p.isTerminated()); |
490 |
+ |
threadStarted.countDown(); |
491 |
|
done.await(); |
492 |
|
}}); |
493 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
494 |
+ |
assertFalse(p.isTerminating()); |
495 |
|
done.countDown(); |
496 |
|
} finally { |
497 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
514 |
|
assertFalse(p.isTerminating()); |
515 |
|
p.execute(new CheckedRunnable() { |
516 |
|
public void realRun() throws InterruptedException { |
436 |
– |
threadStarted.countDown(); |
517 |
|
assertFalse(p.isTerminating()); |
518 |
+ |
threadStarted.countDown(); |
519 |
|
done.await(); |
520 |
|
}}); |
521 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
644 |
|
} |
645 |
|
|
646 |
|
/** |
647 |
< |
* shutDownNow returns a list containing tasks that were not run |
647 |
> |
* shutdownNow returns a list containing tasks that were not run, |
648 |
> |
* and those tasks are drained from the queue |
649 |
|
*/ |
650 |
< |
public void testShutDownNow() { |
650 |
> |
public void testShutdownNow() throws InterruptedException { |
651 |
> |
final int poolSize = 2; |
652 |
> |
final int count = 5; |
653 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
654 |
|
final ThreadPoolExecutor p = |
655 |
< |
new ThreadPoolExecutor(1, 1, |
655 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
656 |
|
LONG_DELAY_MS, MILLISECONDS, |
657 |
|
new ArrayBlockingQueue<Runnable>(10)); |
658 |
< |
List l; |
659 |
< |
try { |
660 |
< |
for (int i = 0; i < 5; i++) |
576 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
577 |
< |
} |
578 |
< |
finally { |
658 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
659 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
660 |
> |
threadsStarted.countDown(); |
661 |
|
try { |
662 |
< |
l = p.shutdownNow(); |
663 |
< |
} catch (SecurityException ok) { return; } |
662 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
663 |
> |
} catch (InterruptedException success) {} |
664 |
> |
ran.getAndIncrement(); |
665 |
> |
}}; |
666 |
> |
for (int i = 0; i < count; i++) |
667 |
> |
p.execute(waiter); |
668 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
669 |
> |
assertEquals(poolSize, p.getActiveCount()); |
670 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
671 |
> |
final List<Runnable> queuedTasks; |
672 |
> |
try { |
673 |
> |
queuedTasks = p.shutdownNow(); |
674 |
> |
} catch (SecurityException ok) { |
675 |
> |
return; // Allowed in case test doesn't have privs |
676 |
|
} |
677 |
|
assertTrue(p.isShutdown()); |
678 |
< |
assertTrue(l.size() <= 4); |
678 |
> |
assertTrue(p.getQueue().isEmpty()); |
679 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
680 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
681 |
> |
assertTrue(p.isTerminated()); |
682 |
> |
assertEquals(poolSize, ran.get()); |
683 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
684 |
|
} |
685 |
|
|
686 |
|
// Exception Tests |
687 |
|
|
589 |
– |
|
688 |
|
/** |
689 |
|
* Constructor throws if corePoolSize argument is less than zero |
690 |
|
*/ |
691 |
|
public void testConstructor1() { |
692 |
|
try { |
693 |
< |
new ThreadPoolExecutor(-1, 1, |
596 |
< |
LONG_DELAY_MS, MILLISECONDS, |
693 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
694 |
|
new ArrayBlockingQueue<Runnable>(10)); |
695 |
|
shouldThrow(); |
696 |
|
} catch (IllegalArgumentException success) {} |
701 |
|
*/ |
702 |
|
public void testConstructor2() { |
703 |
|
try { |
704 |
< |
new ThreadPoolExecutor(1, -1, |
608 |
< |
LONG_DELAY_MS, MILLISECONDS, |
704 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
705 |
|
new ArrayBlockingQueue<Runnable>(10)); |
706 |
|
shouldThrow(); |
707 |
|
} catch (IllegalArgumentException success) {} |
712 |
|
*/ |
713 |
|
public void testConstructor3() { |
714 |
|
try { |
715 |
< |
new ThreadPoolExecutor(1, 0, |
620 |
< |
LONG_DELAY_MS, MILLISECONDS, |
715 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
716 |
|
new ArrayBlockingQueue<Runnable>(10)); |
717 |
|
shouldThrow(); |
718 |
|
} catch (IllegalArgumentException success) {} |
723 |
|
*/ |
724 |
|
public void testConstructor4() { |
725 |
|
try { |
726 |
< |
new ThreadPoolExecutor(1, 2, |
632 |
< |
-1L, MILLISECONDS, |
726 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
727 |
|
new ArrayBlockingQueue<Runnable>(10)); |
728 |
|
shouldThrow(); |
729 |
|
} catch (IllegalArgumentException success) {} |
734 |
|
*/ |
735 |
|
public void testConstructor5() { |
736 |
|
try { |
737 |
< |
new ThreadPoolExecutor(2, 1, |
644 |
< |
LONG_DELAY_MS, MILLISECONDS, |
737 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
738 |
|
new ArrayBlockingQueue<Runnable>(10)); |
739 |
|
shouldThrow(); |
740 |
|
} catch (IllegalArgumentException success) {} |
745 |
|
*/ |
746 |
|
public void testConstructorNullPointerException() { |
747 |
|
try { |
748 |
< |
new ThreadPoolExecutor(1, 2, |
656 |
< |
LONG_DELAY_MS, MILLISECONDS, |
748 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
749 |
|
(BlockingQueue) null); |
750 |
|
shouldThrow(); |
751 |
|
} catch (NullPointerException success) {} |
752 |
|
} |
753 |
|
|
662 |
– |
|
663 |
– |
|
754 |
|
/** |
755 |
|
* Constructor throws if corePoolSize argument is less than zero |
756 |
|
*/ |
757 |
|
public void testConstructor6() { |
758 |
|
try { |
759 |
< |
new ThreadPoolExecutor(-1, 1, |
670 |
< |
LONG_DELAY_MS, MILLISECONDS, |
759 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
760 |
|
new ArrayBlockingQueue<Runnable>(10), |
761 |
|
new SimpleThreadFactory()); |
762 |
|
shouldThrow(); |
768 |
|
*/ |
769 |
|
public void testConstructor7() { |
770 |
|
try { |
771 |
< |
new ThreadPoolExecutor(1, -1, |
683 |
< |
LONG_DELAY_MS, MILLISECONDS, |
771 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
772 |
|
new ArrayBlockingQueue<Runnable>(10), |
773 |
|
new SimpleThreadFactory()); |
774 |
|
shouldThrow(); |
780 |
|
*/ |
781 |
|
public void testConstructor8() { |
782 |
|
try { |
783 |
< |
new ThreadPoolExecutor(1, 0, |
696 |
< |
LONG_DELAY_MS, MILLISECONDS, |
783 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
784 |
|
new ArrayBlockingQueue<Runnable>(10), |
785 |
|
new SimpleThreadFactory()); |
786 |
|
shouldThrow(); |
792 |
|
*/ |
793 |
|
public void testConstructor9() { |
794 |
|
try { |
795 |
< |
new ThreadPoolExecutor(1, 2, |
709 |
< |
-1L, MILLISECONDS, |
795 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
796 |
|
new ArrayBlockingQueue<Runnable>(10), |
797 |
|
new SimpleThreadFactory()); |
798 |
|
shouldThrow(); |
804 |
|
*/ |
805 |
|
public void testConstructor10() { |
806 |
|
try { |
807 |
< |
new ThreadPoolExecutor(2, 1, |
722 |
< |
LONG_DELAY_MS, MILLISECONDS, |
807 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
808 |
|
new ArrayBlockingQueue<Runnable>(10), |
809 |
|
new SimpleThreadFactory()); |
810 |
|
shouldThrow(); |
816 |
|
*/ |
817 |
|
public void testConstructorNullPointerException2() { |
818 |
|
try { |
819 |
< |
new ThreadPoolExecutor(1, 2, |
735 |
< |
LONG_DELAY_MS, MILLISECONDS, |
819 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
820 |
|
(BlockingQueue) null, |
821 |
|
new SimpleThreadFactory()); |
822 |
|
shouldThrow(); |
828 |
|
*/ |
829 |
|
public void testConstructorNullPointerException3() { |
830 |
|
try { |
831 |
< |
new ThreadPoolExecutor(1, 2, |
748 |
< |
LONG_DELAY_MS, MILLISECONDS, |
831 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
832 |
|
new ArrayBlockingQueue<Runnable>(10), |
833 |
|
(ThreadFactory) null); |
834 |
|
shouldThrow(); |
835 |
|
} catch (NullPointerException success) {} |
836 |
|
} |
837 |
|
|
755 |
– |
|
838 |
|
/** |
839 |
|
* Constructor throws if corePoolSize argument is less than zero |
840 |
|
*/ |
841 |
|
public void testConstructor11() { |
842 |
|
try { |
843 |
< |
new ThreadPoolExecutor(-1, 1, |
762 |
< |
LONG_DELAY_MS, MILLISECONDS, |
843 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
844 |
|
new ArrayBlockingQueue<Runnable>(10), |
845 |
|
new NoOpREHandler()); |
846 |
|
shouldThrow(); |
852 |
|
*/ |
853 |
|
public void testConstructor12() { |
854 |
|
try { |
855 |
< |
new ThreadPoolExecutor(1, -1, |
775 |
< |
LONG_DELAY_MS, MILLISECONDS, |
855 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
856 |
|
new ArrayBlockingQueue<Runnable>(10), |
857 |
|
new NoOpREHandler()); |
858 |
|
shouldThrow(); |
864 |
|
*/ |
865 |
|
public void testConstructor13() { |
866 |
|
try { |
867 |
< |
new ThreadPoolExecutor(1, 0, |
788 |
< |
LONG_DELAY_MS, MILLISECONDS, |
867 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
868 |
|
new ArrayBlockingQueue<Runnable>(10), |
869 |
|
new NoOpREHandler()); |
870 |
|
shouldThrow(); |
876 |
|
*/ |
877 |
|
public void testConstructor14() { |
878 |
|
try { |
879 |
< |
new ThreadPoolExecutor(1, 2, |
801 |
< |
-1L, MILLISECONDS, |
879 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
880 |
|
new ArrayBlockingQueue<Runnable>(10), |
881 |
|
new NoOpREHandler()); |
882 |
|
shouldThrow(); |
888 |
|
*/ |
889 |
|
public void testConstructor15() { |
890 |
|
try { |
891 |
< |
new ThreadPoolExecutor(2, 1, |
814 |
< |
LONG_DELAY_MS, MILLISECONDS, |
891 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
892 |
|
new ArrayBlockingQueue<Runnable>(10), |
893 |
|
new NoOpREHandler()); |
894 |
|
shouldThrow(); |
900 |
|
*/ |
901 |
|
public void testConstructorNullPointerException4() { |
902 |
|
try { |
903 |
< |
new ThreadPoolExecutor(1, 2, |
827 |
< |
LONG_DELAY_MS, MILLISECONDS, |
903 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
904 |
|
(BlockingQueue) null, |
905 |
|
new NoOpREHandler()); |
906 |
|
shouldThrow(); |
912 |
|
*/ |
913 |
|
public void testConstructorNullPointerException5() { |
914 |
|
try { |
915 |
< |
new ThreadPoolExecutor(1, 2, |
840 |
< |
LONG_DELAY_MS, MILLISECONDS, |
915 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
916 |
|
new ArrayBlockingQueue<Runnable>(10), |
917 |
|
(RejectedExecutionHandler) null); |
918 |
|
shouldThrow(); |
919 |
|
} catch (NullPointerException success) {} |
920 |
|
} |
921 |
|
|
847 |
– |
|
922 |
|
/** |
923 |
|
* Constructor throws if corePoolSize argument is less than zero |
924 |
|
*/ |
925 |
|
public void testConstructor16() { |
926 |
|
try { |
927 |
< |
new ThreadPoolExecutor(-1, 1, |
854 |
< |
LONG_DELAY_MS, MILLISECONDS, |
927 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
928 |
|
new ArrayBlockingQueue<Runnable>(10), |
929 |
|
new SimpleThreadFactory(), |
930 |
|
new NoOpREHandler()); |
937 |
|
*/ |
938 |
|
public void testConstructor17() { |
939 |
|
try { |
940 |
< |
new ThreadPoolExecutor(1, -1, |
868 |
< |
LONG_DELAY_MS, MILLISECONDS, |
940 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
941 |
|
new ArrayBlockingQueue<Runnable>(10), |
942 |
|
new SimpleThreadFactory(), |
943 |
|
new NoOpREHandler()); |
950 |
|
*/ |
951 |
|
public void testConstructor18() { |
952 |
|
try { |
953 |
< |
new ThreadPoolExecutor(1, 0, |
882 |
< |
LONG_DELAY_MS, MILLISECONDS, |
953 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
954 |
|
new ArrayBlockingQueue<Runnable>(10), |
955 |
|
new SimpleThreadFactory(), |
956 |
|
new NoOpREHandler()); |
963 |
|
*/ |
964 |
|
public void testConstructor19() { |
965 |
|
try { |
966 |
< |
new ThreadPoolExecutor(1, 2, |
896 |
< |
-1L, MILLISECONDS, |
966 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
967 |
|
new ArrayBlockingQueue<Runnable>(10), |
968 |
|
new SimpleThreadFactory(), |
969 |
|
new NoOpREHandler()); |
976 |
|
*/ |
977 |
|
public void testConstructor20() { |
978 |
|
try { |
979 |
< |
new ThreadPoolExecutor(2, 1, |
910 |
< |
LONG_DELAY_MS, MILLISECONDS, |
979 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
980 |
|
new ArrayBlockingQueue<Runnable>(10), |
981 |
|
new SimpleThreadFactory(), |
982 |
|
new NoOpREHandler()); |
989 |
|
*/ |
990 |
|
public void testConstructorNullPointerException6() { |
991 |
|
try { |
992 |
< |
new ThreadPoolExecutor(1, 2, |
924 |
< |
LONG_DELAY_MS, MILLISECONDS, |
992 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
993 |
|
(BlockingQueue) null, |
994 |
|
new SimpleThreadFactory(), |
995 |
|
new NoOpREHandler()); |
1002 |
|
*/ |
1003 |
|
public void testConstructorNullPointerException7() { |
1004 |
|
try { |
1005 |
< |
new ThreadPoolExecutor(1, 2, |
938 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1005 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1006 |
|
new ArrayBlockingQueue<Runnable>(10), |
1007 |
|
new SimpleThreadFactory(), |
1008 |
|
(RejectedExecutionHandler) null); |
1015 |
|
*/ |
1016 |
|
public void testConstructorNullPointerException8() { |
1017 |
|
try { |
1018 |
< |
new ThreadPoolExecutor(1, 2, |
952 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1018 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1019 |
|
new ArrayBlockingQueue<Runnable>(10), |
1020 |
|
(ThreadFactory) null, |
1021 |
|
new NoOpREHandler()); |
1029 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1030 |
|
final ThreadPoolExecutor p = |
1031 |
|
new ThreadPoolExecutor(1, 1, |
1032 |
< |
60, TimeUnit.SECONDS, |
1032 |
> |
60, SECONDS, |
1033 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1034 |
|
|
1035 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1277 |
|
} |
1278 |
|
} |
1279 |
|
|
1214 |
– |
|
1280 |
|
/** |
1281 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1282 |
|
*/ |
1298 |
|
} |
1299 |
|
} |
1300 |
|
|
1236 |
– |
|
1301 |
|
/** |
1302 |
|
* execute(null) throws NPE |
1303 |
|
*/ |
1304 |
|
public void testExecuteNull() { |
1305 |
|
ThreadPoolExecutor p = |
1306 |
< |
new ThreadPoolExecutor(1, 2, |
1243 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1306 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1307 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1308 |
|
try { |
1309 |
|
p.execute(null); |
1369 |
|
joinPool(p); |
1370 |
|
} |
1371 |
|
|
1372 |
+ |
/** |
1373 |
+ |
* Configuration changes that allow core pool size greater than |
1374 |
+ |
* max pool size result in IllegalArgumentException. |
1375 |
+ |
*/ |
1376 |
+ |
public void testPoolSizeInvariants() { |
1377 |
+ |
ThreadPoolExecutor p = |
1378 |
+ |
new ThreadPoolExecutor(1, 1, |
1379 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1380 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1381 |
+ |
for (int s = 1; s < 5; s++) { |
1382 |
+ |
p.setMaximumPoolSize(s); |
1383 |
+ |
p.setCorePoolSize(s); |
1384 |
+ |
try { |
1385 |
+ |
p.setMaximumPoolSize(s - 1); |
1386 |
+ |
shouldThrow(); |
1387 |
+ |
} catch (IllegalArgumentException success) {} |
1388 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1389 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1390 |
+ |
try { |
1391 |
+ |
p.setCorePoolSize(s + 1); |
1392 |
+ |
shouldThrow(); |
1393 |
+ |
} catch (IllegalArgumentException success) {} |
1394 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1395 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1396 |
+ |
} |
1397 |
+ |
joinPool(p); |
1398 |
+ |
} |
1399 |
|
|
1400 |
|
/** |
1401 |
|
* setKeepAliveTime throws IllegalArgumentException |
1422 |
|
public void testTerminated() { |
1423 |
|
ExtendedTPE p = new ExtendedTPE(); |
1424 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1425 |
< |
assertTrue(p.terminatedCalled); |
1425 |
> |
assertTrue(p.terminatedCalled()); |
1426 |
|
joinPool(p); |
1427 |
|
} |
1428 |
|
|
1432 |
|
public void testBeforeAfter() throws InterruptedException { |
1433 |
|
ExtendedTPE p = new ExtendedTPE(); |
1434 |
|
try { |
1435 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1436 |
< |
p.execute(r); |
1437 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1438 |
< |
assertTrue(r.done); |
1439 |
< |
assertTrue(p.beforeCalled); |
1440 |
< |
assertTrue(p.afterCalled); |
1435 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1436 |
> |
p.execute(new CheckedRunnable() { |
1437 |
> |
public void realRun() { |
1438 |
> |
done.countDown(); |
1439 |
> |
}}); |
1440 |
> |
await(p.afterCalled); |
1441 |
> |
assertEquals(0, done.getCount()); |
1442 |
> |
assertTrue(p.afterCalled()); |
1443 |
> |
assertTrue(p.beforeCalled()); |
1444 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1445 |
|
} finally { |
1446 |
|
joinPool(p); |
1498 |
|
} |
1499 |
|
} |
1500 |
|
|
1408 |
– |
|
1501 |
|
/** |
1502 |
|
* invokeAny(null) throws NPE |
1503 |
|
*/ |
1691 |
|
} |
1692 |
|
} |
1693 |
|
|
1602 |
– |
|
1603 |
– |
|
1694 |
|
/** |
1695 |
|
* timed invokeAny(null) throws NPE |
1696 |
|
*/ |
1913 |
|
l.add(new StringTask()); |
1914 |
|
l.add(new StringTask()); |
1915 |
|
List<Future<String>> futures = |
1916 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1916 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1917 |
|
assertEquals(2, futures.size()); |
1918 |
|
for (Future<String> future : futures) |
1919 |
|
assertSame(TEST_STRING, future.get()); |
1931 |
|
LONG_DELAY_MS, MILLISECONDS, |
1932 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1933 |
|
try { |
1934 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1935 |
< |
l.add(new StringTask()); |
1936 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1937 |
< |
l.add(new StringTask()); |
1938 |
< |
List<Future<String>> futures = |
1939 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1940 |
< |
assertEquals(3, futures.size()); |
1941 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1942 |
< |
Future<String> f1 = it.next(); |
1943 |
< |
Future<String> f2 = it.next(); |
1944 |
< |
Future<String> f3 = it.next(); |
1945 |
< |
assertTrue(f1.isDone()); |
1946 |
< |
assertTrue(f2.isDone()); |
1947 |
< |
assertTrue(f3.isDone()); |
1948 |
< |
assertFalse(f1.isCancelled()); |
1949 |
< |
assertTrue(f2.isCancelled()); |
1934 |
> |
for (long timeout = timeoutMillis();;) { |
1935 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1936 |
> |
tasks.add(new StringTask("0")); |
1937 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1938 |
> |
tasks.add(new StringTask("2")); |
1939 |
> |
long startTime = System.nanoTime(); |
1940 |
> |
List<Future<String>> futures = |
1941 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1942 |
> |
assertEquals(tasks.size(), futures.size()); |
1943 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1944 |
> |
for (Future future : futures) |
1945 |
> |
assertTrue(future.isDone()); |
1946 |
> |
assertTrue(futures.get(1).isCancelled()); |
1947 |
> |
try { |
1948 |
> |
assertEquals("0", futures.get(0).get()); |
1949 |
> |
assertEquals("2", futures.get(2).get()); |
1950 |
> |
break; |
1951 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1952 |
> |
timeout *= 2; |
1953 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1954 |
> |
fail("expected exactly one task to be cancelled"); |
1955 |
> |
} |
1956 |
> |
} |
1957 |
|
} finally { |
1958 |
|
joinPool(e); |
1959 |
|
} |
1999 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
2000 |
|
*/ |
2001 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
2002 |
+ |
long keepAliveTime = timeoutMillis(); |
2003 |
|
final ThreadPoolExecutor p = |
2004 |
|
new ThreadPoolExecutor(2, 10, |
2005 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
2005 |
> |
keepAliveTime, MILLISECONDS, |
2006 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2007 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2008 |
|
try { |
2009 |
|
p.allowCoreThreadTimeOut(true); |
2010 |
|
p.execute(new CheckedRunnable() { |
2011 |
< |
public void realRun() throws InterruptedException { |
2011 |
> |
public void realRun() { |
2012 |
|
threadStarted.countDown(); |
2013 |
|
assertEquals(1, p.getPoolSize()); |
2014 |
|
}}); |
2015 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
2016 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
2017 |
< |
if (p.getPoolSize() == 0) |
2018 |
< |
break; |
2019 |
< |
Thread.sleep(10); |
2020 |
< |
} |
2015 |
> |
await(threadStarted); |
2016 |
> |
delay(keepAliveTime); |
2017 |
> |
long startTime = System.nanoTime(); |
2018 |
> |
while (p.getPoolSize() > 0 |
2019 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
2020 |
> |
Thread.yield(); |
2021 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
2022 |
|
assertEquals(0, p.getPoolSize()); |
2023 |
|
} finally { |
2024 |
|
joinPool(p); |
2029 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
2030 |
|
*/ |
2031 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
2032 |
+ |
long keepAliveTime = timeoutMillis(); |
2033 |
|
final ThreadPoolExecutor p = |
2034 |
|
new ThreadPoolExecutor(2, 10, |
2035 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
2035 |
> |
keepAliveTime, MILLISECONDS, |
2036 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2037 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2038 |
|
try { |
2042 |
|
threadStarted.countDown(); |
2043 |
|
assertTrue(p.getPoolSize() >= 1); |
2044 |
|
}}); |
2045 |
< |
Thread.sleep(SMALL_DELAY_MS); |
2045 |
> |
delay(2 * keepAliveTime); |
2046 |
|
assertTrue(p.getPoolSize() >= 1); |
2047 |
|
} finally { |
2048 |
|
joinPool(p); |
2061 |
|
done.countDown(); |
2062 |
|
}}; |
2063 |
|
final ThreadPoolExecutor p = |
2064 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2064 |
> |
new ThreadPoolExecutor(1, 30, |
2065 |
> |
60, SECONDS, |
2066 |
|
new ArrayBlockingQueue(30)); |
2067 |
|
try { |
2068 |
|
for (int i = 0; i < nTasks; ++i) { |
2077 |
|
// enough time to run all tasks |
2078 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2079 |
|
} finally { |
2080 |
< |
p.shutdown(); |
2080 |
> |
joinPool(p); |
2081 |
> |
} |
2082 |
> |
} |
2083 |
> |
|
2084 |
> |
/** |
2085 |
> |
* get(cancelled task) throws CancellationException |
2086 |
> |
*/ |
2087 |
> |
public void testGet_cancelled() throws Exception { |
2088 |
> |
final ExecutorService e = |
2089 |
> |
new ThreadPoolExecutor(1, 1, |
2090 |
> |
LONG_DELAY_MS, MILLISECONDS, |
2091 |
> |
new LinkedBlockingQueue<Runnable>()); |
2092 |
> |
try { |
2093 |
> |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2094 |
> |
final CountDownLatch done = new CountDownLatch(1); |
2095 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
2096 |
> |
for (int i = 0; i < 2; i++) { |
2097 |
> |
Runnable r = new CheckedRunnable() { public void realRun() |
2098 |
> |
throws Throwable { |
2099 |
> |
blockerStarted.countDown(); |
2100 |
> |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2101 |
> |
}}; |
2102 |
> |
futures.add(e.submit(r)); |
2103 |
> |
} |
2104 |
> |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2105 |
> |
for (Future<?> future : futures) future.cancel(false); |
2106 |
> |
for (Future<?> future : futures) { |
2107 |
> |
try { |
2108 |
> |
future.get(); |
2109 |
> |
shouldThrow(); |
2110 |
> |
} catch (CancellationException success) {} |
2111 |
> |
try { |
2112 |
> |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2113 |
> |
shouldThrow(); |
2114 |
> |
} catch (CancellationException success) {} |
2115 |
> |
assertTrue(future.isCancelled()); |
2116 |
> |
assertTrue(future.isDone()); |
2117 |
> |
} |
2118 |
> |
done.countDown(); |
2119 |
> |
} finally { |
2120 |
> |
joinPool(e); |
2121 |
|
} |
2122 |
|
} |
2123 |
|
|