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.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 |
|
*/ |
182 |
|
threadProceed.await(); |
183 |
|
threadDone.countDown(); |
184 |
|
}}); |
185 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
185 |
> |
await(threadStarted); |
186 |
|
assertEquals(0, p.getCompletedTaskCount()); |
187 |
|
threadProceed.countDown(); |
188 |
|
threadDone.await(); |
189 |
< |
Thread.sleep(SHORT_DELAY_MS); |
190 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
189 |
> |
long startTime = System.nanoTime(); |
190 |
> |
while (p.getCompletedTaskCount() != 1) { |
191 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
192 |
> |
fail("timed out"); |
193 |
> |
Thread.yield(); |
194 |
> |
} |
195 |
|
} finally { |
196 |
|
joinPool(p); |
197 |
|
} |
217 |
|
new ThreadPoolExecutor(2, 2, |
218 |
|
1000, MILLISECONDS, |
219 |
|
new ArrayBlockingQueue<Runnable>(10)); |
220 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
220 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
221 |
|
joinPool(p); |
222 |
|
} |
223 |
|
|
188 |
– |
|
224 |
|
/** |
225 |
|
* getThreadFactory returns factory in constructor if not set |
226 |
|
*/ |
250 |
|
joinPool(p); |
251 |
|
} |
252 |
|
|
218 |
– |
|
253 |
|
/** |
254 |
|
* setThreadFactory(null) throws NPE |
255 |
|
*/ |
296 |
|
joinPool(p); |
297 |
|
} |
298 |
|
|
265 |
– |
|
299 |
|
/** |
300 |
|
* setRejectedExecutionHandler(null) throws NPE |
301 |
|
*/ |
313 |
|
} |
314 |
|
} |
315 |
|
|
283 |
– |
|
316 |
|
/** |
317 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
318 |
|
* multiple threads active |
410 |
|
} |
411 |
|
|
412 |
|
/** |
413 |
< |
* isShutDown is false before shutdown, true after |
413 |
> |
* isShutdown is false before shutdown, true after |
414 |
|
*/ |
415 |
|
public void testIsShutdown() { |
416 |
|
final ThreadPoolExecutor p = |
423 |
|
joinPool(p); |
424 |
|
} |
425 |
|
|
426 |
+ |
/** |
427 |
+ |
* awaitTermination on a non-shutdown pool times out |
428 |
+ |
*/ |
429 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
430 |
+ |
final ThreadPoolExecutor p = |
431 |
+ |
new ThreadPoolExecutor(1, 1, |
432 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
433 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
434 |
+ |
assertFalse(p.isTerminated()); |
435 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
436 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
437 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
438 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
439 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
440 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
441 |
+ |
long timeoutNanos = 999999L; |
442 |
+ |
long startTime = System.nanoTime(); |
443 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
444 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
445 |
+ |
assertFalse(p.isTerminated()); |
446 |
+ |
startTime = System.nanoTime(); |
447 |
+ |
long timeoutMillis = timeoutMillis(); |
448 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
449 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
450 |
+ |
assertFalse(p.isTerminated()); |
451 |
+ |
p.shutdown(); |
452 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
453 |
+ |
assertTrue(p.isTerminated()); |
454 |
+ |
} |
455 |
|
|
456 |
|
/** |
457 |
|
* isTerminated is false before termination, true after |
467 |
|
try { |
468 |
|
p.execute(new CheckedRunnable() { |
469 |
|
public void realRun() throws InterruptedException { |
409 |
– |
threadStarted.countDown(); |
470 |
|
assertFalse(p.isTerminated()); |
471 |
+ |
threadStarted.countDown(); |
472 |
|
done.await(); |
473 |
|
}}); |
474 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
475 |
+ |
assertFalse(p.isTerminating()); |
476 |
|
done.countDown(); |
477 |
|
} finally { |
478 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
495 |
|
assertFalse(p.isTerminating()); |
496 |
|
p.execute(new CheckedRunnable() { |
497 |
|
public void realRun() throws InterruptedException { |
436 |
– |
threadStarted.countDown(); |
498 |
|
assertFalse(p.isTerminating()); |
499 |
+ |
threadStarted.countDown(); |
500 |
|
done.await(); |
501 |
|
}}); |
502 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
625 |
|
} |
626 |
|
|
627 |
|
/** |
628 |
< |
* shutDownNow returns a list containing tasks that were not run |
628 |
> |
* shutdownNow returns a list containing tasks that were not run, |
629 |
> |
* and those tasks are drained from the queue |
630 |
|
*/ |
631 |
< |
public void testShutDownNow() { |
631 |
> |
public void testShutdownNow() throws InterruptedException { |
632 |
> |
final int poolSize = 2; |
633 |
> |
final int count = 5; |
634 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
635 |
|
final ThreadPoolExecutor p = |
636 |
< |
new ThreadPoolExecutor(1, 1, |
636 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
637 |
|
LONG_DELAY_MS, MILLISECONDS, |
638 |
|
new ArrayBlockingQueue<Runnable>(10)); |
639 |
< |
List l; |
640 |
< |
try { |
641 |
< |
for (int i = 0; i < 5; i++) |
576 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
577 |
< |
} |
578 |
< |
finally { |
639 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
640 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
641 |
> |
threadsStarted.countDown(); |
642 |
|
try { |
643 |
< |
l = p.shutdownNow(); |
644 |
< |
} catch (SecurityException ok) { return; } |
643 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
644 |
> |
} catch (InterruptedException success) {} |
645 |
> |
ran.getAndIncrement(); |
646 |
> |
}}; |
647 |
> |
for (int i = 0; i < count; i++) |
648 |
> |
p.execute(waiter); |
649 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
650 |
> |
assertEquals(poolSize, p.getActiveCount()); |
651 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
652 |
> |
final List<Runnable> queuedTasks; |
653 |
> |
try { |
654 |
> |
queuedTasks = p.shutdownNow(); |
655 |
> |
} catch (SecurityException ok) { |
656 |
> |
return; // Allowed in case test doesn't have privs |
657 |
|
} |
658 |
|
assertTrue(p.isShutdown()); |
659 |
< |
assertTrue(l.size() <= 4); |
659 |
> |
assertTrue(p.getQueue().isEmpty()); |
660 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
661 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
662 |
> |
assertTrue(p.isTerminated()); |
663 |
> |
assertEquals(poolSize, ran.get()); |
664 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
665 |
|
} |
666 |
|
|
667 |
|
// Exception Tests |
668 |
|
|
589 |
– |
|
669 |
|
/** |
670 |
|
* Constructor throws if corePoolSize argument is less than zero |
671 |
|
*/ |
672 |
|
public void testConstructor1() { |
673 |
|
try { |
674 |
< |
new ThreadPoolExecutor(-1, 1, |
596 |
< |
LONG_DELAY_MS, MILLISECONDS, |
674 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
675 |
|
new ArrayBlockingQueue<Runnable>(10)); |
676 |
|
shouldThrow(); |
677 |
|
} catch (IllegalArgumentException success) {} |
682 |
|
*/ |
683 |
|
public void testConstructor2() { |
684 |
|
try { |
685 |
< |
new ThreadPoolExecutor(1, -1, |
608 |
< |
LONG_DELAY_MS, MILLISECONDS, |
685 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
686 |
|
new ArrayBlockingQueue<Runnable>(10)); |
687 |
|
shouldThrow(); |
688 |
|
} catch (IllegalArgumentException success) {} |
693 |
|
*/ |
694 |
|
public void testConstructor3() { |
695 |
|
try { |
696 |
< |
new ThreadPoolExecutor(1, 0, |
620 |
< |
LONG_DELAY_MS, MILLISECONDS, |
696 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
697 |
|
new ArrayBlockingQueue<Runnable>(10)); |
698 |
|
shouldThrow(); |
699 |
|
} catch (IllegalArgumentException success) {} |
704 |
|
*/ |
705 |
|
public void testConstructor4() { |
706 |
|
try { |
707 |
< |
new ThreadPoolExecutor(1, 2, |
632 |
< |
-1L, MILLISECONDS, |
707 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
708 |
|
new ArrayBlockingQueue<Runnable>(10)); |
709 |
|
shouldThrow(); |
710 |
|
} catch (IllegalArgumentException success) {} |
715 |
|
*/ |
716 |
|
public void testConstructor5() { |
717 |
|
try { |
718 |
< |
new ThreadPoolExecutor(2, 1, |
644 |
< |
LONG_DELAY_MS, MILLISECONDS, |
718 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
719 |
|
new ArrayBlockingQueue<Runnable>(10)); |
720 |
|
shouldThrow(); |
721 |
|
} catch (IllegalArgumentException success) {} |
726 |
|
*/ |
727 |
|
public void testConstructorNullPointerException() { |
728 |
|
try { |
729 |
< |
new ThreadPoolExecutor(1, 2, |
656 |
< |
LONG_DELAY_MS, MILLISECONDS, |
729 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
730 |
|
(BlockingQueue) null); |
731 |
|
shouldThrow(); |
732 |
|
} catch (NullPointerException success) {} |
733 |
|
} |
734 |
|
|
662 |
– |
|
663 |
– |
|
735 |
|
/** |
736 |
|
* Constructor throws if corePoolSize argument is less than zero |
737 |
|
*/ |
738 |
|
public void testConstructor6() { |
739 |
|
try { |
740 |
< |
new ThreadPoolExecutor(-1, 1, |
670 |
< |
LONG_DELAY_MS, MILLISECONDS, |
740 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
741 |
|
new ArrayBlockingQueue<Runnable>(10), |
742 |
|
new SimpleThreadFactory()); |
743 |
|
shouldThrow(); |
749 |
|
*/ |
750 |
|
public void testConstructor7() { |
751 |
|
try { |
752 |
< |
new ThreadPoolExecutor(1, -1, |
683 |
< |
LONG_DELAY_MS, MILLISECONDS, |
752 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
753 |
|
new ArrayBlockingQueue<Runnable>(10), |
754 |
|
new SimpleThreadFactory()); |
755 |
|
shouldThrow(); |
761 |
|
*/ |
762 |
|
public void testConstructor8() { |
763 |
|
try { |
764 |
< |
new ThreadPoolExecutor(1, 0, |
696 |
< |
LONG_DELAY_MS, MILLISECONDS, |
764 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
765 |
|
new ArrayBlockingQueue<Runnable>(10), |
766 |
|
new SimpleThreadFactory()); |
767 |
|
shouldThrow(); |
773 |
|
*/ |
774 |
|
public void testConstructor9() { |
775 |
|
try { |
776 |
< |
new ThreadPoolExecutor(1, 2, |
709 |
< |
-1L, MILLISECONDS, |
776 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
777 |
|
new ArrayBlockingQueue<Runnable>(10), |
778 |
|
new SimpleThreadFactory()); |
779 |
|
shouldThrow(); |
785 |
|
*/ |
786 |
|
public void testConstructor10() { |
787 |
|
try { |
788 |
< |
new ThreadPoolExecutor(2, 1, |
722 |
< |
LONG_DELAY_MS, MILLISECONDS, |
788 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
789 |
|
new ArrayBlockingQueue<Runnable>(10), |
790 |
|
new SimpleThreadFactory()); |
791 |
|
shouldThrow(); |
797 |
|
*/ |
798 |
|
public void testConstructorNullPointerException2() { |
799 |
|
try { |
800 |
< |
new ThreadPoolExecutor(1, 2, |
735 |
< |
LONG_DELAY_MS, MILLISECONDS, |
800 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
801 |
|
(BlockingQueue) null, |
802 |
|
new SimpleThreadFactory()); |
803 |
|
shouldThrow(); |
809 |
|
*/ |
810 |
|
public void testConstructorNullPointerException3() { |
811 |
|
try { |
812 |
< |
new ThreadPoolExecutor(1, 2, |
748 |
< |
LONG_DELAY_MS, MILLISECONDS, |
812 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
813 |
|
new ArrayBlockingQueue<Runnable>(10), |
814 |
|
(ThreadFactory) null); |
815 |
|
shouldThrow(); |
816 |
|
} catch (NullPointerException success) {} |
817 |
|
} |
818 |
|
|
755 |
– |
|
819 |
|
/** |
820 |
|
* Constructor throws if corePoolSize argument is less than zero |
821 |
|
*/ |
822 |
|
public void testConstructor11() { |
823 |
|
try { |
824 |
< |
new ThreadPoolExecutor(-1, 1, |
762 |
< |
LONG_DELAY_MS, MILLISECONDS, |
824 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
825 |
|
new ArrayBlockingQueue<Runnable>(10), |
826 |
|
new NoOpREHandler()); |
827 |
|
shouldThrow(); |
833 |
|
*/ |
834 |
|
public void testConstructor12() { |
835 |
|
try { |
836 |
< |
new ThreadPoolExecutor(1, -1, |
775 |
< |
LONG_DELAY_MS, MILLISECONDS, |
836 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
837 |
|
new ArrayBlockingQueue<Runnable>(10), |
838 |
|
new NoOpREHandler()); |
839 |
|
shouldThrow(); |
845 |
|
*/ |
846 |
|
public void testConstructor13() { |
847 |
|
try { |
848 |
< |
new ThreadPoolExecutor(1, 0, |
788 |
< |
LONG_DELAY_MS, MILLISECONDS, |
848 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
849 |
|
new ArrayBlockingQueue<Runnable>(10), |
850 |
|
new NoOpREHandler()); |
851 |
|
shouldThrow(); |
857 |
|
*/ |
858 |
|
public void testConstructor14() { |
859 |
|
try { |
860 |
< |
new ThreadPoolExecutor(1, 2, |
801 |
< |
-1L, MILLISECONDS, |
860 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
861 |
|
new ArrayBlockingQueue<Runnable>(10), |
862 |
|
new NoOpREHandler()); |
863 |
|
shouldThrow(); |
869 |
|
*/ |
870 |
|
public void testConstructor15() { |
871 |
|
try { |
872 |
< |
new ThreadPoolExecutor(2, 1, |
814 |
< |
LONG_DELAY_MS, MILLISECONDS, |
872 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
873 |
|
new ArrayBlockingQueue<Runnable>(10), |
874 |
|
new NoOpREHandler()); |
875 |
|
shouldThrow(); |
881 |
|
*/ |
882 |
|
public void testConstructorNullPointerException4() { |
883 |
|
try { |
884 |
< |
new ThreadPoolExecutor(1, 2, |
827 |
< |
LONG_DELAY_MS, MILLISECONDS, |
884 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
885 |
|
(BlockingQueue) null, |
886 |
|
new NoOpREHandler()); |
887 |
|
shouldThrow(); |
893 |
|
*/ |
894 |
|
public void testConstructorNullPointerException5() { |
895 |
|
try { |
896 |
< |
new ThreadPoolExecutor(1, 2, |
840 |
< |
LONG_DELAY_MS, MILLISECONDS, |
896 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
897 |
|
new ArrayBlockingQueue<Runnable>(10), |
898 |
|
(RejectedExecutionHandler) null); |
899 |
|
shouldThrow(); |
900 |
|
} catch (NullPointerException success) {} |
901 |
|
} |
902 |
|
|
847 |
– |
|
903 |
|
/** |
904 |
|
* Constructor throws if corePoolSize argument is less than zero |
905 |
|
*/ |
906 |
|
public void testConstructor16() { |
907 |
|
try { |
908 |
< |
new ThreadPoolExecutor(-1, 1, |
854 |
< |
LONG_DELAY_MS, MILLISECONDS, |
908 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
909 |
|
new ArrayBlockingQueue<Runnable>(10), |
910 |
|
new SimpleThreadFactory(), |
911 |
|
new NoOpREHandler()); |
918 |
|
*/ |
919 |
|
public void testConstructor17() { |
920 |
|
try { |
921 |
< |
new ThreadPoolExecutor(1, -1, |
868 |
< |
LONG_DELAY_MS, MILLISECONDS, |
921 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
922 |
|
new ArrayBlockingQueue<Runnable>(10), |
923 |
|
new SimpleThreadFactory(), |
924 |
|
new NoOpREHandler()); |
931 |
|
*/ |
932 |
|
public void testConstructor18() { |
933 |
|
try { |
934 |
< |
new ThreadPoolExecutor(1, 0, |
882 |
< |
LONG_DELAY_MS, MILLISECONDS, |
934 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
935 |
|
new ArrayBlockingQueue<Runnable>(10), |
936 |
|
new SimpleThreadFactory(), |
937 |
|
new NoOpREHandler()); |
944 |
|
*/ |
945 |
|
public void testConstructor19() { |
946 |
|
try { |
947 |
< |
new ThreadPoolExecutor(1, 2, |
896 |
< |
-1L, MILLISECONDS, |
947 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
948 |
|
new ArrayBlockingQueue<Runnable>(10), |
949 |
|
new SimpleThreadFactory(), |
950 |
|
new NoOpREHandler()); |
957 |
|
*/ |
958 |
|
public void testConstructor20() { |
959 |
|
try { |
960 |
< |
new ThreadPoolExecutor(2, 1, |
910 |
< |
LONG_DELAY_MS, MILLISECONDS, |
960 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
961 |
|
new ArrayBlockingQueue<Runnable>(10), |
962 |
|
new SimpleThreadFactory(), |
963 |
|
new NoOpREHandler()); |
970 |
|
*/ |
971 |
|
public void testConstructorNullPointerException6() { |
972 |
|
try { |
973 |
< |
new ThreadPoolExecutor(1, 2, |
924 |
< |
LONG_DELAY_MS, MILLISECONDS, |
973 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
974 |
|
(BlockingQueue) null, |
975 |
|
new SimpleThreadFactory(), |
976 |
|
new NoOpREHandler()); |
983 |
|
*/ |
984 |
|
public void testConstructorNullPointerException7() { |
985 |
|
try { |
986 |
< |
new ThreadPoolExecutor(1, 2, |
938 |
< |
LONG_DELAY_MS, MILLISECONDS, |
986 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
987 |
|
new ArrayBlockingQueue<Runnable>(10), |
988 |
|
new SimpleThreadFactory(), |
989 |
|
(RejectedExecutionHandler) null); |
996 |
|
*/ |
997 |
|
public void testConstructorNullPointerException8() { |
998 |
|
try { |
999 |
< |
new ThreadPoolExecutor(1, 2, |
952 |
< |
LONG_DELAY_MS, MILLISECONDS, |
999 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1000 |
|
new ArrayBlockingQueue<Runnable>(10), |
1001 |
|
(ThreadFactory) null, |
1002 |
|
new NoOpREHandler()); |
1010 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1011 |
|
final ThreadPoolExecutor p = |
1012 |
|
new ThreadPoolExecutor(1, 1, |
1013 |
< |
60, TimeUnit.SECONDS, |
1013 |
> |
60, SECONDS, |
1014 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1015 |
|
|
1016 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1258 |
|
} |
1259 |
|
} |
1260 |
|
|
1214 |
– |
|
1261 |
|
/** |
1262 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1263 |
|
*/ |
1279 |
|
} |
1280 |
|
} |
1281 |
|
|
1236 |
– |
|
1282 |
|
/** |
1283 |
|
* execute(null) throws NPE |
1284 |
|
*/ |
1285 |
|
public void testExecuteNull() { |
1286 |
|
ThreadPoolExecutor p = |
1287 |
< |
new ThreadPoolExecutor(1, 2, |
1243 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1287 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1288 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1289 |
|
try { |
1290 |
|
p.execute(null); |
1350 |
|
joinPool(p); |
1351 |
|
} |
1352 |
|
|
1353 |
+ |
/** |
1354 |
+ |
* Configuration changes that allow core pool size greater than |
1355 |
+ |
* max pool size result in IllegalArgumentException. |
1356 |
+ |
*/ |
1357 |
+ |
public void testPoolSizeInvariants() { |
1358 |
+ |
ThreadPoolExecutor p = |
1359 |
+ |
new ThreadPoolExecutor(1, 1, |
1360 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1361 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1362 |
+ |
for (int s = 1; s < 5; s++) { |
1363 |
+ |
p.setMaximumPoolSize(s); |
1364 |
+ |
p.setCorePoolSize(s); |
1365 |
+ |
try { |
1366 |
+ |
p.setMaximumPoolSize(s - 1); |
1367 |
+ |
shouldThrow(); |
1368 |
+ |
} catch (IllegalArgumentException success) {} |
1369 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1370 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1371 |
+ |
try { |
1372 |
+ |
p.setCorePoolSize(s + 1); |
1373 |
+ |
shouldThrow(); |
1374 |
+ |
} catch (IllegalArgumentException success) {} |
1375 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1376 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1377 |
+ |
} |
1378 |
+ |
joinPool(p); |
1379 |
+ |
} |
1380 |
|
|
1381 |
|
/** |
1382 |
|
* setKeepAliveTime throws IllegalArgumentException |
1403 |
|
public void testTerminated() { |
1404 |
|
ExtendedTPE p = new ExtendedTPE(); |
1405 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1406 |
< |
assertTrue(p.terminatedCalled); |
1406 |
> |
assertTrue(p.terminatedCalled()); |
1407 |
|
joinPool(p); |
1408 |
|
} |
1409 |
|
|
1413 |
|
public void testBeforeAfter() throws InterruptedException { |
1414 |
|
ExtendedTPE p = new ExtendedTPE(); |
1415 |
|
try { |
1416 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1417 |
< |
p.execute(r); |
1418 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1419 |
< |
assertTrue(r.done); |
1420 |
< |
assertTrue(p.beforeCalled); |
1421 |
< |
assertTrue(p.afterCalled); |
1416 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1417 |
> |
p.execute(new CheckedRunnable() { |
1418 |
> |
public void realRun() { |
1419 |
> |
done.countDown(); |
1420 |
> |
}}); |
1421 |
> |
await(p.afterCalled); |
1422 |
> |
assertEquals(0, done.getCount()); |
1423 |
> |
assertTrue(p.afterCalled()); |
1424 |
> |
assertTrue(p.beforeCalled()); |
1425 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1426 |
|
} finally { |
1427 |
|
joinPool(p); |
1479 |
|
} |
1480 |
|
} |
1481 |
|
|
1408 |
– |
|
1482 |
|
/** |
1483 |
|
* invokeAny(null) throws NPE |
1484 |
|
*/ |
1672 |
|
} |
1673 |
|
} |
1674 |
|
|
1602 |
– |
|
1603 |
– |
|
1675 |
|
/** |
1676 |
|
* timed invokeAny(null) throws NPE |
1677 |
|
*/ |
1912 |
|
LONG_DELAY_MS, MILLISECONDS, |
1913 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1914 |
|
try { |
1915 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1916 |
< |
l.add(new StringTask()); |
1917 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1918 |
< |
l.add(new StringTask()); |
1919 |
< |
List<Future<String>> futures = |
1920 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1921 |
< |
assertEquals(3, futures.size()); |
1922 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1923 |
< |
Future<String> f1 = it.next(); |
1924 |
< |
Future<String> f2 = it.next(); |
1925 |
< |
Future<String> f3 = it.next(); |
1926 |
< |
assertTrue(f1.isDone()); |
1927 |
< |
assertTrue(f2.isDone()); |
1928 |
< |
assertTrue(f3.isDone()); |
1929 |
< |
assertFalse(f1.isCancelled()); |
1930 |
< |
assertTrue(f2.isCancelled()); |
1915 |
> |
for (long timeout = timeoutMillis();;) { |
1916 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1917 |
> |
tasks.add(new StringTask("0")); |
1918 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1919 |
> |
tasks.add(new StringTask("2")); |
1920 |
> |
long startTime = System.nanoTime(); |
1921 |
> |
List<Future<String>> futures = |
1922 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1923 |
> |
assertEquals(tasks.size(), futures.size()); |
1924 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1925 |
> |
for (Future future : futures) |
1926 |
> |
assertTrue(future.isDone()); |
1927 |
> |
assertTrue(futures.get(1).isCancelled()); |
1928 |
> |
try { |
1929 |
> |
assertEquals("0", futures.get(0).get()); |
1930 |
> |
assertEquals("2", futures.get(2).get()); |
1931 |
> |
break; |
1932 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1933 |
> |
timeout *= 2; |
1934 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1935 |
> |
fail("expected exactly one task to be cancelled"); |
1936 |
> |
} |
1937 |
> |
} |
1938 |
|
} finally { |
1939 |
|
joinPool(e); |
1940 |
|
} |
1980 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1981 |
|
*/ |
1982 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1983 |
+ |
long keepAliveTime = timeoutMillis(); |
1984 |
|
final ThreadPoolExecutor p = |
1985 |
|
new ThreadPoolExecutor(2, 10, |
1986 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1986 |
> |
keepAliveTime, MILLISECONDS, |
1987 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1988 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1989 |
|
try { |
1990 |
|
p.allowCoreThreadTimeOut(true); |
1991 |
|
p.execute(new CheckedRunnable() { |
1992 |
< |
public void realRun() throws InterruptedException { |
1992 |
> |
public void realRun() { |
1993 |
|
threadStarted.countDown(); |
1994 |
|
assertEquals(1, p.getPoolSize()); |
1995 |
|
}}); |
1996 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1997 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1998 |
< |
if (p.getPoolSize() == 0) |
1999 |
< |
break; |
2000 |
< |
Thread.sleep(10); |
2001 |
< |
} |
1996 |
> |
await(threadStarted); |
1997 |
> |
delay(keepAliveTime); |
1998 |
> |
long startTime = System.nanoTime(); |
1999 |
> |
while (p.getPoolSize() > 0 |
2000 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
2001 |
> |
Thread.yield(); |
2002 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
2003 |
|
assertEquals(0, p.getPoolSize()); |
2004 |
|
} finally { |
2005 |
|
joinPool(p); |
2010 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
2011 |
|
*/ |
2012 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
2013 |
+ |
long keepAliveTime = timeoutMillis(); |
2014 |
|
final ThreadPoolExecutor p = |
2015 |
|
new ThreadPoolExecutor(2, 10, |
2016 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
2016 |
> |
keepAliveTime, MILLISECONDS, |
2017 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2018 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2019 |
|
try { |
2023 |
|
threadStarted.countDown(); |
2024 |
|
assertTrue(p.getPoolSize() >= 1); |
2025 |
|
}}); |
2026 |
< |
Thread.sleep(SMALL_DELAY_MS); |
2026 |
> |
delay(2 * keepAliveTime); |
2027 |
|
assertTrue(p.getPoolSize() >= 1); |
2028 |
|
} finally { |
2029 |
|
joinPool(p); |
2042 |
|
done.countDown(); |
2043 |
|
}}; |
2044 |
|
final ThreadPoolExecutor p = |
2045 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2045 |
> |
new ThreadPoolExecutor(1, 30, |
2046 |
> |
60, SECONDS, |
2047 |
|
new ArrayBlockingQueue(30)); |
2048 |
|
try { |
2049 |
|
for (int i = 0; i < nTasks; ++i) { |
2058 |
|
// enough time to run all tasks |
2059 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2060 |
|
} finally { |
2061 |
< |
p.shutdown(); |
2061 |
> |
joinPool(p); |
2062 |
|
} |
2063 |
|
} |
2064 |
|
|