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.locks.Condition; |
34 |
> |
import java.util.concurrent.locks.ReentrantLock; |
35 |
> |
|
36 |
> |
import junit.framework.Test; |
37 |
> |
import junit.framework.TestSuite; |
38 |
|
|
39 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
40 |
|
public static void main(String[] args) { |
41 |
< |
junit.textui.TestRunner.run(suite()); |
41 |
> |
main(suite(), args); |
42 |
|
} |
43 |
|
public static Test suite() { |
44 |
|
return new TestSuite(ThreadPoolExecutorSubclassTest.class); |
60 |
|
CustomTask(final Runnable r, final V res) { |
61 |
|
if (r == null) throw new NullPointerException(); |
62 |
|
callable = new Callable<V>() { |
63 |
< |
public V call() throws Exception { r.run(); return res; }}; |
63 |
> |
public V call() throws Exception { r.run(); return res; }}; |
64 |
|
} |
65 |
|
public boolean isDone() { |
66 |
|
lock.lock(); try { return done; } finally { lock.unlock() ; } |
138 |
|
} |
139 |
|
} |
140 |
|
|
118 |
– |
|
141 |
|
static class CustomTPE extends ThreadPoolExecutor { |
142 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
143 |
|
return new CustomTask<V>(c); |
184 |
|
workQueue, threadFactory, handler); |
185 |
|
} |
186 |
|
|
187 |
< |
volatile boolean beforeCalled = false; |
188 |
< |
volatile boolean afterCalled = false; |
189 |
< |
volatile boolean terminatedCalled = false; |
187 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
188 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
189 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
190 |
> |
|
191 |
|
public CustomTPE() { |
192 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
193 |
|
} |
194 |
|
protected void beforeExecute(Thread t, Runnable r) { |
195 |
< |
beforeCalled = true; |
195 |
> |
beforeCalled.countDown(); |
196 |
|
} |
197 |
|
protected void afterExecute(Runnable r, Throwable t) { |
198 |
< |
afterCalled = true; |
198 |
> |
afterCalled.countDown(); |
199 |
|
} |
200 |
|
protected void terminated() { |
201 |
< |
terminatedCalled = true; |
201 |
> |
terminatedCalled.countDown(); |
202 |
|
} |
203 |
|
|
204 |
+ |
public boolean beforeCalled() { |
205 |
+ |
return beforeCalled.getCount() == 0; |
206 |
+ |
} |
207 |
+ |
public boolean afterCalled() { |
208 |
+ |
return afterCalled.getCount() == 0; |
209 |
+ |
} |
210 |
+ |
public boolean terminatedCalled() { |
211 |
+ |
return terminatedCalled.getCount() == 0; |
212 |
+ |
} |
213 |
|
} |
214 |
|
|
215 |
|
static class FailingThreadFactory implements ThreadFactory { |
220 |
|
} |
221 |
|
} |
222 |
|
|
191 |
– |
|
223 |
|
/** |
224 |
|
* execute successfully executes a runnable |
225 |
|
*/ |
317 |
|
threadProceed.await(); |
318 |
|
threadDone.countDown(); |
319 |
|
}}); |
320 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
320 |
> |
await(threadStarted); |
321 |
|
assertEquals(0, p.getCompletedTaskCount()); |
322 |
|
threadProceed.countDown(); |
323 |
|
threadDone.await(); |
324 |
< |
delay(SHORT_DELAY_MS); |
325 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
324 |
> |
long startTime = System.nanoTime(); |
325 |
> |
while (p.getCompletedTaskCount() != 1) { |
326 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
327 |
> |
fail("timed out"); |
328 |
> |
Thread.yield(); |
329 |
> |
} |
330 |
|
} finally { |
331 |
|
joinPool(p); |
332 |
|
} |
346 |
|
*/ |
347 |
|
public void testGetKeepAliveTime() { |
348 |
|
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
349 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
349 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
350 |
|
joinPool(p); |
351 |
|
} |
352 |
|
|
318 |
– |
|
353 |
|
/** |
354 |
|
* getThreadFactory returns factory in constructor if not set |
355 |
|
*/ |
371 |
|
joinPool(p); |
372 |
|
} |
373 |
|
|
340 |
– |
|
374 |
|
/** |
375 |
|
* setThreadFactory(null) throws NPE |
376 |
|
*/ |
407 |
|
joinPool(p); |
408 |
|
} |
409 |
|
|
377 |
– |
|
410 |
|
/** |
411 |
|
* setRejectedExecutionHandler(null) throws NPE |
412 |
|
*/ |
421 |
|
} |
422 |
|
} |
423 |
|
|
392 |
– |
|
424 |
|
/** |
425 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
426 |
|
* multiple threads active |
526 |
|
joinPool(p); |
527 |
|
} |
528 |
|
|
498 |
– |
|
529 |
|
/** |
530 |
|
* isTerminated is false before termination, true after |
531 |
|
*/ |
699 |
|
} |
700 |
|
|
701 |
|
/** |
702 |
< |
* shutdownNow returns a list containing tasks that were not run |
702 |
> |
* shutdownNow returns a list containing tasks that were not run, |
703 |
> |
* and those tasks are drained from the queue |
704 |
|
*/ |
705 |
|
public void testShutdownNow() { |
706 |
|
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
715 |
|
} catch (SecurityException ok) { return; } |
716 |
|
} |
717 |
|
assertTrue(p.isShutdown()); |
718 |
+ |
assertTrue(p.getQueue().isEmpty()); |
719 |
|
assertTrue(l.size() <= 4); |
720 |
|
} |
721 |
|
|
722 |
|
// Exception Tests |
723 |
|
|
692 |
– |
|
724 |
|
/** |
725 |
|
* Constructor throws if corePoolSize argument is less than zero |
726 |
|
*/ |
727 |
|
public void testConstructor1() { |
728 |
|
try { |
729 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
729 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
730 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
731 |
|
shouldThrow(); |
732 |
|
} catch (IllegalArgumentException success) {} |
733 |
|
} |
737 |
|
*/ |
738 |
|
public void testConstructor2() { |
739 |
|
try { |
740 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
740 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
741 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
742 |
|
shouldThrow(); |
743 |
|
} catch (IllegalArgumentException success) {} |
744 |
|
} |
748 |
|
*/ |
749 |
|
public void testConstructor3() { |
750 |
|
try { |
751 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
751 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
752 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
753 |
|
shouldThrow(); |
754 |
|
} catch (IllegalArgumentException success) {} |
755 |
|
} |
759 |
|
*/ |
760 |
|
public void testConstructor4() { |
761 |
|
try { |
762 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
762 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
763 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
764 |
|
shouldThrow(); |
765 |
|
} catch (IllegalArgumentException success) {} |
766 |
|
} |
770 |
|
*/ |
771 |
|
public void testConstructor5() { |
772 |
|
try { |
773 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
773 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
774 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
775 |
|
shouldThrow(); |
776 |
|
} catch (IllegalArgumentException success) {} |
777 |
|
} |
781 |
|
*/ |
782 |
|
public void testConstructorNullPointerException() { |
783 |
|
try { |
784 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
784 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
785 |
|
shouldThrow(); |
786 |
|
} catch (NullPointerException success) {} |
787 |
|
} |
788 |
|
|
753 |
– |
|
754 |
– |
|
789 |
|
/** |
790 |
|
* Constructor throws if corePoolSize argument is less than zero |
791 |
|
*/ |
792 |
|
public void testConstructor6() { |
793 |
|
try { |
794 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
794 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
795 |
> |
new ArrayBlockingQueue<Runnable>(10), |
796 |
> |
new SimpleThreadFactory()); |
797 |
|
shouldThrow(); |
798 |
|
} catch (IllegalArgumentException success) {} |
799 |
|
} |
803 |
|
*/ |
804 |
|
public void testConstructor7() { |
805 |
|
try { |
806 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
806 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
807 |
> |
new ArrayBlockingQueue<Runnable>(10), |
808 |
> |
new SimpleThreadFactory()); |
809 |
|
shouldThrow(); |
810 |
|
} catch (IllegalArgumentException success) {} |
811 |
|
} |
815 |
|
*/ |
816 |
|
public void testConstructor8() { |
817 |
|
try { |
818 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
818 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
819 |
> |
new ArrayBlockingQueue<Runnable>(10), |
820 |
> |
new SimpleThreadFactory()); |
821 |
|
shouldThrow(); |
822 |
|
} catch (IllegalArgumentException success) {} |
823 |
|
} |
827 |
|
*/ |
828 |
|
public void testConstructor9() { |
829 |
|
try { |
830 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
830 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
831 |
> |
new ArrayBlockingQueue<Runnable>(10), |
832 |
> |
new SimpleThreadFactory()); |
833 |
|
shouldThrow(); |
834 |
|
} catch (IllegalArgumentException success) {} |
835 |
|
} |
839 |
|
*/ |
840 |
|
public void testConstructor10() { |
841 |
|
try { |
842 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
842 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
843 |
> |
new ArrayBlockingQueue<Runnable>(10), |
844 |
> |
new SimpleThreadFactory()); |
845 |
|
shouldThrow(); |
846 |
|
} catch (IllegalArgumentException success) {} |
847 |
|
} |
851 |
|
*/ |
852 |
|
public void testConstructorNullPointerException2() { |
853 |
|
try { |
854 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
854 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
855 |
|
shouldThrow(); |
856 |
|
} catch (NullPointerException success) {} |
857 |
|
} |
861 |
|
*/ |
862 |
|
public void testConstructorNullPointerException3() { |
863 |
|
try { |
864 |
< |
ThreadFactory f = null; |
865 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
864 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
865 |
> |
new ArrayBlockingQueue<Runnable>(10), |
866 |
> |
(ThreadFactory) null); |
867 |
|
shouldThrow(); |
868 |
|
} catch (NullPointerException success) {} |
869 |
|
} |
870 |
|
|
826 |
– |
|
871 |
|
/** |
872 |
|
* Constructor throws if corePoolSize argument is less than zero |
873 |
|
*/ |
874 |
|
public void testConstructor11() { |
875 |
|
try { |
876 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
876 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
877 |
> |
new ArrayBlockingQueue<Runnable>(10), |
878 |
> |
new NoOpREHandler()); |
879 |
|
shouldThrow(); |
880 |
|
} catch (IllegalArgumentException success) {} |
881 |
|
} |
885 |
|
*/ |
886 |
|
public void testConstructor12() { |
887 |
|
try { |
888 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
888 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
889 |
> |
new ArrayBlockingQueue<Runnable>(10), |
890 |
> |
new NoOpREHandler()); |
891 |
|
shouldThrow(); |
892 |
|
} catch (IllegalArgumentException success) {} |
893 |
|
} |
897 |
|
*/ |
898 |
|
public void testConstructor13() { |
899 |
|
try { |
900 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
900 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
901 |
> |
new ArrayBlockingQueue<Runnable>(10), |
902 |
> |
new NoOpREHandler()); |
903 |
|
shouldThrow(); |
904 |
|
} catch (IllegalArgumentException success) {} |
905 |
|
} |
909 |
|
*/ |
910 |
|
public void testConstructor14() { |
911 |
|
try { |
912 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
912 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
913 |
> |
new ArrayBlockingQueue<Runnable>(10), |
914 |
> |
new NoOpREHandler()); |
915 |
|
shouldThrow(); |
916 |
|
} catch (IllegalArgumentException success) {} |
917 |
|
} |
921 |
|
*/ |
922 |
|
public void testConstructor15() { |
923 |
|
try { |
924 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
924 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
925 |
> |
new ArrayBlockingQueue<Runnable>(10), |
926 |
> |
new NoOpREHandler()); |
927 |
|
shouldThrow(); |
928 |
|
} catch (IllegalArgumentException success) {} |
929 |
|
} |
933 |
|
*/ |
934 |
|
public void testConstructorNullPointerException4() { |
935 |
|
try { |
936 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
936 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
937 |
> |
null, |
938 |
> |
new NoOpREHandler()); |
939 |
|
shouldThrow(); |
940 |
|
} catch (NullPointerException success) {} |
941 |
|
} |
945 |
|
*/ |
946 |
|
public void testConstructorNullPointerException5() { |
947 |
|
try { |
948 |
< |
RejectedExecutionHandler r = null; |
949 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
948 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
949 |
> |
new ArrayBlockingQueue<Runnable>(10), |
950 |
> |
(RejectedExecutionHandler) null); |
951 |
|
shouldThrow(); |
952 |
|
} catch (NullPointerException success) {} |
953 |
|
} |
954 |
|
|
898 |
– |
|
955 |
|
/** |
956 |
|
* Constructor throws if corePoolSize argument is less than zero |
957 |
|
*/ |
958 |
|
public void testConstructor16() { |
959 |
|
try { |
960 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
960 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
961 |
> |
new ArrayBlockingQueue<Runnable>(10), |
962 |
> |
new SimpleThreadFactory(), |
963 |
> |
new NoOpREHandler()); |
964 |
|
shouldThrow(); |
965 |
|
} catch (IllegalArgumentException success) {} |
966 |
|
} |
970 |
|
*/ |
971 |
|
public void testConstructor17() { |
972 |
|
try { |
973 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
973 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
974 |
> |
new ArrayBlockingQueue<Runnable>(10), |
975 |
> |
new SimpleThreadFactory(), |
976 |
> |
new NoOpREHandler()); |
977 |
|
shouldThrow(); |
978 |
|
} catch (IllegalArgumentException success) {} |
979 |
|
} |
983 |
|
*/ |
984 |
|
public void testConstructor18() { |
985 |
|
try { |
986 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
986 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
987 |
> |
new ArrayBlockingQueue<Runnable>(10), |
988 |
> |
new SimpleThreadFactory(), |
989 |
> |
new NoOpREHandler()); |
990 |
|
shouldThrow(); |
991 |
|
} catch (IllegalArgumentException success) {} |
992 |
|
} |
996 |
|
*/ |
997 |
|
public void testConstructor19() { |
998 |
|
try { |
999 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
999 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
1000 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1001 |
> |
new SimpleThreadFactory(), |
1002 |
> |
new NoOpREHandler()); |
1003 |
|
shouldThrow(); |
1004 |
|
} catch (IllegalArgumentException success) {} |
1005 |
|
} |
1009 |
|
*/ |
1010 |
|
public void testConstructor20() { |
1011 |
|
try { |
1012 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1012 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1013 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1014 |
> |
new SimpleThreadFactory(), |
1015 |
> |
new NoOpREHandler()); |
1016 |
|
shouldThrow(); |
1017 |
|
} catch (IllegalArgumentException success) {} |
1018 |
|
} |
1022 |
|
*/ |
1023 |
|
public void testConstructorNullPointerException6() { |
1024 |
|
try { |
1025 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1025 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1026 |
> |
null, |
1027 |
> |
new SimpleThreadFactory(), |
1028 |
> |
new NoOpREHandler()); |
1029 |
|
shouldThrow(); |
1030 |
|
} catch (NullPointerException success) {} |
1031 |
|
} |
1035 |
|
*/ |
1036 |
|
public void testConstructorNullPointerException7() { |
1037 |
|
try { |
1038 |
< |
RejectedExecutionHandler r = null; |
1039 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1038 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1039 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1040 |
> |
new SimpleThreadFactory(), |
1041 |
> |
(RejectedExecutionHandler) null); |
1042 |
|
shouldThrow(); |
1043 |
|
} catch (NullPointerException success) {} |
1044 |
|
} |
1048 |
|
*/ |
1049 |
|
public void testConstructorNullPointerException8() { |
1050 |
|
try { |
1051 |
< |
new CustomTPE(1, 2, |
976 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1051 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1052 |
|
new ArrayBlockingQueue<Runnable>(10), |
1053 |
|
(ThreadFactory) null, |
1054 |
|
new NoOpREHandler()); |
1056 |
|
} catch (NullPointerException success) {} |
1057 |
|
} |
1058 |
|
|
984 |
– |
|
1059 |
|
/** |
1060 |
|
* execute throws RejectedExecutionException if saturated. |
1061 |
|
*/ |
1205 |
|
} |
1206 |
|
} |
1207 |
|
|
1134 |
– |
|
1208 |
|
/** |
1209 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1210 |
|
*/ |
1222 |
|
} |
1223 |
|
} |
1224 |
|
|
1152 |
– |
|
1225 |
|
/** |
1226 |
|
* execute(null) throws NPE |
1227 |
|
*/ |
1228 |
|
public void testExecuteNull() { |
1229 |
< |
ThreadPoolExecutor p = null; |
1229 |
> |
ThreadPoolExecutor p = |
1230 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1231 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1232 |
|
try { |
1159 |
– |
p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1233 |
|
p.execute(null); |
1234 |
|
shouldThrow(); |
1235 |
|
} catch (NullPointerException success) {} |
1287 |
|
joinPool(p); |
1288 |
|
} |
1289 |
|
|
1217 |
– |
|
1290 |
|
/** |
1291 |
|
* setKeepAliveTime throws IllegalArgumentException |
1292 |
|
* when given a negative value |
1311 |
|
public void testTerminated() { |
1312 |
|
CustomTPE p = new CustomTPE(); |
1313 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1314 |
< |
assertTrue(p.terminatedCalled); |
1314 |
> |
assertTrue(p.terminatedCalled()); |
1315 |
|
joinPool(p); |
1316 |
|
} |
1317 |
|
|
1321 |
|
public void testBeforeAfter() throws InterruptedException { |
1322 |
|
CustomTPE p = new CustomTPE(); |
1323 |
|
try { |
1324 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1325 |
< |
p.execute(r); |
1326 |
< |
delay(SHORT_DELAY_MS); |
1327 |
< |
assertTrue(r.done); |
1328 |
< |
assertTrue(p.beforeCalled); |
1329 |
< |
assertTrue(p.afterCalled); |
1324 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1325 |
> |
p.execute(new CheckedRunnable() { |
1326 |
> |
public void realRun() { |
1327 |
> |
done.countDown(); |
1328 |
> |
}}); |
1329 |
> |
await(p.afterCalled); |
1330 |
> |
assertEquals(0, done.getCount()); |
1331 |
> |
assertTrue(p.afterCalled()); |
1332 |
> |
assertTrue(p.beforeCalled()); |
1333 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1334 |
|
} finally { |
1335 |
|
joinPool(p); |
1378 |
|
} |
1379 |
|
} |
1380 |
|
|
1306 |
– |
|
1381 |
|
/** |
1382 |
|
* invokeAny(null) throws NPE |
1383 |
|
*/ |
1539 |
|
} |
1540 |
|
} |
1541 |
|
|
1468 |
– |
|
1469 |
– |
|
1542 |
|
/** |
1543 |
|
* timed invokeAny(null) throws NPE |
1544 |
|
*/ |
1740 |
|
public void testTimedInvokeAll6() throws Exception { |
1741 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1742 |
|
try { |
1743 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1744 |
< |
l.add(new StringTask()); |
1745 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1746 |
< |
l.add(new StringTask()); |
1747 |
< |
List<Future<String>> futures = |
1748 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1749 |
< |
assertEquals(3, futures.size()); |
1750 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1751 |
< |
Future<String> f1 = it.next(); |
1752 |
< |
Future<String> f2 = it.next(); |
1753 |
< |
Future<String> f3 = it.next(); |
1754 |
< |
assertTrue(f1.isDone()); |
1755 |
< |
assertTrue(f2.isDone()); |
1756 |
< |
assertTrue(f3.isDone()); |
1757 |
< |
assertFalse(f1.isCancelled()); |
1758 |
< |
assertTrue(f2.isCancelled()); |
1743 |
> |
for (long timeout = timeoutMillis();;) { |
1744 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1745 |
> |
tasks.add(new StringTask("0")); |
1746 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1747 |
> |
tasks.add(new StringTask("2")); |
1748 |
> |
long startTime = System.nanoTime(); |
1749 |
> |
List<Future<String>> futures = |
1750 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1751 |
> |
assertEquals(tasks.size(), futures.size()); |
1752 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1753 |
> |
for (Future future : futures) |
1754 |
> |
assertTrue(future.isDone()); |
1755 |
> |
assertTrue(futures.get(1).isCancelled()); |
1756 |
> |
try { |
1757 |
> |
assertEquals("0", futures.get(0).get()); |
1758 |
> |
assertEquals("2", futures.get(2).get()); |
1759 |
> |
break; |
1760 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1761 |
> |
timeout *= 2; |
1762 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1763 |
> |
fail("expected exactly one task to be cancelled"); |
1764 |
> |
} |
1765 |
> |
} |
1766 |
|
} finally { |
1767 |
|
joinPool(e); |
1768 |
|
} |
1805 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1806 |
|
*/ |
1807 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1808 |
+ |
long keepAliveTime = timeoutMillis(); |
1809 |
|
final ThreadPoolExecutor p = |
1810 |
|
new CustomTPE(2, 10, |
1811 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1811 |
> |
keepAliveTime, MILLISECONDS, |
1812 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1813 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1814 |
|
try { |
1815 |
|
p.allowCoreThreadTimeOut(true); |
1816 |
|
p.execute(new CheckedRunnable() { |
1817 |
< |
public void realRun() throws InterruptedException { |
1817 |
> |
public void realRun() { |
1818 |
|
threadStarted.countDown(); |
1819 |
|
assertEquals(1, p.getPoolSize()); |
1820 |
|
}}); |
1821 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1822 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1823 |
< |
if (p.getPoolSize() == 0) |
1824 |
< |
break; |
1825 |
< |
delay(10); |
1826 |
< |
} |
1821 |
> |
await(threadStarted); |
1822 |
> |
delay(keepAliveTime); |
1823 |
> |
long startTime = System.nanoTime(); |
1824 |
> |
while (p.getPoolSize() > 0 |
1825 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1826 |
> |
Thread.yield(); |
1827 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1828 |
|
assertEquals(0, p.getPoolSize()); |
1829 |
|
} finally { |
1830 |
|
joinPool(p); |
1835 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1836 |
|
*/ |
1837 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1838 |
+ |
long keepAliveTime = timeoutMillis(); |
1839 |
|
final ThreadPoolExecutor p = |
1840 |
|
new CustomTPE(2, 10, |
1841 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1841 |
> |
keepAliveTime, MILLISECONDS, |
1842 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1843 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1844 |
|
try { |
1848 |
|
threadStarted.countDown(); |
1849 |
|
assertTrue(p.getPoolSize() >= 1); |
1850 |
|
}}); |
1851 |
< |
delay(SMALL_DELAY_MS); |
1851 |
> |
delay(2 * keepAliveTime); |
1852 |
|
assertTrue(p.getPoolSize() >= 1); |
1853 |
|
} finally { |
1854 |
|
joinPool(p); |