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