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.locks.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
< |
import junit.framework.*; |
13 |
< |
import java.util.*; |
12 |
> |
import java.util.ArrayList; |
13 |
> |
import java.util.List; |
14 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.Callable; |
17 |
> |
import java.util.concurrent.CancellationException; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.ExecutionException; |
20 |
> |
import java.util.concurrent.Executors; |
21 |
> |
import java.util.concurrent.ExecutorService; |
22 |
> |
import java.util.concurrent.Future; |
23 |
> |
import java.util.concurrent.FutureTask; |
24 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
25 |
> |
import java.util.concurrent.RejectedExecutionException; |
26 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
27 |
> |
import java.util.concurrent.RunnableFuture; |
28 |
> |
import java.util.concurrent.SynchronousQueue; |
29 |
> |
import java.util.concurrent.ThreadFactory; |
30 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
31 |
> |
import java.util.concurrent.TimeoutException; |
32 |
> |
import java.util.concurrent.TimeUnit; |
33 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
34 |
> |
import java.util.concurrent.locks.Condition; |
35 |
> |
import java.util.concurrent.locks.ReentrantLock; |
36 |
> |
|
37 |
> |
import junit.framework.Test; |
38 |
> |
import junit.framework.TestSuite; |
39 |
|
|
40 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
41 |
|
public static void main(String[] args) { |
42 |
< |
junit.textui.TestRunner.run(suite()); |
42 |
> |
main(suite(), args); |
43 |
|
} |
44 |
|
public static Test suite() { |
45 |
|
return new TestSuite(ThreadPoolExecutorSubclassTest.class); |
61 |
|
CustomTask(final Runnable r, final V res) { |
62 |
|
if (r == null) throw new NullPointerException(); |
63 |
|
callable = new Callable<V>() { |
64 |
< |
public V call() throws Exception { r.run(); return res; }}; |
64 |
> |
public V call() throws Exception { r.run(); return res; }}; |
65 |
|
} |
66 |
|
public boolean isDone() { |
67 |
|
lock.lock(); try { return done; } finally { lock.unlock() ; } |
347 |
|
*/ |
348 |
|
public void testGetKeepAliveTime() { |
349 |
|
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
350 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
350 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
351 |
|
joinPool(p); |
352 |
|
} |
353 |
|
|
700 |
|
} |
701 |
|
|
702 |
|
/** |
703 |
< |
* shutdownNow returns a list containing tasks that were not run |
703 |
> |
* shutdownNow returns a list containing tasks that were not run, |
704 |
> |
* and those tasks are drained from the queue |
705 |
|
*/ |
706 |
< |
public void testShutdownNow() { |
707 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
708 |
< |
List l; |
709 |
< |
try { |
710 |
< |
for (int i = 0; i < 5; i++) |
711 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
712 |
< |
} |
713 |
< |
finally { |
706 |
> |
public void testShutdownNow() throws InterruptedException { |
707 |
> |
final int poolSize = 2; |
708 |
> |
final int count = 5; |
709 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
710 |
> |
ThreadPoolExecutor p = |
711 |
> |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
712 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
713 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
714 |
> |
CheckedRunnable waiter = new CheckedRunnable() { public void realRun() { |
715 |
> |
threadsStarted.countDown(); |
716 |
|
try { |
717 |
< |
l = p.shutdownNow(); |
718 |
< |
} catch (SecurityException ok) { return; } |
717 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
718 |
> |
} catch (InterruptedException success) {} |
719 |
> |
ran.getAndIncrement(); |
720 |
> |
}}; |
721 |
> |
for (int i = 0; i < count; i++) |
722 |
> |
p.execute(waiter); |
723 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
724 |
> |
final List<Runnable> queuedTasks; |
725 |
> |
try { |
726 |
> |
queuedTasks = p.shutdownNow(); |
727 |
> |
} catch (SecurityException ok) { |
728 |
> |
return; // Allowed in case test doesn't have privs |
729 |
|
} |
730 |
|
assertTrue(p.isShutdown()); |
731 |
< |
assertTrue(l.size() <= 4); |
731 |
> |
assertTrue(p.getQueue().isEmpty()); |
732 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
733 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
734 |
> |
assertTrue(p.isTerminated()); |
735 |
> |
assertEquals(poolSize, ran.get()); |
736 |
|
} |
737 |
|
|
738 |
|
// Exception Tests |
742 |
|
*/ |
743 |
|
public void testConstructor1() { |
744 |
|
try { |
745 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
745 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
746 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
747 |
|
shouldThrow(); |
748 |
|
} catch (IllegalArgumentException success) {} |
749 |
|
} |
753 |
|
*/ |
754 |
|
public void testConstructor2() { |
755 |
|
try { |
756 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
756 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
757 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
758 |
|
shouldThrow(); |
759 |
|
} catch (IllegalArgumentException success) {} |
760 |
|
} |
764 |
|
*/ |
765 |
|
public void testConstructor3() { |
766 |
|
try { |
767 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
767 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
768 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
769 |
|
shouldThrow(); |
770 |
|
} catch (IllegalArgumentException success) {} |
771 |
|
} |
775 |
|
*/ |
776 |
|
public void testConstructor4() { |
777 |
|
try { |
778 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
778 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
779 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
780 |
|
shouldThrow(); |
781 |
|
} catch (IllegalArgumentException success) {} |
782 |
|
} |
786 |
|
*/ |
787 |
|
public void testConstructor5() { |
788 |
|
try { |
789 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
789 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
790 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
791 |
|
shouldThrow(); |
792 |
|
} catch (IllegalArgumentException success) {} |
793 |
|
} |
797 |
|
*/ |
798 |
|
public void testConstructorNullPointerException() { |
799 |
|
try { |
800 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
800 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
801 |
|
shouldThrow(); |
802 |
|
} catch (NullPointerException success) {} |
803 |
|
} |
807 |
|
*/ |
808 |
|
public void testConstructor6() { |
809 |
|
try { |
810 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
810 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
811 |
> |
new ArrayBlockingQueue<Runnable>(10), |
812 |
> |
new SimpleThreadFactory()); |
813 |
|
shouldThrow(); |
814 |
|
} catch (IllegalArgumentException success) {} |
815 |
|
} |
819 |
|
*/ |
820 |
|
public void testConstructor7() { |
821 |
|
try { |
822 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
822 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
823 |
> |
new ArrayBlockingQueue<Runnable>(10), |
824 |
> |
new SimpleThreadFactory()); |
825 |
|
shouldThrow(); |
826 |
|
} catch (IllegalArgumentException success) {} |
827 |
|
} |
831 |
|
*/ |
832 |
|
public void testConstructor8() { |
833 |
|
try { |
834 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
834 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
835 |
> |
new ArrayBlockingQueue<Runnable>(10), |
836 |
> |
new SimpleThreadFactory()); |
837 |
|
shouldThrow(); |
838 |
|
} catch (IllegalArgumentException success) {} |
839 |
|
} |
843 |
|
*/ |
844 |
|
public void testConstructor9() { |
845 |
|
try { |
846 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
846 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
847 |
> |
new ArrayBlockingQueue<Runnable>(10), |
848 |
> |
new SimpleThreadFactory()); |
849 |
|
shouldThrow(); |
850 |
|
} catch (IllegalArgumentException success) {} |
851 |
|
} |
855 |
|
*/ |
856 |
|
public void testConstructor10() { |
857 |
|
try { |
858 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
858 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
859 |
> |
new ArrayBlockingQueue<Runnable>(10), |
860 |
> |
new SimpleThreadFactory()); |
861 |
|
shouldThrow(); |
862 |
|
} catch (IllegalArgumentException success) {} |
863 |
|
} |
867 |
|
*/ |
868 |
|
public void testConstructorNullPointerException2() { |
869 |
|
try { |
870 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
870 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
871 |
|
shouldThrow(); |
872 |
|
} catch (NullPointerException success) {} |
873 |
|
} |
877 |
|
*/ |
878 |
|
public void testConstructorNullPointerException3() { |
879 |
|
try { |
880 |
< |
ThreadFactory f = null; |
881 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
880 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
881 |
> |
new ArrayBlockingQueue<Runnable>(10), |
882 |
> |
(ThreadFactory) null); |
883 |
|
shouldThrow(); |
884 |
|
} catch (NullPointerException success) {} |
885 |
|
} |
889 |
|
*/ |
890 |
|
public void testConstructor11() { |
891 |
|
try { |
892 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
892 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
893 |
> |
new ArrayBlockingQueue<Runnable>(10), |
894 |
> |
new NoOpREHandler()); |
895 |
|
shouldThrow(); |
896 |
|
} catch (IllegalArgumentException success) {} |
897 |
|
} |
901 |
|
*/ |
902 |
|
public void testConstructor12() { |
903 |
|
try { |
904 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
904 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
905 |
> |
new ArrayBlockingQueue<Runnable>(10), |
906 |
> |
new NoOpREHandler()); |
907 |
|
shouldThrow(); |
908 |
|
} catch (IllegalArgumentException success) {} |
909 |
|
} |
913 |
|
*/ |
914 |
|
public void testConstructor13() { |
915 |
|
try { |
916 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
916 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
917 |
> |
new ArrayBlockingQueue<Runnable>(10), |
918 |
> |
new NoOpREHandler()); |
919 |
|
shouldThrow(); |
920 |
|
} catch (IllegalArgumentException success) {} |
921 |
|
} |
925 |
|
*/ |
926 |
|
public void testConstructor14() { |
927 |
|
try { |
928 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
928 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
929 |
> |
new ArrayBlockingQueue<Runnable>(10), |
930 |
> |
new NoOpREHandler()); |
931 |
|
shouldThrow(); |
932 |
|
} catch (IllegalArgumentException success) {} |
933 |
|
} |
937 |
|
*/ |
938 |
|
public void testConstructor15() { |
939 |
|
try { |
940 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
940 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
941 |
> |
new ArrayBlockingQueue<Runnable>(10), |
942 |
> |
new NoOpREHandler()); |
943 |
|
shouldThrow(); |
944 |
|
} catch (IllegalArgumentException success) {} |
945 |
|
} |
949 |
|
*/ |
950 |
|
public void testConstructorNullPointerException4() { |
951 |
|
try { |
952 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
952 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
953 |
> |
null, |
954 |
> |
new NoOpREHandler()); |
955 |
|
shouldThrow(); |
956 |
|
} catch (NullPointerException success) {} |
957 |
|
} |
961 |
|
*/ |
962 |
|
public void testConstructorNullPointerException5() { |
963 |
|
try { |
964 |
< |
RejectedExecutionHandler r = null; |
965 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
964 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
965 |
> |
new ArrayBlockingQueue<Runnable>(10), |
966 |
> |
(RejectedExecutionHandler) null); |
967 |
|
shouldThrow(); |
968 |
|
} catch (NullPointerException success) {} |
969 |
|
} |
973 |
|
*/ |
974 |
|
public void testConstructor16() { |
975 |
|
try { |
976 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
976 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
977 |
> |
new ArrayBlockingQueue<Runnable>(10), |
978 |
> |
new SimpleThreadFactory(), |
979 |
> |
new NoOpREHandler()); |
980 |
|
shouldThrow(); |
981 |
|
} catch (IllegalArgumentException success) {} |
982 |
|
} |
986 |
|
*/ |
987 |
|
public void testConstructor17() { |
988 |
|
try { |
989 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
989 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
990 |
> |
new ArrayBlockingQueue<Runnable>(10), |
991 |
> |
new SimpleThreadFactory(), |
992 |
> |
new NoOpREHandler()); |
993 |
|
shouldThrow(); |
994 |
|
} catch (IllegalArgumentException success) {} |
995 |
|
} |
999 |
|
*/ |
1000 |
|
public void testConstructor18() { |
1001 |
|
try { |
1002 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1002 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
1003 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1004 |
> |
new SimpleThreadFactory(), |
1005 |
> |
new NoOpREHandler()); |
1006 |
|
shouldThrow(); |
1007 |
|
} catch (IllegalArgumentException success) {} |
1008 |
|
} |
1012 |
|
*/ |
1013 |
|
public void testConstructor19() { |
1014 |
|
try { |
1015 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1015 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
1016 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1017 |
> |
new SimpleThreadFactory(), |
1018 |
> |
new NoOpREHandler()); |
1019 |
|
shouldThrow(); |
1020 |
|
} catch (IllegalArgumentException success) {} |
1021 |
|
} |
1025 |
|
*/ |
1026 |
|
public void testConstructor20() { |
1027 |
|
try { |
1028 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1028 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1029 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1030 |
> |
new SimpleThreadFactory(), |
1031 |
> |
new NoOpREHandler()); |
1032 |
|
shouldThrow(); |
1033 |
|
} catch (IllegalArgumentException success) {} |
1034 |
|
} |
1038 |
|
*/ |
1039 |
|
public void testConstructorNullPointerException6() { |
1040 |
|
try { |
1041 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1041 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1042 |
> |
null, |
1043 |
> |
new SimpleThreadFactory(), |
1044 |
> |
new NoOpREHandler()); |
1045 |
|
shouldThrow(); |
1046 |
|
} catch (NullPointerException success) {} |
1047 |
|
} |
1051 |
|
*/ |
1052 |
|
public void testConstructorNullPointerException7() { |
1053 |
|
try { |
1054 |
< |
RejectedExecutionHandler r = null; |
1055 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1054 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1055 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1056 |
> |
new SimpleThreadFactory(), |
1057 |
> |
(RejectedExecutionHandler) null); |
1058 |
|
shouldThrow(); |
1059 |
|
} catch (NullPointerException success) {} |
1060 |
|
} |
1064 |
|
*/ |
1065 |
|
public void testConstructorNullPointerException8() { |
1066 |
|
try { |
1067 |
< |
new CustomTPE(1, 2, |
978 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1067 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1068 |
|
new ArrayBlockingQueue<Runnable>(10), |
1069 |
|
(ThreadFactory) null, |
1070 |
|
new NoOpREHandler()); |
1242 |
|
* execute(null) throws NPE |
1243 |
|
*/ |
1244 |
|
public void testExecuteNull() { |
1245 |
< |
ThreadPoolExecutor p = null; |
1245 |
> |
ThreadPoolExecutor p = |
1246 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1247 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1248 |
|
try { |
1158 |
– |
p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1249 |
|
p.execute(null); |
1250 |
|
shouldThrow(); |
1251 |
|
} catch (NullPointerException success) {} |
1338 |
|
CustomTPE p = new CustomTPE(); |
1339 |
|
try { |
1340 |
|
final CountDownLatch done = new CountDownLatch(1); |
1341 |
< |
final CheckedRunnable task = new CheckedRunnable() { |
1341 |
> |
p.execute(new CheckedRunnable() { |
1342 |
|
public void realRun() { |
1343 |
|
done.countDown(); |
1344 |
< |
}}; |
1255 |
< |
p.execute(task); |
1344 |
> |
}}); |
1345 |
|
await(p.afterCalled); |
1346 |
|
assertEquals(0, done.getCount()); |
1347 |
|
assertTrue(p.afterCalled()); |
1756 |
|
public void testTimedInvokeAll6() throws Exception { |
1757 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1758 |
|
try { |
1759 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1760 |
< |
l.add(new StringTask()); |
1761 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1762 |
< |
l.add(new StringTask()); |
1763 |
< |
List<Future<String>> futures = |
1764 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1765 |
< |
assertEquals(l.size(), futures.size()); |
1766 |
< |
for (Future future : futures) |
1767 |
< |
assertTrue(future.isDone()); |
1768 |
< |
assertFalse(futures.get(0).isCancelled()); |
1769 |
< |
assertTrue(futures.get(1).isCancelled()); |
1759 |
> |
for (long timeout = timeoutMillis();;) { |
1760 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1761 |
> |
tasks.add(new StringTask("0")); |
1762 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1763 |
> |
tasks.add(new StringTask("2")); |
1764 |
> |
long startTime = System.nanoTime(); |
1765 |
> |
List<Future<String>> futures = |
1766 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1767 |
> |
assertEquals(tasks.size(), futures.size()); |
1768 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1769 |
> |
for (Future future : futures) |
1770 |
> |
assertTrue(future.isDone()); |
1771 |
> |
assertTrue(futures.get(1).isCancelled()); |
1772 |
> |
try { |
1773 |
> |
assertEquals("0", futures.get(0).get()); |
1774 |
> |
assertEquals("2", futures.get(2).get()); |
1775 |
> |
break; |
1776 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1777 |
> |
timeout *= 2; |
1778 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1779 |
> |
fail("expected exactly one task to be cancelled"); |
1780 |
> |
} |
1781 |
> |
} |
1782 |
|
} finally { |
1783 |
|
joinPool(e); |
1784 |
|
} |
1821 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1822 |
|
*/ |
1823 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1824 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1824 |
> |
long keepAliveTime = timeoutMillis(); |
1825 |
|
final ThreadPoolExecutor p = |
1826 |
|
new CustomTPE(2, 10, |
1827 |
< |
coreThreadTimeOut, MILLISECONDS, |
1827 |
> |
keepAliveTime, MILLISECONDS, |
1828 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1829 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1830 |
|
try { |
1831 |
|
p.allowCoreThreadTimeOut(true); |
1832 |
|
p.execute(new CheckedRunnable() { |
1833 |
< |
public void realRun() throws InterruptedException { |
1833 |
> |
public void realRun() { |
1834 |
|
threadStarted.countDown(); |
1835 |
|
assertEquals(1, p.getPoolSize()); |
1836 |
|
}}); |
1837 |
|
await(threadStarted); |
1838 |
< |
delay(coreThreadTimeOut); |
1838 |
> |
delay(keepAliveTime); |
1839 |
|
long startTime = System.nanoTime(); |
1840 |
|
while (p.getPoolSize() > 0 |
1841 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1851 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1852 |
|
*/ |
1853 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1854 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1854 |
> |
long keepAliveTime = timeoutMillis(); |
1855 |
|
final ThreadPoolExecutor p = |
1856 |
|
new CustomTPE(2, 10, |
1857 |
< |
coreThreadTimeOut, MILLISECONDS, |
1857 |
> |
keepAliveTime, MILLISECONDS, |
1858 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1859 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1860 |
|
try { |
1864 |
|
threadStarted.countDown(); |
1865 |
|
assertTrue(p.getPoolSize() >= 1); |
1866 |
|
}}); |
1867 |
< |
delay(2 * coreThreadTimeOut); |
1867 |
> |
delay(2 * keepAliveTime); |
1868 |
|
assertTrue(p.getPoolSize() >= 1); |
1869 |
|
} finally { |
1870 |
|
joinPool(p); |