6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
> |
|
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
16 |
> |
import java.util.concurrent.BlockingQueue; |
17 |
> |
import java.util.concurrent.Callable; |
18 |
> |
import java.util.concurrent.CancellationException; |
19 |
> |
import java.util.concurrent.CountDownLatch; |
20 |
> |
import java.util.concurrent.ExecutionException; |
21 |
> |
import java.util.concurrent.Executors; |
22 |
> |
import java.util.concurrent.ExecutorService; |
23 |
> |
import java.util.concurrent.Future; |
24 |
> |
import java.util.concurrent.FutureTask; |
25 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
26 |
> |
import java.util.concurrent.RejectedExecutionException; |
27 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
28 |
> |
import java.util.concurrent.SynchronousQueue; |
29 |
> |
import java.util.concurrent.ThreadFactory; |
30 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
31 |
> |
import java.util.concurrent.TimeUnit; |
32 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
33 |
> |
|
34 |
> |
import junit.framework.Test; |
35 |
> |
import junit.framework.TestSuite; |
36 |
|
|
37 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
38 |
|
public static void main(String[] args) { |
39 |
< |
junit.textui.TestRunner.run(suite()); |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
public static Test suite() { |
42 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
43 |
|
} |
44 |
|
|
45 |
|
static class ExtendedTPE extends ThreadPoolExecutor { |
46 |
< |
volatile boolean beforeCalled = false; |
47 |
< |
volatile boolean afterCalled = false; |
48 |
< |
volatile boolean terminatedCalled = false; |
46 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
47 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
48 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
49 |
> |
|
50 |
|
public ExtendedTPE() { |
51 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
52 |
|
} |
53 |
|
protected void beforeExecute(Thread t, Runnable r) { |
54 |
< |
beforeCalled = true; |
54 |
> |
beforeCalled.countDown(); |
55 |
|
} |
56 |
|
protected void afterExecute(Runnable r, Throwable t) { |
57 |
< |
afterCalled = true; |
57 |
> |
afterCalled.countDown(); |
58 |
|
} |
59 |
|
protected void terminated() { |
60 |
< |
terminatedCalled = true; |
60 |
> |
terminatedCalled.countDown(); |
61 |
> |
} |
62 |
> |
|
63 |
> |
public boolean beforeCalled() { |
64 |
> |
return beforeCalled.getCount() == 0; |
65 |
> |
} |
66 |
> |
public boolean afterCalled() { |
67 |
> |
return afterCalled.getCount() == 0; |
68 |
> |
} |
69 |
> |
public boolean terminatedCalled() { |
70 |
> |
return terminatedCalled.getCount() == 0; |
71 |
|
} |
72 |
|
} |
73 |
|
|
79 |
|
} |
80 |
|
} |
81 |
|
|
49 |
– |
|
82 |
|
/** |
83 |
|
* execute successfully executes a runnable |
84 |
|
*/ |
132 |
|
*/ |
133 |
|
public void testPrestartCoreThread() { |
134 |
|
final ThreadPoolExecutor p = |
135 |
< |
new ThreadPoolExecutor(2, 2, |
135 |
> |
new ThreadPoolExecutor(2, 6, |
136 |
|
LONG_DELAY_MS, MILLISECONDS, |
137 |
|
new ArrayBlockingQueue<Runnable>(10)); |
138 |
< |
assertEquals(0, p.getPoolSize()); |
139 |
< |
assertTrue(p.prestartCoreThread()); |
140 |
< |
assertEquals(1, p.getPoolSize()); |
141 |
< |
assertTrue(p.prestartCoreThread()); |
142 |
< |
assertEquals(2, p.getPoolSize()); |
143 |
< |
assertFalse(p.prestartCoreThread()); |
144 |
< |
assertEquals(2, p.getPoolSize()); |
145 |
< |
joinPool(p); |
138 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
139 |
> |
assertEquals(0, p.getPoolSize()); |
140 |
> |
assertTrue(p.prestartCoreThread()); |
141 |
> |
assertEquals(1, p.getPoolSize()); |
142 |
> |
assertTrue(p.prestartCoreThread()); |
143 |
> |
assertEquals(2, p.getPoolSize()); |
144 |
> |
assertFalse(p.prestartCoreThread()); |
145 |
> |
assertEquals(2, p.getPoolSize()); |
146 |
> |
p.setCorePoolSize(4); |
147 |
> |
assertTrue(p.prestartCoreThread()); |
148 |
> |
assertEquals(3, p.getPoolSize()); |
149 |
> |
assertTrue(p.prestartCoreThread()); |
150 |
> |
assertEquals(4, p.getPoolSize()); |
151 |
> |
assertFalse(p.prestartCoreThread()); |
152 |
> |
assertEquals(4, p.getPoolSize()); |
153 |
> |
} |
154 |
|
} |
155 |
|
|
156 |
|
/** |
190 |
|
threadProceed.await(); |
191 |
|
threadDone.countDown(); |
192 |
|
}}); |
193 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
193 |
> |
await(threadStarted); |
194 |
|
assertEquals(0, p.getCompletedTaskCount()); |
195 |
|
threadProceed.countDown(); |
196 |
|
threadDone.await(); |
197 |
< |
Thread.sleep(SHORT_DELAY_MS); |
198 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
197 |
> |
long startTime = System.nanoTime(); |
198 |
> |
while (p.getCompletedTaskCount() != 1) { |
199 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
200 |
> |
fail("timed out"); |
201 |
> |
Thread.yield(); |
202 |
> |
} |
203 |
|
} finally { |
204 |
|
joinPool(p); |
205 |
|
} |
225 |
|
new ThreadPoolExecutor(2, 2, |
226 |
|
1000, MILLISECONDS, |
227 |
|
new ArrayBlockingQueue<Runnable>(10)); |
228 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
228 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
229 |
|
joinPool(p); |
230 |
|
} |
231 |
|
|
188 |
– |
|
232 |
|
/** |
233 |
|
* getThreadFactory returns factory in constructor if not set |
234 |
|
*/ |
258 |
|
joinPool(p); |
259 |
|
} |
260 |
|
|
218 |
– |
|
261 |
|
/** |
262 |
|
* setThreadFactory(null) throws NPE |
263 |
|
*/ |
304 |
|
joinPool(p); |
305 |
|
} |
306 |
|
|
265 |
– |
|
307 |
|
/** |
308 |
|
* setRejectedExecutionHandler(null) throws NPE |
309 |
|
*/ |
321 |
|
} |
322 |
|
} |
323 |
|
|
283 |
– |
|
324 |
|
/** |
325 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
326 |
|
* multiple threads active |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* isShutDown is false before shutdown, true after |
421 |
> |
* isShutdown is false before shutdown, true after |
422 |
|
*/ |
423 |
|
public void testIsShutdown() { |
424 |
|
final ThreadPoolExecutor p = |
431 |
|
joinPool(p); |
432 |
|
} |
433 |
|
|
434 |
+ |
/** |
435 |
+ |
* awaitTermination on a non-shutdown pool times out |
436 |
+ |
*/ |
437 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
438 |
+ |
final ThreadPoolExecutor p = |
439 |
+ |
new ThreadPoolExecutor(1, 1, |
440 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
441 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
442 |
+ |
assertFalse(p.isTerminated()); |
443 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
444 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
445 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
446 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
447 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
448 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
449 |
+ |
long timeoutNanos = 999999L; |
450 |
+ |
long startTime = System.nanoTime(); |
451 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
452 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
453 |
+ |
assertFalse(p.isTerminated()); |
454 |
+ |
startTime = System.nanoTime(); |
455 |
+ |
long timeoutMillis = timeoutMillis(); |
456 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
457 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
458 |
+ |
assertFalse(p.isTerminated()); |
459 |
+ |
p.shutdown(); |
460 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
461 |
+ |
assertTrue(p.isTerminated()); |
462 |
+ |
} |
463 |
|
|
464 |
|
/** |
465 |
|
* isTerminated is false before termination, true after |
633 |
|
} |
634 |
|
|
635 |
|
/** |
636 |
< |
* shutDownNow returns a list containing tasks that were not run |
636 |
> |
* shutdownNow returns a list containing tasks that were not run, |
637 |
> |
* and those tasks are drained from the queue |
638 |
|
*/ |
639 |
< |
public void testShutDownNow() { |
639 |
> |
public void testShutdownNow() throws InterruptedException { |
640 |
> |
final int poolSize = 2; |
641 |
> |
final int count = 5; |
642 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
643 |
|
final ThreadPoolExecutor p = |
644 |
< |
new ThreadPoolExecutor(1, 1, |
644 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
645 |
|
LONG_DELAY_MS, MILLISECONDS, |
646 |
|
new ArrayBlockingQueue<Runnable>(10)); |
647 |
< |
List l; |
648 |
< |
try { |
649 |
< |
for (int i = 0; i < 5; i++) |
577 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
578 |
< |
} |
579 |
< |
finally { |
647 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
648 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
649 |
> |
threadsStarted.countDown(); |
650 |
|
try { |
651 |
< |
l = p.shutdownNow(); |
652 |
< |
} catch (SecurityException ok) { return; } |
651 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
652 |
> |
} catch (InterruptedException success) {} |
653 |
> |
ran.getAndIncrement(); |
654 |
> |
}}; |
655 |
> |
for (int i = 0; i < count; i++) |
656 |
> |
p.execute(waiter); |
657 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
658 |
> |
assertEquals(poolSize, p.getActiveCount()); |
659 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
660 |
> |
final List<Runnable> queuedTasks; |
661 |
> |
try { |
662 |
> |
queuedTasks = p.shutdownNow(); |
663 |
> |
} catch (SecurityException ok) { |
664 |
> |
return; // Allowed in case test doesn't have privs |
665 |
|
} |
666 |
|
assertTrue(p.isShutdown()); |
667 |
< |
assertTrue(l.size() <= 4); |
667 |
> |
assertTrue(p.getQueue().isEmpty()); |
668 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
669 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
670 |
> |
assertTrue(p.isTerminated()); |
671 |
> |
assertEquals(poolSize, ran.get()); |
672 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
673 |
|
} |
674 |
|
|
675 |
|
// Exception Tests |
676 |
|
|
590 |
– |
|
677 |
|
/** |
678 |
|
* Constructor throws if corePoolSize argument is less than zero |
679 |
|
*/ |
680 |
|
public void testConstructor1() { |
681 |
|
try { |
682 |
< |
new ThreadPoolExecutor(-1, 1, |
597 |
< |
LONG_DELAY_MS, MILLISECONDS, |
682 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
683 |
|
new ArrayBlockingQueue<Runnable>(10)); |
684 |
|
shouldThrow(); |
685 |
|
} catch (IllegalArgumentException success) {} |
690 |
|
*/ |
691 |
|
public void testConstructor2() { |
692 |
|
try { |
693 |
< |
new ThreadPoolExecutor(1, -1, |
609 |
< |
LONG_DELAY_MS, MILLISECONDS, |
693 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
694 |
|
new ArrayBlockingQueue<Runnable>(10)); |
695 |
|
shouldThrow(); |
696 |
|
} catch (IllegalArgumentException success) {} |
701 |
|
*/ |
702 |
|
public void testConstructor3() { |
703 |
|
try { |
704 |
< |
new ThreadPoolExecutor(1, 0, |
621 |
< |
LONG_DELAY_MS, MILLISECONDS, |
704 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
705 |
|
new ArrayBlockingQueue<Runnable>(10)); |
706 |
|
shouldThrow(); |
707 |
|
} catch (IllegalArgumentException success) {} |
712 |
|
*/ |
713 |
|
public void testConstructor4() { |
714 |
|
try { |
715 |
< |
new ThreadPoolExecutor(1, 2, |
633 |
< |
-1L, MILLISECONDS, |
715 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
716 |
|
new ArrayBlockingQueue<Runnable>(10)); |
717 |
|
shouldThrow(); |
718 |
|
} catch (IllegalArgumentException success) {} |
723 |
|
*/ |
724 |
|
public void testConstructor5() { |
725 |
|
try { |
726 |
< |
new ThreadPoolExecutor(2, 1, |
645 |
< |
LONG_DELAY_MS, MILLISECONDS, |
726 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
727 |
|
new ArrayBlockingQueue<Runnable>(10)); |
728 |
|
shouldThrow(); |
729 |
|
} catch (IllegalArgumentException success) {} |
734 |
|
*/ |
735 |
|
public void testConstructorNullPointerException() { |
736 |
|
try { |
737 |
< |
new ThreadPoolExecutor(1, 2, |
657 |
< |
LONG_DELAY_MS, MILLISECONDS, |
737 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
738 |
|
(BlockingQueue) null); |
739 |
|
shouldThrow(); |
740 |
|
} catch (NullPointerException success) {} |
741 |
|
} |
742 |
|
|
663 |
– |
|
664 |
– |
|
743 |
|
/** |
744 |
|
* Constructor throws if corePoolSize argument is less than zero |
745 |
|
*/ |
746 |
|
public void testConstructor6() { |
747 |
|
try { |
748 |
< |
new ThreadPoolExecutor(-1, 1, |
671 |
< |
LONG_DELAY_MS, MILLISECONDS, |
748 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
749 |
|
new ArrayBlockingQueue<Runnable>(10), |
750 |
|
new SimpleThreadFactory()); |
751 |
|
shouldThrow(); |
757 |
|
*/ |
758 |
|
public void testConstructor7() { |
759 |
|
try { |
760 |
< |
new ThreadPoolExecutor(1, -1, |
684 |
< |
LONG_DELAY_MS, MILLISECONDS, |
760 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
761 |
|
new ArrayBlockingQueue<Runnable>(10), |
762 |
|
new SimpleThreadFactory()); |
763 |
|
shouldThrow(); |
769 |
|
*/ |
770 |
|
public void testConstructor8() { |
771 |
|
try { |
772 |
< |
new ThreadPoolExecutor(1, 0, |
697 |
< |
LONG_DELAY_MS, MILLISECONDS, |
772 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
773 |
|
new ArrayBlockingQueue<Runnable>(10), |
774 |
|
new SimpleThreadFactory()); |
775 |
|
shouldThrow(); |
781 |
|
*/ |
782 |
|
public void testConstructor9() { |
783 |
|
try { |
784 |
< |
new ThreadPoolExecutor(1, 2, |
710 |
< |
-1L, MILLISECONDS, |
784 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
785 |
|
new ArrayBlockingQueue<Runnable>(10), |
786 |
|
new SimpleThreadFactory()); |
787 |
|
shouldThrow(); |
793 |
|
*/ |
794 |
|
public void testConstructor10() { |
795 |
|
try { |
796 |
< |
new ThreadPoolExecutor(2, 1, |
723 |
< |
LONG_DELAY_MS, MILLISECONDS, |
796 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
797 |
|
new ArrayBlockingQueue<Runnable>(10), |
798 |
|
new SimpleThreadFactory()); |
799 |
|
shouldThrow(); |
805 |
|
*/ |
806 |
|
public void testConstructorNullPointerException2() { |
807 |
|
try { |
808 |
< |
new ThreadPoolExecutor(1, 2, |
736 |
< |
LONG_DELAY_MS, MILLISECONDS, |
808 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
809 |
|
(BlockingQueue) null, |
810 |
|
new SimpleThreadFactory()); |
811 |
|
shouldThrow(); |
817 |
|
*/ |
818 |
|
public void testConstructorNullPointerException3() { |
819 |
|
try { |
820 |
< |
new ThreadPoolExecutor(1, 2, |
749 |
< |
LONG_DELAY_MS, MILLISECONDS, |
820 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
821 |
|
new ArrayBlockingQueue<Runnable>(10), |
822 |
|
(ThreadFactory) null); |
823 |
|
shouldThrow(); |
824 |
|
} catch (NullPointerException success) {} |
825 |
|
} |
826 |
|
|
756 |
– |
|
827 |
|
/** |
828 |
|
* Constructor throws if corePoolSize argument is less than zero |
829 |
|
*/ |
830 |
|
public void testConstructor11() { |
831 |
|
try { |
832 |
< |
new ThreadPoolExecutor(-1, 1, |
763 |
< |
LONG_DELAY_MS, MILLISECONDS, |
832 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
833 |
|
new ArrayBlockingQueue<Runnable>(10), |
834 |
|
new NoOpREHandler()); |
835 |
|
shouldThrow(); |
841 |
|
*/ |
842 |
|
public void testConstructor12() { |
843 |
|
try { |
844 |
< |
new ThreadPoolExecutor(1, -1, |
776 |
< |
LONG_DELAY_MS, MILLISECONDS, |
844 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
845 |
|
new ArrayBlockingQueue<Runnable>(10), |
846 |
|
new NoOpREHandler()); |
847 |
|
shouldThrow(); |
853 |
|
*/ |
854 |
|
public void testConstructor13() { |
855 |
|
try { |
856 |
< |
new ThreadPoolExecutor(1, 0, |
789 |
< |
LONG_DELAY_MS, MILLISECONDS, |
856 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
857 |
|
new ArrayBlockingQueue<Runnable>(10), |
858 |
|
new NoOpREHandler()); |
859 |
|
shouldThrow(); |
865 |
|
*/ |
866 |
|
public void testConstructor14() { |
867 |
|
try { |
868 |
< |
new ThreadPoolExecutor(1, 2, |
802 |
< |
-1L, MILLISECONDS, |
868 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
869 |
|
new ArrayBlockingQueue<Runnable>(10), |
870 |
|
new NoOpREHandler()); |
871 |
|
shouldThrow(); |
877 |
|
*/ |
878 |
|
public void testConstructor15() { |
879 |
|
try { |
880 |
< |
new ThreadPoolExecutor(2, 1, |
815 |
< |
LONG_DELAY_MS, MILLISECONDS, |
880 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
881 |
|
new ArrayBlockingQueue<Runnable>(10), |
882 |
|
new NoOpREHandler()); |
883 |
|
shouldThrow(); |
889 |
|
*/ |
890 |
|
public void testConstructorNullPointerException4() { |
891 |
|
try { |
892 |
< |
new ThreadPoolExecutor(1, 2, |
828 |
< |
LONG_DELAY_MS, MILLISECONDS, |
892 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
893 |
|
(BlockingQueue) null, |
894 |
|
new NoOpREHandler()); |
895 |
|
shouldThrow(); |
901 |
|
*/ |
902 |
|
public void testConstructorNullPointerException5() { |
903 |
|
try { |
904 |
< |
new ThreadPoolExecutor(1, 2, |
841 |
< |
LONG_DELAY_MS, MILLISECONDS, |
904 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
905 |
|
new ArrayBlockingQueue<Runnable>(10), |
906 |
|
(RejectedExecutionHandler) null); |
907 |
|
shouldThrow(); |
908 |
|
} catch (NullPointerException success) {} |
909 |
|
} |
910 |
|
|
848 |
– |
|
911 |
|
/** |
912 |
|
* Constructor throws if corePoolSize argument is less than zero |
913 |
|
*/ |
914 |
|
public void testConstructor16() { |
915 |
|
try { |
916 |
< |
new ThreadPoolExecutor(-1, 1, |
855 |
< |
LONG_DELAY_MS, MILLISECONDS, |
916 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
917 |
|
new ArrayBlockingQueue<Runnable>(10), |
918 |
|
new SimpleThreadFactory(), |
919 |
|
new NoOpREHandler()); |
926 |
|
*/ |
927 |
|
public void testConstructor17() { |
928 |
|
try { |
929 |
< |
new ThreadPoolExecutor(1, -1, |
869 |
< |
LONG_DELAY_MS, MILLISECONDS, |
929 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
930 |
|
new ArrayBlockingQueue<Runnable>(10), |
931 |
|
new SimpleThreadFactory(), |
932 |
|
new NoOpREHandler()); |
939 |
|
*/ |
940 |
|
public void testConstructor18() { |
941 |
|
try { |
942 |
< |
new ThreadPoolExecutor(1, 0, |
883 |
< |
LONG_DELAY_MS, MILLISECONDS, |
942 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
943 |
|
new ArrayBlockingQueue<Runnable>(10), |
944 |
|
new SimpleThreadFactory(), |
945 |
|
new NoOpREHandler()); |
952 |
|
*/ |
953 |
|
public void testConstructor19() { |
954 |
|
try { |
955 |
< |
new ThreadPoolExecutor(1, 2, |
897 |
< |
-1L, MILLISECONDS, |
955 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
956 |
|
new ArrayBlockingQueue<Runnable>(10), |
957 |
|
new SimpleThreadFactory(), |
958 |
|
new NoOpREHandler()); |
965 |
|
*/ |
966 |
|
public void testConstructor20() { |
967 |
|
try { |
968 |
< |
new ThreadPoolExecutor(2, 1, |
911 |
< |
LONG_DELAY_MS, MILLISECONDS, |
968 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
969 |
|
new ArrayBlockingQueue<Runnable>(10), |
970 |
|
new SimpleThreadFactory(), |
971 |
|
new NoOpREHandler()); |
978 |
|
*/ |
979 |
|
public void testConstructorNullPointerException6() { |
980 |
|
try { |
981 |
< |
new ThreadPoolExecutor(1, 2, |
925 |
< |
LONG_DELAY_MS, MILLISECONDS, |
981 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
982 |
|
(BlockingQueue) null, |
983 |
|
new SimpleThreadFactory(), |
984 |
|
new NoOpREHandler()); |
991 |
|
*/ |
992 |
|
public void testConstructorNullPointerException7() { |
993 |
|
try { |
994 |
< |
new ThreadPoolExecutor(1, 2, |
939 |
< |
LONG_DELAY_MS, MILLISECONDS, |
994 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
995 |
|
new ArrayBlockingQueue<Runnable>(10), |
996 |
|
new SimpleThreadFactory(), |
997 |
|
(RejectedExecutionHandler) null); |
1004 |
|
*/ |
1005 |
|
public void testConstructorNullPointerException8() { |
1006 |
|
try { |
1007 |
< |
new ThreadPoolExecutor(1, 2, |
953 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1007 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1008 |
|
new ArrayBlockingQueue<Runnable>(10), |
1009 |
|
(ThreadFactory) null, |
1010 |
|
new NoOpREHandler()); |
1018 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1019 |
|
final ThreadPoolExecutor p = |
1020 |
|
new ThreadPoolExecutor(1, 1, |
1021 |
< |
60, TimeUnit.SECONDS, |
1021 |
> |
60, SECONDS, |
1022 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1023 |
|
|
1024 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1266 |
|
} |
1267 |
|
} |
1268 |
|
|
1215 |
– |
|
1269 |
|
/** |
1270 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1271 |
|
*/ |
1287 |
|
} |
1288 |
|
} |
1289 |
|
|
1237 |
– |
|
1290 |
|
/** |
1291 |
|
* execute(null) throws NPE |
1292 |
|
*/ |
1293 |
|
public void testExecuteNull() { |
1294 |
|
ThreadPoolExecutor p = |
1295 |
< |
new ThreadPoolExecutor(1, 2, |
1244 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1295 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1296 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1297 |
|
try { |
1298 |
|
p.execute(null); |
1358 |
|
joinPool(p); |
1359 |
|
} |
1360 |
|
|
1361 |
+ |
/** |
1362 |
+ |
* Configuration changes that allow core pool size greater than |
1363 |
+ |
* max pool size result in IllegalArgumentException. |
1364 |
+ |
*/ |
1365 |
+ |
public void testPoolSizeInvariants() { |
1366 |
+ |
ThreadPoolExecutor p = |
1367 |
+ |
new ThreadPoolExecutor(1, 1, |
1368 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1369 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1370 |
+ |
for (int s = 1; s < 5; s++) { |
1371 |
+ |
p.setMaximumPoolSize(s); |
1372 |
+ |
p.setCorePoolSize(s); |
1373 |
+ |
try { |
1374 |
+ |
p.setMaximumPoolSize(s - 1); |
1375 |
+ |
shouldThrow(); |
1376 |
+ |
} catch (IllegalArgumentException success) {} |
1377 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1378 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1379 |
+ |
try { |
1380 |
+ |
p.setCorePoolSize(s + 1); |
1381 |
+ |
shouldThrow(); |
1382 |
+ |
} catch (IllegalArgumentException success) {} |
1383 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1384 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1385 |
+ |
} |
1386 |
+ |
joinPool(p); |
1387 |
+ |
} |
1388 |
|
|
1389 |
|
/** |
1390 |
|
* setKeepAliveTime throws IllegalArgumentException |
1411 |
|
public void testTerminated() { |
1412 |
|
ExtendedTPE p = new ExtendedTPE(); |
1413 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1414 |
< |
assertTrue(p.terminatedCalled); |
1414 |
> |
assertTrue(p.terminatedCalled()); |
1415 |
|
joinPool(p); |
1416 |
|
} |
1417 |
|
|
1421 |
|
public void testBeforeAfter() throws InterruptedException { |
1422 |
|
ExtendedTPE p = new ExtendedTPE(); |
1423 |
|
try { |
1424 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1425 |
< |
p.execute(r); |
1426 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1427 |
< |
assertTrue(r.done); |
1428 |
< |
assertTrue(p.beforeCalled); |
1429 |
< |
assertTrue(p.afterCalled); |
1424 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1425 |
> |
p.execute(new CheckedRunnable() { |
1426 |
> |
public void realRun() { |
1427 |
> |
done.countDown(); |
1428 |
> |
}}); |
1429 |
> |
await(p.afterCalled); |
1430 |
> |
assertEquals(0, done.getCount()); |
1431 |
> |
assertTrue(p.afterCalled()); |
1432 |
> |
assertTrue(p.beforeCalled()); |
1433 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1434 |
|
} finally { |
1435 |
|
joinPool(p); |
1487 |
|
} |
1488 |
|
} |
1489 |
|
|
1409 |
– |
|
1490 |
|
/** |
1491 |
|
* invokeAny(null) throws NPE |
1492 |
|
*/ |
1680 |
|
} |
1681 |
|
} |
1682 |
|
|
1603 |
– |
|
1604 |
– |
|
1683 |
|
/** |
1684 |
|
* timed invokeAny(null) throws NPE |
1685 |
|
*/ |
1902 |
|
l.add(new StringTask()); |
1903 |
|
l.add(new StringTask()); |
1904 |
|
List<Future<String>> futures = |
1905 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1905 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1906 |
|
assertEquals(2, futures.size()); |
1907 |
|
for (Future<String> future : futures) |
1908 |
|
assertSame(TEST_STRING, future.get()); |
1920 |
|
LONG_DELAY_MS, MILLISECONDS, |
1921 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1922 |
|
try { |
1923 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1924 |
< |
l.add(new StringTask()); |
1925 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1926 |
< |
l.add(new StringTask()); |
1927 |
< |
List<Future<String>> futures = |
1928 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1929 |
< |
assertEquals(3, futures.size()); |
1930 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1931 |
< |
Future<String> f1 = it.next(); |
1932 |
< |
Future<String> f2 = it.next(); |
1933 |
< |
Future<String> f3 = it.next(); |
1934 |
< |
assertTrue(f1.isDone()); |
1935 |
< |
assertTrue(f2.isDone()); |
1936 |
< |
assertTrue(f3.isDone()); |
1937 |
< |
assertFalse(f1.isCancelled()); |
1938 |
< |
assertTrue(f2.isCancelled()); |
1923 |
> |
for (long timeout = timeoutMillis();;) { |
1924 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1925 |
> |
tasks.add(new StringTask("0")); |
1926 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1927 |
> |
tasks.add(new StringTask("2")); |
1928 |
> |
long startTime = System.nanoTime(); |
1929 |
> |
List<Future<String>> futures = |
1930 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1931 |
> |
assertEquals(tasks.size(), futures.size()); |
1932 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1933 |
> |
for (Future future : futures) |
1934 |
> |
assertTrue(future.isDone()); |
1935 |
> |
assertTrue(futures.get(1).isCancelled()); |
1936 |
> |
try { |
1937 |
> |
assertEquals("0", futures.get(0).get()); |
1938 |
> |
assertEquals("2", futures.get(2).get()); |
1939 |
> |
break; |
1940 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1941 |
> |
timeout *= 2; |
1942 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1943 |
> |
fail("expected exactly one task to be cancelled"); |
1944 |
> |
} |
1945 |
> |
} |
1946 |
|
} finally { |
1947 |
|
joinPool(e); |
1948 |
|
} |
1988 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1989 |
|
*/ |
1990 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1991 |
+ |
long keepAliveTime = timeoutMillis(); |
1992 |
|
final ThreadPoolExecutor p = |
1993 |
|
new ThreadPoolExecutor(2, 10, |
1994 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1994 |
> |
keepAliveTime, MILLISECONDS, |
1995 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1996 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1997 |
|
try { |
1998 |
|
p.allowCoreThreadTimeOut(true); |
1999 |
|
p.execute(new CheckedRunnable() { |
2000 |
< |
public void realRun() throws InterruptedException { |
2000 |
> |
public void realRun() { |
2001 |
|
threadStarted.countDown(); |
2002 |
|
assertEquals(1, p.getPoolSize()); |
2003 |
|
}}); |
2004 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
2005 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
2006 |
< |
if (p.getPoolSize() == 0) |
2007 |
< |
break; |
2008 |
< |
Thread.sleep(10); |
2009 |
< |
} |
2004 |
> |
await(threadStarted); |
2005 |
> |
delay(keepAliveTime); |
2006 |
> |
long startTime = System.nanoTime(); |
2007 |
> |
while (p.getPoolSize() > 0 |
2008 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
2009 |
> |
Thread.yield(); |
2010 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
2011 |
|
assertEquals(0, p.getPoolSize()); |
2012 |
|
} finally { |
2013 |
|
joinPool(p); |
2018 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
2019 |
|
*/ |
2020 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
2021 |
+ |
long keepAliveTime = timeoutMillis(); |
2022 |
|
final ThreadPoolExecutor p = |
2023 |
|
new ThreadPoolExecutor(2, 10, |
2024 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
2024 |
> |
keepAliveTime, MILLISECONDS, |
2025 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2026 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2027 |
|
try { |
2031 |
|
threadStarted.countDown(); |
2032 |
|
assertTrue(p.getPoolSize() >= 1); |
2033 |
|
}}); |
2034 |
< |
Thread.sleep(SMALL_DELAY_MS); |
2034 |
> |
delay(2 * keepAliveTime); |
2035 |
|
assertTrue(p.getPoolSize() >= 1); |
2036 |
|
} finally { |
2037 |
|
joinPool(p); |
2050 |
|
done.countDown(); |
2051 |
|
}}; |
2052 |
|
final ThreadPoolExecutor p = |
2053 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2053 |
> |
new ThreadPoolExecutor(1, 30, |
2054 |
> |
60, SECONDS, |
2055 |
|
new ArrayBlockingQueue(30)); |
2056 |
|
try { |
2057 |
|
for (int i = 0; i < nTasks; ++i) { |
2066 |
|
// enough time to run all tasks |
2067 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2068 |
|
} finally { |
2069 |
< |
p.shutdown(); |
2069 |
> |
joinPool(p); |
2070 |
> |
} |
2071 |
> |
} |
2072 |
> |
|
2073 |
> |
/** |
2074 |
> |
* get(cancelled task) throws CancellationException |
2075 |
> |
*/ |
2076 |
> |
public void testGet_cancelled() throws Exception { |
2077 |
> |
final ExecutorService e = |
2078 |
> |
new ThreadPoolExecutor(1, 1, |
2079 |
> |
LONG_DELAY_MS, MILLISECONDS, |
2080 |
> |
new LinkedBlockingQueue<Runnable>()); |
2081 |
> |
try { |
2082 |
> |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2083 |
> |
final CountDownLatch done = new CountDownLatch(1); |
2084 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
2085 |
> |
for (int i = 0; i < 2; i++) { |
2086 |
> |
Runnable r = new CheckedRunnable() { public void realRun() |
2087 |
> |
throws Throwable { |
2088 |
> |
blockerStarted.countDown(); |
2089 |
> |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2090 |
> |
}}; |
2091 |
> |
futures.add(e.submit(r)); |
2092 |
> |
} |
2093 |
> |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2094 |
> |
for (Future<?> future : futures) future.cancel(false); |
2095 |
> |
for (Future<?> future : futures) { |
2096 |
> |
try { |
2097 |
> |
future.get(); |
2098 |
> |
shouldThrow(); |
2099 |
> |
} catch (CancellationException success) {} |
2100 |
> |
try { |
2101 |
> |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2102 |
> |
shouldThrow(); |
2103 |
> |
} catch (CancellationException success) {} |
2104 |
> |
assertTrue(future.isCancelled()); |
2105 |
> |
assertTrue(future.isDone()); |
2106 |
> |
} |
2107 |
> |
done.countDown(); |
2108 |
> |
} finally { |
2109 |
> |
joinPool(e); |
2110 |
|
} |
2111 |
|
} |
2112 |
|
|