6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import junit.framework.*; |
10 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
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); |
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 { |
206 |
|
fail("timed out"); |
207 |
|
Thread.yield(); |
208 |
|
} |
172 |
– |
} finally { |
173 |
– |
joinPool(p); |
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 |
|
|
238 |
|
/** |
239 |
|
* getThreadFactory returns factory in constructor if not set |
240 |
|
*/ |
241 |
|
public void testGetThreadFactory() { |
242 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
242 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
243 |
|
final ThreadPoolExecutor p = |
244 |
|
new ThreadPoolExecutor(1, 2, |
245 |
|
LONG_DELAY_MS, MILLISECONDS, |
246 |
|
new ArrayBlockingQueue<Runnable>(10), |
247 |
< |
tf, |
247 |
> |
threadFactory, |
248 |
|
new NoOpREHandler()); |
249 |
< |
assertSame(tf, p.getThreadFactory()); |
250 |
< |
joinPool(p); |
249 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
250 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
251 |
> |
} |
252 |
|
} |
253 |
|
|
254 |
|
/** |
259 |
|
new ThreadPoolExecutor(1, 2, |
260 |
|
LONG_DELAY_MS, MILLISECONDS, |
261 |
|
new ArrayBlockingQueue<Runnable>(10)); |
262 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
263 |
< |
p.setThreadFactory(tf); |
264 |
< |
assertSame(tf, p.getThreadFactory()); |
265 |
< |
joinPool(p); |
262 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
263 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
264 |
> |
p.setThreadFactory(threadFactory); |
265 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
266 |
> |
} |
267 |
|
} |
268 |
|
|
269 |
|
/** |
274 |
|
new ThreadPoolExecutor(1, 2, |
275 |
|
LONG_DELAY_MS, MILLISECONDS, |
276 |
|
new ArrayBlockingQueue<Runnable>(10)); |
277 |
< |
try { |
278 |
< |
p.setThreadFactory(null); |
279 |
< |
shouldThrow(); |
280 |
< |
} catch (NullPointerException success) { |
281 |
< |
} finally { |
243 |
< |
joinPool(p); |
277 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
278 |
> |
try { |
279 |
> |
p.setThreadFactory(null); |
280 |
> |
shouldThrow(); |
281 |
> |
} catch (NullPointerException success) {} |
282 |
|
} |
283 |
|
} |
284 |
|
|
286 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
287 |
|
*/ |
288 |
|
public void testGetRejectedExecutionHandler() { |
289 |
< |
final RejectedExecutionHandler h = new NoOpREHandler(); |
289 |
> |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
290 |
|
final ThreadPoolExecutor p = |
291 |
|
new ThreadPoolExecutor(1, 2, |
292 |
|
LONG_DELAY_MS, MILLISECONDS, |
293 |
|
new ArrayBlockingQueue<Runnable>(10), |
294 |
< |
h); |
295 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
296 |
< |
joinPool(p); |
294 |
> |
handler); |
295 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
296 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
297 |
> |
} |
298 |
|
} |
299 |
|
|
300 |
|
/** |
306 |
|
new ThreadPoolExecutor(1, 2, |
307 |
|
LONG_DELAY_MS, MILLISECONDS, |
308 |
|
new ArrayBlockingQueue<Runnable>(10)); |
309 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
310 |
< |
p.setRejectedExecutionHandler(h); |
311 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
312 |
< |
joinPool(p); |
309 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
310 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
311 |
> |
p.setRejectedExecutionHandler(handler); |
312 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
313 |
> |
} |
314 |
|
} |
315 |
|
|
316 |
|
/** |
321 |
|
new ThreadPoolExecutor(1, 2, |
322 |
|
LONG_DELAY_MS, MILLISECONDS, |
323 |
|
new ArrayBlockingQueue<Runnable>(10)); |
324 |
< |
try { |
325 |
< |
p.setRejectedExecutionHandler(null); |
326 |
< |
shouldThrow(); |
327 |
< |
} catch (NullPointerException success) { |
328 |
< |
} finally { |
289 |
< |
joinPool(p); |
324 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
325 |
> |
try { |
326 |
> |
p.setRejectedExecutionHandler(null); |
327 |
> |
shouldThrow(); |
328 |
> |
} catch (NullPointerException success) {} |
329 |
|
} |
330 |
|
} |
331 |
|
|
339 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
340 |
|
LONG_DELAY_MS, MILLISECONDS, |
341 |
|
new ArrayBlockingQueue<Runnable>(10)); |
342 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
343 |
< |
final CountDownLatch done = new CountDownLatch(1); |
344 |
< |
try { |
342 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
343 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
344 |
> |
final CountDownLatch done = new CountDownLatch(1); |
345 |
|
assertEquals(0, p.getLargestPoolSize()); |
346 |
|
for (int i = 0; i < THREADS; i++) |
347 |
|
p.execute(new CheckedRunnable() { |
350 |
|
done.await(); |
351 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
352 |
|
}}); |
353 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
315 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
316 |
< |
} finally { |
317 |
< |
done.countDown(); |
318 |
< |
joinPool(p); |
353 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
354 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
355 |
+ |
done.countDown(); // release pool |
356 |
|
} |
357 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
358 |
|
} |
359 |
|
|
360 |
|
/** |
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 |
472 |
|
*/ |
473 |
|
public void testIsTerminated() throws InterruptedException { |
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++) |
585 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
586 |
< |
} |
587 |
< |
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 |
685 |
|
*/ |
686 |
|
public void testConstructor1() { |
687 |
|
try { |
688 |
< |
new ThreadPoolExecutor(-1, 1, |
604 |
< |
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, |
616 |
< |
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, |
628 |
< |
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, |
640 |
< |
-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, |
652 |
< |
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, |
664 |
< |
LONG_DELAY_MS, MILLISECONDS, |
743 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
744 |
|
(BlockingQueue) null); |
745 |
|
shouldThrow(); |
746 |
|
} catch (NullPointerException success) {} |
751 |
|
*/ |
752 |
|
public void testConstructor6() { |
753 |
|
try { |
754 |
< |
new ThreadPoolExecutor(-1, 1, |
676 |
< |
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, |
689 |
< |
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, |
702 |
< |
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, |
715 |
< |
-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, |
728 |
< |
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, |
741 |
< |
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, |
754 |
< |
LONG_DELAY_MS, MILLISECONDS, |
826 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
827 |
|
new ArrayBlockingQueue<Runnable>(10), |
828 |
|
(ThreadFactory) null); |
829 |
|
shouldThrow(); |
835 |
|
*/ |
836 |
|
public void testConstructor11() { |
837 |
|
try { |
838 |
< |
new ThreadPoolExecutor(-1, 1, |
767 |
< |
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, |
780 |
< |
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, |
793 |
< |
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, |
806 |
< |
-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, |
819 |
< |
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, |
832 |
< |
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, |
845 |
< |
LONG_DELAY_MS, MILLISECONDS, |
910 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
911 |
|
new ArrayBlockingQueue<Runnable>(10), |
912 |
|
(RejectedExecutionHandler) null); |
913 |
|
shouldThrow(); |
919 |
|
*/ |
920 |
|
public void testConstructor16() { |
921 |
|
try { |
922 |
< |
new ThreadPoolExecutor(-1, 1, |
858 |
< |
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, |
872 |
< |
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, |
886 |
< |
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, |
900 |
< |
-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, |
914 |
< |
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, |
928 |
< |
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, |
942 |
< |
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, |
956 |
< |
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); |
1298 |
|
*/ |
1299 |
|
public void testExecuteNull() { |
1300 |
|
ThreadPoolExecutor p = |
1301 |
< |
new ThreadPoolExecutor(1, 2, |
1245 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1301 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1302 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1303 |
|
try { |
1304 |
|
p.execute(null); |
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 |
1397 |
|
* when given a negative value |
1398 |
|
*/ |
1428 |
|
ExtendedTPE p = new ExtendedTPE(); |
1429 |
|
try { |
1430 |
|
final CountDownLatch done = new CountDownLatch(1); |
1431 |
< |
final CheckedRunnable task = new CheckedRunnable() { |
1431 |
> |
p.execute(new CheckedRunnable() { |
1432 |
|
public void realRun() { |
1433 |
|
done.countDown(); |
1434 |
< |
}}; |
1351 |
< |
p.execute(task); |
1434 |
> |
}}); |
1435 |
|
await(p.afterCalled); |
1436 |
|
assertEquals(0, done.getCount()); |
1437 |
|
assertTrue(p.afterCalled()); |
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(l.size(), futures.size()); |
1936 |
< |
for (Future future : futures) |
1937 |
< |
assertTrue(future.isDone()); |
1938 |
< |
assertFalse(futures.get(0).isCancelled()); |
1939 |
< |
assertTrue(futures.get(1).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 coreThreadTimeOut = SHORT_DELAY_MS; |
1997 |
> |
long keepAliveTime = timeoutMillis(); |
1998 |
|
final ThreadPoolExecutor p = |
1999 |
|
new ThreadPoolExecutor(2, 10, |
2000 |
< |
coreThreadTimeOut, MILLISECONDS, |
2000 |
> |
keepAliveTime, MILLISECONDS, |
2001 |
|
new ArrayBlockingQueue<Runnable>(10)); |
2002 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
2003 |
|
try { |
2008 |
|
assertEquals(1, p.getPoolSize()); |
2009 |
|
}}); |
2010 |
|
await(threadStarted); |
2011 |
< |
delay(coreThreadTimeOut); |
2011 |
> |
delay(keepAliveTime); |
2012 |
|
long startTime = System.nanoTime(); |
2013 |
|
while (p.getPoolSize() > 0 |
2014 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
2024 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
2025 |
|
*/ |
2026 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
2027 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
2027 |
> |
long keepAliveTime = timeoutMillis(); |
2028 |
|
final ThreadPoolExecutor p = |
2029 |
|
new ThreadPoolExecutor(2, 10, |
2030 |
< |
coreThreadTimeOut, 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(2 * coreThreadTimeOut); |
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) { |
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 |
+ |
|
2119 |
|
} |