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 |
|
/** |
158 |
|
*/ |
159 |
|
public void testPrestartAllCoreThreads() { |
160 |
|
final ThreadPoolExecutor p = |
161 |
< |
new ThreadPoolExecutor(2, 2, |
161 |
> |
new ThreadPoolExecutor(2, 6, |
162 |
|
LONG_DELAY_MS, MILLISECONDS, |
163 |
|
new ArrayBlockingQueue<Runnable>(10)); |
164 |
< |
assertEquals(0, p.getPoolSize()); |
165 |
< |
p.prestartAllCoreThreads(); |
166 |
< |
assertEquals(2, p.getPoolSize()); |
167 |
< |
p.prestartAllCoreThreads(); |
168 |
< |
assertEquals(2, p.getPoolSize()); |
169 |
< |
joinPool(p); |
164 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
165 |
> |
assertEquals(0, p.getPoolSize()); |
166 |
> |
p.prestartAllCoreThreads(); |
167 |
> |
assertEquals(2, p.getPoolSize()); |
168 |
> |
p.prestartAllCoreThreads(); |
169 |
> |
assertEquals(2, p.getPoolSize()); |
170 |
> |
p.setCorePoolSize(4); |
171 |
> |
p.prestartAllCoreThreads(); |
172 |
> |
assertEquals(4, p.getPoolSize()); |
173 |
> |
p.prestartAllCoreThreads(); |
174 |
> |
assertEquals(4, p.getPoolSize()); |
175 |
> |
} |
176 |
|
} |
177 |
|
|
178 |
|
/** |
184 |
|
new ThreadPoolExecutor(2, 2, |
185 |
|
LONG_DELAY_MS, MILLISECONDS, |
186 |
|
new ArrayBlockingQueue<Runnable>(10)); |
187 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
188 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
189 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
190 |
< |
try { |
187 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
188 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
189 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
190 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
191 |
|
assertEquals(0, p.getCompletedTaskCount()); |
192 |
|
p.execute(new CheckedRunnable() { |
193 |
|
public void realRun() throws InterruptedException { |
196 |
|
threadProceed.await(); |
197 |
|
threadDone.countDown(); |
198 |
|
}}); |
199 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
199 |
> |
await(threadStarted); |
200 |
|
assertEquals(0, p.getCompletedTaskCount()); |
201 |
|
threadProceed.countDown(); |
202 |
|
threadDone.await(); |
203 |
< |
delay(SHORT_DELAY_MS); |
204 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
205 |
< |
} finally { |
206 |
< |
joinPool(p); |
203 |
> |
long startTime = System.nanoTime(); |
204 |
> |
while (p.getCompletedTaskCount() != 1) { |
205 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
206 |
> |
fail("timed out"); |
207 |
> |
Thread.yield(); |
208 |
> |
} |
209 |
|
} |
210 |
|
} |
211 |
|
|
217 |
|
new ThreadPoolExecutor(1, 1, |
218 |
|
LONG_DELAY_MS, MILLISECONDS, |
219 |
|
new ArrayBlockingQueue<Runnable>(10)); |
220 |
< |
assertEquals(1, p.getCorePoolSize()); |
221 |
< |
joinPool(p); |
220 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
221 |
> |
assertEquals(1, p.getCorePoolSize()); |
222 |
> |
} |
223 |
|
} |
224 |
|
|
225 |
|
/** |
230 |
|
new ThreadPoolExecutor(2, 2, |
231 |
|
1000, MILLISECONDS, |
232 |
|
new ArrayBlockingQueue<Runnable>(10)); |
233 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
233 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
234 |
|
joinPool(p); |
235 |
|
} |
236 |
|
|
188 |
– |
|
237 |
|
/** |
238 |
|
* getThreadFactory returns factory in constructor if not set |
239 |
|
*/ |
263 |
|
joinPool(p); |
264 |
|
} |
265 |
|
|
218 |
– |
|
266 |
|
/** |
267 |
|
* setThreadFactory(null) throws NPE |
268 |
|
*/ |
309 |
|
joinPool(p); |
310 |
|
} |
311 |
|
|
265 |
– |
|
312 |
|
/** |
313 |
|
* setRejectedExecutionHandler(null) throws NPE |
314 |
|
*/ |
326 |
|
} |
327 |
|
} |
328 |
|
|
283 |
– |
|
329 |
|
/** |
330 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
331 |
|
* multiple threads active |
423 |
|
} |
424 |
|
|
425 |
|
/** |
426 |
< |
* isShutDown is false before shutdown, true after |
426 |
> |
* isShutdown is false before shutdown, true after |
427 |
|
*/ |
428 |
|
public void testIsShutdown() { |
429 |
|
final ThreadPoolExecutor p = |
436 |
|
joinPool(p); |
437 |
|
} |
438 |
|
|
439 |
+ |
/** |
440 |
+ |
* awaitTermination on a non-shutdown pool times out |
441 |
+ |
*/ |
442 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
443 |
+ |
final ThreadPoolExecutor p = |
444 |
+ |
new ThreadPoolExecutor(1, 1, |
445 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
446 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
447 |
+ |
assertFalse(p.isTerminated()); |
448 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
449 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
450 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
451 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
452 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
453 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
454 |
+ |
long timeoutNanos = 999999L; |
455 |
+ |
long startTime = System.nanoTime(); |
456 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
457 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
458 |
+ |
assertFalse(p.isTerminated()); |
459 |
+ |
startTime = System.nanoTime(); |
460 |
+ |
long timeoutMillis = timeoutMillis(); |
461 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
462 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
463 |
+ |
assertFalse(p.isTerminated()); |
464 |
+ |
p.shutdown(); |
465 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
466 |
+ |
assertTrue(p.isTerminated()); |
467 |
+ |
} |
468 |
|
|
469 |
|
/** |
470 |
|
* isTerminated is false before termination, true after |
638 |
|
} |
639 |
|
|
640 |
|
/** |
641 |
< |
* shutDownNow returns a list containing tasks that were not run |
641 |
> |
* shutdownNow returns a list containing tasks that were not run, |
642 |
> |
* and those tasks are drained from the queue |
643 |
|
*/ |
644 |
< |
public void testShutDownNow() { |
644 |
> |
public void testShutdownNow() throws InterruptedException { |
645 |
> |
final int poolSize = 2; |
646 |
> |
final int count = 5; |
647 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
648 |
|
final ThreadPoolExecutor p = |
649 |
< |
new ThreadPoolExecutor(1, 1, |
649 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
650 |
|
LONG_DELAY_MS, MILLISECONDS, |
651 |
|
new ArrayBlockingQueue<Runnable>(10)); |
652 |
< |
List l; |
653 |
< |
try { |
654 |
< |
for (int i = 0; i < 5; i++) |
577 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
578 |
< |
} |
579 |
< |
finally { |
652 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
653 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
654 |
> |
threadsStarted.countDown(); |
655 |
|
try { |
656 |
< |
l = p.shutdownNow(); |
657 |
< |
} catch (SecurityException ok) { return; } |
656 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
657 |
> |
} catch (InterruptedException success) {} |
658 |
> |
ran.getAndIncrement(); |
659 |
> |
}}; |
660 |
> |
for (int i = 0; i < count; i++) |
661 |
> |
p.execute(waiter); |
662 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
663 |
> |
assertEquals(poolSize, p.getActiveCount()); |
664 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
665 |
> |
final List<Runnable> queuedTasks; |
666 |
> |
try { |
667 |
> |
queuedTasks = p.shutdownNow(); |
668 |
> |
} catch (SecurityException ok) { |
669 |
> |
return; // Allowed in case test doesn't have privs |
670 |
|
} |
671 |
|
assertTrue(p.isShutdown()); |
672 |
< |
assertTrue(l.size() <= 4); |
672 |
> |
assertTrue(p.getQueue().isEmpty()); |
673 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
674 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
675 |
> |
assertTrue(p.isTerminated()); |
676 |
> |
assertEquals(poolSize, ran.get()); |
677 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
678 |
|
} |
679 |
|
|
680 |
|
// Exception Tests |
681 |
|
|
590 |
– |
|
682 |
|
/** |
683 |
|
* Constructor throws if corePoolSize argument is less than zero |
684 |
|
*/ |
685 |
|
public void testConstructor1() { |
686 |
|
try { |
687 |
< |
new ThreadPoolExecutor(-1, 1, |
597 |
< |
LONG_DELAY_MS, MILLISECONDS, |
687 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
688 |
|
new ArrayBlockingQueue<Runnable>(10)); |
689 |
|
shouldThrow(); |
690 |
|
} catch (IllegalArgumentException success) {} |
695 |
|
*/ |
696 |
|
public void testConstructor2() { |
697 |
|
try { |
698 |
< |
new ThreadPoolExecutor(1, -1, |
609 |
< |
LONG_DELAY_MS, MILLISECONDS, |
698 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
699 |
|
new ArrayBlockingQueue<Runnable>(10)); |
700 |
|
shouldThrow(); |
701 |
|
} catch (IllegalArgumentException success) {} |
706 |
|
*/ |
707 |
|
public void testConstructor3() { |
708 |
|
try { |
709 |
< |
new ThreadPoolExecutor(1, 0, |
621 |
< |
LONG_DELAY_MS, MILLISECONDS, |
709 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
710 |
|
new ArrayBlockingQueue<Runnable>(10)); |
711 |
|
shouldThrow(); |
712 |
|
} catch (IllegalArgumentException success) {} |
717 |
|
*/ |
718 |
|
public void testConstructor4() { |
719 |
|
try { |
720 |
< |
new ThreadPoolExecutor(1, 2, |
633 |
< |
-1L, MILLISECONDS, |
720 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
721 |
|
new ArrayBlockingQueue<Runnable>(10)); |
722 |
|
shouldThrow(); |
723 |
|
} catch (IllegalArgumentException success) {} |
728 |
|
*/ |
729 |
|
public void testConstructor5() { |
730 |
|
try { |
731 |
< |
new ThreadPoolExecutor(2, 1, |
645 |
< |
LONG_DELAY_MS, MILLISECONDS, |
731 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
732 |
|
new ArrayBlockingQueue<Runnable>(10)); |
733 |
|
shouldThrow(); |
734 |
|
} catch (IllegalArgumentException success) {} |
739 |
|
*/ |
740 |
|
public void testConstructorNullPointerException() { |
741 |
|
try { |
742 |
< |
new ThreadPoolExecutor(1, 2, |
657 |
< |
LONG_DELAY_MS, MILLISECONDS, |
742 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
743 |
|
(BlockingQueue) null); |
744 |
|
shouldThrow(); |
745 |
|
} catch (NullPointerException success) {} |
746 |
|
} |
747 |
|
|
663 |
– |
|
664 |
– |
|
748 |
|
/** |
749 |
|
* Constructor throws if corePoolSize argument is less than zero |
750 |
|
*/ |
751 |
|
public void testConstructor6() { |
752 |
|
try { |
753 |
< |
new ThreadPoolExecutor(-1, 1, |
671 |
< |
LONG_DELAY_MS, MILLISECONDS, |
753 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
754 |
|
new ArrayBlockingQueue<Runnable>(10), |
755 |
|
new SimpleThreadFactory()); |
756 |
|
shouldThrow(); |
762 |
|
*/ |
763 |
|
public void testConstructor7() { |
764 |
|
try { |
765 |
< |
new ThreadPoolExecutor(1, -1, |
684 |
< |
LONG_DELAY_MS, MILLISECONDS, |
765 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
766 |
|
new ArrayBlockingQueue<Runnable>(10), |
767 |
|
new SimpleThreadFactory()); |
768 |
|
shouldThrow(); |
774 |
|
*/ |
775 |
|
public void testConstructor8() { |
776 |
|
try { |
777 |
< |
new ThreadPoolExecutor(1, 0, |
697 |
< |
LONG_DELAY_MS, MILLISECONDS, |
777 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
778 |
|
new ArrayBlockingQueue<Runnable>(10), |
779 |
|
new SimpleThreadFactory()); |
780 |
|
shouldThrow(); |
786 |
|
*/ |
787 |
|
public void testConstructor9() { |
788 |
|
try { |
789 |
< |
new ThreadPoolExecutor(1, 2, |
710 |
< |
-1L, MILLISECONDS, |
789 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
790 |
|
new ArrayBlockingQueue<Runnable>(10), |
791 |
|
new SimpleThreadFactory()); |
792 |
|
shouldThrow(); |
798 |
|
*/ |
799 |
|
public void testConstructor10() { |
800 |
|
try { |
801 |
< |
new ThreadPoolExecutor(2, 1, |
723 |
< |
LONG_DELAY_MS, MILLISECONDS, |
801 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
802 |
|
new ArrayBlockingQueue<Runnable>(10), |
803 |
|
new SimpleThreadFactory()); |
804 |
|
shouldThrow(); |
810 |
|
*/ |
811 |
|
public void testConstructorNullPointerException2() { |
812 |
|
try { |
813 |
< |
new ThreadPoolExecutor(1, 2, |
736 |
< |
LONG_DELAY_MS, MILLISECONDS, |
813 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
814 |
|
(BlockingQueue) null, |
815 |
|
new SimpleThreadFactory()); |
816 |
|
shouldThrow(); |
822 |
|
*/ |
823 |
|
public void testConstructorNullPointerException3() { |
824 |
|
try { |
825 |
< |
new ThreadPoolExecutor(1, 2, |
749 |
< |
LONG_DELAY_MS, MILLISECONDS, |
825 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
826 |
|
new ArrayBlockingQueue<Runnable>(10), |
827 |
|
(ThreadFactory) null); |
828 |
|
shouldThrow(); |
829 |
|
} catch (NullPointerException success) {} |
830 |
|
} |
831 |
|
|
756 |
– |
|
832 |
|
/** |
833 |
|
* Constructor throws if corePoolSize argument is less than zero |
834 |
|
*/ |
835 |
|
public void testConstructor11() { |
836 |
|
try { |
837 |
< |
new ThreadPoolExecutor(-1, 1, |
763 |
< |
LONG_DELAY_MS, MILLISECONDS, |
837 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
838 |
|
new ArrayBlockingQueue<Runnable>(10), |
839 |
|
new NoOpREHandler()); |
840 |
|
shouldThrow(); |
846 |
|
*/ |
847 |
|
public void testConstructor12() { |
848 |
|
try { |
849 |
< |
new ThreadPoolExecutor(1, -1, |
776 |
< |
LONG_DELAY_MS, MILLISECONDS, |
849 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
850 |
|
new ArrayBlockingQueue<Runnable>(10), |
851 |
|
new NoOpREHandler()); |
852 |
|
shouldThrow(); |
858 |
|
*/ |
859 |
|
public void testConstructor13() { |
860 |
|
try { |
861 |
< |
new ThreadPoolExecutor(1, 0, |
789 |
< |
LONG_DELAY_MS, MILLISECONDS, |
861 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
862 |
|
new ArrayBlockingQueue<Runnable>(10), |
863 |
|
new NoOpREHandler()); |
864 |
|
shouldThrow(); |
870 |
|
*/ |
871 |
|
public void testConstructor14() { |
872 |
|
try { |
873 |
< |
new ThreadPoolExecutor(1, 2, |
802 |
< |
-1L, MILLISECONDS, |
873 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
874 |
|
new ArrayBlockingQueue<Runnable>(10), |
875 |
|
new NoOpREHandler()); |
876 |
|
shouldThrow(); |
882 |
|
*/ |
883 |
|
public void testConstructor15() { |
884 |
|
try { |
885 |
< |
new ThreadPoolExecutor(2, 1, |
815 |
< |
LONG_DELAY_MS, MILLISECONDS, |
885 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
886 |
|
new ArrayBlockingQueue<Runnable>(10), |
887 |
|
new NoOpREHandler()); |
888 |
|
shouldThrow(); |
894 |
|
*/ |
895 |
|
public void testConstructorNullPointerException4() { |
896 |
|
try { |
897 |
< |
new ThreadPoolExecutor(1, 2, |
828 |
< |
LONG_DELAY_MS, MILLISECONDS, |
897 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
898 |
|
(BlockingQueue) null, |
899 |
|
new NoOpREHandler()); |
900 |
|
shouldThrow(); |
906 |
|
*/ |
907 |
|
public void testConstructorNullPointerException5() { |
908 |
|
try { |
909 |
< |
new ThreadPoolExecutor(1, 2, |
841 |
< |
LONG_DELAY_MS, MILLISECONDS, |
909 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
910 |
|
new ArrayBlockingQueue<Runnable>(10), |
911 |
|
(RejectedExecutionHandler) null); |
912 |
|
shouldThrow(); |
913 |
|
} catch (NullPointerException success) {} |
914 |
|
} |
915 |
|
|
848 |
– |
|
916 |
|
/** |
917 |
|
* Constructor throws if corePoolSize argument is less than zero |
918 |
|
*/ |
919 |
|
public void testConstructor16() { |
920 |
|
try { |
921 |
< |
new ThreadPoolExecutor(-1, 1, |
855 |
< |
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 testConstructor17() { |
933 |
|
try { |
934 |
< |
new ThreadPoolExecutor(1, -1, |
869 |
< |
LONG_DELAY_MS, MILLISECONDS, |
934 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
935 |
|
new ArrayBlockingQueue<Runnable>(10), |
936 |
|
new SimpleThreadFactory(), |
937 |
|
new NoOpREHandler()); |
944 |
|
*/ |
945 |
|
public void testConstructor18() { |
946 |
|
try { |
947 |
< |
new ThreadPoolExecutor(1, 0, |
883 |
< |
LONG_DELAY_MS, MILLISECONDS, |
947 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
948 |
|
new ArrayBlockingQueue<Runnable>(10), |
949 |
|
new SimpleThreadFactory(), |
950 |
|
new NoOpREHandler()); |
957 |
|
*/ |
958 |
|
public void testConstructor19() { |
959 |
|
try { |
960 |
< |
new ThreadPoolExecutor(1, 2, |
897 |
< |
-1L, MILLISECONDS, |
960 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
961 |
|
new ArrayBlockingQueue<Runnable>(10), |
962 |
|
new SimpleThreadFactory(), |
963 |
|
new NoOpREHandler()); |
970 |
|
*/ |
971 |
|
public void testConstructor20() { |
972 |
|
try { |
973 |
< |
new ThreadPoolExecutor(2, 1, |
911 |
< |
LONG_DELAY_MS, MILLISECONDS, |
973 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
974 |
|
new ArrayBlockingQueue<Runnable>(10), |
975 |
|
new SimpleThreadFactory(), |
976 |
|
new NoOpREHandler()); |
983 |
|
*/ |
984 |
|
public void testConstructorNullPointerException6() { |
985 |
|
try { |
986 |
< |
new ThreadPoolExecutor(1, 2, |
925 |
< |
LONG_DELAY_MS, MILLISECONDS, |
986 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
987 |
|
(BlockingQueue) null, |
988 |
|
new SimpleThreadFactory(), |
989 |
|
new NoOpREHandler()); |
996 |
|
*/ |
997 |
|
public void testConstructorNullPointerException7() { |
998 |
|
try { |
999 |
< |
new ThreadPoolExecutor(1, 2, |
939 |
< |
LONG_DELAY_MS, MILLISECONDS, |
999 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1000 |
|
new ArrayBlockingQueue<Runnable>(10), |
1001 |
|
new SimpleThreadFactory(), |
1002 |
|
(RejectedExecutionHandler) null); |
1009 |
|
*/ |
1010 |
|
public void testConstructorNullPointerException8() { |
1011 |
|
try { |
1012 |
< |
new ThreadPoolExecutor(1, 2, |
953 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1012 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1013 |
|
new ArrayBlockingQueue<Runnable>(10), |
1014 |
|
(ThreadFactory) null, |
1015 |
|
new NoOpREHandler()); |
1023 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1024 |
|
final ThreadPoolExecutor p = |
1025 |
|
new ThreadPoolExecutor(1, 1, |
1026 |
< |
60, TimeUnit.SECONDS, |
1026 |
> |
60, SECONDS, |
1027 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1028 |
|
|
1029 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1271 |
|
} |
1272 |
|
} |
1273 |
|
|
1215 |
– |
|
1274 |
|
/** |
1275 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1276 |
|
*/ |
1292 |
|
} |
1293 |
|
} |
1294 |
|
|
1237 |
– |
|
1295 |
|
/** |
1296 |
|
* execute(null) throws NPE |
1297 |
|
*/ |
1298 |
|
public void testExecuteNull() { |
1299 |
|
ThreadPoolExecutor p = |
1300 |
< |
new ThreadPoolExecutor(1, 2, |
1244 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1300 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1301 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1302 |
|
try { |
1303 |
|
p.execute(null); |
1363 |
|
joinPool(p); |
1364 |
|
} |
1365 |
|
|
1366 |
+ |
/** |
1367 |
+ |
* Configuration changes that allow core pool size greater than |
1368 |
+ |
* max pool size result in IllegalArgumentException. |
1369 |
+ |
*/ |
1370 |
+ |
public void testPoolSizeInvariants() { |
1371 |
+ |
ThreadPoolExecutor p = |
1372 |
+ |
new ThreadPoolExecutor(1, 1, |
1373 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1374 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1375 |
+ |
for (int s = 1; s < 5; s++) { |
1376 |
+ |
p.setMaximumPoolSize(s); |
1377 |
+ |
p.setCorePoolSize(s); |
1378 |
+ |
try { |
1379 |
+ |
p.setMaximumPoolSize(s - 1); |
1380 |
+ |
shouldThrow(); |
1381 |
+ |
} catch (IllegalArgumentException success) {} |
1382 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1383 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1384 |
+ |
try { |
1385 |
+ |
p.setCorePoolSize(s + 1); |
1386 |
+ |
shouldThrow(); |
1387 |
+ |
} catch (IllegalArgumentException success) {} |
1388 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1389 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1390 |
+ |
} |
1391 |
+ |
joinPool(p); |
1392 |
+ |
} |
1393 |
|
|
1394 |
|
/** |
1395 |
|
* setKeepAliveTime throws IllegalArgumentException |
1416 |
|
public void testTerminated() { |
1417 |
|
ExtendedTPE p = new ExtendedTPE(); |
1418 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1419 |
< |
assertTrue(p.terminatedCalled); |
1419 |
> |
assertTrue(p.terminatedCalled()); |
1420 |
|
joinPool(p); |
1421 |
|
} |
1422 |
|
|
1426 |
|
public void testBeforeAfter() throws InterruptedException { |
1427 |
|
ExtendedTPE p = new ExtendedTPE(); |
1428 |
|
try { |
1429 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1430 |
< |
p.execute(r); |
1431 |
< |
delay(SHORT_DELAY_MS); |
1432 |
< |
assertTrue(r.done); |
1433 |
< |
assertTrue(p.beforeCalled); |
1434 |
< |
assertTrue(p.afterCalled); |
1429 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1430 |
> |
p.execute(new CheckedRunnable() { |
1431 |
> |
public void realRun() { |
1432 |
> |
done.countDown(); |
1433 |
> |
}}); |
1434 |
> |
await(p.afterCalled); |
1435 |
> |
assertEquals(0, done.getCount()); |
1436 |
> |
assertTrue(p.afterCalled()); |
1437 |
> |
assertTrue(p.beforeCalled()); |
1438 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1439 |
|
} finally { |
1440 |
|
joinPool(p); |
1492 |
|
} |
1493 |
|
} |
1494 |
|
|
1409 |
– |
|
1495 |
|
/** |
1496 |
|
* invokeAny(null) throws NPE |
1497 |
|
*/ |
1685 |
|
} |
1686 |
|
} |
1687 |
|
|
1603 |
– |
|
1604 |
– |
|
1688 |
|
/** |
1689 |
|
* timed invokeAny(null) throws NPE |
1690 |
|
*/ |
1907 |
|
l.add(new StringTask()); |
1908 |
|
l.add(new StringTask()); |
1909 |
|
List<Future<String>> futures = |
1910 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1910 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1911 |
|
assertEquals(2, futures.size()); |
1912 |
|
for (Future<String> future : futures) |
1913 |
|
assertSame(TEST_STRING, future.get()); |
1925 |
|
LONG_DELAY_MS, MILLISECONDS, |
1926 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1927 |
|
try { |
1928 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1929 |
< |
l.add(new StringTask()); |
1930 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1931 |
< |
l.add(new StringTask()); |
1932 |
< |
List<Future<String>> futures = |
1933 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1934 |
< |
assertEquals(3, futures.size()); |
1935 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1936 |
< |
Future<String> f1 = it.next(); |
1937 |
< |
Future<String> f2 = it.next(); |
1938 |
< |
Future<String> f3 = it.next(); |
1939 |
< |
assertTrue(f1.isDone()); |
1940 |
< |
assertTrue(f2.isDone()); |
1941 |
< |
assertTrue(f3.isDone()); |
1942 |
< |
assertFalse(f1.isCancelled()); |
1943 |
< |
assertTrue(f2.isCancelled()); |
1928 |
> |
for (long timeout = timeoutMillis();;) { |
1929 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1930 |
> |
tasks.add(new StringTask("0")); |
1931 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1932 |
> |
tasks.add(new StringTask("2")); |
1933 |
> |
long startTime = System.nanoTime(); |
1934 |
> |
List<Future<String>> futures = |
1935 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1936 |
> |
assertEquals(tasks.size(), futures.size()); |
1937 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1938 |
> |
for (Future future : futures) |
1939 |
> |
assertTrue(future.isDone()); |
1940 |
> |
assertTrue(futures.get(1).isCancelled()); |
1941 |
> |
try { |
1942 |
> |
assertEquals("0", futures.get(0).get()); |
1943 |
> |
assertEquals("2", futures.get(2).get()); |
1944 |
> |
break; |
1945 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1946 |
> |
timeout *= 2; |
1947 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1948 |
> |
fail("expected exactly one task to be cancelled"); |
1949 |
> |
} |
1950 |
> |
} |
1951 |
|
} finally { |
1952 |
|
joinPool(e); |
1953 |
|
} |
1993 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1994 |
|
*/ |
1995 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1996 |
+ |
long keepAliveTime = timeoutMillis(); |
1997 |
|
final ThreadPoolExecutor p = |
1998 |
|
new ThreadPoolExecutor(2, 10, |
1999 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1999 |
> |
keepAliveTime, MILLISECONDS, |
2000 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2001 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2002 |
|
try { |
2003 |
|
p.allowCoreThreadTimeOut(true); |
2004 |
|
p.execute(new CheckedRunnable() { |
2005 |
< |
public void realRun() throws InterruptedException { |
2005 |
> |
public void realRun() { |
2006 |
|
threadStarted.countDown(); |
2007 |
|
assertEquals(1, p.getPoolSize()); |
2008 |
|
}}); |
2009 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
2010 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
2011 |
< |
if (p.getPoolSize() == 0) |
2012 |
< |
break; |
2013 |
< |
delay(10); |
2014 |
< |
} |
2009 |
> |
await(threadStarted); |
2010 |
> |
delay(keepAliveTime); |
2011 |
> |
long startTime = System.nanoTime(); |
2012 |
> |
while (p.getPoolSize() > 0 |
2013 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
2014 |
> |
Thread.yield(); |
2015 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
2016 |
|
assertEquals(0, p.getPoolSize()); |
2017 |
|
} finally { |
2018 |
|
joinPool(p); |
2023 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
2024 |
|
*/ |
2025 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
2026 |
+ |
long keepAliveTime = timeoutMillis(); |
2027 |
|
final ThreadPoolExecutor p = |
2028 |
|
new ThreadPoolExecutor(2, 10, |
2029 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
2029 |
> |
keepAliveTime, MILLISECONDS, |
2030 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2031 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2032 |
|
try { |
2036 |
|
threadStarted.countDown(); |
2037 |
|
assertTrue(p.getPoolSize() >= 1); |
2038 |
|
}}); |
2039 |
< |
delay(SMALL_DELAY_MS); |
2039 |
> |
delay(2 * keepAliveTime); |
2040 |
|
assertTrue(p.getPoolSize() >= 1); |
2041 |
|
} finally { |
2042 |
|
joinPool(p); |
2055 |
|
done.countDown(); |
2056 |
|
}}; |
2057 |
|
final ThreadPoolExecutor p = |
2058 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2058 |
> |
new ThreadPoolExecutor(1, 30, |
2059 |
> |
60, SECONDS, |
2060 |
|
new ArrayBlockingQueue(30)); |
2061 |
|
try { |
2062 |
|
for (int i = 0; i < nTasks; ++i) { |
2071 |
|
// enough time to run all tasks |
2072 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2073 |
|
} finally { |
2074 |
< |
p.shutdown(); |
2074 |
> |
joinPool(p); |
2075 |
> |
} |
2076 |
> |
} |
2077 |
> |
|
2078 |
> |
/** |
2079 |
> |
* get(cancelled task) throws CancellationException |
2080 |
> |
*/ |
2081 |
> |
public void testGet_cancelled() throws Exception { |
2082 |
> |
final ExecutorService e = |
2083 |
> |
new ThreadPoolExecutor(1, 1, |
2084 |
> |
LONG_DELAY_MS, MILLISECONDS, |
2085 |
> |
new LinkedBlockingQueue<Runnable>()); |
2086 |
> |
try { |
2087 |
> |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2088 |
> |
final CountDownLatch done = new CountDownLatch(1); |
2089 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
2090 |
> |
for (int i = 0; i < 2; i++) { |
2091 |
> |
Runnable r = new CheckedRunnable() { public void realRun() |
2092 |
> |
throws Throwable { |
2093 |
> |
blockerStarted.countDown(); |
2094 |
> |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2095 |
> |
}}; |
2096 |
> |
futures.add(e.submit(r)); |
2097 |
> |
} |
2098 |
> |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2099 |
> |
for (Future<?> future : futures) future.cancel(false); |
2100 |
> |
for (Future<?> future : futures) { |
2101 |
> |
try { |
2102 |
> |
future.get(); |
2103 |
> |
shouldThrow(); |
2104 |
> |
} catch (CancellationException success) {} |
2105 |
> |
try { |
2106 |
> |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2107 |
> |
shouldThrow(); |
2108 |
> |
} catch (CancellationException success) {} |
2109 |
> |
assertTrue(future.isCancelled()); |
2110 |
> |
assertTrue(future.isDone()); |
2111 |
> |
} |
2112 |
> |
done.countDown(); |
2113 |
> |
} finally { |
2114 |
> |
joinPool(e); |
2115 |
|
} |
2116 |
|
} |
2117 |
|
|