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() ; } |
139 |
|
} |
140 |
|
} |
141 |
|
|
118 |
– |
|
142 |
|
static class CustomTPE extends ThreadPoolExecutor { |
143 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
144 |
|
return new CustomTask<V>(c); |
185 |
|
workQueue, threadFactory, handler); |
186 |
|
} |
187 |
|
|
188 |
< |
volatile boolean beforeCalled = false; |
189 |
< |
volatile boolean afterCalled = false; |
190 |
< |
volatile boolean terminatedCalled = false; |
188 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
189 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
190 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
191 |
> |
|
192 |
|
public CustomTPE() { |
193 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
194 |
|
} |
195 |
|
protected void beforeExecute(Thread t, Runnable r) { |
196 |
< |
beforeCalled = true; |
196 |
> |
beforeCalled.countDown(); |
197 |
|
} |
198 |
|
protected void afterExecute(Runnable r, Throwable t) { |
199 |
< |
afterCalled = true; |
199 |
> |
afterCalled.countDown(); |
200 |
|
} |
201 |
|
protected void terminated() { |
202 |
< |
terminatedCalled = true; |
202 |
> |
terminatedCalled.countDown(); |
203 |
|
} |
204 |
|
|
205 |
+ |
public boolean beforeCalled() { |
206 |
+ |
return beforeCalled.getCount() == 0; |
207 |
+ |
} |
208 |
+ |
public boolean afterCalled() { |
209 |
+ |
return afterCalled.getCount() == 0; |
210 |
+ |
} |
211 |
+ |
public boolean terminatedCalled() { |
212 |
+ |
return terminatedCalled.getCount() == 0; |
213 |
+ |
} |
214 |
|
} |
215 |
|
|
216 |
|
static class FailingThreadFactory implements ThreadFactory { |
221 |
|
} |
222 |
|
} |
223 |
|
|
191 |
– |
|
224 |
|
/** |
225 |
|
* execute successfully executes a runnable |
226 |
|
*/ |
318 |
|
threadProceed.await(); |
319 |
|
threadDone.countDown(); |
320 |
|
}}); |
321 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
321 |
> |
await(threadStarted); |
322 |
|
assertEquals(0, p.getCompletedTaskCount()); |
323 |
|
threadProceed.countDown(); |
324 |
|
threadDone.await(); |
325 |
< |
delay(SHORT_DELAY_MS); |
326 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
325 |
> |
long startTime = System.nanoTime(); |
326 |
> |
while (p.getCompletedTaskCount() != 1) { |
327 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
328 |
> |
fail("timed out"); |
329 |
> |
Thread.yield(); |
330 |
> |
} |
331 |
|
} finally { |
332 |
|
joinPool(p); |
333 |
|
} |
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 |
|
|
318 |
– |
|
354 |
|
/** |
355 |
|
* getThreadFactory returns factory in constructor if not set |
356 |
|
*/ |
372 |
|
joinPool(p); |
373 |
|
} |
374 |
|
|
340 |
– |
|
375 |
|
/** |
376 |
|
* setThreadFactory(null) throws NPE |
377 |
|
*/ |
408 |
|
joinPool(p); |
409 |
|
} |
410 |
|
|
377 |
– |
|
411 |
|
/** |
412 |
|
* setRejectedExecutionHandler(null) throws NPE |
413 |
|
*/ |
422 |
|
} |
423 |
|
} |
424 |
|
|
392 |
– |
|
425 |
|
/** |
426 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
427 |
|
* multiple threads active |
527 |
|
joinPool(p); |
528 |
|
} |
529 |
|
|
498 |
– |
|
530 |
|
/** |
531 |
|
* isTerminated is false before termination, true after |
532 |
|
*/ |
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 |
> |
Runnable 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 |
> |
assertEquals(poolSize, p.getActiveCount()); |
725 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
726 |
> |
final List<Runnable> queuedTasks; |
727 |
> |
try { |
728 |
> |
queuedTasks = p.shutdownNow(); |
729 |
> |
} catch (SecurityException ok) { |
730 |
> |
return; // Allowed in case test doesn't have privs |
731 |
|
} |
732 |
|
assertTrue(p.isShutdown()); |
733 |
< |
assertTrue(l.size() <= 4); |
733 |
> |
assertTrue(p.getQueue().isEmpty()); |
734 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
735 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
736 |
> |
assertTrue(p.isTerminated()); |
737 |
> |
assertEquals(poolSize, ran.get()); |
738 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
739 |
|
} |
740 |
|
|
741 |
|
// Exception Tests |
742 |
|
|
692 |
– |
|
743 |
|
/** |
744 |
|
* Constructor throws if corePoolSize argument is less than zero |
745 |
|
*/ |
746 |
|
public void testConstructor1() { |
747 |
|
try { |
748 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
748 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
749 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
750 |
|
shouldThrow(); |
751 |
|
} catch (IllegalArgumentException success) {} |
752 |
|
} |
756 |
|
*/ |
757 |
|
public void testConstructor2() { |
758 |
|
try { |
759 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
759 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
760 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
761 |
|
shouldThrow(); |
762 |
|
} catch (IllegalArgumentException success) {} |
763 |
|
} |
767 |
|
*/ |
768 |
|
public void testConstructor3() { |
769 |
|
try { |
770 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
770 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
771 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
772 |
|
shouldThrow(); |
773 |
|
} catch (IllegalArgumentException success) {} |
774 |
|
} |
778 |
|
*/ |
779 |
|
public void testConstructor4() { |
780 |
|
try { |
781 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
781 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
782 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
783 |
|
shouldThrow(); |
784 |
|
} catch (IllegalArgumentException success) {} |
785 |
|
} |
789 |
|
*/ |
790 |
|
public void testConstructor5() { |
791 |
|
try { |
792 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
792 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
793 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
794 |
|
shouldThrow(); |
795 |
|
} catch (IllegalArgumentException success) {} |
796 |
|
} |
800 |
|
*/ |
801 |
|
public void testConstructorNullPointerException() { |
802 |
|
try { |
803 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
803 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
804 |
|
shouldThrow(); |
805 |
|
} catch (NullPointerException success) {} |
806 |
|
} |
807 |
|
|
753 |
– |
|
754 |
– |
|
808 |
|
/** |
809 |
|
* Constructor throws if corePoolSize argument is less than zero |
810 |
|
*/ |
811 |
|
public void testConstructor6() { |
812 |
|
try { |
813 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
813 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
814 |
> |
new ArrayBlockingQueue<Runnable>(10), |
815 |
> |
new SimpleThreadFactory()); |
816 |
|
shouldThrow(); |
817 |
|
} catch (IllegalArgumentException success) {} |
818 |
|
} |
822 |
|
*/ |
823 |
|
public void testConstructor7() { |
824 |
|
try { |
825 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
825 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
826 |
> |
new ArrayBlockingQueue<Runnable>(10), |
827 |
> |
new SimpleThreadFactory()); |
828 |
|
shouldThrow(); |
829 |
|
} catch (IllegalArgumentException success) {} |
830 |
|
} |
834 |
|
*/ |
835 |
|
public void testConstructor8() { |
836 |
|
try { |
837 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
837 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
838 |
> |
new ArrayBlockingQueue<Runnable>(10), |
839 |
> |
new SimpleThreadFactory()); |
840 |
|
shouldThrow(); |
841 |
|
} catch (IllegalArgumentException success) {} |
842 |
|
} |
846 |
|
*/ |
847 |
|
public void testConstructor9() { |
848 |
|
try { |
849 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
849 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
850 |
> |
new ArrayBlockingQueue<Runnable>(10), |
851 |
> |
new SimpleThreadFactory()); |
852 |
|
shouldThrow(); |
853 |
|
} catch (IllegalArgumentException success) {} |
854 |
|
} |
858 |
|
*/ |
859 |
|
public void testConstructor10() { |
860 |
|
try { |
861 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
861 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
862 |
> |
new ArrayBlockingQueue<Runnable>(10), |
863 |
> |
new SimpleThreadFactory()); |
864 |
|
shouldThrow(); |
865 |
|
} catch (IllegalArgumentException success) {} |
866 |
|
} |
870 |
|
*/ |
871 |
|
public void testConstructorNullPointerException2() { |
872 |
|
try { |
873 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
873 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
874 |
|
shouldThrow(); |
875 |
|
} catch (NullPointerException success) {} |
876 |
|
} |
880 |
|
*/ |
881 |
|
public void testConstructorNullPointerException3() { |
882 |
|
try { |
883 |
< |
ThreadFactory f = null; |
884 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
883 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
884 |
> |
new ArrayBlockingQueue<Runnable>(10), |
885 |
> |
(ThreadFactory) null); |
886 |
|
shouldThrow(); |
887 |
|
} catch (NullPointerException success) {} |
888 |
|
} |
889 |
|
|
826 |
– |
|
890 |
|
/** |
891 |
|
* Constructor throws if corePoolSize argument is less than zero |
892 |
|
*/ |
893 |
|
public void testConstructor11() { |
894 |
|
try { |
895 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
895 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
896 |
> |
new ArrayBlockingQueue<Runnable>(10), |
897 |
> |
new NoOpREHandler()); |
898 |
|
shouldThrow(); |
899 |
|
} catch (IllegalArgumentException success) {} |
900 |
|
} |
904 |
|
*/ |
905 |
|
public void testConstructor12() { |
906 |
|
try { |
907 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
907 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
908 |
> |
new ArrayBlockingQueue<Runnable>(10), |
909 |
> |
new NoOpREHandler()); |
910 |
|
shouldThrow(); |
911 |
|
} catch (IllegalArgumentException success) {} |
912 |
|
} |
916 |
|
*/ |
917 |
|
public void testConstructor13() { |
918 |
|
try { |
919 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
919 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
920 |
> |
new ArrayBlockingQueue<Runnable>(10), |
921 |
> |
new NoOpREHandler()); |
922 |
|
shouldThrow(); |
923 |
|
} catch (IllegalArgumentException success) {} |
924 |
|
} |
928 |
|
*/ |
929 |
|
public void testConstructor14() { |
930 |
|
try { |
931 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
931 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
932 |
> |
new ArrayBlockingQueue<Runnable>(10), |
933 |
> |
new NoOpREHandler()); |
934 |
|
shouldThrow(); |
935 |
|
} catch (IllegalArgumentException success) {} |
936 |
|
} |
940 |
|
*/ |
941 |
|
public void testConstructor15() { |
942 |
|
try { |
943 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
943 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
944 |
> |
new ArrayBlockingQueue<Runnable>(10), |
945 |
> |
new NoOpREHandler()); |
946 |
|
shouldThrow(); |
947 |
|
} catch (IllegalArgumentException success) {} |
948 |
|
} |
952 |
|
*/ |
953 |
|
public void testConstructorNullPointerException4() { |
954 |
|
try { |
955 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
955 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
956 |
> |
null, |
957 |
> |
new NoOpREHandler()); |
958 |
|
shouldThrow(); |
959 |
|
} catch (NullPointerException success) {} |
960 |
|
} |
964 |
|
*/ |
965 |
|
public void testConstructorNullPointerException5() { |
966 |
|
try { |
967 |
< |
RejectedExecutionHandler r = null; |
968 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
967 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
968 |
> |
new ArrayBlockingQueue<Runnable>(10), |
969 |
> |
(RejectedExecutionHandler) null); |
970 |
|
shouldThrow(); |
971 |
|
} catch (NullPointerException success) {} |
972 |
|
} |
973 |
|
|
898 |
– |
|
974 |
|
/** |
975 |
|
* Constructor throws if corePoolSize argument is less than zero |
976 |
|
*/ |
977 |
|
public void testConstructor16() { |
978 |
|
try { |
979 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
979 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
980 |
> |
new ArrayBlockingQueue<Runnable>(10), |
981 |
> |
new SimpleThreadFactory(), |
982 |
> |
new NoOpREHandler()); |
983 |
|
shouldThrow(); |
984 |
|
} catch (IllegalArgumentException success) {} |
985 |
|
} |
989 |
|
*/ |
990 |
|
public void testConstructor17() { |
991 |
|
try { |
992 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
992 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
993 |
> |
new ArrayBlockingQueue<Runnable>(10), |
994 |
> |
new SimpleThreadFactory(), |
995 |
> |
new NoOpREHandler()); |
996 |
|
shouldThrow(); |
997 |
|
} catch (IllegalArgumentException success) {} |
998 |
|
} |
1002 |
|
*/ |
1003 |
|
public void testConstructor18() { |
1004 |
|
try { |
1005 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1005 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
1006 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1007 |
> |
new SimpleThreadFactory(), |
1008 |
> |
new NoOpREHandler()); |
1009 |
|
shouldThrow(); |
1010 |
|
} catch (IllegalArgumentException success) {} |
1011 |
|
} |
1015 |
|
*/ |
1016 |
|
public void testConstructor19() { |
1017 |
|
try { |
1018 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1018 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
1019 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1020 |
> |
new SimpleThreadFactory(), |
1021 |
> |
new NoOpREHandler()); |
1022 |
|
shouldThrow(); |
1023 |
|
} catch (IllegalArgumentException success) {} |
1024 |
|
} |
1028 |
|
*/ |
1029 |
|
public void testConstructor20() { |
1030 |
|
try { |
1031 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1031 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1032 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1033 |
> |
new SimpleThreadFactory(), |
1034 |
> |
new NoOpREHandler()); |
1035 |
|
shouldThrow(); |
1036 |
|
} catch (IllegalArgumentException success) {} |
1037 |
|
} |
1041 |
|
*/ |
1042 |
|
public void testConstructorNullPointerException6() { |
1043 |
|
try { |
1044 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1044 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1045 |
> |
null, |
1046 |
> |
new SimpleThreadFactory(), |
1047 |
> |
new NoOpREHandler()); |
1048 |
|
shouldThrow(); |
1049 |
|
} catch (NullPointerException success) {} |
1050 |
|
} |
1054 |
|
*/ |
1055 |
|
public void testConstructorNullPointerException7() { |
1056 |
|
try { |
1057 |
< |
RejectedExecutionHandler r = null; |
1058 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1057 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1058 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1059 |
> |
new SimpleThreadFactory(), |
1060 |
> |
(RejectedExecutionHandler) null); |
1061 |
|
shouldThrow(); |
1062 |
|
} catch (NullPointerException success) {} |
1063 |
|
} |
1067 |
|
*/ |
1068 |
|
public void testConstructorNullPointerException8() { |
1069 |
|
try { |
1070 |
< |
new CustomTPE(1, 2, |
976 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1070 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1071 |
|
new ArrayBlockingQueue<Runnable>(10), |
1072 |
|
(ThreadFactory) null, |
1073 |
|
new NoOpREHandler()); |
1075 |
|
} catch (NullPointerException success) {} |
1076 |
|
} |
1077 |
|
|
984 |
– |
|
1078 |
|
/** |
1079 |
|
* execute throws RejectedExecutionException if saturated. |
1080 |
|
*/ |
1224 |
|
} |
1225 |
|
} |
1226 |
|
|
1134 |
– |
|
1227 |
|
/** |
1228 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1229 |
|
*/ |
1241 |
|
} |
1242 |
|
} |
1243 |
|
|
1152 |
– |
|
1244 |
|
/** |
1245 |
|
* execute(null) throws NPE |
1246 |
|
*/ |
1247 |
|
public void testExecuteNull() { |
1248 |
< |
ThreadPoolExecutor p = null; |
1248 |
> |
ThreadPoolExecutor p = |
1249 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1250 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1251 |
|
try { |
1159 |
– |
p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1252 |
|
p.execute(null); |
1253 |
|
shouldThrow(); |
1254 |
|
} catch (NullPointerException success) {} |
1306 |
|
joinPool(p); |
1307 |
|
} |
1308 |
|
|
1217 |
– |
|
1309 |
|
/** |
1310 |
|
* setKeepAliveTime throws IllegalArgumentException |
1311 |
|
* when given a negative value |
1330 |
|
public void testTerminated() { |
1331 |
|
CustomTPE p = new CustomTPE(); |
1332 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1333 |
< |
assertTrue(p.terminatedCalled); |
1333 |
> |
assertTrue(p.terminatedCalled()); |
1334 |
|
joinPool(p); |
1335 |
|
} |
1336 |
|
|
1340 |
|
public void testBeforeAfter() throws InterruptedException { |
1341 |
|
CustomTPE p = new CustomTPE(); |
1342 |
|
try { |
1343 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1344 |
< |
p.execute(r); |
1345 |
< |
delay(SHORT_DELAY_MS); |
1346 |
< |
assertTrue(r.done); |
1347 |
< |
assertTrue(p.beforeCalled); |
1348 |
< |
assertTrue(p.afterCalled); |
1343 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1344 |
> |
p.execute(new CheckedRunnable() { |
1345 |
> |
public void realRun() { |
1346 |
> |
done.countDown(); |
1347 |
> |
}}); |
1348 |
> |
await(p.afterCalled); |
1349 |
> |
assertEquals(0, done.getCount()); |
1350 |
> |
assertTrue(p.afterCalled()); |
1351 |
> |
assertTrue(p.beforeCalled()); |
1352 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1353 |
|
} finally { |
1354 |
|
joinPool(p); |
1397 |
|
} |
1398 |
|
} |
1399 |
|
|
1306 |
– |
|
1400 |
|
/** |
1401 |
|
* invokeAny(null) throws NPE |
1402 |
|
*/ |
1558 |
|
} |
1559 |
|
} |
1560 |
|
|
1468 |
– |
|
1469 |
– |
|
1561 |
|
/** |
1562 |
|
* timed invokeAny(null) throws NPE |
1563 |
|
*/ |
1759 |
|
public void testTimedInvokeAll6() throws Exception { |
1760 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1761 |
|
try { |
1762 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1763 |
< |
l.add(new StringTask()); |
1764 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1765 |
< |
l.add(new StringTask()); |
1766 |
< |
List<Future<String>> futures = |
1767 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1768 |
< |
assertEquals(3, futures.size()); |
1769 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1770 |
< |
Future<String> f1 = it.next(); |
1771 |
< |
Future<String> f2 = it.next(); |
1772 |
< |
Future<String> f3 = it.next(); |
1773 |
< |
assertTrue(f1.isDone()); |
1774 |
< |
assertTrue(f2.isDone()); |
1775 |
< |
assertTrue(f3.isDone()); |
1776 |
< |
assertFalse(f1.isCancelled()); |
1777 |
< |
assertTrue(f2.isCancelled()); |
1762 |
> |
for (long timeout = timeoutMillis();;) { |
1763 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1764 |
> |
tasks.add(new StringTask("0")); |
1765 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1766 |
> |
tasks.add(new StringTask("2")); |
1767 |
> |
long startTime = System.nanoTime(); |
1768 |
> |
List<Future<String>> futures = |
1769 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1770 |
> |
assertEquals(tasks.size(), futures.size()); |
1771 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1772 |
> |
for (Future future : futures) |
1773 |
> |
assertTrue(future.isDone()); |
1774 |
> |
assertTrue(futures.get(1).isCancelled()); |
1775 |
> |
try { |
1776 |
> |
assertEquals("0", futures.get(0).get()); |
1777 |
> |
assertEquals("2", futures.get(2).get()); |
1778 |
> |
break; |
1779 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1780 |
> |
timeout *= 2; |
1781 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1782 |
> |
fail("expected exactly one task to be cancelled"); |
1783 |
> |
} |
1784 |
> |
} |
1785 |
|
} finally { |
1786 |
|
joinPool(e); |
1787 |
|
} |
1824 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1825 |
|
*/ |
1826 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1827 |
+ |
long keepAliveTime = timeoutMillis(); |
1828 |
|
final ThreadPoolExecutor p = |
1829 |
|
new CustomTPE(2, 10, |
1830 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1830 |
> |
keepAliveTime, MILLISECONDS, |
1831 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1832 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1833 |
|
try { |
1834 |
|
p.allowCoreThreadTimeOut(true); |
1835 |
|
p.execute(new CheckedRunnable() { |
1836 |
< |
public void realRun() throws InterruptedException { |
1836 |
> |
public void realRun() { |
1837 |
|
threadStarted.countDown(); |
1838 |
|
assertEquals(1, p.getPoolSize()); |
1839 |
|
}}); |
1840 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1841 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1842 |
< |
if (p.getPoolSize() == 0) |
1843 |
< |
break; |
1844 |
< |
delay(10); |
1845 |
< |
} |
1840 |
> |
await(threadStarted); |
1841 |
> |
delay(keepAliveTime); |
1842 |
> |
long startTime = System.nanoTime(); |
1843 |
> |
while (p.getPoolSize() > 0 |
1844 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1845 |
> |
Thread.yield(); |
1846 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1847 |
|
assertEquals(0, p.getPoolSize()); |
1848 |
|
} finally { |
1849 |
|
joinPool(p); |
1854 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1855 |
|
*/ |
1856 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1857 |
+ |
long keepAliveTime = timeoutMillis(); |
1858 |
|
final ThreadPoolExecutor p = |
1859 |
|
new CustomTPE(2, 10, |
1860 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1860 |
> |
keepAliveTime, MILLISECONDS, |
1861 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1862 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1863 |
|
try { |
1867 |
|
threadStarted.countDown(); |
1868 |
|
assertTrue(p.getPoolSize() >= 1); |
1869 |
|
}}); |
1870 |
< |
delay(SMALL_DELAY_MS); |
1870 |
> |
delay(2 * keepAliveTime); |
1871 |
|
assertTrue(p.getPoolSize() >= 1); |
1872 |
|
} finally { |
1873 |
|
joinPool(p); |