6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
> |
|
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
16 |
> |
import java.util.concurrent.BlockingQueue; |
17 |
> |
import java.util.concurrent.Callable; |
18 |
> |
import java.util.concurrent.CancellationException; |
19 |
> |
import java.util.concurrent.CountDownLatch; |
20 |
> |
import java.util.concurrent.ExecutionException; |
21 |
> |
import java.util.concurrent.Executors; |
22 |
> |
import java.util.concurrent.ExecutorService; |
23 |
> |
import java.util.concurrent.Future; |
24 |
> |
import java.util.concurrent.FutureTask; |
25 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
26 |
> |
import java.util.concurrent.RejectedExecutionException; |
27 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
28 |
> |
import java.util.concurrent.SynchronousQueue; |
29 |
> |
import java.util.concurrent.ThreadFactory; |
30 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
31 |
> |
import java.util.concurrent.TimeUnit; |
32 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
33 |
> |
|
34 |
> |
import junit.framework.Test; |
35 |
> |
import junit.framework.TestSuite; |
36 |
|
|
37 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
38 |
|
public static void main(String[] args) { |
39 |
< |
junit.textui.TestRunner.run(suite()); |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
public static Test suite() { |
42 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
132 |
|
*/ |
133 |
|
public void testPrestartCoreThread() { |
134 |
|
final ThreadPoolExecutor p = |
135 |
< |
new ThreadPoolExecutor(2, 2, |
135 |
> |
new ThreadPoolExecutor(2, 6, |
136 |
|
LONG_DELAY_MS, MILLISECONDS, |
137 |
|
new ArrayBlockingQueue<Runnable>(10)); |
138 |
< |
assertEquals(0, p.getPoolSize()); |
139 |
< |
assertTrue(p.prestartCoreThread()); |
140 |
< |
assertEquals(1, p.getPoolSize()); |
141 |
< |
assertTrue(p.prestartCoreThread()); |
142 |
< |
assertEquals(2, p.getPoolSize()); |
143 |
< |
assertFalse(p.prestartCoreThread()); |
144 |
< |
assertEquals(2, p.getPoolSize()); |
145 |
< |
joinPool(p); |
138 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
139 |
> |
assertEquals(0, p.getPoolSize()); |
140 |
> |
assertTrue(p.prestartCoreThread()); |
141 |
> |
assertEquals(1, p.getPoolSize()); |
142 |
> |
assertTrue(p.prestartCoreThread()); |
143 |
> |
assertEquals(2, p.getPoolSize()); |
144 |
> |
assertFalse(p.prestartCoreThread()); |
145 |
> |
assertEquals(2, p.getPoolSize()); |
146 |
> |
p.setCorePoolSize(4); |
147 |
> |
assertTrue(p.prestartCoreThread()); |
148 |
> |
assertEquals(3, p.getPoolSize()); |
149 |
> |
assertTrue(p.prestartCoreThread()); |
150 |
> |
assertEquals(4, p.getPoolSize()); |
151 |
> |
assertFalse(p.prestartCoreThread()); |
152 |
> |
assertEquals(4, p.getPoolSize()); |
153 |
> |
} |
154 |
|
} |
155 |
|
|
156 |
|
/** |
225 |
|
new ThreadPoolExecutor(2, 2, |
226 |
|
1000, MILLISECONDS, |
227 |
|
new ArrayBlockingQueue<Runnable>(10)); |
228 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
228 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
229 |
|
joinPool(p); |
230 |
|
} |
231 |
|
|
432 |
|
} |
433 |
|
|
434 |
|
/** |
435 |
+ |
* awaitTermination on a non-shutdown pool times out |
436 |
+ |
*/ |
437 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
438 |
+ |
final ThreadPoolExecutor p = |
439 |
+ |
new ThreadPoolExecutor(1, 1, |
440 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
441 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
442 |
+ |
assertFalse(p.isTerminated()); |
443 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
444 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
445 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
446 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
447 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
448 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
449 |
+ |
long timeoutNanos = 999999L; |
450 |
+ |
long startTime = System.nanoTime(); |
451 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
452 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
453 |
+ |
assertFalse(p.isTerminated()); |
454 |
+ |
startTime = System.nanoTime(); |
455 |
+ |
long timeoutMillis = timeoutMillis(); |
456 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
457 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
458 |
+ |
assertFalse(p.isTerminated()); |
459 |
+ |
p.shutdown(); |
460 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
461 |
+ |
assertTrue(p.isTerminated()); |
462 |
+ |
} |
463 |
+ |
|
464 |
+ |
/** |
465 |
|
* isTerminated is false before termination, true after |
466 |
|
*/ |
467 |
|
public void testIsTerminated() throws InterruptedException { |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
< |
* shutdownNow returns a list containing tasks that were not run |
636 |
> |
* shutdownNow returns a list containing tasks that were not run, |
637 |
> |
* and those tasks are drained from the queue |
638 |
|
*/ |
639 |
< |
public void testShutdownNow() { |
639 |
> |
public void testShutdownNow() throws InterruptedException { |
640 |
> |
final int poolSize = 2; |
641 |
> |
final int count = 5; |
642 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
643 |
|
final ThreadPoolExecutor p = |
644 |
< |
new ThreadPoolExecutor(1, 1, |
644 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
645 |
|
LONG_DELAY_MS, MILLISECONDS, |
646 |
|
new ArrayBlockingQueue<Runnable>(10)); |
647 |
< |
List l; |
648 |
< |
try { |
649 |
< |
for (int i = 0; i < 5; i++) |
586 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
587 |
< |
} |
588 |
< |
finally { |
647 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
648 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
649 |
> |
threadsStarted.countDown(); |
650 |
|
try { |
651 |
< |
l = p.shutdownNow(); |
652 |
< |
} catch (SecurityException ok) { return; } |
651 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
652 |
> |
} catch (InterruptedException success) {} |
653 |
> |
ran.getAndIncrement(); |
654 |
> |
}}; |
655 |
> |
for (int i = 0; i < count; i++) |
656 |
> |
p.execute(waiter); |
657 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
658 |
> |
assertEquals(poolSize, p.getActiveCount()); |
659 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
660 |
> |
final List<Runnable> queuedTasks; |
661 |
> |
try { |
662 |
> |
queuedTasks = p.shutdownNow(); |
663 |
> |
} catch (SecurityException ok) { |
664 |
> |
return; // Allowed in case test doesn't have privs |
665 |
|
} |
666 |
|
assertTrue(p.isShutdown()); |
667 |
< |
assertTrue(l.size() <= 4); |
667 |
> |
assertTrue(p.getQueue().isEmpty()); |
668 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
669 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
670 |
> |
assertTrue(p.isTerminated()); |
671 |
> |
assertEquals(poolSize, ran.get()); |
672 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
673 |
|
} |
674 |
|
|
675 |
|
// Exception Tests |
679 |
|
*/ |
680 |
|
public void testConstructor1() { |
681 |
|
try { |
682 |
< |
new ThreadPoolExecutor(-1, 1, |
605 |
< |
LONG_DELAY_MS, MILLISECONDS, |
682 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
683 |
|
new ArrayBlockingQueue<Runnable>(10)); |
684 |
|
shouldThrow(); |
685 |
|
} catch (IllegalArgumentException success) {} |
690 |
|
*/ |
691 |
|
public void testConstructor2() { |
692 |
|
try { |
693 |
< |
new ThreadPoolExecutor(1, -1, |
617 |
< |
LONG_DELAY_MS, MILLISECONDS, |
693 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
694 |
|
new ArrayBlockingQueue<Runnable>(10)); |
695 |
|
shouldThrow(); |
696 |
|
} catch (IllegalArgumentException success) {} |
701 |
|
*/ |
702 |
|
public void testConstructor3() { |
703 |
|
try { |
704 |
< |
new ThreadPoolExecutor(1, 0, |
629 |
< |
LONG_DELAY_MS, MILLISECONDS, |
704 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
705 |
|
new ArrayBlockingQueue<Runnable>(10)); |
706 |
|
shouldThrow(); |
707 |
|
} catch (IllegalArgumentException success) {} |
712 |
|
*/ |
713 |
|
public void testConstructor4() { |
714 |
|
try { |
715 |
< |
new ThreadPoolExecutor(1, 2, |
641 |
< |
-1L, MILLISECONDS, |
715 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
716 |
|
new ArrayBlockingQueue<Runnable>(10)); |
717 |
|
shouldThrow(); |
718 |
|
} catch (IllegalArgumentException success) {} |
723 |
|
*/ |
724 |
|
public void testConstructor5() { |
725 |
|
try { |
726 |
< |
new ThreadPoolExecutor(2, 1, |
653 |
< |
LONG_DELAY_MS, MILLISECONDS, |
726 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
727 |
|
new ArrayBlockingQueue<Runnable>(10)); |
728 |
|
shouldThrow(); |
729 |
|
} catch (IllegalArgumentException success) {} |
734 |
|
*/ |
735 |
|
public void testConstructorNullPointerException() { |
736 |
|
try { |
737 |
< |
new ThreadPoolExecutor(1, 2, |
665 |
< |
LONG_DELAY_MS, MILLISECONDS, |
737 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
738 |
|
(BlockingQueue) null); |
739 |
|
shouldThrow(); |
740 |
|
} catch (NullPointerException success) {} |
745 |
|
*/ |
746 |
|
public void testConstructor6() { |
747 |
|
try { |
748 |
< |
new ThreadPoolExecutor(-1, 1, |
677 |
< |
LONG_DELAY_MS, MILLISECONDS, |
748 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
749 |
|
new ArrayBlockingQueue<Runnable>(10), |
750 |
|
new SimpleThreadFactory()); |
751 |
|
shouldThrow(); |
757 |
|
*/ |
758 |
|
public void testConstructor7() { |
759 |
|
try { |
760 |
< |
new ThreadPoolExecutor(1, -1, |
690 |
< |
LONG_DELAY_MS, MILLISECONDS, |
760 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
761 |
|
new ArrayBlockingQueue<Runnable>(10), |
762 |
|
new SimpleThreadFactory()); |
763 |
|
shouldThrow(); |
769 |
|
*/ |
770 |
|
public void testConstructor8() { |
771 |
|
try { |
772 |
< |
new ThreadPoolExecutor(1, 0, |
703 |
< |
LONG_DELAY_MS, MILLISECONDS, |
772 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
773 |
|
new ArrayBlockingQueue<Runnable>(10), |
774 |
|
new SimpleThreadFactory()); |
775 |
|
shouldThrow(); |
781 |
|
*/ |
782 |
|
public void testConstructor9() { |
783 |
|
try { |
784 |
< |
new ThreadPoolExecutor(1, 2, |
716 |
< |
-1L, MILLISECONDS, |
784 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
785 |
|
new ArrayBlockingQueue<Runnable>(10), |
786 |
|
new SimpleThreadFactory()); |
787 |
|
shouldThrow(); |
793 |
|
*/ |
794 |
|
public void testConstructor10() { |
795 |
|
try { |
796 |
< |
new ThreadPoolExecutor(2, 1, |
729 |
< |
LONG_DELAY_MS, MILLISECONDS, |
796 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
797 |
|
new ArrayBlockingQueue<Runnable>(10), |
798 |
|
new SimpleThreadFactory()); |
799 |
|
shouldThrow(); |
805 |
|
*/ |
806 |
|
public void testConstructorNullPointerException2() { |
807 |
|
try { |
808 |
< |
new ThreadPoolExecutor(1, 2, |
742 |
< |
LONG_DELAY_MS, MILLISECONDS, |
808 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
809 |
|
(BlockingQueue) null, |
810 |
|
new SimpleThreadFactory()); |
811 |
|
shouldThrow(); |
817 |
|
*/ |
818 |
|
public void testConstructorNullPointerException3() { |
819 |
|
try { |
820 |
< |
new ThreadPoolExecutor(1, 2, |
755 |
< |
LONG_DELAY_MS, MILLISECONDS, |
820 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
821 |
|
new ArrayBlockingQueue<Runnable>(10), |
822 |
|
(ThreadFactory) null); |
823 |
|
shouldThrow(); |
829 |
|
*/ |
830 |
|
public void testConstructor11() { |
831 |
|
try { |
832 |
< |
new ThreadPoolExecutor(-1, 1, |
768 |
< |
LONG_DELAY_MS, MILLISECONDS, |
832 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
833 |
|
new ArrayBlockingQueue<Runnable>(10), |
834 |
|
new NoOpREHandler()); |
835 |
|
shouldThrow(); |
841 |
|
*/ |
842 |
|
public void testConstructor12() { |
843 |
|
try { |
844 |
< |
new ThreadPoolExecutor(1, -1, |
781 |
< |
LONG_DELAY_MS, MILLISECONDS, |
844 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
845 |
|
new ArrayBlockingQueue<Runnable>(10), |
846 |
|
new NoOpREHandler()); |
847 |
|
shouldThrow(); |
853 |
|
*/ |
854 |
|
public void testConstructor13() { |
855 |
|
try { |
856 |
< |
new ThreadPoolExecutor(1, 0, |
794 |
< |
LONG_DELAY_MS, MILLISECONDS, |
856 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
857 |
|
new ArrayBlockingQueue<Runnable>(10), |
858 |
|
new NoOpREHandler()); |
859 |
|
shouldThrow(); |
865 |
|
*/ |
866 |
|
public void testConstructor14() { |
867 |
|
try { |
868 |
< |
new ThreadPoolExecutor(1, 2, |
807 |
< |
-1L, MILLISECONDS, |
868 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
869 |
|
new ArrayBlockingQueue<Runnable>(10), |
870 |
|
new NoOpREHandler()); |
871 |
|
shouldThrow(); |
877 |
|
*/ |
878 |
|
public void testConstructor15() { |
879 |
|
try { |
880 |
< |
new ThreadPoolExecutor(2, 1, |
820 |
< |
LONG_DELAY_MS, MILLISECONDS, |
880 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
881 |
|
new ArrayBlockingQueue<Runnable>(10), |
882 |
|
new NoOpREHandler()); |
883 |
|
shouldThrow(); |
889 |
|
*/ |
890 |
|
public void testConstructorNullPointerException4() { |
891 |
|
try { |
892 |
< |
new ThreadPoolExecutor(1, 2, |
833 |
< |
LONG_DELAY_MS, MILLISECONDS, |
892 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
893 |
|
(BlockingQueue) null, |
894 |
|
new NoOpREHandler()); |
895 |
|
shouldThrow(); |
901 |
|
*/ |
902 |
|
public void testConstructorNullPointerException5() { |
903 |
|
try { |
904 |
< |
new ThreadPoolExecutor(1, 2, |
846 |
< |
LONG_DELAY_MS, MILLISECONDS, |
904 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
905 |
|
new ArrayBlockingQueue<Runnable>(10), |
906 |
|
(RejectedExecutionHandler) null); |
907 |
|
shouldThrow(); |
913 |
|
*/ |
914 |
|
public void testConstructor16() { |
915 |
|
try { |
916 |
< |
new ThreadPoolExecutor(-1, 1, |
859 |
< |
LONG_DELAY_MS, MILLISECONDS, |
916 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
917 |
|
new ArrayBlockingQueue<Runnable>(10), |
918 |
|
new SimpleThreadFactory(), |
919 |
|
new NoOpREHandler()); |
926 |
|
*/ |
927 |
|
public void testConstructor17() { |
928 |
|
try { |
929 |
< |
new ThreadPoolExecutor(1, -1, |
873 |
< |
LONG_DELAY_MS, MILLISECONDS, |
929 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
930 |
|
new ArrayBlockingQueue<Runnable>(10), |
931 |
|
new SimpleThreadFactory(), |
932 |
|
new NoOpREHandler()); |
939 |
|
*/ |
940 |
|
public void testConstructor18() { |
941 |
|
try { |
942 |
< |
new ThreadPoolExecutor(1, 0, |
887 |
< |
LONG_DELAY_MS, MILLISECONDS, |
942 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
943 |
|
new ArrayBlockingQueue<Runnable>(10), |
944 |
|
new SimpleThreadFactory(), |
945 |
|
new NoOpREHandler()); |
952 |
|
*/ |
953 |
|
public void testConstructor19() { |
954 |
|
try { |
955 |
< |
new ThreadPoolExecutor(1, 2, |
901 |
< |
-1L, MILLISECONDS, |
955 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
956 |
|
new ArrayBlockingQueue<Runnable>(10), |
957 |
|
new SimpleThreadFactory(), |
958 |
|
new NoOpREHandler()); |
965 |
|
*/ |
966 |
|
public void testConstructor20() { |
967 |
|
try { |
968 |
< |
new ThreadPoolExecutor(2, 1, |
915 |
< |
LONG_DELAY_MS, MILLISECONDS, |
968 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
969 |
|
new ArrayBlockingQueue<Runnable>(10), |
970 |
|
new SimpleThreadFactory(), |
971 |
|
new NoOpREHandler()); |
978 |
|
*/ |
979 |
|
public void testConstructorNullPointerException6() { |
980 |
|
try { |
981 |
< |
new ThreadPoolExecutor(1, 2, |
929 |
< |
LONG_DELAY_MS, MILLISECONDS, |
981 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
982 |
|
(BlockingQueue) null, |
983 |
|
new SimpleThreadFactory(), |
984 |
|
new NoOpREHandler()); |
991 |
|
*/ |
992 |
|
public void testConstructorNullPointerException7() { |
993 |
|
try { |
994 |
< |
new ThreadPoolExecutor(1, 2, |
943 |
< |
LONG_DELAY_MS, MILLISECONDS, |
994 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
995 |
|
new ArrayBlockingQueue<Runnable>(10), |
996 |
|
new SimpleThreadFactory(), |
997 |
|
(RejectedExecutionHandler) null); |
1004 |
|
*/ |
1005 |
|
public void testConstructorNullPointerException8() { |
1006 |
|
try { |
1007 |
< |
new ThreadPoolExecutor(1, 2, |
957 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1007 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1008 |
|
new ArrayBlockingQueue<Runnable>(10), |
1009 |
|
(ThreadFactory) null, |
1010 |
|
new NoOpREHandler()); |
1018 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1019 |
|
final ThreadPoolExecutor p = |
1020 |
|
new ThreadPoolExecutor(1, 1, |
1021 |
< |
60, TimeUnit.SECONDS, |
1021 |
> |
60, SECONDS, |
1022 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1023 |
|
|
1024 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1292 |
|
*/ |
1293 |
|
public void testExecuteNull() { |
1294 |
|
ThreadPoolExecutor p = |
1295 |
< |
new ThreadPoolExecutor(1, 2, |
1246 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1295 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1296 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1297 |
|
try { |
1298 |
|
p.execute(null); |
1359 |
|
} |
1360 |
|
|
1361 |
|
/** |
1362 |
+ |
* Configuration changes that allow core pool size greater than |
1363 |
+ |
* max pool size result in IllegalArgumentException. |
1364 |
+ |
*/ |
1365 |
+ |
public void testPoolSizeInvariants() { |
1366 |
+ |
ThreadPoolExecutor p = |
1367 |
+ |
new ThreadPoolExecutor(1, 1, |
1368 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1369 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1370 |
+ |
for (int s = 1; s < 5; s++) { |
1371 |
+ |
p.setMaximumPoolSize(s); |
1372 |
+ |
p.setCorePoolSize(s); |
1373 |
+ |
try { |
1374 |
+ |
p.setMaximumPoolSize(s - 1); |
1375 |
+ |
shouldThrow(); |
1376 |
+ |
} catch (IllegalArgumentException success) {} |
1377 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1378 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1379 |
+ |
try { |
1380 |
+ |
p.setCorePoolSize(s + 1); |
1381 |
+ |
shouldThrow(); |
1382 |
+ |
} catch (IllegalArgumentException success) {} |
1383 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1384 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1385 |
+ |
} |
1386 |
+ |
joinPool(p); |
1387 |
+ |
} |
1388 |
+ |
|
1389 |
+ |
/** |
1390 |
|
* setKeepAliveTime throws IllegalArgumentException |
1391 |
|
* when given a negative value |
1392 |
|
*/ |
1422 |
|
ExtendedTPE p = new ExtendedTPE(); |
1423 |
|
try { |
1424 |
|
final CountDownLatch done = new CountDownLatch(1); |
1425 |
< |
final CheckedRunnable task = new CheckedRunnable() { |
1425 |
> |
p.execute(new CheckedRunnable() { |
1426 |
|
public void realRun() { |
1427 |
|
done.countDown(); |
1428 |
< |
}}; |
1352 |
< |
p.execute(task); |
1428 |
> |
}}); |
1429 |
|
await(p.afterCalled); |
1430 |
|
assertEquals(0, done.getCount()); |
1431 |
|
assertTrue(p.afterCalled()); |
1902 |
|
l.add(new StringTask()); |
1903 |
|
l.add(new StringTask()); |
1904 |
|
List<Future<String>> futures = |
1905 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1905 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1906 |
|
assertEquals(2, futures.size()); |
1907 |
|
for (Future<String> future : futures) |
1908 |
|
assertSame(TEST_STRING, future.get()); |
1920 |
|
LONG_DELAY_MS, MILLISECONDS, |
1921 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1922 |
|
try { |
1923 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1924 |
< |
l.add(new StringTask()); |
1925 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1926 |
< |
l.add(new StringTask()); |
1927 |
< |
List<Future<String>> futures = |
1928 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1929 |
< |
assertEquals(l.size(), futures.size()); |
1930 |
< |
for (Future future : futures) |
1931 |
< |
assertTrue(future.isDone()); |
1932 |
< |
assertFalse(futures.get(0).isCancelled()); |
1933 |
< |
assertTrue(futures.get(1).isCancelled()); |
1923 |
> |
for (long timeout = timeoutMillis();;) { |
1924 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1925 |
> |
tasks.add(new StringTask("0")); |
1926 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1927 |
> |
tasks.add(new StringTask("2")); |
1928 |
> |
long startTime = System.nanoTime(); |
1929 |
> |
List<Future<String>> futures = |
1930 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1931 |
> |
assertEquals(tasks.size(), futures.size()); |
1932 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1933 |
> |
for (Future future : futures) |
1934 |
> |
assertTrue(future.isDone()); |
1935 |
> |
assertTrue(futures.get(1).isCancelled()); |
1936 |
> |
try { |
1937 |
> |
assertEquals("0", futures.get(0).get()); |
1938 |
> |
assertEquals("2", futures.get(2).get()); |
1939 |
> |
break; |
1940 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1941 |
> |
timeout *= 2; |
1942 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1943 |
> |
fail("expected exactly one task to be cancelled"); |
1944 |
> |
} |
1945 |
> |
} |
1946 |
|
} finally { |
1947 |
|
joinPool(e); |
1948 |
|
} |
1988 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1989 |
|
*/ |
1990 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1991 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1991 |
> |
long keepAliveTime = timeoutMillis(); |
1992 |
|
final ThreadPoolExecutor p = |
1993 |
|
new ThreadPoolExecutor(2, 10, |
1994 |
< |
coreThreadTimeOut, MILLISECONDS, |
1994 |
> |
keepAliveTime, MILLISECONDS, |
1995 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1996 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1997 |
|
try { |
2002 |
|
assertEquals(1, p.getPoolSize()); |
2003 |
|
}}); |
2004 |
|
await(threadStarted); |
2005 |
< |
delay(coreThreadTimeOut); |
2005 |
> |
delay(keepAliveTime); |
2006 |
|
long startTime = System.nanoTime(); |
2007 |
|
while (p.getPoolSize() > 0 |
2008 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
2018 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
2019 |
|
*/ |
2020 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
2021 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
2021 |
> |
long keepAliveTime = timeoutMillis(); |
2022 |
|
final ThreadPoolExecutor p = |
2023 |
|
new ThreadPoolExecutor(2, 10, |
2024 |
< |
coreThreadTimeOut, MILLISECONDS, |
2024 |
> |
keepAliveTime, MILLISECONDS, |
2025 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2026 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2027 |
|
try { |
2031 |
|
threadStarted.countDown(); |
2032 |
|
assertTrue(p.getPoolSize() >= 1); |
2033 |
|
}}); |
2034 |
< |
delay(2 * coreThreadTimeOut); |
2034 |
> |
delay(2 * keepAliveTime); |
2035 |
|
assertTrue(p.getPoolSize() >= 1); |
2036 |
|
} finally { |
2037 |
|
joinPool(p); |
2050 |
|
done.countDown(); |
2051 |
|
}}; |
2052 |
|
final ThreadPoolExecutor p = |
2053 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2053 |
> |
new ThreadPoolExecutor(1, 30, |
2054 |
> |
60, SECONDS, |
2055 |
|
new ArrayBlockingQueue(30)); |
2056 |
|
try { |
2057 |
|
for (int i = 0; i < nTasks; ++i) { |
2070 |
|
} |
2071 |
|
} |
2072 |
|
|
2073 |
+ |
/** |
2074 |
+ |
* get(cancelled task) throws CancellationException |
2075 |
+ |
*/ |
2076 |
+ |
public void testGet_cancelled() throws Exception { |
2077 |
+ |
final ExecutorService e = |
2078 |
+ |
new ThreadPoolExecutor(1, 1, |
2079 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
2080 |
+ |
new LinkedBlockingQueue<Runnable>()); |
2081 |
+ |
try { |
2082 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2083 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
2084 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
2085 |
+ |
for (int i = 0; i < 2; i++) { |
2086 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
2087 |
+ |
throws Throwable { |
2088 |
+ |
blockerStarted.countDown(); |
2089 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2090 |
+ |
}}; |
2091 |
+ |
futures.add(e.submit(r)); |
2092 |
+ |
} |
2093 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2094 |
+ |
for (Future<?> future : futures) future.cancel(false); |
2095 |
+ |
for (Future<?> future : futures) { |
2096 |
+ |
try { |
2097 |
+ |
future.get(); |
2098 |
+ |
shouldThrow(); |
2099 |
+ |
} catch (CancellationException success) {} |
2100 |
+ |
try { |
2101 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2102 |
+ |
shouldThrow(); |
2103 |
+ |
} catch (CancellationException success) {} |
2104 |
+ |
assertTrue(future.isCancelled()); |
2105 |
+ |
assertTrue(future.isDone()); |
2106 |
+ |
} |
2107 |
+ |
done.countDown(); |
2108 |
+ |
} finally { |
2109 |
+ |
joinPool(e); |
2110 |
+ |
} |
2111 |
+ |
} |
2112 |
+ |
|
2113 |
|
} |