1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
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() ; } |
101 |
|
} |
102 |
|
lock.lock(); |
103 |
|
try { |
104 |
< |
result = v; |
105 |
< |
exception = e; |
106 |
< |
done = true; |
107 |
< |
thread = null; |
108 |
< |
cond.signalAll(); |
104 |
> |
if (!done) { |
105 |
> |
result = v; |
106 |
> |
exception = e; |
107 |
> |
done = true; |
108 |
> |
thread = null; |
109 |
> |
cond.signalAll(); |
110 |
> |
} |
111 |
|
} |
112 |
|
finally { lock.unlock(); } |
113 |
|
} |
116 |
|
try { |
117 |
|
while (!done) |
118 |
|
cond.await(); |
119 |
+ |
if (cancelled) |
120 |
+ |
throw new CancellationException(); |
121 |
|
if (exception != null) |
122 |
|
throw new ExecutionException(exception); |
123 |
|
return result; |
129 |
|
long nanos = unit.toNanos(timeout); |
130 |
|
lock.lock(); |
131 |
|
try { |
132 |
< |
for (;;) { |
105 |
< |
if (done) break; |
132 |
> |
while (!done) { |
133 |
|
if (nanos < 0) |
134 |
|
throw new TimeoutException(); |
135 |
|
nanos = cond.awaitNanos(nanos); |
136 |
|
} |
137 |
+ |
if (cancelled) |
138 |
+ |
throw new CancellationException(); |
139 |
|
if (exception != null) |
140 |
|
throw new ExecutionException(exception); |
141 |
|
return result; |
144 |
|
} |
145 |
|
} |
146 |
|
|
118 |
– |
|
147 |
|
static class CustomTPE extends ThreadPoolExecutor { |
148 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
149 |
|
return new CustomTask<V>(c); |
190 |
|
workQueue, threadFactory, handler); |
191 |
|
} |
192 |
|
|
193 |
< |
volatile boolean beforeCalled = false; |
194 |
< |
volatile boolean afterCalled = false; |
195 |
< |
volatile boolean terminatedCalled = false; |
193 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
194 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
195 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
196 |
> |
|
197 |
|
public CustomTPE() { |
198 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
199 |
|
} |
200 |
|
protected void beforeExecute(Thread t, Runnable r) { |
201 |
< |
beforeCalled = true; |
201 |
> |
beforeCalled.countDown(); |
202 |
|
} |
203 |
|
protected void afterExecute(Runnable r, Throwable t) { |
204 |
< |
afterCalled = true; |
204 |
> |
afterCalled.countDown(); |
205 |
|
} |
206 |
|
protected void terminated() { |
207 |
< |
terminatedCalled = true; |
207 |
> |
terminatedCalled.countDown(); |
208 |
|
} |
209 |
|
|
210 |
+ |
public boolean beforeCalled() { |
211 |
+ |
return beforeCalled.getCount() == 0; |
212 |
+ |
} |
213 |
+ |
public boolean afterCalled() { |
214 |
+ |
return afterCalled.getCount() == 0; |
215 |
+ |
} |
216 |
+ |
public boolean terminatedCalled() { |
217 |
+ |
return terminatedCalled.getCount() == 0; |
218 |
+ |
} |
219 |
|
} |
220 |
|
|
221 |
|
static class FailingThreadFactory implements ThreadFactory { |
226 |
|
} |
227 |
|
} |
228 |
|
|
191 |
– |
|
229 |
|
/** |
230 |
|
* execute successfully executes a runnable |
231 |
|
*/ |
323 |
|
threadProceed.await(); |
324 |
|
threadDone.countDown(); |
325 |
|
}}); |
326 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
326 |
> |
await(threadStarted); |
327 |
|
assertEquals(0, p.getCompletedTaskCount()); |
328 |
|
threadProceed.countDown(); |
329 |
|
threadDone.await(); |
330 |
< |
Thread.sleep(SHORT_DELAY_MS); |
331 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
330 |
> |
long startTime = System.nanoTime(); |
331 |
> |
while (p.getCompletedTaskCount() != 1) { |
332 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
333 |
> |
fail("timed out"); |
334 |
> |
Thread.yield(); |
335 |
> |
} |
336 |
|
} finally { |
337 |
|
joinPool(p); |
338 |
|
} |
352 |
|
*/ |
353 |
|
public void testGetKeepAliveTime() { |
354 |
|
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
355 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
355 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
356 |
|
joinPool(p); |
357 |
|
} |
358 |
|
|
318 |
– |
|
359 |
|
/** |
360 |
|
* getThreadFactory returns factory in constructor if not set |
361 |
|
*/ |
377 |
|
joinPool(p); |
378 |
|
} |
379 |
|
|
340 |
– |
|
380 |
|
/** |
381 |
|
* setThreadFactory(null) throws NPE |
382 |
|
*/ |
413 |
|
joinPool(p); |
414 |
|
} |
415 |
|
|
377 |
– |
|
416 |
|
/** |
417 |
|
* setRejectedExecutionHandler(null) throws NPE |
418 |
|
*/ |
427 |
|
} |
428 |
|
} |
429 |
|
|
392 |
– |
|
430 |
|
/** |
431 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
432 |
|
* multiple threads active |
521 |
|
} |
522 |
|
|
523 |
|
/** |
524 |
< |
* isShutDown is false before shutdown, true after |
524 |
> |
* isShutdown is false before shutdown, true after |
525 |
|
*/ |
526 |
|
public void testIsShutdown() { |
527 |
|
|
532 |
|
joinPool(p); |
533 |
|
} |
534 |
|
|
498 |
– |
|
535 |
|
/** |
536 |
|
* isTerminated is false before termination, true after |
537 |
|
*/ |
546 |
|
assertFalse(p.isTerminating()); |
547 |
|
p.execute(new CheckedRunnable() { |
548 |
|
public void realRun() throws InterruptedException { |
513 |
– |
threadStarted.countDown(); |
549 |
|
assertFalse(p.isTerminating()); |
550 |
+ |
threadStarted.countDown(); |
551 |
|
done.await(); |
552 |
|
}}); |
553 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
575 |
|
assertFalse(p.isTerminating()); |
576 |
|
p.execute(new CheckedRunnable() { |
577 |
|
public void realRun() throws InterruptedException { |
542 |
– |
threadStarted.countDown(); |
578 |
|
assertFalse(p.isTerminating()); |
579 |
+ |
threadStarted.countDown(); |
580 |
|
done.await(); |
581 |
|
}}); |
582 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
705 |
|
} |
706 |
|
|
707 |
|
/** |
708 |
< |
* shutDownNow returns a list containing tasks that were not run |
708 |
> |
* shutdownNow returns a list containing tasks that were not run, |
709 |
> |
* and those tasks are drained from the queue |
710 |
|
*/ |
711 |
< |
public void testShutDownNow() { |
712 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
713 |
< |
List l; |
714 |
< |
try { |
715 |
< |
for (int i = 0; i < 5; i++) |
716 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
717 |
< |
} |
718 |
< |
finally { |
711 |
> |
public void testShutdownNow() throws InterruptedException { |
712 |
> |
final int poolSize = 2; |
713 |
> |
final int count = 5; |
714 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
715 |
> |
ThreadPoolExecutor p = |
716 |
> |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
717 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
718 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
719 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
720 |
> |
threadsStarted.countDown(); |
721 |
|
try { |
722 |
< |
l = p.shutdownNow(); |
723 |
< |
} catch (SecurityException ok) { return; } |
722 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
723 |
> |
} catch (InterruptedException success) {} |
724 |
> |
ran.getAndIncrement(); |
725 |
> |
}}; |
726 |
> |
for (int i = 0; i < count; i++) |
727 |
> |
p.execute(waiter); |
728 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
729 |
> |
assertEquals(poolSize, p.getActiveCount()); |
730 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
731 |
> |
final List<Runnable> queuedTasks; |
732 |
> |
try { |
733 |
> |
queuedTasks = p.shutdownNow(); |
734 |
> |
} catch (SecurityException ok) { |
735 |
> |
return; // Allowed in case test doesn't have privs |
736 |
|
} |
737 |
|
assertTrue(p.isShutdown()); |
738 |
< |
assertTrue(l.size() <= 4); |
738 |
> |
assertTrue(p.getQueue().isEmpty()); |
739 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
740 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
741 |
> |
assertTrue(p.isTerminated()); |
742 |
> |
assertEquals(poolSize, ran.get()); |
743 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
744 |
|
} |
745 |
|
|
746 |
|
// Exception Tests |
747 |
|
|
692 |
– |
|
748 |
|
/** |
749 |
|
* Constructor throws if corePoolSize argument is less than zero |
750 |
|
*/ |
751 |
|
public void testConstructor1() { |
752 |
|
try { |
753 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
753 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
754 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
755 |
|
shouldThrow(); |
756 |
|
} catch (IllegalArgumentException success) {} |
757 |
|
} |
761 |
|
*/ |
762 |
|
public void testConstructor2() { |
763 |
|
try { |
764 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
764 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
765 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
766 |
|
shouldThrow(); |
767 |
|
} catch (IllegalArgumentException success) {} |
768 |
|
} |
772 |
|
*/ |
773 |
|
public void testConstructor3() { |
774 |
|
try { |
775 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
775 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
776 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
777 |
|
shouldThrow(); |
778 |
|
} catch (IllegalArgumentException success) {} |
779 |
|
} |
783 |
|
*/ |
784 |
|
public void testConstructor4() { |
785 |
|
try { |
786 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
786 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
787 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
788 |
|
shouldThrow(); |
789 |
|
} catch (IllegalArgumentException success) {} |
790 |
|
} |
794 |
|
*/ |
795 |
|
public void testConstructor5() { |
796 |
|
try { |
797 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
797 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
798 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
799 |
|
shouldThrow(); |
800 |
|
} catch (IllegalArgumentException success) {} |
801 |
|
} |
805 |
|
*/ |
806 |
|
public void testConstructorNullPointerException() { |
807 |
|
try { |
808 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
808 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
809 |
|
shouldThrow(); |
810 |
|
} catch (NullPointerException success) {} |
811 |
|
} |
812 |
|
|
753 |
– |
|
754 |
– |
|
813 |
|
/** |
814 |
|
* Constructor throws if corePoolSize argument is less than zero |
815 |
|
*/ |
816 |
|
public void testConstructor6() { |
817 |
|
try { |
818 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
818 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
819 |
> |
new ArrayBlockingQueue<Runnable>(10), |
820 |
> |
new SimpleThreadFactory()); |
821 |
|
shouldThrow(); |
822 |
|
} catch (IllegalArgumentException success) {} |
823 |
|
} |
827 |
|
*/ |
828 |
|
public void testConstructor7() { |
829 |
|
try { |
830 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
830 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
831 |
> |
new ArrayBlockingQueue<Runnable>(10), |
832 |
> |
new SimpleThreadFactory()); |
833 |
|
shouldThrow(); |
834 |
|
} catch (IllegalArgumentException success) {} |
835 |
|
} |
839 |
|
*/ |
840 |
|
public void testConstructor8() { |
841 |
|
try { |
842 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
842 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
843 |
> |
new ArrayBlockingQueue<Runnable>(10), |
844 |
> |
new SimpleThreadFactory()); |
845 |
|
shouldThrow(); |
846 |
|
} catch (IllegalArgumentException success) {} |
847 |
|
} |
851 |
|
*/ |
852 |
|
public void testConstructor9() { |
853 |
|
try { |
854 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
854 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
855 |
> |
new ArrayBlockingQueue<Runnable>(10), |
856 |
> |
new SimpleThreadFactory()); |
857 |
|
shouldThrow(); |
858 |
|
} catch (IllegalArgumentException success) {} |
859 |
|
} |
863 |
|
*/ |
864 |
|
public void testConstructor10() { |
865 |
|
try { |
866 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
866 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
867 |
> |
new ArrayBlockingQueue<Runnable>(10), |
868 |
> |
new SimpleThreadFactory()); |
869 |
|
shouldThrow(); |
870 |
|
} catch (IllegalArgumentException success) {} |
871 |
|
} |
875 |
|
*/ |
876 |
|
public void testConstructorNullPointerException2() { |
877 |
|
try { |
878 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
878 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
879 |
|
shouldThrow(); |
880 |
|
} catch (NullPointerException success) {} |
881 |
|
} |
885 |
|
*/ |
886 |
|
public void testConstructorNullPointerException3() { |
887 |
|
try { |
888 |
< |
ThreadFactory f = null; |
889 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
888 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
889 |
> |
new ArrayBlockingQueue<Runnable>(10), |
890 |
> |
(ThreadFactory) null); |
891 |
|
shouldThrow(); |
892 |
|
} catch (NullPointerException success) {} |
893 |
|
} |
894 |
|
|
826 |
– |
|
895 |
|
/** |
896 |
|
* Constructor throws if corePoolSize argument is less than zero |
897 |
|
*/ |
898 |
|
public void testConstructor11() { |
899 |
|
try { |
900 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
900 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
901 |
> |
new ArrayBlockingQueue<Runnable>(10), |
902 |
> |
new NoOpREHandler()); |
903 |
|
shouldThrow(); |
904 |
|
} catch (IllegalArgumentException success) {} |
905 |
|
} |
909 |
|
*/ |
910 |
|
public void testConstructor12() { |
911 |
|
try { |
912 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
912 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
913 |
> |
new ArrayBlockingQueue<Runnable>(10), |
914 |
> |
new NoOpREHandler()); |
915 |
|
shouldThrow(); |
916 |
|
} catch (IllegalArgumentException success) {} |
917 |
|
} |
921 |
|
*/ |
922 |
|
public void testConstructor13() { |
923 |
|
try { |
924 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
924 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
925 |
> |
new ArrayBlockingQueue<Runnable>(10), |
926 |
> |
new NoOpREHandler()); |
927 |
|
shouldThrow(); |
928 |
|
} catch (IllegalArgumentException success) {} |
929 |
|
} |
933 |
|
*/ |
934 |
|
public void testConstructor14() { |
935 |
|
try { |
936 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
936 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
937 |
> |
new ArrayBlockingQueue<Runnable>(10), |
938 |
> |
new NoOpREHandler()); |
939 |
|
shouldThrow(); |
940 |
|
} catch (IllegalArgumentException success) {} |
941 |
|
} |
945 |
|
*/ |
946 |
|
public void testConstructor15() { |
947 |
|
try { |
948 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
948 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
949 |
> |
new ArrayBlockingQueue<Runnable>(10), |
950 |
> |
new NoOpREHandler()); |
951 |
|
shouldThrow(); |
952 |
|
} catch (IllegalArgumentException success) {} |
953 |
|
} |
957 |
|
*/ |
958 |
|
public void testConstructorNullPointerException4() { |
959 |
|
try { |
960 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
960 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
961 |
> |
null, |
962 |
> |
new NoOpREHandler()); |
963 |
|
shouldThrow(); |
964 |
|
} catch (NullPointerException success) {} |
965 |
|
} |
969 |
|
*/ |
970 |
|
public void testConstructorNullPointerException5() { |
971 |
|
try { |
972 |
< |
RejectedExecutionHandler r = null; |
973 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
972 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
973 |
> |
new ArrayBlockingQueue<Runnable>(10), |
974 |
> |
(RejectedExecutionHandler) null); |
975 |
|
shouldThrow(); |
976 |
|
} catch (NullPointerException success) {} |
977 |
|
} |
978 |
|
|
898 |
– |
|
979 |
|
/** |
980 |
|
* Constructor throws if corePoolSize argument is less than zero |
981 |
|
*/ |
982 |
|
public void testConstructor16() { |
983 |
|
try { |
984 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
984 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
985 |
> |
new ArrayBlockingQueue<Runnable>(10), |
986 |
> |
new SimpleThreadFactory(), |
987 |
> |
new NoOpREHandler()); |
988 |
|
shouldThrow(); |
989 |
|
} catch (IllegalArgumentException success) {} |
990 |
|
} |
994 |
|
*/ |
995 |
|
public void testConstructor17() { |
996 |
|
try { |
997 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
997 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
998 |
> |
new ArrayBlockingQueue<Runnable>(10), |
999 |
> |
new SimpleThreadFactory(), |
1000 |
> |
new NoOpREHandler()); |
1001 |
|
shouldThrow(); |
1002 |
|
} catch (IllegalArgumentException success) {} |
1003 |
|
} |
1007 |
|
*/ |
1008 |
|
public void testConstructor18() { |
1009 |
|
try { |
1010 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1010 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
1011 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1012 |
> |
new SimpleThreadFactory(), |
1013 |
> |
new NoOpREHandler()); |
1014 |
|
shouldThrow(); |
1015 |
|
} catch (IllegalArgumentException success) {} |
1016 |
|
} |
1020 |
|
*/ |
1021 |
|
public void testConstructor19() { |
1022 |
|
try { |
1023 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1023 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
1024 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1025 |
> |
new SimpleThreadFactory(), |
1026 |
> |
new NoOpREHandler()); |
1027 |
|
shouldThrow(); |
1028 |
|
} catch (IllegalArgumentException success) {} |
1029 |
|
} |
1033 |
|
*/ |
1034 |
|
public void testConstructor20() { |
1035 |
|
try { |
1036 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1036 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1037 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1038 |
> |
new SimpleThreadFactory(), |
1039 |
> |
new NoOpREHandler()); |
1040 |
|
shouldThrow(); |
1041 |
|
} catch (IllegalArgumentException success) {} |
1042 |
|
} |
1046 |
|
*/ |
1047 |
|
public void testConstructorNullPointerException6() { |
1048 |
|
try { |
1049 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1049 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1050 |
> |
null, |
1051 |
> |
new SimpleThreadFactory(), |
1052 |
> |
new NoOpREHandler()); |
1053 |
|
shouldThrow(); |
1054 |
|
} catch (NullPointerException success) {} |
1055 |
|
} |
1059 |
|
*/ |
1060 |
|
public void testConstructorNullPointerException7() { |
1061 |
|
try { |
1062 |
< |
RejectedExecutionHandler r = null; |
1063 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1062 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1063 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1064 |
> |
new SimpleThreadFactory(), |
1065 |
> |
(RejectedExecutionHandler) null); |
1066 |
|
shouldThrow(); |
1067 |
|
} catch (NullPointerException success) {} |
1068 |
|
} |
1072 |
|
*/ |
1073 |
|
public void testConstructorNullPointerException8() { |
1074 |
|
try { |
1075 |
< |
new CustomTPE(1, 2, |
976 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1075 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1076 |
|
new ArrayBlockingQueue<Runnable>(10), |
1077 |
|
(ThreadFactory) null, |
1078 |
|
new NoOpREHandler()); |
1080 |
|
} catch (NullPointerException success) {} |
1081 |
|
} |
1082 |
|
|
984 |
– |
|
1083 |
|
/** |
1084 |
|
* execute throws RejectedExecutionException if saturated. |
1085 |
|
*/ |
1229 |
|
} |
1230 |
|
} |
1231 |
|
|
1134 |
– |
|
1232 |
|
/** |
1233 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1234 |
|
*/ |
1246 |
|
} |
1247 |
|
} |
1248 |
|
|
1152 |
– |
|
1249 |
|
/** |
1250 |
|
* execute(null) throws NPE |
1251 |
|
*/ |
1252 |
|
public void testExecuteNull() { |
1253 |
< |
ThreadPoolExecutor p = null; |
1253 |
> |
ThreadPoolExecutor p = |
1254 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1255 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1256 |
|
try { |
1159 |
– |
p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1257 |
|
p.execute(null); |
1258 |
|
shouldThrow(); |
1259 |
|
} catch (NullPointerException success) {} |
1311 |
|
joinPool(p); |
1312 |
|
} |
1313 |
|
|
1217 |
– |
|
1314 |
|
/** |
1315 |
|
* setKeepAliveTime throws IllegalArgumentException |
1316 |
|
* when given a negative value |
1335 |
|
public void testTerminated() { |
1336 |
|
CustomTPE p = new CustomTPE(); |
1337 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1338 |
< |
assertTrue(p.terminatedCalled); |
1338 |
> |
assertTrue(p.terminatedCalled()); |
1339 |
|
joinPool(p); |
1340 |
|
} |
1341 |
|
|
1345 |
|
public void testBeforeAfter() throws InterruptedException { |
1346 |
|
CustomTPE p = new CustomTPE(); |
1347 |
|
try { |
1348 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1349 |
< |
p.execute(r); |
1350 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1351 |
< |
assertTrue(r.done); |
1352 |
< |
assertTrue(p.beforeCalled); |
1353 |
< |
assertTrue(p.afterCalled); |
1348 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1349 |
> |
p.execute(new CheckedRunnable() { |
1350 |
> |
public void realRun() { |
1351 |
> |
done.countDown(); |
1352 |
> |
}}); |
1353 |
> |
await(p.afterCalled); |
1354 |
> |
assertEquals(0, done.getCount()); |
1355 |
> |
assertTrue(p.afterCalled()); |
1356 |
> |
assertTrue(p.beforeCalled()); |
1357 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1358 |
|
} finally { |
1359 |
|
joinPool(p); |
1402 |
|
} |
1403 |
|
} |
1404 |
|
|
1306 |
– |
|
1405 |
|
/** |
1406 |
|
* invokeAny(null) throws NPE |
1407 |
|
*/ |
1563 |
|
} |
1564 |
|
} |
1565 |
|
|
1468 |
– |
|
1469 |
– |
|
1566 |
|
/** |
1567 |
|
* timed invokeAny(null) throws NPE |
1568 |
|
*/ |
1764 |
|
public void testTimedInvokeAll6() throws Exception { |
1765 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1766 |
|
try { |
1767 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1768 |
< |
l.add(new StringTask()); |
1769 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1770 |
< |
l.add(new StringTask()); |
1771 |
< |
List<Future<String>> futures = |
1772 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1773 |
< |
assertEquals(3, futures.size()); |
1774 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1775 |
< |
Future<String> f1 = it.next(); |
1776 |
< |
Future<String> f2 = it.next(); |
1777 |
< |
Future<String> f3 = it.next(); |
1778 |
< |
assertTrue(f1.isDone()); |
1779 |
< |
assertTrue(f2.isDone()); |
1780 |
< |
assertTrue(f3.isDone()); |
1781 |
< |
assertFalse(f1.isCancelled()); |
1782 |
< |
assertTrue(f2.isCancelled()); |
1767 |
> |
for (long timeout = timeoutMillis();;) { |
1768 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1769 |
> |
tasks.add(new StringTask("0")); |
1770 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1771 |
> |
tasks.add(new StringTask("2")); |
1772 |
> |
long startTime = System.nanoTime(); |
1773 |
> |
List<Future<String>> futures = |
1774 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1775 |
> |
assertEquals(tasks.size(), futures.size()); |
1776 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1777 |
> |
for (Future future : futures) |
1778 |
> |
assertTrue(future.isDone()); |
1779 |
> |
assertTrue(futures.get(1).isCancelled()); |
1780 |
> |
try { |
1781 |
> |
assertEquals("0", futures.get(0).get()); |
1782 |
> |
assertEquals("2", futures.get(2).get()); |
1783 |
> |
break; |
1784 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1785 |
> |
timeout *= 2; |
1786 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1787 |
> |
fail("expected exactly one task to be cancelled"); |
1788 |
> |
} |
1789 |
> |
} |
1790 |
|
} finally { |
1791 |
|
joinPool(e); |
1792 |
|
} |
1829 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1830 |
|
*/ |
1831 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1832 |
+ |
long keepAliveTime = timeoutMillis(); |
1833 |
|
final ThreadPoolExecutor p = |
1834 |
|
new CustomTPE(2, 10, |
1835 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1835 |
> |
keepAliveTime, MILLISECONDS, |
1836 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1837 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1838 |
|
try { |
1839 |
|
p.allowCoreThreadTimeOut(true); |
1840 |
|
p.execute(new CheckedRunnable() { |
1841 |
< |
public void realRun() throws InterruptedException { |
1841 |
> |
public void realRun() { |
1842 |
|
threadStarted.countDown(); |
1843 |
|
assertEquals(1, p.getPoolSize()); |
1844 |
|
}}); |
1845 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1846 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1847 |
< |
if (p.getPoolSize() == 0) |
1848 |
< |
break; |
1849 |
< |
Thread.sleep(10); |
1850 |
< |
} |
1845 |
> |
await(threadStarted); |
1846 |
> |
delay(keepAliveTime); |
1847 |
> |
long startTime = System.nanoTime(); |
1848 |
> |
while (p.getPoolSize() > 0 |
1849 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1850 |
> |
Thread.yield(); |
1851 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1852 |
|
assertEquals(0, p.getPoolSize()); |
1853 |
|
} finally { |
1854 |
|
joinPool(p); |
1859 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1860 |
|
*/ |
1861 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1862 |
+ |
long keepAliveTime = timeoutMillis(); |
1863 |
|
final ThreadPoolExecutor p = |
1864 |
|
new CustomTPE(2, 10, |
1865 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1865 |
> |
keepAliveTime, MILLISECONDS, |
1866 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1867 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1868 |
|
try { |
1872 |
|
threadStarted.countDown(); |
1873 |
|
assertTrue(p.getPoolSize() >= 1); |
1874 |
|
}}); |
1875 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1875 |
> |
delay(2 * keepAliveTime); |
1876 |
|
assertTrue(p.getPoolSize() >= 1); |
1877 |
|
} finally { |
1878 |
|
joinPool(p); |
1879 |
|
} |
1880 |
|
} |
1881 |
|
|
1882 |
+ |
/** |
1883 |
+ |
* get(cancelled task) throws CancellationException |
1884 |
+ |
* (in part, a test of CustomTPE itself) |
1885 |
+ |
*/ |
1886 |
+ |
public void testGet_cancelled() throws Exception { |
1887 |
+ |
final ExecutorService e = |
1888 |
+ |
new CustomTPE(1, 1, |
1889 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1890 |
+ |
new LinkedBlockingQueue<Runnable>()); |
1891 |
+ |
try { |
1892 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
1893 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1894 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
1895 |
+ |
for (int i = 0; i < 2; i++) { |
1896 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
1897 |
+ |
throws Throwable { |
1898 |
+ |
blockerStarted.countDown(); |
1899 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
1900 |
+ |
}}; |
1901 |
+ |
futures.add(e.submit(r)); |
1902 |
+ |
} |
1903 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1904 |
+ |
for (Future<?> future : futures) future.cancel(false); |
1905 |
+ |
for (Future<?> future : futures) { |
1906 |
+ |
try { |
1907 |
+ |
future.get(); |
1908 |
+ |
shouldThrow(); |
1909 |
+ |
} catch (CancellationException success) {} |
1910 |
+ |
try { |
1911 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
1912 |
+ |
shouldThrow(); |
1913 |
+ |
} catch (CancellationException success) {} |
1914 |
+ |
assertTrue(future.isCancelled()); |
1915 |
+ |
assertTrue(future.isDone()); |
1916 |
+ |
} |
1917 |
+ |
done.countDown(); |
1918 |
+ |
} finally { |
1919 |
+ |
joinPool(e); |
1920 |
+ |
} |
1921 |
+ |
} |
1922 |
+ |
|
1923 |
|
} |