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); |
119 |
|
assertEquals(1, p.getActiveCount()); |
120 |
|
done.await(); |
121 |
|
}}); |
122 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
122 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
123 |
|
assertEquals(1, p.getActiveCount()); |
124 |
|
} finally { |
125 |
|
done.countDown(); |
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 |
|
} |
173 |
– |
} finally { |
174 |
– |
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 |
|
/** |
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 { |
244 |
< |
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 |
|
|
316 |
|
/** |
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 { |
290 |
< |
joinPool(p); |
324 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
325 |
> |
try { |
326 |
> |
p.setRejectedExecutionHandler(null); |
327 |
> |
shouldThrow(); |
328 |
> |
} catch (NullPointerException success) {} |
329 |
|
} |
330 |
|
} |
331 |
|
|
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)); |
316 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
317 |
< |
} finally { |
318 |
< |
done.countDown(); |
319 |
< |
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 |
|
/** |
384 |
|
new ThreadPoolExecutor(1, 1, |
385 |
|
LONG_DELAY_MS, MILLISECONDS, |
386 |
|
new ArrayBlockingQueue<Runnable>(10)); |
387 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
388 |
< |
final CountDownLatch done = new CountDownLatch(1); |
389 |
< |
try { |
387 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
388 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
389 |
> |
final CountDownLatch done = new CountDownLatch(1); |
390 |
|
assertEquals(0, p.getPoolSize()); |
391 |
|
p.execute(new CheckedRunnable() { |
392 |
|
public void realRun() throws InterruptedException { |
394 |
|
assertEquals(1, p.getPoolSize()); |
395 |
|
done.await(); |
396 |
|
}}); |
397 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
397 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
398 |
|
assertEquals(1, p.getPoolSize()); |
399 |
< |
} finally { |
359 |
< |
done.countDown(); |
360 |
< |
joinPool(p); |
399 |
> |
done.countDown(); // release pool |
400 |
|
} |
401 |
|
} |
402 |
|
|
418 |
|
assertEquals(1, p.getTaskCount()); |
419 |
|
done.await(); |
420 |
|
}}); |
421 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
421 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
422 |
|
assertEquals(1, p.getTaskCount()); |
423 |
|
} finally { |
424 |
|
done.countDown(); |
441 |
|
} |
442 |
|
|
443 |
|
/** |
444 |
+ |
* awaitTermination on a non-shutdown pool times out |
445 |
+ |
*/ |
446 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
447 |
+ |
final ThreadPoolExecutor p = |
448 |
+ |
new ThreadPoolExecutor(1, 1, |
449 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
450 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
451 |
+ |
assertFalse(p.isTerminated()); |
452 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
453 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
454 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
455 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
456 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
457 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
458 |
+ |
long timeoutNanos = 999999L; |
459 |
+ |
long startTime = System.nanoTime(); |
460 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
461 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
462 |
+ |
assertFalse(p.isTerminated()); |
463 |
+ |
startTime = System.nanoTime(); |
464 |
+ |
long timeoutMillis = timeoutMillis(); |
465 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
466 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
467 |
+ |
assertFalse(p.isTerminated()); |
468 |
+ |
p.shutdown(); |
469 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
470 |
+ |
assertTrue(p.isTerminated()); |
471 |
+ |
} |
472 |
+ |
|
473 |
+ |
/** |
474 |
|
* isTerminated is false before termination, true after |
475 |
|
*/ |
476 |
|
public void testIsTerminated() throws InterruptedException { |
488 |
|
threadStarted.countDown(); |
489 |
|
done.await(); |
490 |
|
}}); |
491 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
491 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
492 |
|
assertFalse(p.isTerminating()); |
493 |
|
done.countDown(); |
494 |
|
} finally { |
516 |
|
threadStarted.countDown(); |
517 |
|
done.await(); |
518 |
|
}}); |
519 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
519 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
520 |
|
assertFalse(p.isTerminating()); |
521 |
|
done.countDown(); |
522 |
|
} finally { |
551 |
|
tasks[i] = new FutureTask(task); |
552 |
|
p.execute(tasks[i]); |
553 |
|
} |
554 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
554 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
555 |
|
assertSame(q, p.getQueue()); |
556 |
|
assertFalse(q.contains(tasks[0])); |
557 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
583 |
|
}}; |
584 |
|
p.execute(tasks[i]); |
585 |
|
} |
586 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
586 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
587 |
|
assertFalse(p.remove(tasks[0])); |
588 |
|
assertTrue(q.contains(tasks[4])); |
589 |
|
assertTrue(q.contains(tasks[3])); |
622 |
|
tasks[i] = new FutureTask(task); |
623 |
|
p.execute(tasks[i]); |
624 |
|
} |
625 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
625 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
626 |
|
assertEquals(tasks.length, p.getTaskCount()); |
627 |
|
assertEquals(tasks.length - 1, q.size()); |
628 |
|
assertEquals(1L, p.getActiveCount()); |
642 |
|
} |
643 |
|
|
644 |
|
/** |
645 |
< |
* shutdownNow returns a list containing tasks that were not run |
645 |
> |
* shutdownNow returns a list containing tasks that were not run, |
646 |
> |
* and those tasks are drained from the queue |
647 |
|
*/ |
648 |
< |
public void testShutdownNow() { |
648 |
> |
public void testShutdownNow() throws InterruptedException { |
649 |
> |
final int poolSize = 2; |
650 |
> |
final int count = 5; |
651 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
652 |
|
final ThreadPoolExecutor p = |
653 |
< |
new ThreadPoolExecutor(1, 1, |
653 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
654 |
|
LONG_DELAY_MS, MILLISECONDS, |
655 |
|
new ArrayBlockingQueue<Runnable>(10)); |
656 |
< |
List l; |
657 |
< |
try { |
658 |
< |
for (int i = 0; i < 5; i++) |
586 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
587 |
< |
} |
588 |
< |
finally { |
656 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
657 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
658 |
> |
threadsStarted.countDown(); |
659 |
|
try { |
660 |
< |
l = p.shutdownNow(); |
661 |
< |
} catch (SecurityException ok) { return; } |
660 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
661 |
> |
} catch (InterruptedException success) {} |
662 |
> |
ran.getAndIncrement(); |
663 |
> |
}}; |
664 |
> |
for (int i = 0; i < count; i++) |
665 |
> |
p.execute(waiter); |
666 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
667 |
> |
assertEquals(poolSize, p.getActiveCount()); |
668 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
669 |
> |
final List<Runnable> queuedTasks; |
670 |
> |
try { |
671 |
> |
queuedTasks = p.shutdownNow(); |
672 |
> |
} catch (SecurityException ok) { |
673 |
> |
return; // Allowed in case test doesn't have privs |
674 |
|
} |
675 |
|
assertTrue(p.isShutdown()); |
676 |
< |
assertTrue(l.size() <= 4); |
676 |
> |
assertTrue(p.getQueue().isEmpty()); |
677 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
678 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
679 |
> |
assertTrue(p.isTerminated()); |
680 |
> |
assertEquals(poolSize, ran.get()); |
681 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
682 |
|
} |
683 |
|
|
684 |
|
// Exception Tests |
688 |
|
*/ |
689 |
|
public void testConstructor1() { |
690 |
|
try { |
691 |
< |
new ThreadPoolExecutor(-1, 1, |
605 |
< |
LONG_DELAY_MS, MILLISECONDS, |
691 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
692 |
|
new ArrayBlockingQueue<Runnable>(10)); |
693 |
|
shouldThrow(); |
694 |
|
} catch (IllegalArgumentException success) {} |
699 |
|
*/ |
700 |
|
public void testConstructor2() { |
701 |
|
try { |
702 |
< |
new ThreadPoolExecutor(1, -1, |
617 |
< |
LONG_DELAY_MS, MILLISECONDS, |
702 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
703 |
|
new ArrayBlockingQueue<Runnable>(10)); |
704 |
|
shouldThrow(); |
705 |
|
} catch (IllegalArgumentException success) {} |
710 |
|
*/ |
711 |
|
public void testConstructor3() { |
712 |
|
try { |
713 |
< |
new ThreadPoolExecutor(1, 0, |
629 |
< |
LONG_DELAY_MS, MILLISECONDS, |
713 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
714 |
|
new ArrayBlockingQueue<Runnable>(10)); |
715 |
|
shouldThrow(); |
716 |
|
} catch (IllegalArgumentException success) {} |
721 |
|
*/ |
722 |
|
public void testConstructor4() { |
723 |
|
try { |
724 |
< |
new ThreadPoolExecutor(1, 2, |
641 |
< |
-1L, MILLISECONDS, |
724 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
725 |
|
new ArrayBlockingQueue<Runnable>(10)); |
726 |
|
shouldThrow(); |
727 |
|
} catch (IllegalArgumentException success) {} |
732 |
|
*/ |
733 |
|
public void testConstructor5() { |
734 |
|
try { |
735 |
< |
new ThreadPoolExecutor(2, 1, |
653 |
< |
LONG_DELAY_MS, MILLISECONDS, |
735 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
736 |
|
new ArrayBlockingQueue<Runnable>(10)); |
737 |
|
shouldThrow(); |
738 |
|
} catch (IllegalArgumentException success) {} |
743 |
|
*/ |
744 |
|
public void testConstructorNullPointerException() { |
745 |
|
try { |
746 |
< |
new ThreadPoolExecutor(1, 2, |
665 |
< |
LONG_DELAY_MS, MILLISECONDS, |
746 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
747 |
|
(BlockingQueue) null); |
748 |
|
shouldThrow(); |
749 |
|
} catch (NullPointerException success) {} |
754 |
|
*/ |
755 |
|
public void testConstructor6() { |
756 |
|
try { |
757 |
< |
new ThreadPoolExecutor(-1, 1, |
677 |
< |
LONG_DELAY_MS, MILLISECONDS, |
757 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
758 |
|
new ArrayBlockingQueue<Runnable>(10), |
759 |
|
new SimpleThreadFactory()); |
760 |
|
shouldThrow(); |
766 |
|
*/ |
767 |
|
public void testConstructor7() { |
768 |
|
try { |
769 |
< |
new ThreadPoolExecutor(1, -1, |
690 |
< |
LONG_DELAY_MS, MILLISECONDS, |
769 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
770 |
|
new ArrayBlockingQueue<Runnable>(10), |
771 |
|
new SimpleThreadFactory()); |
772 |
|
shouldThrow(); |
778 |
|
*/ |
779 |
|
public void testConstructor8() { |
780 |
|
try { |
781 |
< |
new ThreadPoolExecutor(1, 0, |
703 |
< |
LONG_DELAY_MS, MILLISECONDS, |
781 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
782 |
|
new ArrayBlockingQueue<Runnable>(10), |
783 |
|
new SimpleThreadFactory()); |
784 |
|
shouldThrow(); |
790 |
|
*/ |
791 |
|
public void testConstructor9() { |
792 |
|
try { |
793 |
< |
new ThreadPoolExecutor(1, 2, |
716 |
< |
-1L, MILLISECONDS, |
793 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
794 |
|
new ArrayBlockingQueue<Runnable>(10), |
795 |
|
new SimpleThreadFactory()); |
796 |
|
shouldThrow(); |
802 |
|
*/ |
803 |
|
public void testConstructor10() { |
804 |
|
try { |
805 |
< |
new ThreadPoolExecutor(2, 1, |
729 |
< |
LONG_DELAY_MS, MILLISECONDS, |
805 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
806 |
|
new ArrayBlockingQueue<Runnable>(10), |
807 |
|
new SimpleThreadFactory()); |
808 |
|
shouldThrow(); |
814 |
|
*/ |
815 |
|
public void testConstructorNullPointerException2() { |
816 |
|
try { |
817 |
< |
new ThreadPoolExecutor(1, 2, |
742 |
< |
LONG_DELAY_MS, MILLISECONDS, |
817 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
818 |
|
(BlockingQueue) null, |
819 |
|
new SimpleThreadFactory()); |
820 |
|
shouldThrow(); |
826 |
|
*/ |
827 |
|
public void testConstructorNullPointerException3() { |
828 |
|
try { |
829 |
< |
new ThreadPoolExecutor(1, 2, |
755 |
< |
LONG_DELAY_MS, MILLISECONDS, |
829 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
830 |
|
new ArrayBlockingQueue<Runnable>(10), |
831 |
|
(ThreadFactory) null); |
832 |
|
shouldThrow(); |
838 |
|
*/ |
839 |
|
public void testConstructor11() { |
840 |
|
try { |
841 |
< |
new ThreadPoolExecutor(-1, 1, |
768 |
< |
LONG_DELAY_MS, MILLISECONDS, |
841 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
842 |
|
new ArrayBlockingQueue<Runnable>(10), |
843 |
|
new NoOpREHandler()); |
844 |
|
shouldThrow(); |
850 |
|
*/ |
851 |
|
public void testConstructor12() { |
852 |
|
try { |
853 |
< |
new ThreadPoolExecutor(1, -1, |
781 |
< |
LONG_DELAY_MS, MILLISECONDS, |
853 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
854 |
|
new ArrayBlockingQueue<Runnable>(10), |
855 |
|
new NoOpREHandler()); |
856 |
|
shouldThrow(); |
862 |
|
*/ |
863 |
|
public void testConstructor13() { |
864 |
|
try { |
865 |
< |
new ThreadPoolExecutor(1, 0, |
794 |
< |
LONG_DELAY_MS, MILLISECONDS, |
865 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
866 |
|
new ArrayBlockingQueue<Runnable>(10), |
867 |
|
new NoOpREHandler()); |
868 |
|
shouldThrow(); |
874 |
|
*/ |
875 |
|
public void testConstructor14() { |
876 |
|
try { |
877 |
< |
new ThreadPoolExecutor(1, 2, |
807 |
< |
-1L, MILLISECONDS, |
877 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
878 |
|
new ArrayBlockingQueue<Runnable>(10), |
879 |
|
new NoOpREHandler()); |
880 |
|
shouldThrow(); |
886 |
|
*/ |
887 |
|
public void testConstructor15() { |
888 |
|
try { |
889 |
< |
new ThreadPoolExecutor(2, 1, |
820 |
< |
LONG_DELAY_MS, MILLISECONDS, |
889 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
890 |
|
new ArrayBlockingQueue<Runnable>(10), |
891 |
|
new NoOpREHandler()); |
892 |
|
shouldThrow(); |
898 |
|
*/ |
899 |
|
public void testConstructorNullPointerException4() { |
900 |
|
try { |
901 |
< |
new ThreadPoolExecutor(1, 2, |
833 |
< |
LONG_DELAY_MS, MILLISECONDS, |
901 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
902 |
|
(BlockingQueue) null, |
903 |
|
new NoOpREHandler()); |
904 |
|
shouldThrow(); |
910 |
|
*/ |
911 |
|
public void testConstructorNullPointerException5() { |
912 |
|
try { |
913 |
< |
new ThreadPoolExecutor(1, 2, |
846 |
< |
LONG_DELAY_MS, MILLISECONDS, |
913 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
914 |
|
new ArrayBlockingQueue<Runnable>(10), |
915 |
|
(RejectedExecutionHandler) null); |
916 |
|
shouldThrow(); |
922 |
|
*/ |
923 |
|
public void testConstructor16() { |
924 |
|
try { |
925 |
< |
new ThreadPoolExecutor(-1, 1, |
859 |
< |
LONG_DELAY_MS, MILLISECONDS, |
925 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
926 |
|
new ArrayBlockingQueue<Runnable>(10), |
927 |
|
new SimpleThreadFactory(), |
928 |
|
new NoOpREHandler()); |
935 |
|
*/ |
936 |
|
public void testConstructor17() { |
937 |
|
try { |
938 |
< |
new ThreadPoolExecutor(1, -1, |
873 |
< |
LONG_DELAY_MS, MILLISECONDS, |
938 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
939 |
|
new ArrayBlockingQueue<Runnable>(10), |
940 |
|
new SimpleThreadFactory(), |
941 |
|
new NoOpREHandler()); |
948 |
|
*/ |
949 |
|
public void testConstructor18() { |
950 |
|
try { |
951 |
< |
new ThreadPoolExecutor(1, 0, |
887 |
< |
LONG_DELAY_MS, MILLISECONDS, |
951 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
952 |
|
new ArrayBlockingQueue<Runnable>(10), |
953 |
|
new SimpleThreadFactory(), |
954 |
|
new NoOpREHandler()); |
961 |
|
*/ |
962 |
|
public void testConstructor19() { |
963 |
|
try { |
964 |
< |
new ThreadPoolExecutor(1, 2, |
901 |
< |
-1L, MILLISECONDS, |
964 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
965 |
|
new ArrayBlockingQueue<Runnable>(10), |
966 |
|
new SimpleThreadFactory(), |
967 |
|
new NoOpREHandler()); |
974 |
|
*/ |
975 |
|
public void testConstructor20() { |
976 |
|
try { |
977 |
< |
new ThreadPoolExecutor(2, 1, |
915 |
< |
LONG_DELAY_MS, MILLISECONDS, |
977 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
978 |
|
new ArrayBlockingQueue<Runnable>(10), |
979 |
|
new SimpleThreadFactory(), |
980 |
|
new NoOpREHandler()); |
987 |
|
*/ |
988 |
|
public void testConstructorNullPointerException6() { |
989 |
|
try { |
990 |
< |
new ThreadPoolExecutor(1, 2, |
929 |
< |
LONG_DELAY_MS, MILLISECONDS, |
990 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
991 |
|
(BlockingQueue) null, |
992 |
|
new SimpleThreadFactory(), |
993 |
|
new NoOpREHandler()); |
1000 |
|
*/ |
1001 |
|
public void testConstructorNullPointerException7() { |
1002 |
|
try { |
1003 |
< |
new ThreadPoolExecutor(1, 2, |
943 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1003 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1004 |
|
new ArrayBlockingQueue<Runnable>(10), |
1005 |
|
new SimpleThreadFactory(), |
1006 |
|
(RejectedExecutionHandler) null); |
1013 |
|
*/ |
1014 |
|
public void testConstructorNullPointerException8() { |
1015 |
|
try { |
1016 |
< |
new ThreadPoolExecutor(1, 2, |
957 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1016 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1017 |
|
new ArrayBlockingQueue<Runnable>(10), |
1018 |
|
(ThreadFactory) null, |
1019 |
|
new NoOpREHandler()); |
1027 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1028 |
|
final ThreadPoolExecutor p = |
1029 |
|
new ThreadPoolExecutor(1, 1, |
1030 |
< |
60, TimeUnit.SECONDS, |
1030 |
> |
60, SECONDS, |
1031 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1032 |
|
|
1033 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1044 |
|
p.submit(task).get(); |
1045 |
|
}}); |
1046 |
|
|
1047 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1047 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
1048 |
|
t.interrupt(); |
1049 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1050 |
|
} finally { |
1301 |
|
*/ |
1302 |
|
public void testExecuteNull() { |
1303 |
|
ThreadPoolExecutor p = |
1304 |
< |
new ThreadPoolExecutor(1, 2, |
1246 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1304 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1305 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1306 |
|
try { |
1307 |
|
p.execute(null); |
1368 |
|
} |
1369 |
|
|
1370 |
|
/** |
1371 |
+ |
* Configuration changes that allow core pool size greater than |
1372 |
+ |
* max pool size result in IllegalArgumentException. |
1373 |
+ |
*/ |
1374 |
+ |
public void testPoolSizeInvariants() { |
1375 |
+ |
ThreadPoolExecutor p = |
1376 |
+ |
new ThreadPoolExecutor(1, 1, |
1377 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1378 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1379 |
+ |
for (int s = 1; s < 5; s++) { |
1380 |
+ |
p.setMaximumPoolSize(s); |
1381 |
+ |
p.setCorePoolSize(s); |
1382 |
+ |
try { |
1383 |
+ |
p.setMaximumPoolSize(s - 1); |
1384 |
+ |
shouldThrow(); |
1385 |
+ |
} catch (IllegalArgumentException success) {} |
1386 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1387 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1388 |
+ |
try { |
1389 |
+ |
p.setCorePoolSize(s + 1); |
1390 |
+ |
shouldThrow(); |
1391 |
+ |
} catch (IllegalArgumentException success) {} |
1392 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1393 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1394 |
+ |
} |
1395 |
+ |
joinPool(p); |
1396 |
+ |
} |
1397 |
+ |
|
1398 |
+ |
/** |
1399 |
|
* setKeepAliveTime throws IllegalArgumentException |
1400 |
|
* when given a negative value |
1401 |
|
*/ |
1431 |
|
ExtendedTPE p = new ExtendedTPE(); |
1432 |
|
try { |
1433 |
|
final CountDownLatch done = new CountDownLatch(1); |
1434 |
< |
final CheckedRunnable task = new CheckedRunnable() { |
1434 |
> |
p.execute(new CheckedRunnable() { |
1435 |
|
public void realRun() { |
1436 |
|
done.countDown(); |
1437 |
< |
}}; |
1352 |
< |
p.execute(task); |
1437 |
> |
}}); |
1438 |
|
await(p.afterCalled); |
1439 |
|
assertEquals(0, done.getCount()); |
1440 |
|
assertTrue(p.afterCalled()); |
1911 |
|
l.add(new StringTask()); |
1912 |
|
l.add(new StringTask()); |
1913 |
|
List<Future<String>> futures = |
1914 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1914 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1915 |
|
assertEquals(2, futures.size()); |
1916 |
|
for (Future<String> future : futures) |
1917 |
|
assertSame(TEST_STRING, future.get()); |
1929 |
|
LONG_DELAY_MS, MILLISECONDS, |
1930 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1931 |
|
try { |
1932 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1933 |
< |
l.add(new StringTask()); |
1934 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1935 |
< |
l.add(new StringTask()); |
1936 |
< |
List<Future<String>> futures = |
1937 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1938 |
< |
assertEquals(l.size(), futures.size()); |
1939 |
< |
for (Future future : futures) |
1940 |
< |
assertTrue(future.isDone()); |
1941 |
< |
assertFalse(futures.get(0).isCancelled()); |
1942 |
< |
assertTrue(futures.get(1).isCancelled()); |
1932 |
> |
for (long timeout = timeoutMillis();;) { |
1933 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1934 |
> |
tasks.add(new StringTask("0")); |
1935 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1936 |
> |
tasks.add(new StringTask("2")); |
1937 |
> |
long startTime = System.nanoTime(); |
1938 |
> |
List<Future<String>> futures = |
1939 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1940 |
> |
assertEquals(tasks.size(), futures.size()); |
1941 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1942 |
> |
for (Future future : futures) |
1943 |
> |
assertTrue(future.isDone()); |
1944 |
> |
assertTrue(futures.get(1).isCancelled()); |
1945 |
> |
try { |
1946 |
> |
assertEquals("0", futures.get(0).get()); |
1947 |
> |
assertEquals("2", futures.get(2).get()); |
1948 |
> |
break; |
1949 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1950 |
> |
timeout *= 2; |
1951 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1952 |
> |
fail("expected exactly one task to be cancelled"); |
1953 |
> |
} |
1954 |
> |
} |
1955 |
|
} finally { |
1956 |
|
joinPool(e); |
1957 |
|
} |
1997 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1998 |
|
*/ |
1999 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
2000 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
2000 |
> |
long keepAliveTime = timeoutMillis(); |
2001 |
|
final ThreadPoolExecutor p = |
2002 |
|
new ThreadPoolExecutor(2, 10, |
2003 |
< |
coreThreadTimeOut, MILLISECONDS, |
2003 |
> |
keepAliveTime, MILLISECONDS, |
2004 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2005 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2006 |
|
try { |
2011 |
|
assertEquals(1, p.getPoolSize()); |
2012 |
|
}}); |
2013 |
|
await(threadStarted); |
2014 |
< |
delay(coreThreadTimeOut); |
2014 |
> |
delay(keepAliveTime); |
2015 |
|
long startTime = System.nanoTime(); |
2016 |
|
while (p.getPoolSize() > 0 |
2017 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
2027 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
2028 |
|
*/ |
2029 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
2030 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
2030 |
> |
long keepAliveTime = timeoutMillis(); |
2031 |
|
final ThreadPoolExecutor p = |
2032 |
|
new ThreadPoolExecutor(2, 10, |
2033 |
< |
coreThreadTimeOut, MILLISECONDS, |
2033 |
> |
keepAliveTime, MILLISECONDS, |
2034 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2035 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2036 |
|
try { |
2040 |
|
threadStarted.countDown(); |
2041 |
|
assertTrue(p.getPoolSize() >= 1); |
2042 |
|
}}); |
2043 |
< |
delay(2 * coreThreadTimeOut); |
2043 |
> |
delay(2 * keepAliveTime); |
2044 |
|
assertTrue(p.getPoolSize() >= 1); |
2045 |
|
} finally { |
2046 |
|
joinPool(p); |
2059 |
|
done.countDown(); |
2060 |
|
}}; |
2061 |
|
final ThreadPoolExecutor p = |
2062 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2062 |
> |
new ThreadPoolExecutor(1, 30, |
2063 |
> |
60, SECONDS, |
2064 |
|
new ArrayBlockingQueue(30)); |
2065 |
|
try { |
2066 |
|
for (int i = 0; i < nTasks; ++i) { |
2075 |
|
// enough time to run all tasks |
2076 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2077 |
|
} finally { |
2078 |
< |
p.shutdown(); |
2078 |
> |
joinPool(p); |
2079 |
> |
} |
2080 |
> |
} |
2081 |
> |
|
2082 |
> |
/** |
2083 |
> |
* get(cancelled task) throws CancellationException |
2084 |
> |
*/ |
2085 |
> |
public void testGet_cancelled() throws Exception { |
2086 |
> |
final ExecutorService e = |
2087 |
> |
new ThreadPoolExecutor(1, 1, |
2088 |
> |
LONG_DELAY_MS, MILLISECONDS, |
2089 |
> |
new LinkedBlockingQueue<Runnable>()); |
2090 |
> |
try { |
2091 |
> |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2092 |
> |
final CountDownLatch done = new CountDownLatch(1); |
2093 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
2094 |
> |
for (int i = 0; i < 2; i++) { |
2095 |
> |
Runnable r = new CheckedRunnable() { public void realRun() |
2096 |
> |
throws Throwable { |
2097 |
> |
blockerStarted.countDown(); |
2098 |
> |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2099 |
> |
}}; |
2100 |
> |
futures.add(e.submit(r)); |
2101 |
> |
} |
2102 |
> |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2103 |
> |
for (Future<?> future : futures) future.cancel(false); |
2104 |
> |
for (Future<?> future : futures) { |
2105 |
> |
try { |
2106 |
> |
future.get(); |
2107 |
> |
shouldThrow(); |
2108 |
> |
} catch (CancellationException success) {} |
2109 |
> |
try { |
2110 |
> |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2111 |
> |
shouldThrow(); |
2112 |
> |
} catch (CancellationException success) {} |
2113 |
> |
assertTrue(future.isCancelled()); |
2114 |
> |
assertTrue(future.isDone()); |
2115 |
> |
} |
2116 |
> |
done.countDown(); |
2117 |
> |
} finally { |
2118 |
> |
joinPool(e); |
2119 |
|
} |
2120 |
|
} |
2121 |
|
|