252 |
|
new CustomTPE(2, 2, |
253 |
|
LONG_DELAY_MS, MILLISECONDS, |
254 |
|
new ArrayBlockingQueue<Runnable>(10)); |
255 |
– |
final CountDownLatch threadStarted = new CountDownLatch(1); |
256 |
– |
final CountDownLatch done = new CountDownLatch(1); |
255 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
256 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
257 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
258 |
|
assertEquals(0, p.getActiveCount()); |
259 |
|
p.execute(new CheckedRunnable() { |
260 |
|
public void realRun() throws InterruptedException { |
262 |
|
assertEquals(1, p.getActiveCount()); |
263 |
|
done.await(); |
264 |
|
}}); |
265 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
265 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
266 |
|
assertEquals(1, p.getActiveCount()); |
267 |
|
done.countDown(); |
268 |
|
} |
272 |
|
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
273 |
|
*/ |
274 |
|
public void testPrestartCoreThread() { |
275 |
< |
ThreadPoolExecutor p = |
275 |
> |
final ThreadPoolExecutor p = |
276 |
|
new CustomTPE(2, 6, |
277 |
|
LONG_DELAY_MS, MILLISECONDS, |
278 |
|
new ArrayBlockingQueue<Runnable>(10)); |
298 |
|
* prestartAllCoreThreads starts all corePoolSize threads |
299 |
|
*/ |
300 |
|
public void testPrestartAllCoreThreads() { |
301 |
< |
ThreadPoolExecutor p = |
301 |
> |
final ThreadPoolExecutor p = |
302 |
|
new CustomTPE(2, 6, |
303 |
|
LONG_DELAY_MS, MILLISECONDS, |
304 |
|
new ArrayBlockingQueue<Runnable>(10)); |
354 |
|
* getCorePoolSize returns size given in constructor if not otherwise set |
355 |
|
*/ |
356 |
|
public void testGetCorePoolSize() { |
357 |
< |
ThreadPoolExecutor p = |
357 |
> |
final ThreadPoolExecutor p = |
358 |
|
new CustomTPE(1, 1, |
359 |
|
LONG_DELAY_MS, MILLISECONDS, |
360 |
|
new ArrayBlockingQueue<Runnable>(10)); |
367 |
|
* getKeepAliveTime returns value given in constructor if not otherwise set |
368 |
|
*/ |
369 |
|
public void testGetKeepAliveTime() { |
370 |
< |
ThreadPoolExecutor p = |
370 |
> |
final ThreadPoolExecutor p = |
371 |
|
new CustomTPE(2, 2, |
372 |
|
1000, MILLISECONDS, |
373 |
|
new ArrayBlockingQueue<Runnable>(10)); |
380 |
|
* getThreadFactory returns factory in constructor if not set |
381 |
|
*/ |
382 |
|
public void testGetThreadFactory() { |
383 |
< |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
384 |
< |
ThreadPoolExecutor p = |
383 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
384 |
> |
final ThreadPoolExecutor p = |
385 |
|
new CustomTPE(1, 2, |
386 |
|
LONG_DELAY_MS, MILLISECONDS, |
387 |
|
new ArrayBlockingQueue<Runnable>(10), |
396 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
397 |
|
*/ |
398 |
|
public void testSetThreadFactory() { |
399 |
< |
ThreadPoolExecutor p = |
399 |
> |
final ThreadPoolExecutor p = |
400 |
|
new CustomTPE(1, 2, |
401 |
|
LONG_DELAY_MS, MILLISECONDS, |
402 |
|
new ArrayBlockingQueue<Runnable>(10)); |
411 |
|
* setThreadFactory(null) throws NPE |
412 |
|
*/ |
413 |
|
public void testSetThreadFactoryNull() { |
414 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
415 |
< |
try { |
416 |
< |
p.setThreadFactory(null); |
417 |
< |
shouldThrow(); |
418 |
< |
} catch (NullPointerException success) { |
419 |
< |
} finally { |
420 |
< |
joinPool(p); |
414 |
> |
final ThreadPoolExecutor p = |
415 |
> |
new CustomTPE(1, 2, |
416 |
> |
LONG_DELAY_MS, MILLISECONDS, |
417 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
418 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
419 |
> |
try { |
420 |
> |
p.setThreadFactory(null); |
421 |
> |
shouldThrow(); |
422 |
> |
} catch (NullPointerException success) {} |
423 |
|
} |
424 |
|
} |
425 |
|
|
427 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
428 |
|
*/ |
429 |
|
public void testGetRejectedExecutionHandler() { |
430 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
431 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
432 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
433 |
< |
joinPool(p); |
430 |
> |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
431 |
> |
final ThreadPoolExecutor p = |
432 |
> |
new CustomTPE(1, 2, |
433 |
> |
LONG_DELAY_MS, MILLISECONDS, |
434 |
> |
new ArrayBlockingQueue<Runnable>(10), |
435 |
> |
handler); |
436 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
437 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
438 |
> |
} |
439 |
|
} |
440 |
|
|
441 |
|
/** |
443 |
|
* getRejectedExecutionHandler |
444 |
|
*/ |
445 |
|
public void testSetRejectedExecutionHandler() { |
446 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
447 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
448 |
< |
p.setRejectedExecutionHandler(h); |
449 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
450 |
< |
joinPool(p); |
446 |
> |
final ThreadPoolExecutor p = |
447 |
> |
new CustomTPE(1, 2, |
448 |
> |
LONG_DELAY_MS, MILLISECONDS, |
449 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
450 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
451 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
452 |
> |
p.setRejectedExecutionHandler(handler); |
453 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
454 |
> |
} |
455 |
|
} |
456 |
|
|
457 |
|
/** |
458 |
|
* setRejectedExecutionHandler(null) throws NPE |
459 |
|
*/ |
460 |
|
public void testSetRejectedExecutionHandlerNull() { |
461 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
462 |
< |
try { |
463 |
< |
p.setRejectedExecutionHandler(null); |
464 |
< |
shouldThrow(); |
465 |
< |
} catch (NullPointerException success) { |
466 |
< |
} finally { |
467 |
< |
joinPool(p); |
461 |
> |
final ThreadPoolExecutor p = |
462 |
> |
new CustomTPE(1, 2, |
463 |
> |
LONG_DELAY_MS, MILLISECONDS, |
464 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
465 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
466 |
> |
try { |
467 |
> |
p.setRejectedExecutionHandler(null); |
468 |
> |
shouldThrow(); |
469 |
> |
} catch (NullPointerException success) {} |
470 |
|
} |
471 |
|
} |
472 |
|
|
480 |
|
new CustomTPE(THREADS, THREADS, |
481 |
|
LONG_DELAY_MS, MILLISECONDS, |
482 |
|
new ArrayBlockingQueue<Runnable>(10)); |
483 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
484 |
< |
final CountDownLatch done = new CountDownLatch(1); |
485 |
< |
try { |
483 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
484 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
485 |
> |
final CountDownLatch done = new CountDownLatch(1); |
486 |
|
assertEquals(0, p.getLargestPoolSize()); |
487 |
|
for (int i = 0; i < THREADS; i++) |
488 |
|
p.execute(new CheckedRunnable() { |
491 |
|
done.await(); |
492 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
493 |
|
}}); |
494 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
482 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
483 |
< |
} finally { |
484 |
< |
done.countDown(); |
485 |
< |
joinPool(p); |
494 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
495 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
496 |
+ |
done.countDown(); // release pool |
497 |
|
} |
498 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
503 |
|
* otherwise set |
504 |
|
*/ |
505 |
|
public void testGetMaximumPoolSize() { |
506 |
< |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
507 |
< |
assertEquals(2, p.getMaximumPoolSize()); |
508 |
< |
joinPool(p); |
506 |
> |
final ThreadPoolExecutor p = |
507 |
> |
new CustomTPE(2, 3, |
508 |
> |
LONG_DELAY_MS, MILLISECONDS, |
509 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
510 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
511 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
512 |
> |
p.setMaximumPoolSize(5); |
513 |
> |
assertEquals(5, p.getMaximumPoolSize()); |
514 |
> |
p.setMaximumPoolSize(4); |
515 |
> |
assertEquals(4, p.getMaximumPoolSize()); |
516 |
> |
} |
517 |
|
} |
518 |
|
|
519 |
|
/** |
525 |
|
new CustomTPE(1, 1, |
526 |
|
LONG_DELAY_MS, MILLISECONDS, |
527 |
|
new ArrayBlockingQueue<Runnable>(10)); |
528 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
529 |
< |
final CountDownLatch done = new CountDownLatch(1); |
530 |
< |
try { |
528 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
529 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
530 |
> |
final CountDownLatch done = new CountDownLatch(1); |
531 |
|
assertEquals(0, p.getPoolSize()); |
532 |
|
p.execute(new CheckedRunnable() { |
533 |
|
public void realRun() throws InterruptedException { |
535 |
|
assertEquals(1, p.getPoolSize()); |
536 |
|
done.await(); |
537 |
|
}}); |
538 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
538 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
539 |
|
assertEquals(1, p.getPoolSize()); |
540 |
< |
} finally { |
522 |
< |
done.countDown(); |
523 |
< |
joinPool(p); |
540 |
> |
done.countDown(); // release pool |
541 |
|
} |
542 |
|
} |
543 |
|
|
549 |
|
new CustomTPE(1, 1, |
550 |
|
LONG_DELAY_MS, MILLISECONDS, |
551 |
|
new ArrayBlockingQueue<Runnable>(10)); |
552 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
553 |
< |
final CountDownLatch done = new CountDownLatch(1); |
554 |
< |
try { |
552 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
553 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
554 |
> |
final CountDownLatch done = new CountDownLatch(1); |
555 |
|
assertEquals(0, p.getTaskCount()); |
556 |
|
p.execute(new CheckedRunnable() { |
557 |
|
public void realRun() throws InterruptedException { |
559 |
|
assertEquals(1, p.getTaskCount()); |
560 |
|
done.await(); |
561 |
|
}}); |
562 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
562 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
563 |
|
assertEquals(1, p.getTaskCount()); |
547 |
– |
} finally { |
564 |
|
done.countDown(); |
549 |
– |
joinPool(p); |
565 |
|
} |
566 |
|
} |
567 |
|
|
569 |
|
* isShutdown is false before shutdown, true after |
570 |
|
*/ |
571 |
|
public void testIsShutdown() { |
572 |
< |
|
573 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
574 |
< |
assertFalse(p.isShutdown()); |
575 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
576 |
< |
assertTrue(p.isShutdown()); |
577 |
< |
joinPool(p); |
572 |
> |
final ThreadPoolExecutor p = |
573 |
> |
new CustomTPE(1, 1, |
574 |
> |
LONG_DELAY_MS, MILLISECONDS, |
575 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
576 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
577 |
> |
assertFalse(p.isShutdown()); |
578 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
579 |
> |
assertTrue(p.isShutdown()); |
580 |
> |
} |
581 |
|
} |
582 |
|
|
583 |
|
/** |
588 |
|
new CustomTPE(1, 1, |
589 |
|
LONG_DELAY_MS, MILLISECONDS, |
590 |
|
new ArrayBlockingQueue<Runnable>(10)); |
591 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
592 |
< |
final CountDownLatch done = new CountDownLatch(1); |
593 |
< |
try { |
591 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
592 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
593 |
> |
final CountDownLatch done = new CountDownLatch(1); |
594 |
|
assertFalse(p.isTerminating()); |
595 |
|
p.execute(new CheckedRunnable() { |
596 |
|
public void realRun() throws InterruptedException { |
598 |
|
threadStarted.countDown(); |
599 |
|
done.await(); |
600 |
|
}}); |
601 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
601 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
602 |
|
assertFalse(p.isTerminating()); |
603 |
|
done.countDown(); |
586 |
– |
} finally { |
604 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
605 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
606 |
+ |
assertTrue(p.isTerminated()); |
607 |
+ |
assertFalse(p.isTerminating()); |
608 |
|
} |
589 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
590 |
– |
assertTrue(p.isTerminated()); |
591 |
– |
assertFalse(p.isTerminating()); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
616 |
|
new CustomTPE(1, 1, |
617 |
|
LONG_DELAY_MS, MILLISECONDS, |
618 |
|
new ArrayBlockingQueue<Runnable>(10)); |
619 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
620 |
< |
final CountDownLatch done = new CountDownLatch(1); |
621 |
< |
try { |
619 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
620 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
621 |
> |
final CountDownLatch done = new CountDownLatch(1); |
622 |
|
assertFalse(p.isTerminating()); |
623 |
|
p.execute(new CheckedRunnable() { |
624 |
|
public void realRun() throws InterruptedException { |
626 |
|
threadStarted.countDown(); |
627 |
|
done.await(); |
628 |
|
}}); |
629 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
629 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
630 |
|
assertFalse(p.isTerminating()); |
631 |
|
done.countDown(); |
615 |
– |
} finally { |
632 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
633 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
634 |
+ |
assertTrue(p.isTerminated()); |
635 |
+ |
assertFalse(p.isTerminating()); |
636 |
|
} |
618 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
619 |
– |
assertTrue(p.isTerminated()); |
620 |
– |
assertFalse(p.isTerminating()); |
637 |
|
} |
638 |
|
|
639 |
|
/** |
645 |
|
new CustomTPE(1, 1, |
646 |
|
LONG_DELAY_MS, MILLISECONDS, |
647 |
|
q); |
648 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
649 |
< |
final CountDownLatch done = new CountDownLatch(1); |
650 |
< |
try { |
648 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
649 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
650 |
> |
final CountDownLatch done = new CountDownLatch(1); |
651 |
|
FutureTask[] tasks = new FutureTask[5]; |
652 |
|
for (int i = 0; i < tasks.length; i++) { |
653 |
|
Callable task = new CheckedCallable<Boolean>() { |
660 |
|
tasks[i] = new FutureTask(task); |
661 |
|
p.execute(tasks[i]); |
662 |
|
} |
663 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
663 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
664 |
|
assertSame(q, p.getQueue()); |
665 |
|
assertFalse(q.contains(tasks[0])); |
666 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
667 |
|
assertEquals(tasks.length - 1, q.size()); |
652 |
– |
} finally { |
668 |
|
done.countDown(); |
654 |
– |
joinPool(p); |
669 |
|
} |
670 |
|
} |
671 |
|
|
678 |
|
new CustomTPE(1, 1, |
679 |
|
LONG_DELAY_MS, MILLISECONDS, |
680 |
|
q); |
681 |
< |
Runnable[] tasks = new Runnable[6]; |
682 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
683 |
< |
final CountDownLatch done = new CountDownLatch(1); |
684 |
< |
try { |
681 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
682 |
> |
Runnable[] tasks = new Runnable[6]; |
683 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
684 |
> |
final CountDownLatch done = new CountDownLatch(1); |
685 |
|
for (int i = 0; i < tasks.length; i++) { |
686 |
|
tasks[i] = new CheckedRunnable() { |
687 |
< |
public void realRun() throws InterruptedException { |
688 |
< |
threadStarted.countDown(); |
689 |
< |
done.await(); |
690 |
< |
}}; |
687 |
> |
public void realRun() throws InterruptedException { |
688 |
> |
threadStarted.countDown(); |
689 |
> |
done.await(); |
690 |
> |
}}; |
691 |
|
p.execute(tasks[i]); |
692 |
|
} |
693 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
693 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
694 |
|
assertFalse(p.remove(tasks[0])); |
695 |
|
assertTrue(q.contains(tasks[4])); |
696 |
|
assertTrue(q.contains(tasks[3])); |
700 |
|
assertTrue(q.contains(tasks[3])); |
701 |
|
assertTrue(p.remove(tasks[3])); |
702 |
|
assertFalse(q.contains(tasks[3])); |
689 |
– |
} finally { |
703 |
|
done.countDown(); |
691 |
– |
joinPool(p); |
704 |
|
} |
705 |
|
} |
706 |
|
|
715 |
|
new CustomTPE(1, 1, |
716 |
|
LONG_DELAY_MS, MILLISECONDS, |
717 |
|
q); |
718 |
< |
FutureTask[] tasks = new FutureTask[5]; |
719 |
< |
try { |
718 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
719 |
> |
FutureTask[] tasks = new FutureTask[5]; |
720 |
|
for (int i = 0; i < tasks.length; i++) { |
721 |
|
Callable task = new CheckedCallable<Boolean>() { |
722 |
|
public Boolean realCall() throws InterruptedException { |
727 |
|
tasks[i] = new FutureTask(task); |
728 |
|
p.execute(tasks[i]); |
729 |
|
} |
730 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
730 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
731 |
|
assertEquals(tasks.length, p.getTaskCount()); |
732 |
|
assertEquals(tasks.length - 1, q.size()); |
733 |
|
assertEquals(1L, p.getActiveCount()); |
740 |
|
p.purge(); // Nothing to do |
741 |
|
assertEquals(tasks.length - 3, q.size()); |
742 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
731 |
– |
} finally { |
743 |
|
done.countDown(); |
733 |
– |
joinPool(p); |
744 |
|
} |
745 |
|
} |
746 |
|
|
752 |
|
final int poolSize = 2; |
753 |
|
final int count = 5; |
754 |
|
final AtomicInteger ran = new AtomicInteger(0); |
755 |
< |
ThreadPoolExecutor p = |
756 |
< |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
755 |
> |
final ThreadPoolExecutor p = |
756 |
> |
new CustomTPE(poolSize, poolSize, |
757 |
> |
LONG_DELAY_MS, MILLISECONDS, |
758 |
|
new ArrayBlockingQueue<Runnable>(10)); |
759 |
|
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
760 |
|
Runnable waiter = new CheckedRunnable() { public void realRun() { |
1125 |
|
* execute throws RejectedExecutionException if saturated. |
1126 |
|
*/ |
1127 |
|
public void testSaturatedExecute() { |
1128 |
< |
ThreadPoolExecutor p = |
1128 |
> |
final ThreadPoolExecutor p = |
1129 |
|
new CustomTPE(1, 1, |
1130 |
|
LONG_DELAY_MS, MILLISECONDS, |
1131 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1132 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1133 |
< |
try { |
1132 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1133 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1134 |
|
Runnable task = new CheckedRunnable() { |
1135 |
|
public void realRun() throws InterruptedException { |
1136 |
|
done.await(); |
1144 |
|
} catch (RejectedExecutionException success) {} |
1145 |
|
assertTrue(p.getTaskCount() <= 2); |
1146 |
|
} |
1136 |
– |
} finally { |
1147 |
|
done.countDown(); |
1138 |
– |
joinPool(p); |
1148 |
|
} |
1149 |
|
} |
1150 |
|
|
1152 |
|
* executor using CallerRunsPolicy runs task if saturated. |
1153 |
|
*/ |
1154 |
|
public void testSaturatedExecute2() { |
1155 |
< |
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
1156 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, |
1157 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1158 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1159 |
< |
h); |
1160 |
< |
try { |
1155 |
> |
final ThreadPoolExecutor p = |
1156 |
> |
new CustomTPE(1, 1, |
1157 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1158 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1159 |
> |
new CustomTPE.CallerRunsPolicy()); |
1160 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1161 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1162 |
> |
Runnable blocker = new CheckedRunnable() { |
1163 |
> |
public void realRun() throws InterruptedException { |
1164 |
> |
done.await(); |
1165 |
> |
}}; |
1166 |
> |
p.execute(blocker); |
1167 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1168 |
< |
for (int i = 0; i < tasks.length; ++i) |
1168 |
> |
for (int i = 0; i < tasks.length; i++) |
1169 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1170 |
< |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1156 |
< |
p.execute(mr); |
1157 |
< |
for (int i = 0; i < tasks.length; ++i) |
1170 |
> |
for (int i = 0; i < tasks.length; i++) |
1171 |
|
p.execute(tasks[i]); |
1172 |
< |
for (int i = 1; i < tasks.length; ++i) |
1172 |
> |
for (int i = 1; i < tasks.length; i++) |
1173 |
|
assertTrue(tasks[i].done); |
1174 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1175 |
< |
} finally { |
1163 |
< |
joinPool(p); |
1174 |
> |
assertFalse(tasks[0].done); // waiting in queue |
1175 |
> |
done.countDown(); |
1176 |
|
} |
1177 |
|
} |
1178 |
|
|
1180 |
|
* executor using DiscardPolicy drops task if saturated. |
1181 |
|
*/ |
1182 |
|
public void testSaturatedExecute3() { |
1183 |
< |
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
1184 |
< |
ThreadPoolExecutor p = |
1183 |
> |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1184 |
> |
for (int i = 0; i < tasks.length; ++i) |
1185 |
> |
tasks[i] = new TrackedNoOpRunnable(); |
1186 |
> |
final ThreadPoolExecutor p = |
1187 |
|
new CustomTPE(1, 1, |
1188 |
|
LONG_DELAY_MS, MILLISECONDS, |
1189 |
|
new ArrayBlockingQueue<Runnable>(1), |
1190 |
< |
h); |
1191 |
< |
try { |
1192 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1193 |
< |
for (int i = 0; i < tasks.length; ++i) |
1194 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1181 |
< |
p.execute(new TrackedLongRunnable()); |
1190 |
> |
new CustomTPE.DiscardPolicy()); |
1191 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1192 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1193 |
> |
p.execute(awaiter(done)); |
1194 |
> |
|
1195 |
|
for (TrackedNoOpRunnable task : tasks) |
1196 |
|
p.execute(task); |
1197 |
< |
for (TrackedNoOpRunnable task : tasks) |
1198 |
< |
assertFalse(task.done); |
1199 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1187 |
< |
} finally { |
1188 |
< |
joinPool(p); |
1197 |
> |
for (int i = 1; i < tasks.length; i++) |
1198 |
> |
assertFalse(tasks[i].done); |
1199 |
> |
done.countDown(); |
1200 |
|
} |
1201 |
+ |
for (int i = 1; i < tasks.length; i++) |
1202 |
+ |
assertFalse(tasks[i].done); |
1203 |
+ |
assertTrue(tasks[0].done); // was waiting in queue |
1204 |
|
} |
1205 |
|
|
1206 |
|
/** |
1207 |
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1208 |
|
*/ |
1209 |
|
public void testSaturatedExecute4() { |
1210 |
< |
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
1211 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1212 |
< |
try { |
1213 |
< |
p.execute(new TrackedLongRunnable()); |
1214 |
< |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1210 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1211 |
> |
LatchAwaiter r1 = awaiter(done); |
1212 |
> |
LatchAwaiter r2 = awaiter(done); |
1213 |
> |
LatchAwaiter r3 = awaiter(done); |
1214 |
> |
final ThreadPoolExecutor p = |
1215 |
> |
new CustomTPE(1, 1, |
1216 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1217 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1218 |
> |
new CustomTPE.DiscardOldestPolicy()); |
1219 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1220 |
> |
assertEquals(LatchAwaiter.NEW, r1.state); |
1221 |
> |
assertEquals(LatchAwaiter.NEW, r2.state); |
1222 |
> |
assertEquals(LatchAwaiter.NEW, r3.state); |
1223 |
> |
p.execute(r1); |
1224 |
|
p.execute(r2); |
1225 |
|
assertTrue(p.getQueue().contains(r2)); |
1203 |
– |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
1226 |
|
p.execute(r3); |
1227 |
|
assertFalse(p.getQueue().contains(r2)); |
1228 |
|
assertTrue(p.getQueue().contains(r3)); |
1229 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1208 |
< |
} finally { |
1209 |
< |
joinPool(p); |
1229 |
> |
done.countDown(); |
1230 |
|
} |
1231 |
+ |
assertEquals(LatchAwaiter.DONE, r1.state); |
1232 |
+ |
assertEquals(LatchAwaiter.NEW, r2.state); |
1233 |
+ |
assertEquals(LatchAwaiter.DONE, r3.state); |
1234 |
|
} |
1235 |
|
|
1236 |
|
/** |
1237 |
|
* execute throws RejectedExecutionException if shutdown |
1238 |
|
*/ |
1239 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1240 |
< |
ThreadPoolExecutor p = |
1241 |
< |
new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1240 |
> |
final ThreadPoolExecutor p = |
1241 |
> |
new CustomTPE(1, 1, |
1242 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1243 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1244 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1245 |
< |
try { |
1246 |
< |
p.execute(new NoOpRunnable()); |
1247 |
< |
shouldThrow(); |
1248 |
< |
} catch (RejectedExecutionException success) {} |
1249 |
< |
|
1250 |
< |
joinPool(p); |
1245 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1246 |
> |
try { |
1247 |
> |
p.execute(new NoOpRunnable()); |
1248 |
> |
shouldThrow(); |
1249 |
> |
} catch (RejectedExecutionException success) {} |
1250 |
> |
} |
1251 |
|
} |
1252 |
|
|
1253 |
|
/** |
1254 |
|
* execute using CallerRunsPolicy drops task on shutdown |
1255 |
|
*/ |
1256 |
|
public void testCallerRunsOnShutdown() { |
1257 |
< |
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
1258 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1259 |
< |
|
1257 |
> |
final ThreadPoolExecutor p = |
1258 |
> |
new CustomTPE(1, 1, |
1259 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1260 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1261 |
> |
new CustomTPE.CallerRunsPolicy()); |
1262 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1263 |
< |
try { |
1263 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1264 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1265 |
|
p.execute(r); |
1266 |
|
assertFalse(r.done); |
1240 |
– |
} finally { |
1241 |
– |
joinPool(p); |
1267 |
|
} |
1268 |
|
} |
1269 |
|
|
1271 |
|
* execute using DiscardPolicy drops task on shutdown |
1272 |
|
*/ |
1273 |
|
public void testDiscardOnShutdown() { |
1274 |
< |
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
1275 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1276 |
< |
|
1274 |
> |
final ThreadPoolExecutor p = |
1275 |
> |
new CustomTPE(1, 1, |
1276 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1277 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1278 |
> |
new CustomTPE.DiscardPolicy()); |
1279 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1280 |
< |
try { |
1280 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1281 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1282 |
|
p.execute(r); |
1283 |
|
assertFalse(r.done); |
1257 |
– |
} finally { |
1258 |
– |
joinPool(p); |
1284 |
|
} |
1285 |
|
} |
1286 |
|
|
1288 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1289 |
|
*/ |
1290 |
|
public void testDiscardOldestOnShutdown() { |
1291 |
< |
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
1292 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1291 |
> |
final ThreadPoolExecutor p = |
1292 |
> |
new CustomTPE(1, 1, |
1293 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1294 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1295 |
> |
new CustomTPE.DiscardOldestPolicy()); |
1296 |
|
|
1297 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1298 |
< |
try { |
1298 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1299 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1300 |
|
p.execute(r); |
1301 |
|
assertFalse(r.done); |
1274 |
– |
} finally { |
1275 |
– |
joinPool(p); |
1302 |
|
} |
1303 |
|
} |
1304 |
|
|
1306 |
|
* execute(null) throws NPE |
1307 |
|
*/ |
1308 |
|
public void testExecuteNull() { |
1309 |
< |
ThreadPoolExecutor p = |
1310 |
< |
new CustomTPE(1, 2, 1L, SECONDS, |
1309 |
> |
final ThreadPoolExecutor p = |
1310 |
> |
new CustomTPE(1, 2, |
1311 |
> |
1L, SECONDS, |
1312 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1313 |
< |
try { |
1314 |
< |
p.execute(null); |
1315 |
< |
shouldThrow(); |
1316 |
< |
} catch (NullPointerException success) {} |
1317 |
< |
|
1318 |
< |
joinPool(p); |
1313 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1314 |
> |
try { |
1315 |
> |
p.execute(null); |
1316 |
> |
shouldThrow(); |
1317 |
> |
} catch (NullPointerException success) {} |
1318 |
> |
} |
1319 |
|
} |
1320 |
|
|
1321 |
|
/** |
1322 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1323 |
|
*/ |
1324 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1325 |
< |
ThreadPoolExecutor p = |
1326 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1327 |
< |
try { |
1328 |
< |
p.setCorePoolSize(-1); |
1329 |
< |
shouldThrow(); |
1330 |
< |
} catch (IllegalArgumentException success) { |
1331 |
< |
} finally { |
1332 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1325 |
> |
final ThreadPoolExecutor p = |
1326 |
> |
new CustomTPE(1, 2, |
1327 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1328 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1329 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1330 |
> |
try { |
1331 |
> |
p.setCorePoolSize(-1); |
1332 |
> |
shouldThrow(); |
1333 |
> |
} catch (IllegalArgumentException success) {} |
1334 |
|
} |
1307 |
– |
joinPool(p); |
1335 |
|
} |
1336 |
|
|
1337 |
|
/** |
1339 |
|
* if given a value less the core pool size |
1340 |
|
*/ |
1341 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1342 |
< |
ThreadPoolExecutor p = |
1343 |
< |
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1342 |
> |
final ThreadPoolExecutor p = |
1343 |
> |
new CustomTPE(2, 3, |
1344 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1345 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1346 |
|
try { |
1347 |
|
p.setMaximumPoolSize(1); |
1348 |
|
shouldThrow(); |
1358 |
|
* if given a negative value |
1359 |
|
*/ |
1360 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1361 |
< |
ThreadPoolExecutor p = |
1362 |
< |
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1361 |
> |
final ThreadPoolExecutor p = |
1362 |
> |
new CustomTPE(2, 3, |
1363 |
> |
LONG_DELAY_MS, |
1364 |
> |
MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1365 |
|
try { |
1366 |
|
p.setMaximumPoolSize(-1); |
1367 |
|
shouldThrow(); |
1377 |
|
* when given a negative value |
1378 |
|
*/ |
1379 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1380 |
< |
ThreadPoolExecutor p = |
1381 |
< |
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1380 |
> |
final ThreadPoolExecutor p = |
1381 |
> |
new CustomTPE(2, 3, |
1382 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1383 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1384 |
|
|
1385 |
|
try { |
1386 |
|
p.setKeepAliveTime(-1,MILLISECONDS); |
1427 |
|
* completed submit of callable returns result |
1428 |
|
*/ |
1429 |
|
public void testSubmitCallable() throws Exception { |
1430 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1430 |
> |
final ExecutorService e = |
1431 |
> |
new CustomTPE(2, 2, |
1432 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1433 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1434 |
|
try { |
1435 |
|
Future<String> future = e.submit(new StringTask()); |
1436 |
|
String result = future.get(); |
1444 |
|
* completed submit of runnable returns successfully |
1445 |
|
*/ |
1446 |
|
public void testSubmitRunnable() throws Exception { |
1447 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1447 |
> |
final ExecutorService e = |
1448 |
> |
new CustomTPE(2, 2, |
1449 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1450 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1451 |
|
try { |
1452 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1453 |
|
future.get(); |
1461 |
|
* completed submit of (runnable, result) returns result |
1462 |
|
*/ |
1463 |
|
public void testSubmitRunnable2() throws Exception { |
1464 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1464 |
> |
final ExecutorService e = |
1465 |
> |
new CustomTPE(2, 2, |
1466 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1467 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1468 |
|
try { |
1469 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1470 |
|
String result = future.get(); |
1478 |
|
* invokeAny(null) throws NPE |
1479 |
|
*/ |
1480 |
|
public void testInvokeAny1() throws Exception { |
1481 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1481 |
> |
final ExecutorService e = |
1482 |
> |
new CustomTPE(2, 2, |
1483 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1484 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1485 |
|
try { |
1486 |
|
e.invokeAny(null); |
1487 |
|
shouldThrow(); |
1495 |
|
* invokeAny(empty collection) throws IAE |
1496 |
|
*/ |
1497 |
|
public void testInvokeAny2() throws Exception { |
1498 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1498 |
> |
final ExecutorService e = |
1499 |
> |
new CustomTPE(2, 2, |
1500 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1501 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1502 |
|
try { |
1503 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1504 |
|
shouldThrow(); |
1513 |
|
*/ |
1514 |
|
public void testInvokeAny3() throws Exception { |
1515 |
|
CountDownLatch latch = new CountDownLatch(1); |
1516 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1516 |
> |
final ExecutorService e = |
1517 |
> |
new CustomTPE(2, 2, |
1518 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1519 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1520 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1521 |
|
l.add(latchAwaitingStringTask(latch)); |
1522 |
|
l.add(null); |
1534 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1535 |
|
*/ |
1536 |
|
public void testInvokeAny4() throws Exception { |
1537 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1537 |
> |
final ExecutorService e = |
1538 |
> |
new CustomTPE(2, 2, |
1539 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1540 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1541 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1542 |
|
l.add(new NPETask()); |
1543 |
|
try { |
1554 |
|
* invokeAny(c) returns result of some task |
1555 |
|
*/ |
1556 |
|
public void testInvokeAny5() throws Exception { |
1557 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1557 |
> |
final ExecutorService e = |
1558 |
> |
new CustomTPE(2, 2, |
1559 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1560 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1561 |
|
try { |
1562 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1563 |
|
l.add(new StringTask()); |
1573 |
|
* invokeAll(null) throws NPE |
1574 |
|
*/ |
1575 |
|
public void testInvokeAll1() throws Exception { |
1576 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1576 |
> |
final ExecutorService e = |
1577 |
> |
new CustomTPE(2, 2, |
1578 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1579 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1580 |
|
try { |
1581 |
|
e.invokeAll(null); |
1582 |
|
shouldThrow(); |
1590 |
|
* invokeAll(empty collection) returns empty collection |
1591 |
|
*/ |
1592 |
|
public void testInvokeAll2() throws Exception { |
1593 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1593 |
> |
final ExecutorService e = |
1594 |
> |
new CustomTPE(2, 2, |
1595 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1596 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1597 |
|
try { |
1598 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1599 |
|
assertTrue(r.isEmpty()); |
1606 |
|
* invokeAll(c) throws NPE if c has null elements |
1607 |
|
*/ |
1608 |
|
public void testInvokeAll3() throws Exception { |
1609 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1609 |
> |
final ExecutorService e = |
1610 |
> |
new CustomTPE(2, 2, |
1611 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1612 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1613 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1614 |
|
l.add(new StringTask()); |
1615 |
|
l.add(null); |
1626 |
|
* get of element of invokeAll(c) throws exception on failed task |
1627 |
|
*/ |
1628 |
|
public void testInvokeAll4() throws Exception { |
1629 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1629 |
> |
final ExecutorService e = |
1630 |
> |
new CustomTPE(2, 2, |
1631 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1632 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1633 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1634 |
|
l.add(new NPETask()); |
1635 |
|
List<Future<String>> futures = e.invokeAll(l); |
1648 |
|
* invokeAll(c) returns results of all completed tasks |
1649 |
|
*/ |
1650 |
|
public void testInvokeAll5() throws Exception { |
1651 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1651 |
> |
final ExecutorService e = |
1652 |
> |
new CustomTPE(2, 2, |
1653 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1654 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1655 |
|
try { |
1656 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1657 |
|
l.add(new StringTask()); |
1669 |
|
* timed invokeAny(null) throws NPE |
1670 |
|
*/ |
1671 |
|
public void testTimedInvokeAny1() throws Exception { |
1672 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1672 |
> |
final ExecutorService e = |
1673 |
> |
new CustomTPE(2, 2, |
1674 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1675 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1676 |
|
try { |
1677 |
|
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1678 |
|
shouldThrow(); |
1686 |
|
* timed invokeAny(,,null) throws NPE |
1687 |
|
*/ |
1688 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1689 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1689 |
> |
final ExecutorService e = |
1690 |
> |
new CustomTPE(2, 2, |
1691 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1692 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1693 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1694 |
|
l.add(new StringTask()); |
1695 |
|
try { |
1705 |
|
* timed invokeAny(empty collection) throws IAE |
1706 |
|
*/ |
1707 |
|
public void testTimedInvokeAny2() throws Exception { |
1708 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1708 |
> |
final ExecutorService e = |
1709 |
> |
new CustomTPE(2, 2, |
1710 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1711 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1712 |
|
try { |
1713 |
|
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1714 |
|
shouldThrow(); |
1723 |
|
*/ |
1724 |
|
public void testTimedInvokeAny3() throws Exception { |
1725 |
|
CountDownLatch latch = new CountDownLatch(1); |
1726 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1726 |
> |
final ExecutorService e = |
1727 |
> |
new CustomTPE(2, 2, |
1728 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1729 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1730 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1731 |
|
l.add(latchAwaitingStringTask(latch)); |
1732 |
|
l.add(null); |
1744 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1745 |
|
*/ |
1746 |
|
public void testTimedInvokeAny4() throws Exception { |
1747 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1747 |
> |
final ExecutorService e = |
1748 |
> |
new CustomTPE(2, 2, |
1749 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1750 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1751 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1752 |
|
l.add(new NPETask()); |
1753 |
|
try { |
1764 |
|
* timed invokeAny(c) returns result of some task |
1765 |
|
*/ |
1766 |
|
public void testTimedInvokeAny5() throws Exception { |
1767 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1767 |
> |
final ExecutorService e = |
1768 |
> |
new CustomTPE(2, 2, |
1769 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1770 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1771 |
|
try { |
1772 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1773 |
|
l.add(new StringTask()); |
1783 |
|
* timed invokeAll(null) throws NPE |
1784 |
|
*/ |
1785 |
|
public void testTimedInvokeAll1() throws Exception { |
1786 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1786 |
> |
final ExecutorService e = |
1787 |
> |
new CustomTPE(2, 2, |
1788 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1789 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1790 |
|
try { |
1791 |
|
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1792 |
|
shouldThrow(); |
1800 |
|
* timed invokeAll(,,null) throws NPE |
1801 |
|
*/ |
1802 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1803 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1803 |
> |
final ExecutorService e = |
1804 |
> |
new CustomTPE(2, 2, |
1805 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1806 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1807 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1808 |
|
l.add(new StringTask()); |
1809 |
|
try { |
1819 |
|
* timed invokeAll(empty collection) returns empty collection |
1820 |
|
*/ |
1821 |
|
public void testTimedInvokeAll2() throws Exception { |
1822 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1822 |
> |
final ExecutorService e = |
1823 |
> |
new CustomTPE(2, 2, |
1824 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1825 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1826 |
|
try { |
1827 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1828 |
|
assertTrue(r.isEmpty()); |
1835 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1836 |
|
*/ |
1837 |
|
public void testTimedInvokeAll3() throws Exception { |
1838 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1838 |
> |
final ExecutorService e = |
1839 |
> |
new CustomTPE(2, 2, |
1840 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1841 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1842 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1843 |
|
l.add(new StringTask()); |
1844 |
|
l.add(null); |
1855 |
|
* get of element of invokeAll(c) throws exception on failed task |
1856 |
|
*/ |
1857 |
|
public void testTimedInvokeAll4() throws Exception { |
1858 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1858 |
> |
final ExecutorService e = |
1859 |
> |
new CustomTPE(2, 2, |
1860 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1861 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1862 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1863 |
|
l.add(new NPETask()); |
1864 |
|
List<Future<String>> futures = |
1878 |
|
* timed invokeAll(c) returns results of all completed tasks |
1879 |
|
*/ |
1880 |
|
public void testTimedInvokeAll5() throws Exception { |
1881 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1881 |
> |
final ExecutorService e = |
1882 |
> |
new CustomTPE(2, 2, |
1883 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1884 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1885 |
|
try { |
1886 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1887 |
|
l.add(new StringTask()); |
1900 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1901 |
|
*/ |
1902 |
|
public void testTimedInvokeAll6() throws Exception { |
1903 |
< |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1903 |
> |
final ExecutorService e = |
1904 |
> |
new CustomTPE(2, 2, |
1905 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1906 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1907 |
|
try { |
1908 |
|
for (long timeout = timeoutMillis();;) { |
1909 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1961 |
|
* allowsCoreThreadTimeOut is by default false. |
1962 |
|
*/ |
1963 |
|
public void testAllowsCoreThreadTimeOut() { |
1964 |
< |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1964 |
> |
final ThreadPoolExecutor p = |
1965 |
> |
new CustomTPE(2, 2, |
1966 |
> |
1000, MILLISECONDS, |
1967 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1968 |
|
assertFalse(p.allowsCoreThreadTimeOut()); |
1969 |
|
joinPool(p); |
1970 |
|
} |