11 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
|
13 |
|
import java.util.ArrayList; |
14 |
+ |
import java.util.Collection; |
15 |
+ |
import java.util.Collections; |
16 |
|
import java.util.List; |
17 |
|
import java.util.concurrent.ArrayBlockingQueue; |
18 |
|
import java.util.concurrent.BlockingQueue; |
20 |
|
import java.util.concurrent.CancellationException; |
21 |
|
import java.util.concurrent.CountDownLatch; |
22 |
|
import java.util.concurrent.ExecutionException; |
21 |
– |
import java.util.concurrent.Executors; |
23 |
|
import java.util.concurrent.ExecutorService; |
24 |
|
import java.util.concurrent.Future; |
25 |
|
import java.util.concurrent.FutureTask; |
28 |
|
import java.util.concurrent.RejectedExecutionHandler; |
29 |
|
import java.util.concurrent.SynchronousQueue; |
30 |
|
import java.util.concurrent.ThreadFactory; |
31 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
32 |
|
import java.util.concurrent.ThreadPoolExecutor; |
33 |
< |
import java.util.concurrent.TimeUnit; |
33 |
> |
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; |
34 |
> |
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; |
35 |
> |
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy; |
36 |
> |
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy; |
37 |
|
import java.util.concurrent.atomic.AtomicInteger; |
38 |
+ |
import java.util.concurrent.atomic.AtomicReference; |
39 |
|
|
40 |
|
import junit.framework.Test; |
41 |
|
import junit.framework.TestSuite; |
93 |
|
new ThreadPoolExecutor(1, 1, |
94 |
|
LONG_DELAY_MS, MILLISECONDS, |
95 |
|
new ArrayBlockingQueue<Runnable>(10)); |
96 |
< |
final CountDownLatch done = new CountDownLatch(1); |
97 |
< |
final Runnable task = new CheckedRunnable() { |
98 |
< |
public void realRun() { |
99 |
< |
done.countDown(); |
94 |
< |
}}; |
95 |
< |
try { |
96 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
97 |
> |
final CountDownLatch done = new CountDownLatch(1); |
98 |
> |
final Runnable task = new CheckedRunnable() { |
99 |
> |
public void realRun() { done.countDown(); }}; |
100 |
|
p.execute(task); |
101 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
98 |
< |
} finally { |
99 |
< |
joinPool(p); |
101 |
> |
await(done); |
102 |
|
} |
103 |
|
} |
104 |
|
|
107 |
|
* thread becomes active |
108 |
|
*/ |
109 |
|
public void testGetActiveCount() throws InterruptedException { |
110 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
111 |
|
final ThreadPoolExecutor p = |
112 |
|
new ThreadPoolExecutor(2, 2, |
113 |
|
LONG_DELAY_MS, MILLISECONDS, |
114 |
|
new ArrayBlockingQueue<Runnable>(10)); |
115 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
116 |
< |
final CountDownLatch done = new CountDownLatch(1); |
114 |
< |
try { |
115 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
116 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
117 |
|
assertEquals(0, p.getActiveCount()); |
118 |
|
p.execute(new CheckedRunnable() { |
119 |
|
public void realRun() throws InterruptedException { |
120 |
|
threadStarted.countDown(); |
121 |
|
assertEquals(1, p.getActiveCount()); |
122 |
< |
done.await(); |
122 |
> |
await(done); |
123 |
|
}}); |
124 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
124 |
> |
await(threadStarted); |
125 |
|
assertEquals(1, p.getActiveCount()); |
124 |
– |
} finally { |
125 |
– |
done.countDown(); |
126 |
– |
joinPool(p); |
126 |
|
} |
127 |
|
} |
128 |
|
|
192 |
|
public void realRun() throws InterruptedException { |
193 |
|
threadStarted.countDown(); |
194 |
|
assertEquals(0, p.getCompletedTaskCount()); |
195 |
< |
threadProceed.await(); |
195 |
> |
await(threadProceed); |
196 |
|
threadDone.countDown(); |
197 |
|
}}); |
198 |
|
await(threadStarted); |
199 |
|
assertEquals(0, p.getCompletedTaskCount()); |
200 |
|
threadProceed.countDown(); |
201 |
< |
threadDone.await(); |
201 |
> |
await(threadDone); |
202 |
|
long startTime = System.nanoTime(); |
203 |
|
while (p.getCompletedTaskCount() != 1) { |
204 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
+ |
* The default rejected execution handler is AbortPolicy. |
286 |
+ |
*/ |
287 |
+ |
public void testDefaultRejectedExecutionHandler() { |
288 |
+ |
final ThreadPoolExecutor p = |
289 |
+ |
new ThreadPoolExecutor(1, 2, |
290 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
291 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
292 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
293 |
+ |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
294 |
+ |
} |
295 |
+ |
} |
296 |
+ |
|
297 |
+ |
/** |
298 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
299 |
|
*/ |
300 |
|
public void testGetRejectedExecutionHandler() { |
318 |
|
new ThreadPoolExecutor(1, 2, |
319 |
|
LONG_DELAY_MS, MILLISECONDS, |
320 |
|
new ArrayBlockingQueue<Runnable>(10)); |
321 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
322 |
< |
p.setRejectedExecutionHandler(h); |
323 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
324 |
< |
joinPool(p); |
321 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
322 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
323 |
> |
p.setRejectedExecutionHandler(handler); |
324 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
325 |
> |
} |
326 |
|
} |
327 |
|
|
328 |
|
/** |
333 |
|
new ThreadPoolExecutor(1, 2, |
334 |
|
LONG_DELAY_MS, MILLISECONDS, |
335 |
|
new ArrayBlockingQueue<Runnable>(10)); |
336 |
< |
try { |
337 |
< |
p.setRejectedExecutionHandler(null); |
338 |
< |
shouldThrow(); |
339 |
< |
} catch (NullPointerException success) { |
340 |
< |
} finally { |
328 |
< |
joinPool(p); |
336 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
> |
try { |
338 |
> |
p.setRejectedExecutionHandler(null); |
339 |
> |
shouldThrow(); |
340 |
> |
} catch (NullPointerException success) {} |
341 |
|
} |
342 |
|
} |
343 |
|
|
347 |
|
*/ |
348 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
349 |
|
final int THREADS = 3; |
350 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
351 |
|
final ThreadPoolExecutor p = |
352 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
353 |
|
LONG_DELAY_MS, MILLISECONDS, |
354 |
|
new ArrayBlockingQueue<Runnable>(10)); |
355 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
343 |
< |
final CountDownLatch done = new CountDownLatch(1); |
344 |
< |
try { |
355 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
356 |
|
assertEquals(0, p.getLargestPoolSize()); |
357 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
358 |
|
for (int i = 0; i < THREADS; i++) |
359 |
|
p.execute(new CheckedRunnable() { |
360 |
|
public void realRun() throws InterruptedException { |
361 |
|
threadsStarted.countDown(); |
362 |
< |
done.await(); |
362 |
> |
await(done); |
363 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
364 |
|
}}); |
365 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
354 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
355 |
< |
} finally { |
356 |
< |
done.countDown(); |
357 |
< |
joinPool(p); |
365 |
> |
await(threadsStarted); |
366 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
367 |
|
} |
368 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
377 |
|
new ThreadPoolExecutor(2, 3, |
378 |
|
LONG_DELAY_MS, MILLISECONDS, |
379 |
|
new ArrayBlockingQueue<Runnable>(10)); |
380 |
< |
assertEquals(3, p.getMaximumPoolSize()); |
381 |
< |
joinPool(p); |
380 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
381 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
382 |
> |
p.setMaximumPoolSize(5); |
383 |
> |
assertEquals(5, p.getMaximumPoolSize()); |
384 |
> |
p.setMaximumPoolSize(4); |
385 |
> |
assertEquals(4, p.getMaximumPoolSize()); |
386 |
> |
} |
387 |
|
} |
388 |
|
|
389 |
|
/** |
391 |
|
* become active |
392 |
|
*/ |
393 |
|
public void testGetPoolSize() throws InterruptedException { |
394 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
395 |
|
final ThreadPoolExecutor p = |
396 |
|
new ThreadPoolExecutor(1, 1, |
397 |
|
LONG_DELAY_MS, MILLISECONDS, |
398 |
|
new ArrayBlockingQueue<Runnable>(10)); |
399 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
385 |
< |
final CountDownLatch done = new CountDownLatch(1); |
386 |
< |
try { |
399 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
400 |
|
assertEquals(0, p.getPoolSize()); |
401 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
402 |
|
p.execute(new CheckedRunnable() { |
403 |
|
public void realRun() throws InterruptedException { |
404 |
|
threadStarted.countDown(); |
405 |
|
assertEquals(1, p.getPoolSize()); |
406 |
< |
done.await(); |
406 |
> |
await(done); |
407 |
|
}}); |
408 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
408 |
> |
await(threadStarted); |
409 |
|
assertEquals(1, p.getPoolSize()); |
396 |
– |
} finally { |
397 |
– |
done.countDown(); |
398 |
– |
joinPool(p); |
410 |
|
} |
411 |
|
} |
412 |
|
|
414 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
415 |
|
*/ |
416 |
|
public void testGetTaskCount() throws InterruptedException { |
417 |
+ |
final int TASKS = 3; |
418 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
419 |
|
final ThreadPoolExecutor p = |
420 |
|
new ThreadPoolExecutor(1, 1, |
421 |
|
LONG_DELAY_MS, MILLISECONDS, |
422 |
|
new ArrayBlockingQueue<Runnable>(10)); |
423 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
424 |
< |
final CountDownLatch done = new CountDownLatch(1); |
412 |
< |
try { |
423 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
424 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
425 |
|
assertEquals(0, p.getTaskCount()); |
426 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
427 |
|
p.execute(new CheckedRunnable() { |
428 |
|
public void realRun() throws InterruptedException { |
429 |
|
threadStarted.countDown(); |
430 |
< |
assertEquals(1, p.getTaskCount()); |
418 |
< |
done.await(); |
430 |
> |
await(done); |
431 |
|
}}); |
432 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
432 |
> |
await(threadStarted); |
433 |
|
assertEquals(1, p.getTaskCount()); |
434 |
< |
} finally { |
435 |
< |
done.countDown(); |
436 |
< |
joinPool(p); |
434 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
435 |
> |
for (int i = 0; i < TASKS; i++) { |
436 |
> |
assertEquals(1 + i, p.getTaskCount()); |
437 |
> |
p.execute(new CheckedRunnable() { |
438 |
> |
public void realRun() throws InterruptedException { |
439 |
> |
threadStarted.countDown(); |
440 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
441 |
> |
await(done); |
442 |
> |
}}); |
443 |
> |
} |
444 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
445 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
446 |
|
} |
447 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
448 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
449 |
|
} |
450 |
|
|
451 |
|
/** |
456 |
|
new ThreadPoolExecutor(1, 1, |
457 |
|
LONG_DELAY_MS, MILLISECONDS, |
458 |
|
new ArrayBlockingQueue<Runnable>(10)); |
459 |
< |
assertFalse(p.isShutdown()); |
460 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
461 |
< |
assertTrue(p.isShutdown()); |
462 |
< |
joinPool(p); |
459 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
460 |
> |
assertFalse(p.isShutdown()); |
461 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
462 |
> |
assertTrue(p.isShutdown()); |
463 |
> |
} |
464 |
|
} |
465 |
|
|
466 |
|
/** |
471 |
|
new ThreadPoolExecutor(1, 1, |
472 |
|
LONG_DELAY_MS, MILLISECONDS, |
473 |
|
new ArrayBlockingQueue<Runnable>(10)); |
474 |
< |
assertFalse(p.isTerminated()); |
475 |
< |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
476 |
< |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
477 |
< |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
478 |
< |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
479 |
< |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
480 |
< |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
481 |
< |
long timeoutNanos = 999999L; |
482 |
< |
long startTime = System.nanoTime(); |
483 |
< |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
484 |
< |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
485 |
< |
assertFalse(p.isTerminated()); |
486 |
< |
startTime = System.nanoTime(); |
487 |
< |
long timeoutMillis = timeoutMillis(); |
488 |
< |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
489 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
490 |
< |
assertFalse(p.isTerminated()); |
491 |
< |
p.shutdown(); |
492 |
< |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
493 |
< |
assertTrue(p.isTerminated()); |
474 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
475 |
> |
assertFalse(p.isTerminated()); |
476 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
477 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
478 |
> |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
479 |
> |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
480 |
> |
assertFalse(p.awaitTermination(randomExpiredTimeout(), |
481 |
> |
randomTimeUnit())); |
482 |
> |
long timeoutNanos = 999999L; |
483 |
> |
long startTime = System.nanoTime(); |
484 |
> |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
485 |
> |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
486 |
> |
assertFalse(p.isTerminated()); |
487 |
> |
startTime = System.nanoTime(); |
488 |
> |
long timeoutMillis = timeoutMillis(); |
489 |
> |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
490 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
491 |
> |
assertFalse(p.isTerminated()); |
492 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
493 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
494 |
> |
assertTrue(p.isTerminated()); |
495 |
> |
} |
496 |
|
} |
497 |
|
|
498 |
|
/** |
503 |
|
new ThreadPoolExecutor(1, 1, |
504 |
|
LONG_DELAY_MS, MILLISECONDS, |
505 |
|
new ArrayBlockingQueue<Runnable>(10)); |
506 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
507 |
< |
final CountDownLatch done = new CountDownLatch(1); |
508 |
< |
assertFalse(p.isTerminated()); |
509 |
< |
try { |
506 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
507 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
508 |
> |
final CountDownLatch done = new CountDownLatch(1); |
509 |
> |
assertFalse(p.isTerminating()); |
510 |
|
p.execute(new CheckedRunnable() { |
511 |
|
public void realRun() throws InterruptedException { |
512 |
< |
assertFalse(p.isTerminated()); |
512 |
> |
assertFalse(p.isTerminating()); |
513 |
|
threadStarted.countDown(); |
514 |
< |
done.await(); |
514 |
> |
await(done); |
515 |
|
}}); |
516 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
516 |
> |
await(threadStarted); |
517 |
|
assertFalse(p.isTerminating()); |
518 |
|
done.countDown(); |
493 |
– |
} finally { |
519 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
520 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
521 |
+ |
assertTrue(p.isTerminated()); |
522 |
+ |
assertFalse(p.isTerminating()); |
523 |
|
} |
496 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
497 |
– |
assertTrue(p.isTerminated()); |
524 |
|
} |
525 |
|
|
526 |
|
/** |
531 |
|
new ThreadPoolExecutor(1, 1, |
532 |
|
LONG_DELAY_MS, MILLISECONDS, |
533 |
|
new ArrayBlockingQueue<Runnable>(10)); |
534 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
535 |
< |
final CountDownLatch done = new CountDownLatch(1); |
536 |
< |
try { |
534 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
535 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
536 |
> |
final CountDownLatch done = new CountDownLatch(1); |
537 |
|
assertFalse(p.isTerminating()); |
538 |
|
p.execute(new CheckedRunnable() { |
539 |
|
public void realRun() throws InterruptedException { |
540 |
|
assertFalse(p.isTerminating()); |
541 |
|
threadStarted.countDown(); |
542 |
< |
done.await(); |
542 |
> |
await(done); |
543 |
|
}}); |
544 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
544 |
> |
await(threadStarted); |
545 |
|
assertFalse(p.isTerminating()); |
546 |
|
done.countDown(); |
521 |
– |
} finally { |
547 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
548 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
549 |
+ |
assertTrue(p.isTerminated()); |
550 |
+ |
assertFalse(p.isTerminating()); |
551 |
|
} |
524 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
525 |
– |
assertTrue(p.isTerminated()); |
526 |
– |
assertFalse(p.isTerminating()); |
552 |
|
} |
553 |
|
|
554 |
|
/** |
555 |
|
* getQueue returns the work queue, which contains queued tasks |
556 |
|
*/ |
557 |
|
public void testGetQueue() throws InterruptedException { |
558 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
558 |
> |
final CountDownLatch done = new CountDownLatch(1); |
559 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
560 |
|
final ThreadPoolExecutor p = |
561 |
|
new ThreadPoolExecutor(1, 1, |
562 |
|
LONG_DELAY_MS, MILLISECONDS, |
563 |
|
q); |
564 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
565 |
< |
final CountDownLatch done = new CountDownLatch(1); |
540 |
< |
try { |
564 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
565 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
566 |
|
FutureTask[] tasks = new FutureTask[5]; |
567 |
|
for (int i = 0; i < tasks.length; i++) { |
568 |
|
Callable task = new CheckedCallable<Boolean>() { |
569 |
|
public Boolean realCall() throws InterruptedException { |
570 |
|
threadStarted.countDown(); |
571 |
|
assertSame(q, p.getQueue()); |
572 |
< |
done.await(); |
572 |
> |
await(done); |
573 |
|
return Boolean.TRUE; |
574 |
|
}}; |
575 |
|
tasks[i] = new FutureTask(task); |
576 |
|
p.execute(tasks[i]); |
577 |
|
} |
578 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
578 |
> |
await(threadStarted); |
579 |
|
assertSame(q, p.getQueue()); |
580 |
|
assertFalse(q.contains(tasks[0])); |
581 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
582 |
|
assertEquals(tasks.length - 1, q.size()); |
558 |
– |
} finally { |
559 |
– |
done.countDown(); |
560 |
– |
joinPool(p); |
583 |
|
} |
584 |
|
} |
585 |
|
|
587 |
|
* remove(task) removes queued task, and fails to remove active task |
588 |
|
*/ |
589 |
|
public void testRemove() throws InterruptedException { |
590 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
590 |
> |
final CountDownLatch done = new CountDownLatch(1); |
591 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
592 |
|
final ThreadPoolExecutor p = |
593 |
|
new ThreadPoolExecutor(1, 1, |
594 |
|
LONG_DELAY_MS, MILLISECONDS, |
595 |
|
q); |
596 |
< |
Runnable[] tasks = new Runnable[5]; |
597 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
598 |
< |
final CountDownLatch done = new CountDownLatch(1); |
576 |
< |
try { |
596 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
597 |
> |
Runnable[] tasks = new Runnable[6]; |
598 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
599 |
|
for (int i = 0; i < tasks.length; i++) { |
600 |
|
tasks[i] = new CheckedRunnable() { |
601 |
|
public void realRun() throws InterruptedException { |
602 |
|
threadStarted.countDown(); |
603 |
< |
done.await(); |
603 |
> |
await(done); |
604 |
|
}}; |
605 |
|
p.execute(tasks[i]); |
606 |
|
} |
607 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
607 |
> |
await(threadStarted); |
608 |
|
assertFalse(p.remove(tasks[0])); |
609 |
|
assertTrue(q.contains(tasks[4])); |
610 |
|
assertTrue(q.contains(tasks[3])); |
614 |
|
assertTrue(q.contains(tasks[3])); |
615 |
|
assertTrue(p.remove(tasks[3])); |
616 |
|
assertFalse(q.contains(tasks[3])); |
595 |
– |
} finally { |
596 |
– |
done.countDown(); |
597 |
– |
joinPool(p); |
617 |
|
} |
618 |
|
} |
619 |
|
|
623 |
|
public void testPurge() throws InterruptedException { |
624 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
625 |
|
final CountDownLatch done = new CountDownLatch(1); |
626 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
626 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
627 |
|
final ThreadPoolExecutor p = |
628 |
|
new ThreadPoolExecutor(1, 1, |
629 |
|
LONG_DELAY_MS, MILLISECONDS, |
630 |
|
q); |
631 |
< |
FutureTask[] tasks = new FutureTask[5]; |
632 |
< |
try { |
631 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
632 |
> |
FutureTask[] tasks = new FutureTask[5]; |
633 |
|
for (int i = 0; i < tasks.length; i++) { |
634 |
|
Callable task = new CheckedCallable<Boolean>() { |
635 |
|
public Boolean realCall() throws InterruptedException { |
636 |
|
threadStarted.countDown(); |
637 |
< |
done.await(); |
637 |
> |
await(done); |
638 |
|
return Boolean.TRUE; |
639 |
|
}}; |
640 |
|
tasks[i] = new FutureTask(task); |
641 |
|
p.execute(tasks[i]); |
642 |
|
} |
643 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
643 |
> |
await(threadStarted); |
644 |
|
assertEquals(tasks.length, p.getTaskCount()); |
645 |
|
assertEquals(tasks.length - 1, q.size()); |
646 |
|
assertEquals(1L, p.getActiveCount()); |
653 |
|
p.purge(); // Nothing to do |
654 |
|
assertEquals(tasks.length - 3, q.size()); |
655 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
637 |
– |
} finally { |
638 |
– |
done.countDown(); |
639 |
– |
joinPool(p); |
656 |
|
} |
657 |
|
} |
658 |
|
|
668 |
|
new ThreadPoolExecutor(poolSize, poolSize, |
669 |
|
LONG_DELAY_MS, MILLISECONDS, |
670 |
|
new ArrayBlockingQueue<Runnable>(10)); |
671 |
< |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
671 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
672 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
673 |
|
threadsStarted.countDown(); |
674 |
|
try { |
678 |
|
}}; |
679 |
|
for (int i = 0; i < count; i++) |
680 |
|
p.execute(waiter); |
681 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
681 |
> |
await(threadsStarted); |
682 |
|
assertEquals(poolSize, p.getActiveCount()); |
683 |
|
assertEquals(0, p.getCompletedTaskCount()); |
684 |
|
final List<Runnable> queuedTasks; |
1040 |
|
* get of submitted callable throws InterruptedException if interrupted |
1041 |
|
*/ |
1042 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1043 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1044 |
|
final ThreadPoolExecutor p = |
1045 |
|
new ThreadPoolExecutor(1, 1, |
1046 |
|
60, SECONDS, |
1047 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1048 |
|
|
1049 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1050 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1034 |
< |
try { |
1049 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1050 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1051 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1052 |
|
public void realRun() throws Exception { |
1053 |
|
Callable task = new CheckedCallable<Boolean>() { |
1054 |
|
public Boolean realCall() throws InterruptedException { |
1055 |
|
threadStarted.countDown(); |
1056 |
< |
done.await(); |
1056 |
> |
await(done); |
1057 |
|
return Boolean.TRUE; |
1058 |
|
}}; |
1059 |
|
p.submit(task).get(); |
1060 |
|
}}); |
1061 |
|
|
1062 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1062 |
> |
await(threadStarted); // ensure quiescence |
1063 |
|
t.interrupt(); |
1064 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1049 |
< |
} finally { |
1050 |
< |
done.countDown(); |
1051 |
< |
joinPool(p); |
1064 |
> |
awaitTermination(t); |
1065 |
|
} |
1066 |
|
} |
1067 |
|
|
1068 |
|
/** |
1069 |
< |
* execute throws RejectedExecutionException if saturated. |
1069 |
> |
* Submitted tasks are rejected when saturated or shutdown |
1070 |
|
*/ |
1071 |
< |
public void testSaturatedExecute() { |
1072 |
< |
ThreadPoolExecutor p = |
1073 |
< |
new ThreadPoolExecutor(1, 1, |
1074 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1075 |
< |
new ArrayBlockingQueue<Runnable>(1)); |
1071 |
> |
public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException { |
1072 |
> |
final ThreadPoolExecutor p = new ThreadPoolExecutor( |
1073 |
> |
1, 1, 1, SECONDS, new ArrayBlockingQueue<Runnable>(1)); |
1074 |
> |
final int saturatedSize = saturatedSize(p); |
1075 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1076 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize()); |
1077 |
|
final CountDownLatch done = new CountDownLatch(1); |
1078 |
< |
try { |
1079 |
< |
Runnable task = new CheckedRunnable() { |
1080 |
< |
public void realRun() throws InterruptedException { |
1067 |
< |
done.await(); |
1068 |
< |
}}; |
1069 |
< |
for (int i = 0; i < 2; ++i) |
1070 |
< |
p.execute(task); |
1071 |
< |
for (int i = 0; i < 2; ++i) { |
1078 |
> |
final Runnable r = () -> { |
1079 |
> |
threadsStarted.countDown(); |
1080 |
> |
for (;;) { |
1081 |
|
try { |
1073 |
– |
p.execute(task); |
1074 |
– |
shouldThrow(); |
1075 |
– |
} catch (RejectedExecutionException success) {} |
1076 |
– |
assertTrue(p.getTaskCount() <= 2); |
1077 |
– |
} |
1078 |
– |
} finally { |
1079 |
– |
done.countDown(); |
1080 |
– |
joinPool(p); |
1081 |
– |
} |
1082 |
– |
} |
1083 |
– |
|
1084 |
– |
/** |
1085 |
– |
* submit(runnable) throws RejectedExecutionException if saturated. |
1086 |
– |
*/ |
1087 |
– |
public void testSaturatedSubmitRunnable() { |
1088 |
– |
ThreadPoolExecutor p = |
1089 |
– |
new ThreadPoolExecutor(1, 1, |
1090 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1091 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
1092 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1093 |
– |
try { |
1094 |
– |
Runnable task = new CheckedRunnable() { |
1095 |
– |
public void realRun() throws InterruptedException { |
1082 |
|
done.await(); |
1083 |
< |
}}; |
1084 |
< |
for (int i = 0; i < 2; ++i) |
1085 |
< |
p.submit(task); |
1086 |
< |
for (int i = 0; i < 2; ++i) { |
1083 |
> |
return; |
1084 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1085 |
> |
}}; |
1086 |
> |
final Callable<Boolean> c = () -> { |
1087 |
> |
threadsStarted.countDown(); |
1088 |
> |
for (;;) { |
1089 |
|
try { |
1102 |
– |
p.execute(task); |
1103 |
– |
shouldThrow(); |
1104 |
– |
} catch (RejectedExecutionException success) {} |
1105 |
– |
assertTrue(p.getTaskCount() <= 2); |
1106 |
– |
} |
1107 |
– |
} finally { |
1108 |
– |
done.countDown(); |
1109 |
– |
joinPool(p); |
1110 |
– |
} |
1111 |
– |
} |
1112 |
– |
|
1113 |
– |
/** |
1114 |
– |
* submit(callable) throws RejectedExecutionException if saturated. |
1115 |
– |
*/ |
1116 |
– |
public void testSaturatedSubmitCallable() { |
1117 |
– |
ThreadPoolExecutor p = |
1118 |
– |
new ThreadPoolExecutor(1, 1, |
1119 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1120 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
1121 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1122 |
– |
try { |
1123 |
– |
Runnable task = new CheckedRunnable() { |
1124 |
– |
public void realRun() throws InterruptedException { |
1090 |
|
done.await(); |
1091 |
< |
}}; |
1092 |
< |
for (int i = 0; i < 2; ++i) |
1093 |
< |
p.submit(Executors.callable(task)); |
1094 |
< |
for (int i = 0; i < 2; ++i) { |
1095 |
< |
try { |
1096 |
< |
p.execute(task); |
1097 |
< |
shouldThrow(); |
1098 |
< |
} catch (RejectedExecutionException success) {} |
1099 |
< |
assertTrue(p.getTaskCount() <= 2); |
1091 |
> |
return Boolean.TRUE; |
1092 |
> |
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1093 |
> |
}}; |
1094 |
> |
final boolean shutdownNow = rnd.nextBoolean(); |
1095 |
> |
|
1096 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1097 |
> |
// saturate |
1098 |
> |
for (int i = saturatedSize; i--> 0; ) { |
1099 |
> |
switch (rnd.nextInt(4)) { |
1100 |
> |
case 0: p.execute(r); break; |
1101 |
> |
case 1: assertFalse(p.submit(r).isDone()); break; |
1102 |
> |
case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break; |
1103 |
> |
case 3: assertFalse(p.submit(c).isDone()); break; |
1104 |
> |
} |
1105 |
|
} |
1136 |
– |
} finally { |
1137 |
– |
done.countDown(); |
1138 |
– |
joinPool(p); |
1139 |
– |
} |
1140 |
– |
} |
1106 |
|
|
1107 |
< |
/** |
1108 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1144 |
< |
*/ |
1145 |
< |
public void testSaturatedExecute2() { |
1146 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1147 |
< |
final ThreadPoolExecutor p = |
1148 |
< |
new ThreadPoolExecutor(1, 1, |
1149 |
< |
LONG_DELAY_MS, |
1150 |
< |
MILLISECONDS, |
1151 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1152 |
< |
h); |
1153 |
< |
try { |
1154 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1155 |
< |
for (int i = 0; i < tasks.length; ++i) |
1156 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1157 |
< |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1158 |
< |
p.execute(mr); |
1159 |
< |
for (int i = 0; i < tasks.length; ++i) |
1160 |
< |
p.execute(tasks[i]); |
1161 |
< |
for (int i = 1; i < tasks.length; ++i) |
1162 |
< |
assertTrue(tasks[i].done); |
1163 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1164 |
< |
} finally { |
1165 |
< |
joinPool(p); |
1166 |
< |
} |
1167 |
< |
} |
1107 |
> |
await(threadsStarted); |
1108 |
> |
assertTaskSubmissionsAreRejected(p); |
1109 |
|
|
1110 |
< |
/** |
1111 |
< |
* executor using DiscardPolicy drops task if saturated. |
1112 |
< |
*/ |
1113 |
< |
public void testSaturatedExecute3() { |
1114 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1115 |
< |
final ThreadPoolExecutor p = |
1116 |
< |
new ThreadPoolExecutor(1, 1, |
1117 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1118 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1119 |
< |
h); |
1120 |
< |
try { |
1121 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1122 |
< |
for (int i = 0; i < tasks.length; ++i) |
1123 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1124 |
< |
p.execute(new TrackedLongRunnable()); |
1125 |
< |
for (TrackedNoOpRunnable task : tasks) |
1185 |
< |
p.execute(task); |
1186 |
< |
for (TrackedNoOpRunnable task : tasks) |
1187 |
< |
assertFalse(task.done); |
1188 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1189 |
< |
} finally { |
1190 |
< |
joinPool(p); |
1191 |
< |
} |
1110 |
> |
if (shutdownNow) |
1111 |
> |
p.shutdownNow(); |
1112 |
> |
else |
1113 |
> |
p.shutdown(); |
1114 |
> |
// Pool is shutdown, but not yet terminated |
1115 |
> |
assertTaskSubmissionsAreRejected(p); |
1116 |
> |
assertFalse(p.isTerminated()); |
1117 |
> |
|
1118 |
> |
done.countDown(); // release blocking tasks |
1119 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
1120 |
> |
|
1121 |
> |
assertTaskSubmissionsAreRejected(p); |
1122 |
> |
} |
1123 |
> |
assertEquals(saturatedSize(p) |
1124 |
> |
- (shutdownNow ? p.getQueue().remainingCapacity() : 0), |
1125 |
> |
p.getCompletedTaskCount()); |
1126 |
|
} |
1127 |
|
|
1128 |
|
/** |
1129 |
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1130 |
|
*/ |
1131 |
< |
public void testSaturatedExecute4() { |
1132 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1131 |
> |
public void testSaturatedExecute_DiscardOldestPolicy() { |
1132 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1133 |
> |
LatchAwaiter r1 = awaiter(done); |
1134 |
> |
LatchAwaiter r2 = awaiter(done); |
1135 |
> |
LatchAwaiter r3 = awaiter(done); |
1136 |
|
final ThreadPoolExecutor p = |
1137 |
|
new ThreadPoolExecutor(1, 1, |
1138 |
|
LONG_DELAY_MS, MILLISECONDS, |
1139 |
|
new ArrayBlockingQueue<Runnable>(1), |
1140 |
< |
h); |
1141 |
< |
try { |
1142 |
< |
p.execute(new TrackedLongRunnable()); |
1143 |
< |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1140 |
> |
new DiscardOldestPolicy()); |
1141 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1142 |
> |
assertEquals(LatchAwaiter.NEW, r1.state); |
1143 |
> |
assertEquals(LatchAwaiter.NEW, r2.state); |
1144 |
> |
assertEquals(LatchAwaiter.NEW, r3.state); |
1145 |
> |
p.execute(r1); |
1146 |
|
p.execute(r2); |
1147 |
|
assertTrue(p.getQueue().contains(r2)); |
1209 |
– |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
1148 |
|
p.execute(r3); |
1149 |
|
assertFalse(p.getQueue().contains(r2)); |
1150 |
|
assertTrue(p.getQueue().contains(r3)); |
1213 |
– |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1214 |
– |
} finally { |
1215 |
– |
joinPool(p); |
1216 |
– |
} |
1217 |
– |
} |
1218 |
– |
|
1219 |
– |
/** |
1220 |
– |
* execute throws RejectedExecutionException if shutdown |
1221 |
– |
*/ |
1222 |
– |
public void testRejectedExecutionExceptionOnShutdown() { |
1223 |
– |
ThreadPoolExecutor p = |
1224 |
– |
new ThreadPoolExecutor(1, 1, |
1225 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1226 |
– |
new ArrayBlockingQueue<Runnable>(1)); |
1227 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1228 |
– |
try { |
1229 |
– |
p.execute(new NoOpRunnable()); |
1230 |
– |
shouldThrow(); |
1231 |
– |
} catch (RejectedExecutionException success) {} |
1232 |
– |
|
1233 |
– |
joinPool(p); |
1234 |
– |
} |
1235 |
– |
|
1236 |
– |
/** |
1237 |
– |
* execute using CallerRunsPolicy drops task on shutdown |
1238 |
– |
*/ |
1239 |
– |
public void testCallerRunsOnShutdown() { |
1240 |
– |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1241 |
– |
final ThreadPoolExecutor p = |
1242 |
– |
new ThreadPoolExecutor(1, 1, |
1243 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1244 |
– |
new ArrayBlockingQueue<Runnable>(1), h); |
1245 |
– |
|
1246 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1247 |
– |
try { |
1248 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1249 |
– |
p.execute(r); |
1250 |
– |
assertFalse(r.done); |
1251 |
– |
} finally { |
1252 |
– |
joinPool(p); |
1253 |
– |
} |
1254 |
– |
} |
1255 |
– |
|
1256 |
– |
/** |
1257 |
– |
* execute using DiscardPolicy drops task on shutdown |
1258 |
– |
*/ |
1259 |
– |
public void testDiscardOnShutdown() { |
1260 |
– |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1261 |
– |
ThreadPoolExecutor p = |
1262 |
– |
new ThreadPoolExecutor(1, 1, |
1263 |
– |
LONG_DELAY_MS, MILLISECONDS, |
1264 |
– |
new ArrayBlockingQueue<Runnable>(1), |
1265 |
– |
h); |
1266 |
– |
|
1267 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1268 |
– |
try { |
1269 |
– |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1270 |
– |
p.execute(r); |
1271 |
– |
assertFalse(r.done); |
1272 |
– |
} finally { |
1273 |
– |
joinPool(p); |
1151 |
|
} |
1152 |
+ |
assertEquals(LatchAwaiter.DONE, r1.state); |
1153 |
+ |
assertEquals(LatchAwaiter.NEW, r2.state); |
1154 |
+ |
assertEquals(LatchAwaiter.DONE, r3.state); |
1155 |
|
} |
1156 |
|
|
1157 |
|
/** |
1158 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1159 |
|
*/ |
1160 |
|
public void testDiscardOldestOnShutdown() { |
1161 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1282 |
< |
ThreadPoolExecutor p = |
1161 |
> |
final ThreadPoolExecutor p = |
1162 |
|
new ThreadPoolExecutor(1, 1, |
1163 |
|
LONG_DELAY_MS, MILLISECONDS, |
1164 |
|
new ArrayBlockingQueue<Runnable>(1), |
1165 |
< |
h); |
1165 |
> |
new DiscardOldestPolicy()); |
1166 |
|
|
1167 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1168 |
< |
try { |
1168 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1169 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1170 |
|
p.execute(r); |
1171 |
|
assertFalse(r.done); |
1293 |
– |
} finally { |
1294 |
– |
joinPool(p); |
1172 |
|
} |
1173 |
|
} |
1174 |
|
|
1175 |
|
/** |
1176 |
< |
* execute(null) throws NPE |
1176 |
> |
* Submitting null tasks throws NullPointerException |
1177 |
|
*/ |
1178 |
< |
public void testExecuteNull() { |
1179 |
< |
ThreadPoolExecutor p = |
1180 |
< |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1178 |
> |
public void testNullTaskSubmission() { |
1179 |
> |
final ThreadPoolExecutor p = |
1180 |
> |
new ThreadPoolExecutor(1, 2, |
1181 |
> |
1L, SECONDS, |
1182 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1183 |
< |
try { |
1184 |
< |
p.execute(null); |
1185 |
< |
shouldThrow(); |
1308 |
< |
} catch (NullPointerException success) {} |
1309 |
< |
|
1310 |
< |
joinPool(p); |
1183 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1184 |
> |
assertNullTaskSubmissionThrowsNullPointerException(p); |
1185 |
> |
} |
1186 |
|
} |
1187 |
|
|
1188 |
|
/** |
1189 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1190 |
|
*/ |
1191 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1192 |
< |
ThreadPoolExecutor p = |
1192 |
> |
final ThreadPoolExecutor p = |
1193 |
|
new ThreadPoolExecutor(1, 2, |
1194 |
|
LONG_DELAY_MS, MILLISECONDS, |
1195 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1196 |
< |
try { |
1197 |
< |
p.setCorePoolSize(-1); |
1198 |
< |
shouldThrow(); |
1199 |
< |
} catch (IllegalArgumentException success) { |
1200 |
< |
} finally { |
1326 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1196 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1197 |
> |
try { |
1198 |
> |
p.setCorePoolSize(-1); |
1199 |
> |
shouldThrow(); |
1200 |
> |
} catch (IllegalArgumentException success) {} |
1201 |
|
} |
1328 |
– |
joinPool(p); |
1202 |
|
} |
1203 |
|
|
1204 |
|
/** |
1206 |
|
* given a value less the core pool size |
1207 |
|
*/ |
1208 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1209 |
< |
ThreadPoolExecutor p = |
1209 |
> |
final ThreadPoolExecutor p = |
1210 |
|
new ThreadPoolExecutor(2, 3, |
1211 |
|
LONG_DELAY_MS, MILLISECONDS, |
1212 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1213 |
< |
try { |
1214 |
< |
p.setMaximumPoolSize(1); |
1215 |
< |
shouldThrow(); |
1216 |
< |
} catch (IllegalArgumentException success) { |
1217 |
< |
} finally { |
1345 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1213 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1214 |
> |
try { |
1215 |
> |
p.setMaximumPoolSize(1); |
1216 |
> |
shouldThrow(); |
1217 |
> |
} catch (IllegalArgumentException success) {} |
1218 |
|
} |
1347 |
– |
joinPool(p); |
1219 |
|
} |
1220 |
|
|
1221 |
|
/** |
1223 |
|
* if given a negative value |
1224 |
|
*/ |
1225 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1226 |
< |
ThreadPoolExecutor p = |
1226 |
> |
final ThreadPoolExecutor p = |
1227 |
|
new ThreadPoolExecutor(2, 3, |
1228 |
|
LONG_DELAY_MS, MILLISECONDS, |
1229 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1230 |
< |
try { |
1231 |
< |
p.setMaximumPoolSize(-1); |
1232 |
< |
shouldThrow(); |
1233 |
< |
} catch (IllegalArgumentException success) { |
1234 |
< |
} finally { |
1364 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1230 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1231 |
> |
try { |
1232 |
> |
p.setMaximumPoolSize(-1); |
1233 |
> |
shouldThrow(); |
1234 |
> |
} catch (IllegalArgumentException success) {} |
1235 |
|
} |
1366 |
– |
joinPool(p); |
1236 |
|
} |
1237 |
|
|
1238 |
|
/** |
1240 |
|
* max pool size result in IllegalArgumentException. |
1241 |
|
*/ |
1242 |
|
public void testPoolSizeInvariants() { |
1243 |
< |
ThreadPoolExecutor p = |
1243 |
> |
final ThreadPoolExecutor p = |
1244 |
|
new ThreadPoolExecutor(1, 1, |
1245 |
|
LONG_DELAY_MS, MILLISECONDS, |
1246 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1247 |
< |
for (int s = 1; s < 5; s++) { |
1248 |
< |
p.setMaximumPoolSize(s); |
1249 |
< |
p.setCorePoolSize(s); |
1250 |
< |
try { |
1251 |
< |
p.setMaximumPoolSize(s - 1); |
1252 |
< |
shouldThrow(); |
1253 |
< |
} catch (IllegalArgumentException success) {} |
1254 |
< |
assertEquals(s, p.getCorePoolSize()); |
1255 |
< |
assertEquals(s, p.getMaximumPoolSize()); |
1256 |
< |
try { |
1257 |
< |
p.setCorePoolSize(s + 1); |
1258 |
< |
shouldThrow(); |
1259 |
< |
} catch (IllegalArgumentException success) {} |
1260 |
< |
assertEquals(s, p.getCorePoolSize()); |
1261 |
< |
assertEquals(s, p.getMaximumPoolSize()); |
1247 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1248 |
> |
for (int s = 1; s < 5; s++) { |
1249 |
> |
p.setMaximumPoolSize(s); |
1250 |
> |
p.setCorePoolSize(s); |
1251 |
> |
try { |
1252 |
> |
p.setMaximumPoolSize(s - 1); |
1253 |
> |
shouldThrow(); |
1254 |
> |
} catch (IllegalArgumentException success) {} |
1255 |
> |
assertEquals(s, p.getCorePoolSize()); |
1256 |
> |
assertEquals(s, p.getMaximumPoolSize()); |
1257 |
> |
try { |
1258 |
> |
p.setCorePoolSize(s + 1); |
1259 |
> |
shouldThrow(); |
1260 |
> |
} catch (IllegalArgumentException success) {} |
1261 |
> |
assertEquals(s, p.getCorePoolSize()); |
1262 |
> |
assertEquals(s, p.getMaximumPoolSize()); |
1263 |
> |
} |
1264 |
|
} |
1394 |
– |
joinPool(p); |
1265 |
|
} |
1266 |
|
|
1267 |
|
/** |
1269 |
|
* when given a negative value |
1270 |
|
*/ |
1271 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1272 |
< |
ThreadPoolExecutor p = |
1272 |
> |
final ThreadPoolExecutor p = |
1273 |
|
new ThreadPoolExecutor(2, 3, |
1274 |
|
LONG_DELAY_MS, MILLISECONDS, |
1275 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1276 |
< |
try { |
1277 |
< |
p.setKeepAliveTime(-1,MILLISECONDS); |
1278 |
< |
shouldThrow(); |
1279 |
< |
} catch (IllegalArgumentException success) { |
1280 |
< |
} finally { |
1411 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1276 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1277 |
> |
try { |
1278 |
> |
p.setKeepAliveTime(-1, MILLISECONDS); |
1279 |
> |
shouldThrow(); |
1280 |
> |
} catch (IllegalArgumentException success) {} |
1281 |
|
} |
1413 |
– |
joinPool(p); |
1282 |
|
} |
1283 |
|
|
1284 |
|
/** |
1286 |
|
*/ |
1287 |
|
public void testTerminated() { |
1288 |
|
ExtendedTPE p = new ExtendedTPE(); |
1289 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1290 |
< |
assertTrue(p.terminatedCalled()); |
1291 |
< |
joinPool(p); |
1289 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1290 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1291 |
> |
assertTrue(p.terminatedCalled()); |
1292 |
> |
assertTrue(p.isShutdown()); |
1293 |
> |
} |
1294 |
|
} |
1295 |
|
|
1296 |
|
/** |
1298 |
|
*/ |
1299 |
|
public void testBeforeAfter() throws InterruptedException { |
1300 |
|
ExtendedTPE p = new ExtendedTPE(); |
1301 |
< |
try { |
1301 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1302 |
|
final CountDownLatch done = new CountDownLatch(1); |
1303 |
|
p.execute(new CheckedRunnable() { |
1304 |
|
public void realRun() { |
1308 |
|
assertEquals(0, done.getCount()); |
1309 |
|
assertTrue(p.afterCalled()); |
1310 |
|
assertTrue(p.beforeCalled()); |
1441 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1442 |
– |
} finally { |
1443 |
– |
joinPool(p); |
1311 |
|
} |
1312 |
|
} |
1313 |
|
|
1315 |
|
* completed submit of callable returns result |
1316 |
|
*/ |
1317 |
|
public void testSubmitCallable() throws Exception { |
1318 |
< |
ExecutorService e = |
1318 |
> |
final ExecutorService e = |
1319 |
|
new ThreadPoolExecutor(2, 2, |
1320 |
|
LONG_DELAY_MS, MILLISECONDS, |
1321 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1322 |
< |
try { |
1322 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1323 |
|
Future<String> future = e.submit(new StringTask()); |
1324 |
|
String result = future.get(); |
1325 |
|
assertSame(TEST_STRING, result); |
1459 |
– |
} finally { |
1460 |
– |
joinPool(e); |
1326 |
|
} |
1327 |
|
} |
1328 |
|
|
1330 |
|
* completed submit of runnable returns successfully |
1331 |
|
*/ |
1332 |
|
public void testSubmitRunnable() throws Exception { |
1333 |
< |
ExecutorService e = |
1333 |
> |
final ExecutorService e = |
1334 |
|
new ThreadPoolExecutor(2, 2, |
1335 |
|
LONG_DELAY_MS, MILLISECONDS, |
1336 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1337 |
< |
try { |
1337 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1338 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1339 |
|
future.get(); |
1340 |
|
assertTrue(future.isDone()); |
1476 |
– |
} finally { |
1477 |
– |
joinPool(e); |
1341 |
|
} |
1342 |
|
} |
1343 |
|
|
1345 |
|
* completed submit of (runnable, result) returns result |
1346 |
|
*/ |
1347 |
|
public void testSubmitRunnable2() throws Exception { |
1348 |
< |
ExecutorService e = |
1348 |
> |
final ExecutorService e = |
1349 |
|
new ThreadPoolExecutor(2, 2, |
1350 |
|
LONG_DELAY_MS, MILLISECONDS, |
1351 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1352 |
< |
try { |
1352 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1353 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1354 |
|
String result = future.get(); |
1355 |
|
assertSame(TEST_STRING, result); |
1493 |
– |
} finally { |
1494 |
– |
joinPool(e); |
1356 |
|
} |
1357 |
|
} |
1358 |
|
|
1360 |
|
* invokeAny(null) throws NPE |
1361 |
|
*/ |
1362 |
|
public void testInvokeAny1() throws Exception { |
1363 |
< |
ExecutorService e = |
1363 |
> |
final ExecutorService e = |
1364 |
|
new ThreadPoolExecutor(2, 2, |
1365 |
|
LONG_DELAY_MS, MILLISECONDS, |
1366 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1367 |
< |
try { |
1368 |
< |
e.invokeAny(null); |
1369 |
< |
shouldThrow(); |
1370 |
< |
} catch (NullPointerException success) { |
1371 |
< |
} finally { |
1511 |
< |
joinPool(e); |
1367 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1368 |
> |
try { |
1369 |
> |
e.invokeAny(null); |
1370 |
> |
shouldThrow(); |
1371 |
> |
} catch (NullPointerException success) {} |
1372 |
|
} |
1373 |
|
} |
1374 |
|
|
1375 |
|
/** |
1376 |
< |
* invokeAny(empty collection) throws IAE |
1376 |
> |
* invokeAny(empty collection) throws IllegalArgumentException |
1377 |
|
*/ |
1378 |
|
public void testInvokeAny2() throws Exception { |
1379 |
< |
ExecutorService e = |
1379 |
> |
final ExecutorService e = |
1380 |
|
new ThreadPoolExecutor(2, 2, |
1381 |
|
LONG_DELAY_MS, MILLISECONDS, |
1382 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1383 |
< |
try { |
1384 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
1385 |
< |
shouldThrow(); |
1386 |
< |
} catch (IllegalArgumentException success) { |
1387 |
< |
} finally { |
1528 |
< |
joinPool(e); |
1383 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1384 |
> |
try { |
1385 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
1386 |
> |
shouldThrow(); |
1387 |
> |
} catch (IllegalArgumentException success) {} |
1388 |
|
} |
1389 |
|
} |
1390 |
|
|
1397 |
|
new ThreadPoolExecutor(2, 2, |
1398 |
|
LONG_DELAY_MS, MILLISECONDS, |
1399 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1400 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1401 |
< |
l.add(latchAwaitingStringTask(latch)); |
1402 |
< |
l.add(null); |
1403 |
< |
try { |
1404 |
< |
e.invokeAny(l); |
1405 |
< |
shouldThrow(); |
1406 |
< |
} catch (NullPointerException success) { |
1407 |
< |
} finally { |
1400 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1401 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1402 |
> |
l.add(latchAwaitingStringTask(latch)); |
1403 |
> |
l.add(null); |
1404 |
> |
try { |
1405 |
> |
e.invokeAny(l); |
1406 |
> |
shouldThrow(); |
1407 |
> |
} catch (NullPointerException success) {} |
1408 |
|
latch.countDown(); |
1550 |
– |
joinPool(e); |
1409 |
|
} |
1410 |
|
} |
1411 |
|
|
1413 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1414 |
|
*/ |
1415 |
|
public void testInvokeAny4() throws Exception { |
1416 |
< |
ExecutorService e = |
1416 |
> |
final ExecutorService e = |
1417 |
|
new ThreadPoolExecutor(2, 2, |
1418 |
|
LONG_DELAY_MS, MILLISECONDS, |
1419 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1420 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1421 |
< |
l.add(new NPETask()); |
1422 |
< |
try { |
1423 |
< |
e.invokeAny(l); |
1424 |
< |
shouldThrow(); |
1425 |
< |
} catch (ExecutionException success) { |
1426 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1427 |
< |
} finally { |
1428 |
< |
joinPool(e); |
1420 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1421 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1422 |
> |
l.add(new NPETask()); |
1423 |
> |
try { |
1424 |
> |
e.invokeAny(l); |
1425 |
> |
shouldThrow(); |
1426 |
> |
} catch (ExecutionException success) { |
1427 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1428 |
> |
} |
1429 |
|
} |
1430 |
|
} |
1431 |
|
|
1433 |
|
* invokeAny(c) returns result of some task |
1434 |
|
*/ |
1435 |
|
public void testInvokeAny5() throws Exception { |
1436 |
< |
ExecutorService e = |
1436 |
> |
final ExecutorService e = |
1437 |
|
new ThreadPoolExecutor(2, 2, |
1438 |
|
LONG_DELAY_MS, MILLISECONDS, |
1439 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1440 |
< |
try { |
1441 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1440 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1441 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1442 |
|
l.add(new StringTask()); |
1443 |
|
l.add(new StringTask()); |
1444 |
|
String result = e.invokeAny(l); |
1445 |
|
assertSame(TEST_STRING, result); |
1588 |
– |
} finally { |
1589 |
– |
joinPool(e); |
1446 |
|
} |
1447 |
|
} |
1448 |
|
|
1450 |
|
* invokeAll(null) throws NPE |
1451 |
|
*/ |
1452 |
|
public void testInvokeAll1() throws Exception { |
1453 |
< |
ExecutorService e = |
1453 |
> |
final ExecutorService e = |
1454 |
|
new ThreadPoolExecutor(2, 2, |
1455 |
|
LONG_DELAY_MS, MILLISECONDS, |
1456 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1457 |
< |
try { |
1458 |
< |
e.invokeAll(null); |
1459 |
< |
shouldThrow(); |
1460 |
< |
} catch (NullPointerException success) { |
1461 |
< |
} finally { |
1606 |
< |
joinPool(e); |
1457 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1458 |
> |
try { |
1459 |
> |
e.invokeAll(null); |
1460 |
> |
shouldThrow(); |
1461 |
> |
} catch (NullPointerException success) {} |
1462 |
|
} |
1463 |
|
} |
1464 |
|
|
1465 |
|
/** |
1466 |
< |
* invokeAll(empty collection) returns empty collection |
1466 |
> |
* invokeAll(empty collection) returns empty list |
1467 |
|
*/ |
1468 |
|
public void testInvokeAll2() throws InterruptedException { |
1469 |
< |
ExecutorService e = |
1469 |
> |
final ExecutorService e = |
1470 |
|
new ThreadPoolExecutor(2, 2, |
1471 |
|
LONG_DELAY_MS, MILLISECONDS, |
1472 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1473 |
< |
try { |
1474 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1473 |
> |
final Collection<Callable<String>> emptyCollection |
1474 |
> |
= Collections.emptyList(); |
1475 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1476 |
> |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1477 |
|
assertTrue(r.isEmpty()); |
1621 |
– |
} finally { |
1622 |
– |
joinPool(e); |
1478 |
|
} |
1479 |
|
} |
1480 |
|
|
1482 |
|
* invokeAll(c) throws NPE if c has null elements |
1483 |
|
*/ |
1484 |
|
public void testInvokeAll3() throws Exception { |
1485 |
< |
ExecutorService e = |
1485 |
> |
final ExecutorService e = |
1486 |
|
new ThreadPoolExecutor(2, 2, |
1487 |
|
LONG_DELAY_MS, MILLISECONDS, |
1488 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1489 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1490 |
< |
l.add(new StringTask()); |
1491 |
< |
l.add(null); |
1492 |
< |
try { |
1493 |
< |
e.invokeAll(l); |
1494 |
< |
shouldThrow(); |
1495 |
< |
} catch (NullPointerException success) { |
1496 |
< |
} finally { |
1642 |
< |
joinPool(e); |
1489 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1490 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1491 |
> |
l.add(new StringTask()); |
1492 |
> |
l.add(null); |
1493 |
> |
try { |
1494 |
> |
e.invokeAll(l); |
1495 |
> |
shouldThrow(); |
1496 |
> |
} catch (NullPointerException success) {} |
1497 |
|
} |
1498 |
|
} |
1499 |
|
|
1501 |
|
* get of element of invokeAll(c) throws exception on failed task |
1502 |
|
*/ |
1503 |
|
public void testInvokeAll4() throws Exception { |
1504 |
< |
ExecutorService e = |
1504 |
> |
final ExecutorService e = |
1505 |
|
new ThreadPoolExecutor(2, 2, |
1506 |
|
LONG_DELAY_MS, MILLISECONDS, |
1507 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1508 |
< |
try { |
1509 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1508 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1509 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1510 |
|
l.add(new NPETask()); |
1511 |
|
List<Future<String>> futures = e.invokeAll(l); |
1512 |
|
assertEquals(1, futures.size()); |
1516 |
|
} catch (ExecutionException success) { |
1517 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1518 |
|
} |
1665 |
– |
} finally { |
1666 |
– |
joinPool(e); |
1519 |
|
} |
1520 |
|
} |
1521 |
|
|
1523 |
|
* invokeAll(c) returns results of all completed tasks |
1524 |
|
*/ |
1525 |
|
public void testInvokeAll5() throws Exception { |
1526 |
< |
ExecutorService e = |
1526 |
> |
final ExecutorService e = |
1527 |
|
new ThreadPoolExecutor(2, 2, |
1528 |
|
LONG_DELAY_MS, MILLISECONDS, |
1529 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1530 |
< |
try { |
1531 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1530 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1531 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1532 |
|
l.add(new StringTask()); |
1533 |
|
l.add(new StringTask()); |
1534 |
|
List<Future<String>> futures = e.invokeAll(l); |
1535 |
|
assertEquals(2, futures.size()); |
1536 |
|
for (Future<String> future : futures) |
1537 |
|
assertSame(TEST_STRING, future.get()); |
1686 |
– |
} finally { |
1687 |
– |
joinPool(e); |
1538 |
|
} |
1539 |
|
} |
1540 |
|
|
1542 |
|
* timed invokeAny(null) throws NPE |
1543 |
|
*/ |
1544 |
|
public void testTimedInvokeAny1() throws Exception { |
1545 |
< |
ExecutorService e = |
1545 |
> |
final ExecutorService e = |
1546 |
|
new ThreadPoolExecutor(2, 2, |
1547 |
|
LONG_DELAY_MS, MILLISECONDS, |
1548 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1549 |
< |
try { |
1550 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1551 |
< |
shouldThrow(); |
1552 |
< |
} catch (NullPointerException success) { |
1553 |
< |
} finally { |
1704 |
< |
joinPool(e); |
1549 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1550 |
> |
try { |
1551 |
> |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1552 |
> |
shouldThrow(); |
1553 |
> |
} catch (NullPointerException success) {} |
1554 |
|
} |
1555 |
|
} |
1556 |
|
|
1558 |
|
* timed invokeAny(,,null) throws NPE |
1559 |
|
*/ |
1560 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1561 |
< |
ExecutorService e = |
1561 |
> |
final ExecutorService e = |
1562 |
|
new ThreadPoolExecutor(2, 2, |
1563 |
|
LONG_DELAY_MS, MILLISECONDS, |
1564 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1565 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1566 |
< |
l.add(new StringTask()); |
1567 |
< |
try { |
1568 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1569 |
< |
shouldThrow(); |
1570 |
< |
} catch (NullPointerException success) { |
1571 |
< |
} finally { |
1723 |
< |
joinPool(e); |
1565 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1566 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1567 |
> |
l.add(new StringTask()); |
1568 |
> |
try { |
1569 |
> |
e.invokeAny(l, randomTimeout(), null); |
1570 |
> |
shouldThrow(); |
1571 |
> |
} catch (NullPointerException success) {} |
1572 |
|
} |
1573 |
|
} |
1574 |
|
|
1575 |
|
/** |
1576 |
< |
* timed invokeAny(empty collection) throws IAE |
1576 |
> |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1577 |
|
*/ |
1578 |
|
public void testTimedInvokeAny2() throws Exception { |
1579 |
< |
ExecutorService e = |
1579 |
> |
final ExecutorService e = |
1580 |
|
new ThreadPoolExecutor(2, 2, |
1581 |
|
LONG_DELAY_MS, MILLISECONDS, |
1582 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1583 |
< |
try { |
1584 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1585 |
< |
shouldThrow(); |
1586 |
< |
} catch (IllegalArgumentException success) { |
1587 |
< |
} finally { |
1588 |
< |
joinPool(e); |
1583 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1584 |
> |
try { |
1585 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
1586 |
> |
randomTimeout(), randomTimeUnit()); |
1587 |
> |
shouldThrow(); |
1588 |
> |
} catch (IllegalArgumentException success) {} |
1589 |
|
} |
1590 |
|
} |
1591 |
|
|
1592 |
|
/** |
1593 |
< |
* timed invokeAny(c) throws NPE if c has null elements |
1593 |
> |
* timed invokeAny(c) throws NullPointerException if c has null elements |
1594 |
|
*/ |
1595 |
|
public void testTimedInvokeAny3() throws Exception { |
1596 |
|
final CountDownLatch latch = new CountDownLatch(1); |
1598 |
|
new ThreadPoolExecutor(2, 2, |
1599 |
|
LONG_DELAY_MS, MILLISECONDS, |
1600 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1601 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1602 |
< |
l.add(latchAwaitingStringTask(latch)); |
1603 |
< |
l.add(null); |
1604 |
< |
try { |
1605 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1606 |
< |
shouldThrow(); |
1607 |
< |
} catch (NullPointerException success) { |
1608 |
< |
} finally { |
1601 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1602 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1603 |
> |
l.add(latchAwaitingStringTask(latch)); |
1604 |
> |
l.add(null); |
1605 |
> |
try { |
1606 |
> |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
1607 |
> |
shouldThrow(); |
1608 |
> |
} catch (NullPointerException success) {} |
1609 |
|
latch.countDown(); |
1762 |
– |
joinPool(e); |
1610 |
|
} |
1611 |
|
} |
1612 |
|
|
1614 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1615 |
|
*/ |
1616 |
|
public void testTimedInvokeAny4() throws Exception { |
1617 |
< |
ExecutorService e = |
1617 |
> |
final ExecutorService e = |
1618 |
|
new ThreadPoolExecutor(2, 2, |
1619 |
|
LONG_DELAY_MS, MILLISECONDS, |
1620 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1621 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1622 |
< |
l.add(new NPETask()); |
1623 |
< |
try { |
1624 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1625 |
< |
shouldThrow(); |
1626 |
< |
} catch (ExecutionException success) { |
1627 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1628 |
< |
} finally { |
1629 |
< |
joinPool(e); |
1621 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1622 |
> |
long startTime = System.nanoTime(); |
1623 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1624 |
> |
l.add(new NPETask()); |
1625 |
> |
try { |
1626 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1627 |
> |
shouldThrow(); |
1628 |
> |
} catch (ExecutionException success) { |
1629 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1630 |
> |
} |
1631 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1632 |
|
} |
1633 |
|
} |
1634 |
|
|
1636 |
|
* timed invokeAny(c) returns result of some task |
1637 |
|
*/ |
1638 |
|
public void testTimedInvokeAny5() throws Exception { |
1639 |
< |
ExecutorService e = |
1639 |
> |
final ExecutorService e = |
1640 |
|
new ThreadPoolExecutor(2, 2, |
1641 |
|
LONG_DELAY_MS, MILLISECONDS, |
1642 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1643 |
< |
try { |
1644 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1643 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1644 |
> |
long startTime = System.nanoTime(); |
1645 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1646 |
|
l.add(new StringTask()); |
1647 |
|
l.add(new StringTask()); |
1648 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1648 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1649 |
|
assertSame(TEST_STRING, result); |
1650 |
< |
} finally { |
1801 |
< |
joinPool(e); |
1650 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1651 |
|
} |
1652 |
|
} |
1653 |
|
|
1655 |
|
* timed invokeAll(null) throws NPE |
1656 |
|
*/ |
1657 |
|
public void testTimedInvokeAll1() throws Exception { |
1658 |
< |
ExecutorService e = |
1658 |
> |
final ExecutorService e = |
1659 |
|
new ThreadPoolExecutor(2, 2, |
1660 |
|
LONG_DELAY_MS, MILLISECONDS, |
1661 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1662 |
< |
try { |
1663 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1664 |
< |
shouldThrow(); |
1665 |
< |
} catch (NullPointerException success) { |
1666 |
< |
} finally { |
1818 |
< |
joinPool(e); |
1662 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1663 |
> |
try { |
1664 |
> |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1665 |
> |
shouldThrow(); |
1666 |
> |
} catch (NullPointerException success) {} |
1667 |
|
} |
1668 |
|
} |
1669 |
|
|
1671 |
|
* timed invokeAll(,,null) throws NPE |
1672 |
|
*/ |
1673 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1674 |
< |
ExecutorService e = |
1674 |
> |
final ExecutorService e = |
1675 |
|
new ThreadPoolExecutor(2, 2, |
1676 |
|
LONG_DELAY_MS, MILLISECONDS, |
1677 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1678 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1679 |
< |
l.add(new StringTask()); |
1680 |
< |
try { |
1681 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1682 |
< |
shouldThrow(); |
1683 |
< |
} catch (NullPointerException success) { |
1684 |
< |
} finally { |
1837 |
< |
joinPool(e); |
1678 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1679 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1680 |
> |
l.add(new StringTask()); |
1681 |
> |
try { |
1682 |
> |
e.invokeAll(l, randomTimeout(), null); |
1683 |
> |
shouldThrow(); |
1684 |
> |
} catch (NullPointerException success) {} |
1685 |
|
} |
1686 |
|
} |
1687 |
|
|
1688 |
|
/** |
1689 |
< |
* timed invokeAll(empty collection) returns empty collection |
1689 |
> |
* timed invokeAll(empty collection) returns empty list |
1690 |
|
*/ |
1691 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1692 |
< |
ExecutorService e = |
1692 |
> |
final ExecutorService e = |
1693 |
|
new ThreadPoolExecutor(2, 2, |
1694 |
|
LONG_DELAY_MS, MILLISECONDS, |
1695 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1696 |
< |
try { |
1697 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1696 |
> |
final Collection<Callable<String>> emptyCollection |
1697 |
> |
= Collections.emptyList(); |
1698 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1699 |
> |
List<Future<String>> r = |
1700 |
> |
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1701 |
|
assertTrue(r.isEmpty()); |
1852 |
– |
} finally { |
1853 |
– |
joinPool(e); |
1702 |
|
} |
1703 |
|
} |
1704 |
|
|
1706 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1707 |
|
*/ |
1708 |
|
public void testTimedInvokeAll3() throws Exception { |
1709 |
< |
ExecutorService e = |
1709 |
> |
final ExecutorService e = |
1710 |
|
new ThreadPoolExecutor(2, 2, |
1711 |
|
LONG_DELAY_MS, MILLISECONDS, |
1712 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1713 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1714 |
< |
l.add(new StringTask()); |
1715 |
< |
l.add(null); |
1716 |
< |
try { |
1717 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1718 |
< |
shouldThrow(); |
1719 |
< |
} catch (NullPointerException success) { |
1720 |
< |
} finally { |
1873 |
< |
joinPool(e); |
1713 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1714 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1715 |
> |
l.add(new StringTask()); |
1716 |
> |
l.add(null); |
1717 |
> |
try { |
1718 |
> |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1719 |
> |
shouldThrow(); |
1720 |
> |
} catch (NullPointerException success) {} |
1721 |
|
} |
1722 |
|
} |
1723 |
|
|
1725 |
|
* get of element of invokeAll(c) throws exception on failed task |
1726 |
|
*/ |
1727 |
|
public void testTimedInvokeAll4() throws Exception { |
1728 |
< |
ExecutorService e = |
1728 |
> |
final ExecutorService e = |
1729 |
|
new ThreadPoolExecutor(2, 2, |
1730 |
|
LONG_DELAY_MS, MILLISECONDS, |
1731 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1732 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1733 |
< |
l.add(new NPETask()); |
1734 |
< |
List<Future<String>> futures = |
1735 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1736 |
< |
assertEquals(1, futures.size()); |
1737 |
< |
try { |
1738 |
< |
futures.get(0).get(); |
1739 |
< |
shouldThrow(); |
1740 |
< |
} catch (ExecutionException success) { |
1741 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1742 |
< |
} finally { |
1743 |
< |
joinPool(e); |
1732 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1733 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1734 |
> |
l.add(new NPETask()); |
1735 |
> |
List<Future<String>> futures = |
1736 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1737 |
> |
assertEquals(1, futures.size()); |
1738 |
> |
try { |
1739 |
> |
futures.get(0).get(); |
1740 |
> |
shouldThrow(); |
1741 |
> |
} catch (ExecutionException success) { |
1742 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1743 |
> |
} |
1744 |
|
} |
1745 |
|
} |
1746 |
|
|
1748 |
|
* timed invokeAll(c) returns results of all completed tasks |
1749 |
|
*/ |
1750 |
|
public void testTimedInvokeAll5() throws Exception { |
1751 |
< |
ExecutorService e = |
1751 |
> |
final ExecutorService e = |
1752 |
|
new ThreadPoolExecutor(2, 2, |
1753 |
|
LONG_DELAY_MS, MILLISECONDS, |
1754 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1755 |
< |
try { |
1756 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1755 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1756 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1757 |
|
l.add(new StringTask()); |
1758 |
|
l.add(new StringTask()); |
1759 |
|
List<Future<String>> futures = |
1761 |
|
assertEquals(2, futures.size()); |
1762 |
|
for (Future<String> future : futures) |
1763 |
|
assertSame(TEST_STRING, future.get()); |
1917 |
– |
} finally { |
1918 |
– |
joinPool(e); |
1764 |
|
} |
1765 |
|
} |
1766 |
|
|
1768 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1769 |
|
*/ |
1770 |
|
public void testTimedInvokeAll6() throws Exception { |
1771 |
< |
ExecutorService e = |
1772 |
< |
new ThreadPoolExecutor(2, 2, |
1773 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1774 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1775 |
< |
try { |
1776 |
< |
for (long timeout = timeoutMillis();;) { |
1771 |
> |
for (long timeout = timeoutMillis();;) { |
1772 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1773 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1774 |
> |
public String realCall() { |
1775 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1776 |
> |
catch (InterruptedException ok) {} |
1777 |
> |
return "1"; }}; |
1778 |
> |
final ExecutorService p = |
1779 |
> |
new ThreadPoolExecutor(2, 2, |
1780 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1781 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1782 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1783 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1784 |
|
tasks.add(new StringTask("0")); |
1785 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1785 |
> |
tasks.add(waiter); |
1786 |
|
tasks.add(new StringTask("2")); |
1787 |
|
long startTime = System.nanoTime(); |
1788 |
|
List<Future<String>> futures = |
1789 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1789 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1790 |
|
assertEquals(tasks.size(), futures.size()); |
1791 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1792 |
|
for (Future future : futures) |
1802 |
|
fail("expected exactly one task to be cancelled"); |
1803 |
|
} |
1804 |
|
} |
1954 |
– |
} finally { |
1955 |
– |
joinPool(e); |
1805 |
|
} |
1806 |
|
} |
1807 |
|
|
1815 |
|
LONG_DELAY_MS, MILLISECONDS, |
1816 |
|
new LinkedBlockingQueue<Runnable>(), |
1817 |
|
new FailingThreadFactory()); |
1818 |
< |
try { |
1818 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1819 |
|
final int TASKS = 100; |
1820 |
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1821 |
|
for (int k = 0; k < TASKS; ++k) |
1823 |
|
public void realRun() { |
1824 |
|
done.countDown(); |
1825 |
|
}}); |
1826 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1978 |
< |
} finally { |
1979 |
< |
joinPool(e); |
1826 |
> |
await(done); |
1827 |
|
} |
1828 |
|
} |
1829 |
|
|
1835 |
|
new ThreadPoolExecutor(2, 2, |
1836 |
|
1000, MILLISECONDS, |
1837 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1838 |
< |
assertFalse(p.allowsCoreThreadTimeOut()); |
1839 |
< |
joinPool(p); |
1838 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1839 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1840 |
> |
} |
1841 |
|
} |
1842 |
|
|
1843 |
|
/** |
1849 |
|
new ThreadPoolExecutor(2, 10, |
1850 |
|
keepAliveTime, MILLISECONDS, |
1851 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1852 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1853 |
< |
try { |
1852 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1853 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1854 |
|
p.allowCoreThreadTimeOut(true); |
1855 |
|
p.execute(new CheckedRunnable() { |
1856 |
|
public void realRun() { |
1865 |
|
Thread.yield(); |
1866 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1867 |
|
assertEquals(0, p.getPoolSize()); |
2020 |
– |
} finally { |
2021 |
– |
joinPool(p); |
1868 |
|
} |
1869 |
|
} |
1870 |
|
|
1877 |
|
new ThreadPoolExecutor(2, 10, |
1878 |
|
keepAliveTime, MILLISECONDS, |
1879 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1880 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1881 |
< |
try { |
1880 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1881 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1882 |
|
p.allowCoreThreadTimeOut(false); |
1883 |
|
p.execute(new CheckedRunnable() { |
1884 |
|
public void realRun() throws InterruptedException { |
1887 |
|
}}); |
1888 |
|
delay(2 * keepAliveTime); |
1889 |
|
assertTrue(p.getPoolSize() >= 1); |
2044 |
– |
} finally { |
2045 |
– |
joinPool(p); |
1890 |
|
} |
1891 |
|
} |
1892 |
|
|
1905 |
|
new ThreadPoolExecutor(1, 30, |
1906 |
|
60, SECONDS, |
1907 |
|
new ArrayBlockingQueue(30)); |
1908 |
< |
try { |
1908 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1909 |
|
for (int i = 0; i < nTasks; ++i) { |
1910 |
|
for (;;) { |
1911 |
|
try { |
1916 |
|
} |
1917 |
|
} |
1918 |
|
// enough time to run all tasks |
1919 |
< |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2076 |
< |
} finally { |
2077 |
< |
joinPool(p); |
1919 |
> |
await(done, nTasks * SHORT_DELAY_MS); |
1920 |
|
} |
1921 |
|
} |
1922 |
|
|
1924 |
|
* get(cancelled task) throws CancellationException |
1925 |
|
*/ |
1926 |
|
public void testGet_cancelled() throws Exception { |
1927 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1928 |
|
final ExecutorService e = |
1929 |
|
new ThreadPoolExecutor(1, 1, |
1930 |
|
LONG_DELAY_MS, MILLISECONDS, |
1931 |
|
new LinkedBlockingQueue<Runnable>()); |
1932 |
< |
try { |
1932 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
1933 |
|
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2091 |
– |
final CountDownLatch done = new CountDownLatch(1); |
1934 |
|
final List<Future<?>> futures = new ArrayList<>(); |
1935 |
|
for (int i = 0; i < 2; i++) { |
1936 |
|
Runnable r = new CheckedRunnable() { public void realRun() |
1940 |
|
}}; |
1941 |
|
futures.add(e.submit(r)); |
1942 |
|
} |
1943 |
< |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1943 |
> |
await(blockerStarted); |
1944 |
|
for (Future<?> future : futures) future.cancel(false); |
1945 |
|
for (Future<?> future : futures) { |
1946 |
|
try { |
1954 |
|
assertTrue(future.isCancelled()); |
1955 |
|
assertTrue(future.isDone()); |
1956 |
|
} |
2115 |
– |
done.countDown(); |
2116 |
– |
} finally { |
2117 |
– |
joinPool(e); |
1957 |
|
} |
1958 |
|
} |
1959 |
|
|
1960 |
+ |
/** Directly test simple ThreadPoolExecutor RejectedExecutionHandlers. */ |
1961 |
+ |
public void testStandardRejectedExecutionHandlers() { |
1962 |
+ |
final ThreadPoolExecutor p = |
1963 |
+ |
new ThreadPoolExecutor(1, 1, 1, SECONDS, |
1964 |
+ |
new ArrayBlockingQueue<Runnable>(1)); |
1965 |
+ |
final AtomicReference<Thread> thread = new AtomicReference<>(); |
1966 |
+ |
final Runnable r = new Runnable() { public void run() { |
1967 |
+ |
thread.set(Thread.currentThread()); }}; |
1968 |
+ |
|
1969 |
+ |
try { |
1970 |
+ |
new AbortPolicy().rejectedExecution(r, p); |
1971 |
+ |
shouldThrow(); |
1972 |
+ |
} catch (RejectedExecutionException success) {} |
1973 |
+ |
assertNull(thread.get()); |
1974 |
+ |
|
1975 |
+ |
new DiscardPolicy().rejectedExecution(r, p); |
1976 |
+ |
assertNull(thread.get()); |
1977 |
+ |
|
1978 |
+ |
new CallerRunsPolicy().rejectedExecution(r, p); |
1979 |
+ |
assertSame(Thread.currentThread(), thread.get()); |
1980 |
+ |
|
1981 |
+ |
// check that pool was not perturbed by handlers |
1982 |
+ |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
1983 |
+ |
assertEquals(0, p.getTaskCount()); |
1984 |
+ |
assertTrue(p.getQueue().isEmpty()); |
1985 |
+ |
} |
1986 |
+ |
|
1987 |
|
} |