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 |
> |
|
33 |
> |
import junit.framework.Test; |
34 |
> |
import junit.framework.TestSuite; |
35 |
|
|
36 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run(suite()); |
38 |
> |
main(suite(), args); |
39 |
|
} |
40 |
|
public static Test suite() { |
41 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
42 |
|
} |
43 |
|
|
44 |
|
static class ExtendedTPE extends ThreadPoolExecutor { |
45 |
< |
volatile boolean beforeCalled = false; |
46 |
< |
volatile boolean afterCalled = false; |
47 |
< |
volatile boolean terminatedCalled = false; |
45 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
46 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
47 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
48 |
> |
|
49 |
|
public ExtendedTPE() { |
50 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
51 |
|
} |
52 |
|
protected void beforeExecute(Thread t, Runnable r) { |
53 |
< |
beforeCalled = true; |
53 |
> |
beforeCalled.countDown(); |
54 |
|
} |
55 |
|
protected void afterExecute(Runnable r, Throwable t) { |
56 |
< |
afterCalled = true; |
56 |
> |
afterCalled.countDown(); |
57 |
|
} |
58 |
|
protected void terminated() { |
59 |
< |
terminatedCalled = true; |
59 |
> |
terminatedCalled.countDown(); |
60 |
> |
} |
61 |
> |
|
62 |
> |
public boolean beforeCalled() { |
63 |
> |
return beforeCalled.getCount() == 0; |
64 |
> |
} |
65 |
> |
public boolean afterCalled() { |
66 |
> |
return afterCalled.getCount() == 0; |
67 |
> |
} |
68 |
> |
public boolean terminatedCalled() { |
69 |
> |
return terminatedCalled.getCount() == 0; |
70 |
|
} |
71 |
|
} |
72 |
|
|
78 |
|
} |
79 |
|
} |
80 |
|
|
49 |
– |
|
81 |
|
/** |
82 |
|
* execute successfully executes a runnable |
83 |
|
*/ |
181 |
|
threadProceed.await(); |
182 |
|
threadDone.countDown(); |
183 |
|
}}); |
184 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
184 |
> |
await(threadStarted); |
185 |
|
assertEquals(0, p.getCompletedTaskCount()); |
186 |
|
threadProceed.countDown(); |
187 |
|
threadDone.await(); |
188 |
< |
delay(SHORT_DELAY_MS); |
189 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
188 |
> |
long startTime = System.nanoTime(); |
189 |
> |
while (p.getCompletedTaskCount() != 1) { |
190 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
191 |
> |
fail("timed out"); |
192 |
> |
Thread.yield(); |
193 |
> |
} |
194 |
|
} finally { |
195 |
|
joinPool(p); |
196 |
|
} |
216 |
|
new ThreadPoolExecutor(2, 2, |
217 |
|
1000, MILLISECONDS, |
218 |
|
new ArrayBlockingQueue<Runnable>(10)); |
219 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
219 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
220 |
|
joinPool(p); |
221 |
|
} |
222 |
|
|
188 |
– |
|
223 |
|
/** |
224 |
|
* getThreadFactory returns factory in constructor if not set |
225 |
|
*/ |
249 |
|
joinPool(p); |
250 |
|
} |
251 |
|
|
218 |
– |
|
252 |
|
/** |
253 |
|
* setThreadFactory(null) throws NPE |
254 |
|
*/ |
295 |
|
joinPool(p); |
296 |
|
} |
297 |
|
|
265 |
– |
|
298 |
|
/** |
299 |
|
* setRejectedExecutionHandler(null) throws NPE |
300 |
|
*/ |
312 |
|
} |
313 |
|
} |
314 |
|
|
283 |
– |
|
315 |
|
/** |
316 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
317 |
|
* multiple threads active |
409 |
|
} |
410 |
|
|
411 |
|
/** |
412 |
< |
* isShutDown is false before shutdown, true after |
412 |
> |
* isShutdown is false before shutdown, true after |
413 |
|
*/ |
414 |
|
public void testIsShutdown() { |
415 |
|
final ThreadPoolExecutor p = |
422 |
|
joinPool(p); |
423 |
|
} |
424 |
|
|
425 |
+ |
/** |
426 |
+ |
* awaitTermination on a non-shutdown pool times out |
427 |
+ |
*/ |
428 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
429 |
+ |
final ThreadPoolExecutor p = |
430 |
+ |
new ThreadPoolExecutor(1, 1, |
431 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
432 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
433 |
+ |
assertFalse(p.isTerminated()); |
434 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
435 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
436 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
437 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
438 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
439 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
440 |
+ |
long timeoutNanos = 999999L; |
441 |
+ |
long startTime = System.nanoTime(); |
442 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
443 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
444 |
+ |
assertFalse(p.isTerminated()); |
445 |
+ |
startTime = System.nanoTime(); |
446 |
+ |
long timeoutMillis = timeoutMillis(); |
447 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
448 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
449 |
+ |
assertFalse(p.isTerminated()); |
450 |
+ |
p.shutdown(); |
451 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
452 |
+ |
assertTrue(p.isTerminated()); |
453 |
+ |
} |
454 |
|
|
455 |
|
/** |
456 |
|
* isTerminated is false before termination, true after |
624 |
|
} |
625 |
|
|
626 |
|
/** |
627 |
< |
* shutDownNow returns a list containing tasks that were not run |
627 |
> |
* shutdownNow returns a list containing tasks that were not run |
628 |
|
*/ |
629 |
< |
public void testShutDownNow() { |
629 |
> |
public void testShutdownNow() { |
630 |
|
final ThreadPoolExecutor p = |
631 |
|
new ThreadPoolExecutor(1, 1, |
632 |
|
LONG_DELAY_MS, MILLISECONDS, |
647 |
|
|
648 |
|
// Exception Tests |
649 |
|
|
590 |
– |
|
650 |
|
/** |
651 |
|
* Constructor throws if corePoolSize argument is less than zero |
652 |
|
*/ |
653 |
|
public void testConstructor1() { |
654 |
|
try { |
655 |
< |
new ThreadPoolExecutor(-1, 1, |
597 |
< |
LONG_DELAY_MS, MILLISECONDS, |
655 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
656 |
|
new ArrayBlockingQueue<Runnable>(10)); |
657 |
|
shouldThrow(); |
658 |
|
} catch (IllegalArgumentException success) {} |
663 |
|
*/ |
664 |
|
public void testConstructor2() { |
665 |
|
try { |
666 |
< |
new ThreadPoolExecutor(1, -1, |
609 |
< |
LONG_DELAY_MS, MILLISECONDS, |
666 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
667 |
|
new ArrayBlockingQueue<Runnable>(10)); |
668 |
|
shouldThrow(); |
669 |
|
} catch (IllegalArgumentException success) {} |
674 |
|
*/ |
675 |
|
public void testConstructor3() { |
676 |
|
try { |
677 |
< |
new ThreadPoolExecutor(1, 0, |
621 |
< |
LONG_DELAY_MS, MILLISECONDS, |
677 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
678 |
|
new ArrayBlockingQueue<Runnable>(10)); |
679 |
|
shouldThrow(); |
680 |
|
} catch (IllegalArgumentException success) {} |
685 |
|
*/ |
686 |
|
public void testConstructor4() { |
687 |
|
try { |
688 |
< |
new ThreadPoolExecutor(1, 2, |
633 |
< |
-1L, MILLISECONDS, |
688 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
689 |
|
new ArrayBlockingQueue<Runnable>(10)); |
690 |
|
shouldThrow(); |
691 |
|
} catch (IllegalArgumentException success) {} |
696 |
|
*/ |
697 |
|
public void testConstructor5() { |
698 |
|
try { |
699 |
< |
new ThreadPoolExecutor(2, 1, |
645 |
< |
LONG_DELAY_MS, MILLISECONDS, |
699 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
700 |
|
new ArrayBlockingQueue<Runnable>(10)); |
701 |
|
shouldThrow(); |
702 |
|
} catch (IllegalArgumentException success) {} |
707 |
|
*/ |
708 |
|
public void testConstructorNullPointerException() { |
709 |
|
try { |
710 |
< |
new ThreadPoolExecutor(1, 2, |
657 |
< |
LONG_DELAY_MS, MILLISECONDS, |
710 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
711 |
|
(BlockingQueue) null); |
712 |
|
shouldThrow(); |
713 |
|
} catch (NullPointerException success) {} |
714 |
|
} |
715 |
|
|
663 |
– |
|
664 |
– |
|
716 |
|
/** |
717 |
|
* Constructor throws if corePoolSize argument is less than zero |
718 |
|
*/ |
719 |
|
public void testConstructor6() { |
720 |
|
try { |
721 |
< |
new ThreadPoolExecutor(-1, 1, |
671 |
< |
LONG_DELAY_MS, MILLISECONDS, |
721 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
722 |
|
new ArrayBlockingQueue<Runnable>(10), |
723 |
|
new SimpleThreadFactory()); |
724 |
|
shouldThrow(); |
730 |
|
*/ |
731 |
|
public void testConstructor7() { |
732 |
|
try { |
733 |
< |
new ThreadPoolExecutor(1, -1, |
684 |
< |
LONG_DELAY_MS, MILLISECONDS, |
733 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
734 |
|
new ArrayBlockingQueue<Runnable>(10), |
735 |
|
new SimpleThreadFactory()); |
736 |
|
shouldThrow(); |
742 |
|
*/ |
743 |
|
public void testConstructor8() { |
744 |
|
try { |
745 |
< |
new ThreadPoolExecutor(1, 0, |
697 |
< |
LONG_DELAY_MS, MILLISECONDS, |
745 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
746 |
|
new ArrayBlockingQueue<Runnable>(10), |
747 |
|
new SimpleThreadFactory()); |
748 |
|
shouldThrow(); |
754 |
|
*/ |
755 |
|
public void testConstructor9() { |
756 |
|
try { |
757 |
< |
new ThreadPoolExecutor(1, 2, |
710 |
< |
-1L, MILLISECONDS, |
757 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
758 |
|
new ArrayBlockingQueue<Runnable>(10), |
759 |
|
new SimpleThreadFactory()); |
760 |
|
shouldThrow(); |
766 |
|
*/ |
767 |
|
public void testConstructor10() { |
768 |
|
try { |
769 |
< |
new ThreadPoolExecutor(2, 1, |
723 |
< |
LONG_DELAY_MS, MILLISECONDS, |
769 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
770 |
|
new ArrayBlockingQueue<Runnable>(10), |
771 |
|
new SimpleThreadFactory()); |
772 |
|
shouldThrow(); |
778 |
|
*/ |
779 |
|
public void testConstructorNullPointerException2() { |
780 |
|
try { |
781 |
< |
new ThreadPoolExecutor(1, 2, |
736 |
< |
LONG_DELAY_MS, MILLISECONDS, |
781 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
782 |
|
(BlockingQueue) null, |
783 |
|
new SimpleThreadFactory()); |
784 |
|
shouldThrow(); |
790 |
|
*/ |
791 |
|
public void testConstructorNullPointerException3() { |
792 |
|
try { |
793 |
< |
new ThreadPoolExecutor(1, 2, |
749 |
< |
LONG_DELAY_MS, MILLISECONDS, |
793 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
794 |
|
new ArrayBlockingQueue<Runnable>(10), |
795 |
|
(ThreadFactory) null); |
796 |
|
shouldThrow(); |
797 |
|
} catch (NullPointerException success) {} |
798 |
|
} |
799 |
|
|
756 |
– |
|
800 |
|
/** |
801 |
|
* Constructor throws if corePoolSize argument is less than zero |
802 |
|
*/ |
803 |
|
public void testConstructor11() { |
804 |
|
try { |
805 |
< |
new ThreadPoolExecutor(-1, 1, |
763 |
< |
LONG_DELAY_MS, MILLISECONDS, |
805 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
806 |
|
new ArrayBlockingQueue<Runnable>(10), |
807 |
|
new NoOpREHandler()); |
808 |
|
shouldThrow(); |
814 |
|
*/ |
815 |
|
public void testConstructor12() { |
816 |
|
try { |
817 |
< |
new ThreadPoolExecutor(1, -1, |
776 |
< |
LONG_DELAY_MS, MILLISECONDS, |
817 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
818 |
|
new ArrayBlockingQueue<Runnable>(10), |
819 |
|
new NoOpREHandler()); |
820 |
|
shouldThrow(); |
826 |
|
*/ |
827 |
|
public void testConstructor13() { |
828 |
|
try { |
829 |
< |
new ThreadPoolExecutor(1, 0, |
789 |
< |
LONG_DELAY_MS, MILLISECONDS, |
829 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
830 |
|
new ArrayBlockingQueue<Runnable>(10), |
831 |
|
new NoOpREHandler()); |
832 |
|
shouldThrow(); |
838 |
|
*/ |
839 |
|
public void testConstructor14() { |
840 |
|
try { |
841 |
< |
new ThreadPoolExecutor(1, 2, |
802 |
< |
-1L, MILLISECONDS, |
841 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
842 |
|
new ArrayBlockingQueue<Runnable>(10), |
843 |
|
new NoOpREHandler()); |
844 |
|
shouldThrow(); |
850 |
|
*/ |
851 |
|
public void testConstructor15() { |
852 |
|
try { |
853 |
< |
new ThreadPoolExecutor(2, 1, |
815 |
< |
LONG_DELAY_MS, MILLISECONDS, |
853 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
854 |
|
new ArrayBlockingQueue<Runnable>(10), |
855 |
|
new NoOpREHandler()); |
856 |
|
shouldThrow(); |
862 |
|
*/ |
863 |
|
public void testConstructorNullPointerException4() { |
864 |
|
try { |
865 |
< |
new ThreadPoolExecutor(1, 2, |
828 |
< |
LONG_DELAY_MS, MILLISECONDS, |
865 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
866 |
|
(BlockingQueue) null, |
867 |
|
new NoOpREHandler()); |
868 |
|
shouldThrow(); |
874 |
|
*/ |
875 |
|
public void testConstructorNullPointerException5() { |
876 |
|
try { |
877 |
< |
new ThreadPoolExecutor(1, 2, |
841 |
< |
LONG_DELAY_MS, MILLISECONDS, |
877 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
878 |
|
new ArrayBlockingQueue<Runnable>(10), |
879 |
|
(RejectedExecutionHandler) null); |
880 |
|
shouldThrow(); |
881 |
|
} catch (NullPointerException success) {} |
882 |
|
} |
883 |
|
|
848 |
– |
|
884 |
|
/** |
885 |
|
* Constructor throws if corePoolSize argument is less than zero |
886 |
|
*/ |
887 |
|
public void testConstructor16() { |
888 |
|
try { |
889 |
< |
new ThreadPoolExecutor(-1, 1, |
855 |
< |
LONG_DELAY_MS, MILLISECONDS, |
889 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
890 |
|
new ArrayBlockingQueue<Runnable>(10), |
891 |
|
new SimpleThreadFactory(), |
892 |
|
new NoOpREHandler()); |
899 |
|
*/ |
900 |
|
public void testConstructor17() { |
901 |
|
try { |
902 |
< |
new ThreadPoolExecutor(1, -1, |
869 |
< |
LONG_DELAY_MS, MILLISECONDS, |
902 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
903 |
|
new ArrayBlockingQueue<Runnable>(10), |
904 |
|
new SimpleThreadFactory(), |
905 |
|
new NoOpREHandler()); |
912 |
|
*/ |
913 |
|
public void testConstructor18() { |
914 |
|
try { |
915 |
< |
new ThreadPoolExecutor(1, 0, |
883 |
< |
LONG_DELAY_MS, MILLISECONDS, |
915 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
916 |
|
new ArrayBlockingQueue<Runnable>(10), |
917 |
|
new SimpleThreadFactory(), |
918 |
|
new NoOpREHandler()); |
925 |
|
*/ |
926 |
|
public void testConstructor19() { |
927 |
|
try { |
928 |
< |
new ThreadPoolExecutor(1, 2, |
897 |
< |
-1L, MILLISECONDS, |
928 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
929 |
|
new ArrayBlockingQueue<Runnable>(10), |
930 |
|
new SimpleThreadFactory(), |
931 |
|
new NoOpREHandler()); |
938 |
|
*/ |
939 |
|
public void testConstructor20() { |
940 |
|
try { |
941 |
< |
new ThreadPoolExecutor(2, 1, |
911 |
< |
LONG_DELAY_MS, MILLISECONDS, |
941 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
942 |
|
new ArrayBlockingQueue<Runnable>(10), |
943 |
|
new SimpleThreadFactory(), |
944 |
|
new NoOpREHandler()); |
951 |
|
*/ |
952 |
|
public void testConstructorNullPointerException6() { |
953 |
|
try { |
954 |
< |
new ThreadPoolExecutor(1, 2, |
925 |
< |
LONG_DELAY_MS, MILLISECONDS, |
954 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
955 |
|
(BlockingQueue) null, |
956 |
|
new SimpleThreadFactory(), |
957 |
|
new NoOpREHandler()); |
964 |
|
*/ |
965 |
|
public void testConstructorNullPointerException7() { |
966 |
|
try { |
967 |
< |
new ThreadPoolExecutor(1, 2, |
939 |
< |
LONG_DELAY_MS, MILLISECONDS, |
967 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
968 |
|
new ArrayBlockingQueue<Runnable>(10), |
969 |
|
new SimpleThreadFactory(), |
970 |
|
(RejectedExecutionHandler) null); |
977 |
|
*/ |
978 |
|
public void testConstructorNullPointerException8() { |
979 |
|
try { |
980 |
< |
new ThreadPoolExecutor(1, 2, |
953 |
< |
LONG_DELAY_MS, MILLISECONDS, |
980 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
981 |
|
new ArrayBlockingQueue<Runnable>(10), |
982 |
|
(ThreadFactory) null, |
983 |
|
new NoOpREHandler()); |
991 |
|
public void testInterruptedSubmit() throws InterruptedException { |
992 |
|
final ThreadPoolExecutor p = |
993 |
|
new ThreadPoolExecutor(1, 1, |
994 |
< |
60, TimeUnit.SECONDS, |
994 |
> |
60, SECONDS, |
995 |
|
new ArrayBlockingQueue<Runnable>(10)); |
996 |
|
|
997 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1239 |
|
} |
1240 |
|
} |
1241 |
|
|
1215 |
– |
|
1242 |
|
/** |
1243 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1244 |
|
*/ |
1260 |
|
} |
1261 |
|
} |
1262 |
|
|
1237 |
– |
|
1263 |
|
/** |
1264 |
|
* execute(null) throws NPE |
1265 |
|
*/ |
1266 |
|
public void testExecuteNull() { |
1267 |
|
ThreadPoolExecutor p = |
1268 |
< |
new ThreadPoolExecutor(1, 2, |
1244 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1268 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1269 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1270 |
|
try { |
1271 |
|
p.execute(null); |
1331 |
|
joinPool(p); |
1332 |
|
} |
1333 |
|
|
1334 |
+ |
/** |
1335 |
+ |
* Configuration changes that allow core pool size greater than |
1336 |
+ |
* max pool size result in IllegalArgumentException. |
1337 |
+ |
*/ |
1338 |
+ |
public void testPoolSizeInvariants() { |
1339 |
+ |
ThreadPoolExecutor p = |
1340 |
+ |
new ThreadPoolExecutor(1, 1, |
1341 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1342 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1343 |
+ |
for (int s = 1; s < 5; s++) { |
1344 |
+ |
p.setMaximumPoolSize(s); |
1345 |
+ |
p.setCorePoolSize(s); |
1346 |
+ |
try { |
1347 |
+ |
p.setMaximumPoolSize(s - 1); |
1348 |
+ |
shouldThrow(); |
1349 |
+ |
} catch (IllegalArgumentException success) {} |
1350 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1351 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1352 |
+ |
try { |
1353 |
+ |
p.setCorePoolSize(s + 1); |
1354 |
+ |
shouldThrow(); |
1355 |
+ |
} catch (IllegalArgumentException success) {} |
1356 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1357 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1358 |
+ |
} |
1359 |
+ |
joinPool(p); |
1360 |
+ |
} |
1361 |
|
|
1362 |
|
/** |
1363 |
|
* setKeepAliveTime throws IllegalArgumentException |
1384 |
|
public void testTerminated() { |
1385 |
|
ExtendedTPE p = new ExtendedTPE(); |
1386 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1387 |
< |
assertTrue(p.terminatedCalled); |
1387 |
> |
assertTrue(p.terminatedCalled()); |
1388 |
|
joinPool(p); |
1389 |
|
} |
1390 |
|
|
1394 |
|
public void testBeforeAfter() throws InterruptedException { |
1395 |
|
ExtendedTPE p = new ExtendedTPE(); |
1396 |
|
try { |
1397 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1398 |
< |
p.execute(r); |
1399 |
< |
delay(SHORT_DELAY_MS); |
1400 |
< |
assertTrue(r.done); |
1401 |
< |
assertTrue(p.beforeCalled); |
1402 |
< |
assertTrue(p.afterCalled); |
1397 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1398 |
> |
p.execute(new CheckedRunnable() { |
1399 |
> |
public void realRun() { |
1400 |
> |
done.countDown(); |
1401 |
> |
}}); |
1402 |
> |
await(p.afterCalled); |
1403 |
> |
assertEquals(0, done.getCount()); |
1404 |
> |
assertTrue(p.afterCalled()); |
1405 |
> |
assertTrue(p.beforeCalled()); |
1406 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1407 |
|
} finally { |
1408 |
|
joinPool(p); |
1460 |
|
} |
1461 |
|
} |
1462 |
|
|
1409 |
– |
|
1463 |
|
/** |
1464 |
|
* invokeAny(null) throws NPE |
1465 |
|
*/ |
1653 |
|
} |
1654 |
|
} |
1655 |
|
|
1603 |
– |
|
1604 |
– |
|
1656 |
|
/** |
1657 |
|
* timed invokeAny(null) throws NPE |
1658 |
|
*/ |
1893 |
|
LONG_DELAY_MS, MILLISECONDS, |
1894 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1895 |
|
try { |
1896 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1897 |
< |
l.add(new StringTask()); |
1898 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1899 |
< |
l.add(new StringTask()); |
1900 |
< |
List<Future<String>> futures = |
1901 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1902 |
< |
assertEquals(3, futures.size()); |
1903 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1904 |
< |
Future<String> f1 = it.next(); |
1905 |
< |
Future<String> f2 = it.next(); |
1906 |
< |
Future<String> f3 = it.next(); |
1907 |
< |
assertTrue(f1.isDone()); |
1908 |
< |
assertTrue(f2.isDone()); |
1909 |
< |
assertTrue(f3.isDone()); |
1910 |
< |
assertFalse(f1.isCancelled()); |
1911 |
< |
assertTrue(f2.isCancelled()); |
1896 |
> |
for (long timeout = timeoutMillis();;) { |
1897 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1898 |
> |
tasks.add(new StringTask("0")); |
1899 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1900 |
> |
tasks.add(new StringTask("2")); |
1901 |
> |
long startTime = System.nanoTime(); |
1902 |
> |
List<Future<String>> futures = |
1903 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1904 |
> |
assertEquals(tasks.size(), futures.size()); |
1905 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1906 |
> |
for (Future future : futures) |
1907 |
> |
assertTrue(future.isDone()); |
1908 |
> |
assertTrue(futures.get(1).isCancelled()); |
1909 |
> |
try { |
1910 |
> |
assertEquals("0", futures.get(0).get()); |
1911 |
> |
assertEquals("2", futures.get(2).get()); |
1912 |
> |
break; |
1913 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1914 |
> |
timeout *= 2; |
1915 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1916 |
> |
fail("expected exactly one task to be cancelled"); |
1917 |
> |
} |
1918 |
> |
} |
1919 |
|
} finally { |
1920 |
|
joinPool(e); |
1921 |
|
} |
1961 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1962 |
|
*/ |
1963 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1964 |
+ |
long keepAliveTime = timeoutMillis(); |
1965 |
|
final ThreadPoolExecutor p = |
1966 |
|
new ThreadPoolExecutor(2, 10, |
1967 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1967 |
> |
keepAliveTime, MILLISECONDS, |
1968 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1969 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1970 |
|
try { |
1971 |
|
p.allowCoreThreadTimeOut(true); |
1972 |
|
p.execute(new CheckedRunnable() { |
1973 |
< |
public void realRun() throws InterruptedException { |
1973 |
> |
public void realRun() { |
1974 |
|
threadStarted.countDown(); |
1975 |
|
assertEquals(1, p.getPoolSize()); |
1976 |
|
}}); |
1977 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1978 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1979 |
< |
if (p.getPoolSize() == 0) |
1980 |
< |
break; |
1981 |
< |
delay(10); |
1982 |
< |
} |
1977 |
> |
await(threadStarted); |
1978 |
> |
delay(keepAliveTime); |
1979 |
> |
long startTime = System.nanoTime(); |
1980 |
> |
while (p.getPoolSize() > 0 |
1981 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1982 |
> |
Thread.yield(); |
1983 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1984 |
|
assertEquals(0, p.getPoolSize()); |
1985 |
|
} finally { |
1986 |
|
joinPool(p); |
1991 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1992 |
|
*/ |
1993 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1994 |
+ |
long keepAliveTime = timeoutMillis(); |
1995 |
|
final ThreadPoolExecutor p = |
1996 |
|
new ThreadPoolExecutor(2, 10, |
1997 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1997 |
> |
keepAliveTime, MILLISECONDS, |
1998 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1999 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2000 |
|
try { |
2004 |
|
threadStarted.countDown(); |
2005 |
|
assertTrue(p.getPoolSize() >= 1); |
2006 |
|
}}); |
2007 |
< |
delay(SMALL_DELAY_MS); |
2007 |
> |
delay(2 * keepAliveTime); |
2008 |
|
assertTrue(p.getPoolSize() >= 1); |
2009 |
|
} finally { |
2010 |
|
joinPool(p); |
2023 |
|
done.countDown(); |
2024 |
|
}}; |
2025 |
|
final ThreadPoolExecutor p = |
2026 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2026 |
> |
new ThreadPoolExecutor(1, 30, |
2027 |
> |
60, SECONDS, |
2028 |
|
new ArrayBlockingQueue(30)); |
2029 |
|
try { |
2030 |
|
for (int i = 0; i < nTasks; ++i) { |
2039 |
|
// enough time to run all tasks |
2040 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2041 |
|
} finally { |
2042 |
< |
p.shutdown(); |
2042 |
> |
joinPool(p); |
2043 |
|
} |
2044 |
|
} |
2045 |
|
|