ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.115
Committed: Mon Mar 20 00:21:54 2017 UTC (7 years, 2 months ago) by jsr166
Branch: MAIN
Changes since 1.114: +1 -2 lines
Log Message:
testSaturatedSubmitCallable: simplify, fixing errorprone warning: [FutureReturnValueIgnored]

File Contents

# User Rev Content
1 dl 1.1 /*
2 dl 1.15 * Written by Doug Lea with assistance from members of JCP JSR-166
3     * Expert Group and released to the public domain, as explained at
4 jsr166 1.40 * http://creativecommons.org/publicdomain/zero/1.0/
5 jsr166 1.24 * Other contributors include Andrew Wright, Jeffrey Hayes,
6     * Pat Fisher, Mike Judd.
7 dl 1.1 */
8    
9 jsr166 1.27 import static java.util.concurrent.TimeUnit.MILLISECONDS;
10 jsr166 1.48 import static java.util.concurrent.TimeUnit.NANOSECONDS;
11 jsr166 1.52 import static java.util.concurrent.TimeUnit.SECONDS;
12 jsr166 1.50
13     import java.util.ArrayList;
14     import java.util.List;
15     import java.util.concurrent.ArrayBlockingQueue;
16     import java.util.concurrent.BlockingQueue;
17 jsr166 1.58 import java.util.concurrent.Callable;
18 jsr166 1.56 import java.util.concurrent.CancellationException;
19 jsr166 1.50 import java.util.concurrent.CountDownLatch;
20     import java.util.concurrent.ExecutionException;
21     import java.util.concurrent.ExecutorService;
22     import java.util.concurrent.Future;
23     import java.util.concurrent.FutureTask;
24     import java.util.concurrent.LinkedBlockingQueue;
25     import java.util.concurrent.RejectedExecutionException;
26     import java.util.concurrent.RejectedExecutionHandler;
27     import java.util.concurrent.SynchronousQueue;
28     import java.util.concurrent.ThreadFactory;
29     import java.util.concurrent.ThreadPoolExecutor;
30 jsr166 1.61 import java.util.concurrent.atomic.AtomicInteger;
31 jsr166 1.50
32     import junit.framework.Test;
33     import junit.framework.TestSuite;
34 dl 1.1
35 dl 1.3 public class ThreadPoolExecutorTest extends JSR166TestCase {
36 dl 1.1 public static void main(String[] args) {
37 jsr166 1.51 main(suite(), args);
38 dl 1.1 }
39     public static Test suite() {
40     return new TestSuite(ThreadPoolExecutorTest.class);
41     }
42 jsr166 1.24
43 dl 1.8 static class ExtendedTPE extends ThreadPoolExecutor {
44 jsr166 1.45 final CountDownLatch beforeCalled = new CountDownLatch(1);
45     final CountDownLatch afterCalled = new CountDownLatch(1);
46     final CountDownLatch terminatedCalled = new CountDownLatch(1);
47    
48 dl 1.8 public ExtendedTPE() {
49 jsr166 1.27 super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
50 dl 1.8 }
51     protected void beforeExecute(Thread t, Runnable r) {
52 jsr166 1.45 beforeCalled.countDown();
53 dl 1.8 }
54     protected void afterExecute(Runnable r, Throwable t) {
55 jsr166 1.45 afterCalled.countDown();
56 dl 1.8 }
57     protected void terminated() {
58 jsr166 1.45 terminatedCalled.countDown();
59     }
60    
61     public boolean beforeCalled() {
62     return beforeCalled.getCount() == 0;
63     }
64     public boolean afterCalled() {
65     return afterCalled.getCount() == 0;
66     }
67     public boolean terminatedCalled() {
68     return terminatedCalled.getCount() == 0;
69 dl 1.8 }
70     }
71 dl 1.1
72 jsr166 1.26 static class FailingThreadFactory implements ThreadFactory {
73 dl 1.19 int calls = 0;
74 jsr166 1.26 public Thread newThread(Runnable r) {
75 dl 1.20 if (++calls > 1) return null;
76 dl 1.19 return new Thread(r);
77 jsr166 1.24 }
78 dl 1.19 }
79 jsr166 1.24
80 dl 1.3 /**
81 jsr166 1.35 * execute successfully executes a runnable
82 dl 1.1 */
83 jsr166 1.27 public void testExecute() throws InterruptedException {
84 jsr166 1.37 final ThreadPoolExecutor p =
85     new ThreadPoolExecutor(1, 1,
86     LONG_DELAY_MS, MILLISECONDS,
87     new ArrayBlockingQueue<Runnable>(10));
88 jsr166 1.80 try (PoolCleaner cleaner = cleaner(p)) {
89     final CountDownLatch done = new CountDownLatch(1);
90     final Runnable task = new CheckedRunnable() {
91     public void realRun() { done.countDown(); }};
92 jsr166 1.37 p.execute(task);
93 jsr166 1.80 assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
94 jsr166 1.24 }
95 dl 1.1 }
96    
97     /**
98 jsr166 1.35 * getActiveCount increases but doesn't overestimate, when a
99     * thread becomes active
100 dl 1.1 */
101 jsr166 1.27 public void testGetActiveCount() throws InterruptedException {
102 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
103 jsr166 1.37 final ThreadPoolExecutor p =
104     new ThreadPoolExecutor(2, 2,
105     LONG_DELAY_MS, MILLISECONDS,
106     new ArrayBlockingQueue<Runnable>(10));
107 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
108 jsr166 1.80 final CountDownLatch threadStarted = new CountDownLatch(1);
109 jsr166 1.37 assertEquals(0, p.getActiveCount());
110     p.execute(new CheckedRunnable() {
111     public void realRun() throws InterruptedException {
112     threadStarted.countDown();
113     assertEquals(1, p.getActiveCount());
114 jsr166 1.106 await(done);
115 jsr166 1.37 }});
116 jsr166 1.108 await(threadStarted);
117 jsr166 1.37 assertEquals(1, p.getActiveCount());
118     }
119 dl 1.1 }
120 dl 1.8
121     /**
122 jsr166 1.35 * prestartCoreThread starts a thread if under corePoolSize, else doesn't
123 dl 1.8 */
124     public void testPrestartCoreThread() {
125 jsr166 1.37 final ThreadPoolExecutor p =
126 jsr166 1.66 new ThreadPoolExecutor(2, 6,
127 jsr166 1.37 LONG_DELAY_MS, MILLISECONDS,
128     new ArrayBlockingQueue<Runnable>(10));
129 jsr166 1.66 try (PoolCleaner cleaner = cleaner(p)) {
130     assertEquals(0, p.getPoolSize());
131     assertTrue(p.prestartCoreThread());
132     assertEquals(1, p.getPoolSize());
133     assertTrue(p.prestartCoreThread());
134     assertEquals(2, p.getPoolSize());
135     assertFalse(p.prestartCoreThread());
136     assertEquals(2, p.getPoolSize());
137     p.setCorePoolSize(4);
138     assertTrue(p.prestartCoreThread());
139     assertEquals(3, p.getPoolSize());
140     assertTrue(p.prestartCoreThread());
141     assertEquals(4, p.getPoolSize());
142     assertFalse(p.prestartCoreThread());
143     assertEquals(4, p.getPoolSize());
144     }
145 dl 1.8 }
146    
147     /**
148 jsr166 1.35 * prestartAllCoreThreads starts all corePoolSize threads
149 dl 1.8 */
150     public void testPrestartAllCoreThreads() {
151 jsr166 1.37 final ThreadPoolExecutor p =
152 jsr166 1.67 new ThreadPoolExecutor(2, 6,
153 jsr166 1.37 LONG_DELAY_MS, MILLISECONDS,
154     new ArrayBlockingQueue<Runnable>(10));
155 jsr166 1.67 try (PoolCleaner cleaner = cleaner(p)) {
156     assertEquals(0, p.getPoolSize());
157     p.prestartAllCoreThreads();
158     assertEquals(2, p.getPoolSize());
159     p.prestartAllCoreThreads();
160     assertEquals(2, p.getPoolSize());
161     p.setCorePoolSize(4);
162     p.prestartAllCoreThreads();
163     assertEquals(4, p.getPoolSize());
164     p.prestartAllCoreThreads();
165     assertEquals(4, p.getPoolSize());
166     }
167 dl 1.8 }
168 jsr166 1.24
169 dl 1.1 /**
170 jsr166 1.35 * getCompletedTaskCount increases, but doesn't overestimate,
171     * when tasks complete
172 dl 1.1 */
173 jsr166 1.27 public void testGetCompletedTaskCount() throws InterruptedException {
174 jsr166 1.37 final ThreadPoolExecutor p =
175     new ThreadPoolExecutor(2, 2,
176     LONG_DELAY_MS, MILLISECONDS,
177     new ArrayBlockingQueue<Runnable>(10));
178 jsr166 1.68 try (PoolCleaner cleaner = cleaner(p)) {
179     final CountDownLatch threadStarted = new CountDownLatch(1);
180     final CountDownLatch threadProceed = new CountDownLatch(1);
181     final CountDownLatch threadDone = new CountDownLatch(1);
182 jsr166 1.37 assertEquals(0, p.getCompletedTaskCount());
183     p.execute(new CheckedRunnable() {
184     public void realRun() throws InterruptedException {
185     threadStarted.countDown();
186     assertEquals(0, p.getCompletedTaskCount());
187     threadProceed.await();
188     threadDone.countDown();
189     }});
190 jsr166 1.45 await(threadStarted);
191 jsr166 1.37 assertEquals(0, p.getCompletedTaskCount());
192     threadProceed.countDown();
193     threadDone.await();
194 jsr166 1.45 long startTime = System.nanoTime();
195     while (p.getCompletedTaskCount() != 1) {
196     if (millisElapsedSince(startTime) > LONG_DELAY_MS)
197     fail("timed out");
198     Thread.yield();
199     }
200 jsr166 1.37 }
201 dl 1.1 }
202 jsr166 1.24
203 dl 1.1 /**
204 jsr166 1.35 * getCorePoolSize returns size given in constructor if not otherwise set
205 dl 1.1 */
206 dl 1.5 public void testGetCorePoolSize() {
207 jsr166 1.37 final ThreadPoolExecutor p =
208     new ThreadPoolExecutor(1, 1,
209     LONG_DELAY_MS, MILLISECONDS,
210     new ArrayBlockingQueue<Runnable>(10));
211 jsr166 1.69 try (PoolCleaner cleaner = cleaner(p)) {
212     assertEquals(1, p.getCorePoolSize());
213     }
214 dl 1.1 }
215 jsr166 1.24
216 dl 1.1 /**
217 jsr166 1.35 * getKeepAliveTime returns value given in constructor if not otherwise set
218 dl 1.1 */
219 dl 1.5 public void testGetKeepAliveTime() {
220 jsr166 1.37 final ThreadPoolExecutor p =
221     new ThreadPoolExecutor(2, 2,
222     1000, MILLISECONDS,
223     new ArrayBlockingQueue<Runnable>(10));
224 jsr166 1.70 try (PoolCleaner cleaner = cleaner(p)) {
225     assertEquals(1, p.getKeepAliveTime(SECONDS));
226     }
227 dl 1.1 }
228 dl 1.8
229 jsr166 1.24 /**
230 dl 1.8 * getThreadFactory returns factory in constructor if not set
231     */
232     public void testGetThreadFactory() {
233 jsr166 1.71 ThreadFactory threadFactory = new SimpleThreadFactory();
234 jsr166 1.37 final ThreadPoolExecutor p =
235     new ThreadPoolExecutor(1, 2,
236     LONG_DELAY_MS, MILLISECONDS,
237     new ArrayBlockingQueue<Runnable>(10),
238 jsr166 1.71 threadFactory,
239 jsr166 1.37 new NoOpREHandler());
240 jsr166 1.71 try (PoolCleaner cleaner = cleaner(p)) {
241     assertSame(threadFactory, p.getThreadFactory());
242     }
243 dl 1.8 }
244    
245 jsr166 1.24 /**
246 dl 1.8 * setThreadFactory sets the thread factory returned by getThreadFactory
247     */
248     public void testSetThreadFactory() {
249 jsr166 1.37 final ThreadPoolExecutor p =
250     new ThreadPoolExecutor(1, 2,
251     LONG_DELAY_MS, MILLISECONDS,
252     new ArrayBlockingQueue<Runnable>(10));
253 jsr166 1.71 try (PoolCleaner cleaner = cleaner(p)) {
254     ThreadFactory threadFactory = new SimpleThreadFactory();
255     p.setThreadFactory(threadFactory);
256     assertSame(threadFactory, p.getThreadFactory());
257     }
258 dl 1.8 }
259    
260 jsr166 1.24 /**
261 dl 1.8 * setThreadFactory(null) throws NPE
262     */
263     public void testSetThreadFactoryNull() {
264 jsr166 1.37 final ThreadPoolExecutor p =
265     new ThreadPoolExecutor(1, 2,
266     LONG_DELAY_MS, MILLISECONDS,
267     new ArrayBlockingQueue<Runnable>(10));
268 jsr166 1.72 try (PoolCleaner cleaner = cleaner(p)) {
269     try {
270     p.setThreadFactory(null);
271     shouldThrow();
272     } catch (NullPointerException success) {}
273 dl 1.8 }
274     }
275    
276 jsr166 1.24 /**
277 dl 1.9 * getRejectedExecutionHandler returns handler in constructor if not set
278     */
279     public void testGetRejectedExecutionHandler() {
280 jsr166 1.73 final RejectedExecutionHandler handler = new NoOpREHandler();
281 jsr166 1.37 final ThreadPoolExecutor p =
282     new ThreadPoolExecutor(1, 2,
283     LONG_DELAY_MS, MILLISECONDS,
284     new ArrayBlockingQueue<Runnable>(10),
285 jsr166 1.73 handler);
286     try (PoolCleaner cleaner = cleaner(p)) {
287     assertSame(handler, p.getRejectedExecutionHandler());
288     }
289 dl 1.9 }
290    
291 jsr166 1.24 /**
292 dl 1.9 * setRejectedExecutionHandler sets the handler returned by
293     * getRejectedExecutionHandler
294     */
295     public void testSetRejectedExecutionHandler() {
296 jsr166 1.37 final ThreadPoolExecutor p =
297     new ThreadPoolExecutor(1, 2,
298     LONG_DELAY_MS, MILLISECONDS,
299     new ArrayBlockingQueue<Runnable>(10));
300 jsr166 1.74 try (PoolCleaner cleaner = cleaner(p)) {
301     RejectedExecutionHandler handler = new NoOpREHandler();
302     p.setRejectedExecutionHandler(handler);
303     assertSame(handler, p.getRejectedExecutionHandler());
304     }
305 dl 1.9 }
306    
307 jsr166 1.24 /**
308 dl 1.9 * setRejectedExecutionHandler(null) throws NPE
309     */
310     public void testSetRejectedExecutionHandlerNull() {
311 jsr166 1.37 final ThreadPoolExecutor p =
312     new ThreadPoolExecutor(1, 2,
313     LONG_DELAY_MS, MILLISECONDS,
314     new ArrayBlockingQueue<Runnable>(10));
315 jsr166 1.75 try (PoolCleaner cleaner = cleaner(p)) {
316     try {
317     p.setRejectedExecutionHandler(null);
318     shouldThrow();
319     } catch (NullPointerException success) {}
320 dl 1.9 }
321     }
322    
323 dl 1.1 /**
324 jsr166 1.35 * getLargestPoolSize increases, but doesn't overestimate, when
325     * multiple threads active
326 dl 1.1 */
327 jsr166 1.27 public void testGetLargestPoolSize() throws InterruptedException {
328 jsr166 1.37 final int THREADS = 3;
329 jsr166 1.103 final CountDownLatch done = new CountDownLatch(1);
330 jsr166 1.37 final ThreadPoolExecutor p =
331     new ThreadPoolExecutor(THREADS, THREADS,
332     LONG_DELAY_MS, MILLISECONDS,
333     new ArrayBlockingQueue<Runnable>(10));
334 jsr166 1.103 try (PoolCleaner cleaner = cleaner(p, done)) {
335     assertEquals(0, p.getLargestPoolSize());
336 jsr166 1.76 final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
337 jsr166 1.37 for (int i = 0; i < THREADS; i++)
338     p.execute(new CheckedRunnable() {
339     public void realRun() throws InterruptedException {
340     threadsStarted.countDown();
341 jsr166 1.106 await(done);
342 jsr166 1.37 assertEquals(THREADS, p.getLargestPoolSize());
343     }});
344 jsr166 1.107 await(threadsStarted);
345 jsr166 1.37 assertEquals(THREADS, p.getLargestPoolSize());
346     }
347 jsr166 1.76 assertEquals(THREADS, p.getLargestPoolSize());
348 dl 1.1 }
349 jsr166 1.24
350 dl 1.1 /**
351 jsr166 1.35 * getMaximumPoolSize returns value given in constructor if not
352     * otherwise set
353 dl 1.1 */
354 dl 1.5 public void testGetMaximumPoolSize() {
355 jsr166 1.37 final ThreadPoolExecutor p =
356     new ThreadPoolExecutor(2, 3,
357     LONG_DELAY_MS, MILLISECONDS,
358     new ArrayBlockingQueue<Runnable>(10));
359 jsr166 1.77 try (PoolCleaner cleaner = cleaner(p)) {
360     assertEquals(3, p.getMaximumPoolSize());
361     p.setMaximumPoolSize(5);
362     assertEquals(5, p.getMaximumPoolSize());
363     p.setMaximumPoolSize(4);
364     assertEquals(4, p.getMaximumPoolSize());
365     }
366 dl 1.1 }
367 jsr166 1.24
368 dl 1.1 /**
369 jsr166 1.35 * getPoolSize increases, but doesn't overestimate, when threads
370     * become active
371 dl 1.1 */
372 jsr166 1.37 public void testGetPoolSize() throws InterruptedException {
373 jsr166 1.103 final CountDownLatch done = new CountDownLatch(1);
374 jsr166 1.37 final ThreadPoolExecutor p =
375     new ThreadPoolExecutor(1, 1,
376     LONG_DELAY_MS, MILLISECONDS,
377     new ArrayBlockingQueue<Runnable>(10));
378 jsr166 1.103 try (PoolCleaner cleaner = cleaner(p, done)) {
379     assertEquals(0, p.getPoolSize());
380 jsr166 1.78 final CountDownLatch threadStarted = new CountDownLatch(1);
381 jsr166 1.37 p.execute(new CheckedRunnable() {
382     public void realRun() throws InterruptedException {
383     threadStarted.countDown();
384     assertEquals(1, p.getPoolSize());
385 jsr166 1.106 await(done);
386 jsr166 1.37 }});
387 jsr166 1.108 await(threadStarted);
388 jsr166 1.37 assertEquals(1, p.getPoolSize());
389     }
390 dl 1.1 }
391 jsr166 1.24
392 dl 1.1 /**
393 jsr166 1.35 * getTaskCount increases, but doesn't overestimate, when tasks submitted
394 dl 1.1 */
395 jsr166 1.27 public void testGetTaskCount() throws InterruptedException {
396 jsr166 1.100 final int TASKS = 3;
397     final CountDownLatch done = new CountDownLatch(1);
398 jsr166 1.37 final ThreadPoolExecutor p =
399     new ThreadPoolExecutor(1, 1,
400     LONG_DELAY_MS, MILLISECONDS,
401     new ArrayBlockingQueue<Runnable>(10));
402 jsr166 1.100 try (PoolCleaner cleaner = cleaner(p, done)) {
403 jsr166 1.81 final CountDownLatch threadStarted = new CountDownLatch(1);
404 jsr166 1.37 assertEquals(0, p.getTaskCount());
405 jsr166 1.100 assertEquals(0, p.getCompletedTaskCount());
406 jsr166 1.37 p.execute(new CheckedRunnable() {
407     public void realRun() throws InterruptedException {
408     threadStarted.countDown();
409 jsr166 1.106 await(done);
410 jsr166 1.37 }});
411 jsr166 1.108 await(threadStarted);
412 jsr166 1.37 assertEquals(1, p.getTaskCount());
413 jsr166 1.100 assertEquals(0, p.getCompletedTaskCount());
414     for (int i = 0; i < TASKS; i++) {
415     assertEquals(1 + i, p.getTaskCount());
416     p.execute(new CheckedRunnable() {
417     public void realRun() throws InterruptedException {
418     threadStarted.countDown();
419     assertEquals(1 + TASKS, p.getTaskCount());
420 jsr166 1.106 await(done);
421 jsr166 1.100 }});
422     }
423     assertEquals(1 + TASKS, p.getTaskCount());
424     assertEquals(0, p.getCompletedTaskCount());
425 jsr166 1.37 }
426 jsr166 1.100 assertEquals(1 + TASKS, p.getTaskCount());
427     assertEquals(1 + TASKS, p.getCompletedTaskCount());
428 dl 1.1 }
429 jsr166 1.24
430 dl 1.1 /**
431 jsr166 1.43 * isShutdown is false before shutdown, true after
432 dl 1.1 */
433 dl 1.5 public void testIsShutdown() {
434 jsr166 1.37 final ThreadPoolExecutor p =
435     new ThreadPoolExecutor(1, 1,
436     LONG_DELAY_MS, MILLISECONDS,
437     new ArrayBlockingQueue<Runnable>(10));
438 jsr166 1.82 try (PoolCleaner cleaner = cleaner(p)) {
439     assertFalse(p.isShutdown());
440     try { p.shutdown(); } catch (SecurityException ok) { return; }
441     assertTrue(p.isShutdown());
442     }
443 dl 1.1 }
444    
445     /**
446 jsr166 1.48 * awaitTermination on a non-shutdown pool times out
447     */
448     public void testAwaitTermination_timesOut() throws InterruptedException {
449     final ThreadPoolExecutor p =
450     new ThreadPoolExecutor(1, 1,
451     LONG_DELAY_MS, MILLISECONDS,
452     new ArrayBlockingQueue<Runnable>(10));
453 jsr166 1.84 try (PoolCleaner cleaner = cleaner(p)) {
454     assertFalse(p.isTerminated());
455     assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
456     assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
457     assertFalse(p.awaitTermination(-1L, NANOSECONDS));
458     assertFalse(p.awaitTermination(-1L, MILLISECONDS));
459     assertFalse(p.awaitTermination(0L, NANOSECONDS));
460     assertFalse(p.awaitTermination(0L, MILLISECONDS));
461     long timeoutNanos = 999999L;
462     long startTime = System.nanoTime();
463     assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
464     assertTrue(System.nanoTime() - startTime >= timeoutNanos);
465     assertFalse(p.isTerminated());
466     startTime = System.nanoTime();
467     long timeoutMillis = timeoutMillis();
468     assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
469     assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
470     assertFalse(p.isTerminated());
471     try { p.shutdown(); } catch (SecurityException ok) { return; }
472     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
473     assertTrue(p.isTerminated());
474     }
475 jsr166 1.48 }
476    
477     /**
478 jsr166 1.35 * isTerminated is false before termination, true after
479 dl 1.1 */
480 jsr166 1.27 public void testIsTerminated() throws InterruptedException {
481 jsr166 1.37 final ThreadPoolExecutor p =
482     new ThreadPoolExecutor(1, 1,
483     LONG_DELAY_MS, MILLISECONDS,
484     new ArrayBlockingQueue<Runnable>(10));
485 jsr166 1.83 try (PoolCleaner cleaner = cleaner(p)) {
486     final CountDownLatch threadStarted = new CountDownLatch(1);
487     final CountDownLatch done = new CountDownLatch(1);
488     assertFalse(p.isTerminating());
489 jsr166 1.37 p.execute(new CheckedRunnable() {
490     public void realRun() throws InterruptedException {
491 jsr166 1.83 assertFalse(p.isTerminating());
492 jsr166 1.37 threadStarted.countDown();
493 jsr166 1.106 await(done);
494 jsr166 1.37 }});
495 jsr166 1.108 await(threadStarted);
496 jsr166 1.39 assertFalse(p.isTerminating());
497 jsr166 1.37 done.countDown();
498     try { p.shutdown(); } catch (SecurityException ok) { return; }
499 jsr166 1.83 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
500     assertTrue(p.isTerminated());
501     assertFalse(p.isTerminating());
502 dl 1.1 }
503 dl 1.5 }
504    
505     /**
506 jsr166 1.35 * isTerminating is not true when running or when terminated
507 dl 1.5 */
508 jsr166 1.27 public void testIsTerminating() throws InterruptedException {
509 jsr166 1.37 final ThreadPoolExecutor p =
510     new ThreadPoolExecutor(1, 1,
511     LONG_DELAY_MS, MILLISECONDS,
512     new ArrayBlockingQueue<Runnable>(10));
513 jsr166 1.85 try (PoolCleaner cleaner = cleaner(p)) {
514     final CountDownLatch threadStarted = new CountDownLatch(1);
515     final CountDownLatch done = new CountDownLatch(1);
516 jsr166 1.37 assertFalse(p.isTerminating());
517     p.execute(new CheckedRunnable() {
518     public void realRun() throws InterruptedException {
519 jsr166 1.38 assertFalse(p.isTerminating());
520 jsr166 1.37 threadStarted.countDown();
521 jsr166 1.106 await(done);
522 jsr166 1.37 }});
523 jsr166 1.108 await(threadStarted);
524 jsr166 1.37 assertFalse(p.isTerminating());
525     done.countDown();
526     try { p.shutdown(); } catch (SecurityException ok) { return; }
527 jsr166 1.85 assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
528     assertTrue(p.isTerminated());
529     assertFalse(p.isTerminating());
530 jsr166 1.37 }
531 dl 1.1 }
532    
533     /**
534 dl 1.8 * getQueue returns the work queue, which contains queued tasks
535     */
536 jsr166 1.27 public void testGetQueue() throws InterruptedException {
537 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
538 jsr166 1.114 final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
539 jsr166 1.37 final ThreadPoolExecutor p =
540     new ThreadPoolExecutor(1, 1,
541     LONG_DELAY_MS, MILLISECONDS,
542     q);
543 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
544 jsr166 1.86 final CountDownLatch threadStarted = new CountDownLatch(1);
545 jsr166 1.37 FutureTask[] tasks = new FutureTask[5];
546     for (int i = 0; i < tasks.length; i++) {
547     Callable task = new CheckedCallable<Boolean>() {
548     public Boolean realCall() throws InterruptedException {
549     threadStarted.countDown();
550     assertSame(q, p.getQueue());
551 jsr166 1.106 await(done);
552 jsr166 1.37 return Boolean.TRUE;
553     }};
554     tasks[i] = new FutureTask(task);
555     p.execute(tasks[i]);
556     }
557 jsr166 1.108 await(threadStarted);
558 jsr166 1.37 assertSame(q, p.getQueue());
559     assertFalse(q.contains(tasks[0]));
560     assertTrue(q.contains(tasks[tasks.length - 1]));
561     assertEquals(tasks.length - 1, q.size());
562 dl 1.8 }
563     }
564    
565     /**
566     * remove(task) removes queued task, and fails to remove active task
567     */
568 jsr166 1.27 public void testRemove() throws InterruptedException {
569 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
570 jsr166 1.114 BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
571 jsr166 1.37 final ThreadPoolExecutor p =
572     new ThreadPoolExecutor(1, 1,
573     LONG_DELAY_MS, MILLISECONDS,
574     q);
575 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
576 jsr166 1.87 Runnable[] tasks = new Runnable[6];
577     final CountDownLatch threadStarted = new CountDownLatch(1);
578 jsr166 1.37 for (int i = 0; i < tasks.length; i++) {
579     tasks[i] = new CheckedRunnable() {
580     public void realRun() throws InterruptedException {
581     threadStarted.countDown();
582 jsr166 1.106 await(done);
583 jsr166 1.37 }};
584     p.execute(tasks[i]);
585     }
586 jsr166 1.108 await(threadStarted);
587 jsr166 1.37 assertFalse(p.remove(tasks[0]));
588 dl 1.8 assertTrue(q.contains(tasks[4]));
589     assertTrue(q.contains(tasks[3]));
590 jsr166 1.37 assertTrue(p.remove(tasks[4]));
591     assertFalse(p.remove(tasks[4]));
592 dl 1.8 assertFalse(q.contains(tasks[4]));
593     assertTrue(q.contains(tasks[3]));
594 jsr166 1.37 assertTrue(p.remove(tasks[3]));
595 dl 1.8 assertFalse(q.contains(tasks[3]));
596     }
597     }
598    
599     /**
600 jsr166 1.35 * purge removes cancelled tasks from the queue
601 dl 1.1 */
602 jsr166 1.37 public void testPurge() throws InterruptedException {
603     final CountDownLatch threadStarted = new CountDownLatch(1);
604     final CountDownLatch done = new CountDownLatch(1);
605 jsr166 1.114 final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10);
606 jsr166 1.37 final ThreadPoolExecutor p =
607     new ThreadPoolExecutor(1, 1,
608     LONG_DELAY_MS, MILLISECONDS,
609     q);
610 jsr166 1.101 try (PoolCleaner cleaner = cleaner(p, done)) {
611 jsr166 1.87 FutureTask[] tasks = new FutureTask[5];
612 jsr166 1.37 for (int i = 0; i < tasks.length; i++) {
613     Callable task = new CheckedCallable<Boolean>() {
614     public Boolean realCall() throws InterruptedException {
615     threadStarted.countDown();
616 jsr166 1.106 await(done);
617 jsr166 1.37 return Boolean.TRUE;
618     }};
619     tasks[i] = new FutureTask(task);
620     p.execute(tasks[i]);
621     }
622 jsr166 1.108 await(threadStarted);
623 jsr166 1.37 assertEquals(tasks.length, p.getTaskCount());
624     assertEquals(tasks.length - 1, q.size());
625     assertEquals(1L, p.getActiveCount());
626     assertEquals(0L, p.getCompletedTaskCount());
627     tasks[4].cancel(true);
628     tasks[3].cancel(false);
629     p.purge();
630     assertEquals(tasks.length - 3, q.size());
631     assertEquals(tasks.length - 2, p.getTaskCount());
632     p.purge(); // Nothing to do
633     assertEquals(tasks.length - 3, q.size());
634     assertEquals(tasks.length - 2, p.getTaskCount());
635     }
636 dl 1.1 }
637    
638     /**
639 jsr166 1.60 * shutdownNow returns a list containing tasks that were not run,
640     * and those tasks are drained from the queue
641 dl 1.1 */
642 jsr166 1.61 public void testShutdownNow() throws InterruptedException {
643     final int poolSize = 2;
644     final int count = 5;
645     final AtomicInteger ran = new AtomicInteger(0);
646 jsr166 1.37 final ThreadPoolExecutor p =
647 jsr166 1.61 new ThreadPoolExecutor(poolSize, poolSize,
648 jsr166 1.37 LONG_DELAY_MS, MILLISECONDS,
649     new ArrayBlockingQueue<Runnable>(10));
650 jsr166 1.99 final CountDownLatch threadsStarted = new CountDownLatch(poolSize);
651 jsr166 1.63 Runnable waiter = new CheckedRunnable() { public void realRun() {
652 jsr166 1.61 threadsStarted.countDown();
653 dl 1.17 try {
654 jsr166 1.61 MILLISECONDS.sleep(2 * LONG_DELAY_MS);
655     } catch (InterruptedException success) {}
656     ran.getAndIncrement();
657     }};
658     for (int i = 0; i < count; i++)
659     p.execute(waiter);
660 jsr166 1.107 await(threadsStarted);
661 jsr166 1.62 assertEquals(poolSize, p.getActiveCount());
662     assertEquals(0, p.getCompletedTaskCount());
663 jsr166 1.61 final List<Runnable> queuedTasks;
664     try {
665     queuedTasks = p.shutdownNow();
666     } catch (SecurityException ok) {
667     return; // Allowed in case test doesn't have privs
668 dl 1.1 }
669 jsr166 1.37 assertTrue(p.isShutdown());
670 jsr166 1.60 assertTrue(p.getQueue().isEmpty());
671 jsr166 1.61 assertEquals(count - poolSize, queuedTasks.size());
672     assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
673     assertTrue(p.isTerminated());
674     assertEquals(poolSize, ran.get());
675 jsr166 1.62 assertEquals(poolSize, p.getCompletedTaskCount());
676 dl 1.1 }
677    
678     // Exception Tests
679    
680 jsr166 1.24 /**
681     * Constructor throws if corePoolSize argument is less than zero
682 dl 1.6 */
683 dl 1.1 public void testConstructor1() {
684 dl 1.5 try {
685 jsr166 1.52 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
686 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
687 dl 1.5 shouldThrow();
688 jsr166 1.28 } catch (IllegalArgumentException success) {}
689 dl 1.1 }
690 jsr166 1.24
691     /**
692     * Constructor throws if maximumPoolSize is less than zero
693 dl 1.6 */
694 dl 1.1 public void testConstructor2() {
695 dl 1.5 try {
696 jsr166 1.52 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
697 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
698 dl 1.5 shouldThrow();
699 jsr166 1.28 } catch (IllegalArgumentException success) {}
700 dl 1.1 }
701 jsr166 1.24
702     /**
703     * Constructor throws if maximumPoolSize is equal to zero
704 dl 1.6 */
705 dl 1.1 public void testConstructor3() {
706 dl 1.5 try {
707 jsr166 1.52 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
708 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
709 dl 1.5 shouldThrow();
710 jsr166 1.28 } catch (IllegalArgumentException success) {}
711 dl 1.1 }
712    
713 jsr166 1.24 /**
714     * Constructor throws if keepAliveTime is less than zero
715 dl 1.6 */
716 dl 1.1 public void testConstructor4() {
717 dl 1.5 try {
718 jsr166 1.52 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
719 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
720 dl 1.5 shouldThrow();
721 jsr166 1.28 } catch (IllegalArgumentException success) {}
722 dl 1.1 }
723    
724 jsr166 1.24 /**
725     * Constructor throws if corePoolSize is greater than the maximumPoolSize
726 dl 1.6 */
727 dl 1.1 public void testConstructor5() {
728 dl 1.5 try {
729 jsr166 1.52 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
730 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
731 dl 1.5 shouldThrow();
732 jsr166 1.28 } catch (IllegalArgumentException success) {}
733 dl 1.1 }
734 jsr166 1.24
735     /**
736     * Constructor throws if workQueue is set to null
737 dl 1.6 */
738 dl 1.8 public void testConstructorNullPointerException() {
739 dl 1.5 try {
740 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
741 jsr166 1.37 (BlockingQueue) null);
742 dl 1.5 shouldThrow();
743 jsr166 1.28 } catch (NullPointerException success) {}
744 dl 1.1 }
745    
746 jsr166 1.24 /**
747     * Constructor throws if corePoolSize argument is less than zero
748 dl 1.6 */
749 dl 1.1 public void testConstructor6() {
750 dl 1.5 try {
751 jsr166 1.52 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
752 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
753     new SimpleThreadFactory());
754 dl 1.5 shouldThrow();
755 jsr166 1.28 } catch (IllegalArgumentException success) {}
756 dl 1.1 }
757 jsr166 1.24
758     /**
759     * Constructor throws if maximumPoolSize is less than zero
760 dl 1.6 */
761 dl 1.1 public void testConstructor7() {
762 dl 1.5 try {
763 jsr166 1.52 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
764 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
765     new SimpleThreadFactory());
766 dl 1.5 shouldThrow();
767 jsr166 1.28 } catch (IllegalArgumentException success) {}
768 dl 1.1 }
769    
770 jsr166 1.24 /**
771     * Constructor throws if maximumPoolSize is equal to zero
772 dl 1.6 */
773 dl 1.1 public void testConstructor8() {
774 dl 1.5 try {
775 jsr166 1.52 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
776 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
777     new SimpleThreadFactory());
778 dl 1.5 shouldThrow();
779 jsr166 1.28 } catch (IllegalArgumentException success) {}
780 dl 1.1 }
781    
782 jsr166 1.24 /**
783     * Constructor throws if keepAliveTime is less than zero
784 dl 1.6 */
785 dl 1.1 public void testConstructor9() {
786 dl 1.5 try {
787 jsr166 1.52 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
788 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
789     new SimpleThreadFactory());
790 dl 1.5 shouldThrow();
791 jsr166 1.28 } catch (IllegalArgumentException success) {}
792 dl 1.1 }
793    
794 jsr166 1.24 /**
795     * Constructor throws if corePoolSize is greater than the maximumPoolSize
796 dl 1.6 */
797 dl 1.1 public void testConstructor10() {
798 dl 1.5 try {
799 jsr166 1.52 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
800 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
801     new SimpleThreadFactory());
802 dl 1.5 shouldThrow();
803 jsr166 1.28 } catch (IllegalArgumentException success) {}
804 dl 1.1 }
805    
806 jsr166 1.24 /**
807     * Constructor throws if workQueue is set to null
808 dl 1.6 */
809 dl 1.8 public void testConstructorNullPointerException2() {
810 dl 1.5 try {
811 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
812 jsr166 1.37 (BlockingQueue) null,
813     new SimpleThreadFactory());
814 dl 1.5 shouldThrow();
815 jsr166 1.28 } catch (NullPointerException success) {}
816 dl 1.1 }
817    
818 jsr166 1.24 /**
819     * Constructor throws if threadFactory is set to null
820 dl 1.6 */
821 dl 1.8 public void testConstructorNullPointerException3() {
822 dl 1.5 try {
823 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
824 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
825     (ThreadFactory) null);
826 dl 1.5 shouldThrow();
827 jsr166 1.28 } catch (NullPointerException success) {}
828 dl 1.1 }
829 jsr166 1.24
830     /**
831     * Constructor throws if corePoolSize argument is less than zero
832 dl 1.6 */
833 dl 1.1 public void testConstructor11() {
834 dl 1.5 try {
835 jsr166 1.52 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
836 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
837     new NoOpREHandler());
838 dl 1.5 shouldThrow();
839 jsr166 1.28 } catch (IllegalArgumentException success) {}
840 dl 1.1 }
841    
842 jsr166 1.24 /**
843     * Constructor throws if maximumPoolSize is less than zero
844 dl 1.6 */
845 dl 1.1 public void testConstructor12() {
846 dl 1.5 try {
847 jsr166 1.52 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
848 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
849     new NoOpREHandler());
850 dl 1.5 shouldThrow();
851 jsr166 1.28 } catch (IllegalArgumentException success) {}
852 dl 1.1 }
853    
854 jsr166 1.24 /**
855     * Constructor throws if maximumPoolSize is equal to zero
856 dl 1.6 */
857 dl 1.1 public void testConstructor13() {
858 dl 1.5 try {
859 jsr166 1.52 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
860 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
861     new NoOpREHandler());
862 dl 1.5 shouldThrow();
863 jsr166 1.28 } catch (IllegalArgumentException success) {}
864 dl 1.1 }
865    
866 jsr166 1.24 /**
867     * Constructor throws if keepAliveTime is less than zero
868 dl 1.6 */
869 dl 1.1 public void testConstructor14() {
870 dl 1.5 try {
871 jsr166 1.52 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
872 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
873     new NoOpREHandler());
874 dl 1.5 shouldThrow();
875 jsr166 1.28 } catch (IllegalArgumentException success) {}
876 dl 1.1 }
877    
878 jsr166 1.24 /**
879     * Constructor throws if corePoolSize is greater than the maximumPoolSize
880 dl 1.6 */
881 dl 1.1 public void testConstructor15() {
882 dl 1.5 try {
883 jsr166 1.52 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
884 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
885     new NoOpREHandler());
886 dl 1.5 shouldThrow();
887 jsr166 1.28 } catch (IllegalArgumentException success) {}
888 dl 1.1 }
889    
890 jsr166 1.24 /**
891     * Constructor throws if workQueue is set to null
892 dl 1.6 */
893 dl 1.8 public void testConstructorNullPointerException4() {
894 dl 1.5 try {
895 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
896 jsr166 1.37 (BlockingQueue) null,
897     new NoOpREHandler());
898 dl 1.5 shouldThrow();
899 jsr166 1.28 } catch (NullPointerException success) {}
900 dl 1.1 }
901    
902 jsr166 1.24 /**
903     * Constructor throws if handler is set to null
904 dl 1.6 */
905 dl 1.8 public void testConstructorNullPointerException5() {
906 dl 1.5 try {
907 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
908 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
909     (RejectedExecutionHandler) null);
910 dl 1.5 shouldThrow();
911 jsr166 1.28 } catch (NullPointerException success) {}
912 dl 1.1 }
913    
914 jsr166 1.24 /**
915     * Constructor throws if corePoolSize argument is less than zero
916 dl 1.6 */
917 dl 1.1 public void testConstructor16() {
918 dl 1.5 try {
919 jsr166 1.52 new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
920 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
921     new SimpleThreadFactory(),
922     new NoOpREHandler());
923 dl 1.5 shouldThrow();
924 jsr166 1.28 } catch (IllegalArgumentException success) {}
925 dl 1.1 }
926    
927 jsr166 1.24 /**
928     * Constructor throws if maximumPoolSize is less than zero
929 dl 1.6 */
930 dl 1.1 public void testConstructor17() {
931 dl 1.5 try {
932 jsr166 1.52 new ThreadPoolExecutor(1, -1, 1L, SECONDS,
933 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
934     new SimpleThreadFactory(),
935     new NoOpREHandler());
936 dl 1.5 shouldThrow();
937 jsr166 1.28 } catch (IllegalArgumentException success) {}
938 dl 1.1 }
939    
940 jsr166 1.24 /**
941     * Constructor throws if maximumPoolSize is equal to zero
942 dl 1.6 */
943 dl 1.1 public void testConstructor18() {
944 dl 1.5 try {
945 jsr166 1.52 new ThreadPoolExecutor(1, 0, 1L, SECONDS,
946 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
947     new SimpleThreadFactory(),
948     new NoOpREHandler());
949 dl 1.5 shouldThrow();
950 jsr166 1.28 } catch (IllegalArgumentException success) {}
951 dl 1.1 }
952    
953 jsr166 1.24 /**
954     * Constructor throws if keepAliveTime is less than zero
955 dl 1.6 */
956 dl 1.1 public void testConstructor19() {
957 dl 1.5 try {
958 jsr166 1.52 new ThreadPoolExecutor(1, 2, -1L, SECONDS,
959 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
960     new SimpleThreadFactory(),
961     new NoOpREHandler());
962 dl 1.5 shouldThrow();
963 jsr166 1.28 } catch (IllegalArgumentException success) {}
964 dl 1.1 }
965    
966 jsr166 1.24 /**
967     * Constructor throws if corePoolSize is greater than the maximumPoolSize
968 dl 1.6 */
969 dl 1.1 public void testConstructor20() {
970 dl 1.5 try {
971 jsr166 1.52 new ThreadPoolExecutor(2, 1, 1L, SECONDS,
972 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
973     new SimpleThreadFactory(),
974     new NoOpREHandler());
975 dl 1.5 shouldThrow();
976 jsr166 1.28 } catch (IllegalArgumentException success) {}
977 dl 1.1 }
978    
979 jsr166 1.24 /**
980 jsr166 1.36 * Constructor throws if workQueue is null
981 dl 1.6 */
982 dl 1.8 public void testConstructorNullPointerException6() {
983 dl 1.5 try {
984 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
985 jsr166 1.37 (BlockingQueue) null,
986     new SimpleThreadFactory(),
987     new NoOpREHandler());
988 dl 1.5 shouldThrow();
989 jsr166 1.28 } catch (NullPointerException success) {}
990 dl 1.1 }
991    
992 jsr166 1.24 /**
993 jsr166 1.36 * Constructor throws if handler is null
994 dl 1.6 */
995 dl 1.8 public void testConstructorNullPointerException7() {
996 dl 1.5 try {
997 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
998 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
999     new SimpleThreadFactory(),
1000     (RejectedExecutionHandler) null);
1001 dl 1.5 shouldThrow();
1002 jsr166 1.28 } catch (NullPointerException success) {}
1003 dl 1.1 }
1004    
1005 jsr166 1.24 /**
1006 jsr166 1.36 * Constructor throws if ThreadFactory is null
1007 dl 1.6 */
1008 dl 1.8 public void testConstructorNullPointerException8() {
1009 dl 1.5 try {
1010 jsr166 1.52 new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1011 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10),
1012     (ThreadFactory) null,
1013     new NoOpREHandler());
1014 dl 1.5 shouldThrow();
1015 jsr166 1.28 } catch (NullPointerException success) {}
1016 dl 1.1 }
1017 jsr166 1.24
1018 jsr166 1.37 /**
1019     * get of submitted callable throws InterruptedException if interrupted
1020     */
1021     public void testInterruptedSubmit() throws InterruptedException {
1022 jsr166 1.104 final CountDownLatch done = new CountDownLatch(1);
1023 jsr166 1.37 final ThreadPoolExecutor p =
1024     new ThreadPoolExecutor(1, 1,
1025 jsr166 1.55 60, SECONDS,
1026 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
1027    
1028 jsr166 1.104 try (PoolCleaner cleaner = cleaner(p, done)) {
1029 jsr166 1.88 final CountDownLatch threadStarted = new CountDownLatch(1);
1030 jsr166 1.37 Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1031     public void realRun() throws Exception {
1032     Callable task = new CheckedCallable<Boolean>() {
1033     public Boolean realCall() throws InterruptedException {
1034     threadStarted.countDown();
1035 jsr166 1.106 await(done);
1036 jsr166 1.37 return Boolean.TRUE;
1037     }};
1038     p.submit(task).get();
1039     }});
1040    
1041 jsr166 1.104 await(threadStarted);
1042 jsr166 1.37 t.interrupt();
1043 jsr166 1.104 awaitTermination(t);
1044 jsr166 1.37 }
1045     }
1046 dl 1.1
1047     /**
1048 jsr166 1.35 * execute throws RejectedExecutionException if saturated.
1049 dl 1.8 */
1050     public void testSaturatedExecute() {
1051 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
1052 jsr166 1.89 final ThreadPoolExecutor p =
1053 jsr166 1.27 new ThreadPoolExecutor(1, 1,
1054     LONG_DELAY_MS, MILLISECONDS,
1055     new ArrayBlockingQueue<Runnable>(1));
1056 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1057 jsr166 1.37 Runnable task = new CheckedRunnable() {
1058     public void realRun() throws InterruptedException {
1059 jsr166 1.106 await(done);
1060 jsr166 1.37 }};
1061     for (int i = 0; i < 2; ++i)
1062     p.execute(task);
1063     for (int i = 0; i < 2; ++i) {
1064     try {
1065     p.execute(task);
1066     shouldThrow();
1067     } catch (RejectedExecutionException success) {}
1068     assertTrue(p.getTaskCount() <= 2);
1069     }
1070     }
1071     }
1072    
1073     /**
1074     * submit(runnable) throws RejectedExecutionException if saturated.
1075     */
1076     public void testSaturatedSubmitRunnable() {
1077 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
1078 jsr166 1.96 final ThreadPoolExecutor p =
1079 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1080     LONG_DELAY_MS, MILLISECONDS,
1081     new ArrayBlockingQueue<Runnable>(1));
1082 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1083 jsr166 1.37 Runnable task = new CheckedRunnable() {
1084     public void realRun() throws InterruptedException {
1085 jsr166 1.106 await(done);
1086 jsr166 1.37 }};
1087     for (int i = 0; i < 2; ++i)
1088     p.submit(task);
1089     for (int i = 0; i < 2; ++i) {
1090     try {
1091     p.execute(task);
1092     shouldThrow();
1093     } catch (RejectedExecutionException success) {}
1094     assertTrue(p.getTaskCount() <= 2);
1095     }
1096     }
1097     }
1098    
1099     /**
1100     * submit(callable) throws RejectedExecutionException if saturated.
1101     */
1102     public void testSaturatedSubmitCallable() {
1103 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
1104 jsr166 1.96 final ThreadPoolExecutor p =
1105 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1106     LONG_DELAY_MS, MILLISECONDS,
1107     new ArrayBlockingQueue<Runnable>(1));
1108 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1109 jsr166 1.37 Runnable task = new CheckedRunnable() {
1110     public void realRun() throws InterruptedException {
1111 jsr166 1.106 await(done);
1112 jsr166 1.37 }};
1113 jsr166 1.27 for (int i = 0; i < 2; ++i)
1114 jsr166 1.115 p.execute(task);
1115 jsr166 1.27 for (int i = 0; i < 2; ++i) {
1116     try {
1117 jsr166 1.37 p.execute(task);
1118 jsr166 1.27 shouldThrow();
1119     } catch (RejectedExecutionException success) {}
1120 jsr166 1.37 assertTrue(p.getTaskCount() <= 2);
1121 dl 1.8 }
1122 jsr166 1.27 }
1123 dl 1.8 }
1124    
1125     /**
1126 jsr166 1.35 * executor using CallerRunsPolicy runs task if saturated.
1127 dl 1.8 */
1128     public void testSaturatedExecute2() {
1129 jsr166 1.37 final ThreadPoolExecutor p =
1130     new ThreadPoolExecutor(1, 1,
1131     LONG_DELAY_MS,
1132     MILLISECONDS,
1133     new ArrayBlockingQueue<Runnable>(1),
1134 jsr166 1.90 new ThreadPoolExecutor.CallerRunsPolicy());
1135     try (PoolCleaner cleaner = cleaner(p)) {
1136     final CountDownLatch done = new CountDownLatch(1);
1137     Runnable blocker = new CheckedRunnable() {
1138     public void realRun() throws InterruptedException {
1139 jsr166 1.106 await(done);
1140 jsr166 1.90 }};
1141     p.execute(blocker);
1142 dl 1.8 TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1143 jsr166 1.90 for (int i = 0; i < tasks.length; i++)
1144 dl 1.8 tasks[i] = new TrackedNoOpRunnable();
1145 jsr166 1.90 for (int i = 0; i < tasks.length; i++)
1146 dl 1.8 p.execute(tasks[i]);
1147 jsr166 1.90 for (int i = 1; i < tasks.length; i++)
1148 dl 1.8 assertTrue(tasks[i].done);
1149 jsr166 1.91 assertFalse(tasks[0].done); // waiting in queue
1150 jsr166 1.90 done.countDown();
1151 dl 1.8 }
1152     }
1153    
1154     /**
1155 jsr166 1.35 * executor using DiscardPolicy drops task if saturated.
1156 dl 1.8 */
1157     public void testSaturatedExecute3() {
1158 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
1159 jsr166 1.91 final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1160     for (int i = 0; i < tasks.length; ++i)
1161     tasks[i] = new TrackedNoOpRunnable();
1162 jsr166 1.37 final ThreadPoolExecutor p =
1163     new ThreadPoolExecutor(1, 1,
1164 jsr166 1.91 LONG_DELAY_MS, MILLISECONDS,
1165     new ArrayBlockingQueue<Runnable>(1),
1166     new ThreadPoolExecutor.DiscardPolicy());
1167 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1168 jsr166 1.91 p.execute(awaiter(done));
1169    
1170 jsr166 1.37 for (TrackedNoOpRunnable task : tasks)
1171     p.execute(task);
1172 jsr166 1.91 for (int i = 1; i < tasks.length; i++)
1173     assertFalse(tasks[i].done);
1174 dl 1.8 }
1175 jsr166 1.91 for (int i = 1; i < tasks.length; i++)
1176     assertFalse(tasks[i].done);
1177     assertTrue(tasks[0].done); // was waiting in queue
1178 dl 1.8 }
1179    
1180     /**
1181 jsr166 1.35 * executor using DiscardOldestPolicy drops oldest task if saturated.
1182 dl 1.8 */
1183     public void testSaturatedExecute4() {
1184 jsr166 1.92 final CountDownLatch done = new CountDownLatch(1);
1185     LatchAwaiter r1 = awaiter(done);
1186     LatchAwaiter r2 = awaiter(done);
1187     LatchAwaiter r3 = awaiter(done);
1188 jsr166 1.37 final ThreadPoolExecutor p =
1189     new ThreadPoolExecutor(1, 1,
1190     LONG_DELAY_MS, MILLISECONDS,
1191     new ArrayBlockingQueue<Runnable>(1),
1192 jsr166 1.92 new ThreadPoolExecutor.DiscardOldestPolicy());
1193 jsr166 1.109 try (PoolCleaner cleaner = cleaner(p, done)) {
1194 jsr166 1.92 assertEquals(LatchAwaiter.NEW, r1.state);
1195     assertEquals(LatchAwaiter.NEW, r2.state);
1196     assertEquals(LatchAwaiter.NEW, r3.state);
1197     p.execute(r1);
1198 dl 1.8 p.execute(r2);
1199     assertTrue(p.getQueue().contains(r2));
1200     p.execute(r3);
1201     assertFalse(p.getQueue().contains(r2));
1202     assertTrue(p.getQueue().contains(r3));
1203     }
1204 jsr166 1.92 assertEquals(LatchAwaiter.DONE, r1.state);
1205     assertEquals(LatchAwaiter.NEW, r2.state);
1206     assertEquals(LatchAwaiter.DONE, r3.state);
1207 dl 1.8 }
1208    
1209     /**
1210 jsr166 1.35 * execute throws RejectedExecutionException if shutdown
1211 dl 1.1 */
1212 dl 1.8 public void testRejectedExecutionExceptionOnShutdown() {
1213 jsr166 1.96 final ThreadPoolExecutor p =
1214 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1215     LONG_DELAY_MS, MILLISECONDS,
1216     new ArrayBlockingQueue<Runnable>(1));
1217     try { p.shutdown(); } catch (SecurityException ok) { return; }
1218 jsr166 1.93 try (PoolCleaner cleaner = cleaner(p)) {
1219     try {
1220     p.execute(new NoOpRunnable());
1221     shouldThrow();
1222     } catch (RejectedExecutionException success) {}
1223     }
1224 dl 1.1 }
1225 dl 1.6
1226     /**
1227 jsr166 1.35 * execute using CallerRunsPolicy drops task on shutdown
1228 dl 1.8 */
1229     public void testCallerRunsOnShutdown() {
1230     RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1231 jsr166 1.37 final ThreadPoolExecutor p =
1232     new ThreadPoolExecutor(1, 1,
1233     LONG_DELAY_MS, MILLISECONDS,
1234     new ArrayBlockingQueue<Runnable>(1), h);
1235 dl 1.8
1236 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
1237 jsr166 1.94 try (PoolCleaner cleaner = cleaner(p)) {
1238 dl 1.8 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1239 jsr166 1.31 p.execute(r);
1240 dl 1.8 assertFalse(r.done);
1241     }
1242     }
1243    
1244     /**
1245 jsr166 1.35 * execute using DiscardPolicy drops task on shutdown
1246 dl 1.8 */
1247     public void testDiscardOnShutdown() {
1248 jsr166 1.95 final ThreadPoolExecutor p =
1249 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1250     LONG_DELAY_MS, MILLISECONDS,
1251     new ArrayBlockingQueue<Runnable>(1),
1252 jsr166 1.95 new ThreadPoolExecutor.DiscardPolicy());
1253 dl 1.8
1254 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
1255 jsr166 1.95 try (PoolCleaner cleaner = cleaner(p)) {
1256 dl 1.8 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1257 jsr166 1.31 p.execute(r);
1258 dl 1.8 assertFalse(r.done);
1259     }
1260     }
1261    
1262     /**
1263 jsr166 1.35 * execute using DiscardOldestPolicy drops task on shutdown
1264 dl 1.6 */
1265 dl 1.8 public void testDiscardOldestOnShutdown() {
1266 jsr166 1.95 final ThreadPoolExecutor p =
1267 jsr166 1.37 new ThreadPoolExecutor(1, 1,
1268     LONG_DELAY_MS, MILLISECONDS,
1269     new ArrayBlockingQueue<Runnable>(1),
1270 jsr166 1.95 new ThreadPoolExecutor.DiscardOldestPolicy());
1271 dl 1.8
1272 jsr166 1.25 try { p.shutdown(); } catch (SecurityException ok) { return; }
1273 jsr166 1.95 try (PoolCleaner cleaner = cleaner(p)) {
1274 dl 1.8 TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1275 jsr166 1.31 p.execute(r);
1276 dl 1.8 assertFalse(r.done);
1277     }
1278 dl 1.6 }
1279    
1280     /**
1281 jsr166 1.34 * execute(null) throws NPE
1282 dl 1.6 */
1283     public void testExecuteNull() {
1284 jsr166 1.95 final ThreadPoolExecutor p =
1285     new ThreadPoolExecutor(1, 2,
1286     1L, SECONDS,
1287 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
1288 jsr166 1.95 try (PoolCleaner cleaner = cleaner(p)) {
1289     try {
1290     p.execute(null);
1291     shouldThrow();
1292     } catch (NullPointerException success) {}
1293     }
1294 dl 1.6 }
1295 jsr166 1.24
1296 dl 1.1 /**
1297 jsr166 1.34 * setCorePoolSize of negative value throws IllegalArgumentException
1298 dl 1.1 */
1299 dl 1.5 public void testCorePoolSizeIllegalArgumentException() {
1300 jsr166 1.96 final ThreadPoolExecutor p =
1301 jsr166 1.27 new ThreadPoolExecutor(1, 2,
1302     LONG_DELAY_MS, MILLISECONDS,
1303     new ArrayBlockingQueue<Runnable>(10));
1304 jsr166 1.97 try (PoolCleaner cleaner = cleaner(p)) {
1305     try {
1306     p.setCorePoolSize(-1);
1307     shouldThrow();
1308     } catch (IllegalArgumentException success) {}
1309 dl 1.1 }
1310 jsr166 1.24 }
1311 dl 1.1
1312     /**
1313 jsr166 1.35 * setMaximumPoolSize(int) throws IllegalArgumentException if
1314     * given a value less the core pool size
1315 jsr166 1.24 */
1316 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException() {
1317 jsr166 1.96 final ThreadPoolExecutor p =
1318 jsr166 1.27 new ThreadPoolExecutor(2, 3,
1319     LONG_DELAY_MS, MILLISECONDS,
1320     new ArrayBlockingQueue<Runnable>(10));
1321 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1322     try {
1323     p.setMaximumPoolSize(1);
1324     shouldThrow();
1325     } catch (IllegalArgumentException success) {}
1326 dl 1.1 }
1327     }
1328 jsr166 1.24
1329 dl 1.1 /**
1330 jsr166 1.35 * setMaximumPoolSize throws IllegalArgumentException
1331     * if given a negative value
1332 dl 1.1 */
1333 dl 1.5 public void testMaximumPoolSizeIllegalArgumentException2() {
1334 jsr166 1.96 final ThreadPoolExecutor p =
1335 jsr166 1.27 new ThreadPoolExecutor(2, 3,
1336     LONG_DELAY_MS, MILLISECONDS,
1337     new ArrayBlockingQueue<Runnable>(10));
1338 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1339     try {
1340     p.setMaximumPoolSize(-1);
1341     shouldThrow();
1342     } catch (IllegalArgumentException success) {}
1343 dl 1.1 }
1344     }
1345 jsr166 1.24
1346 dl 1.1 /**
1347 jsr166 1.54 * Configuration changes that allow core pool size greater than
1348     * max pool size result in IllegalArgumentException.
1349     */
1350     public void testPoolSizeInvariants() {
1351 jsr166 1.96 final ThreadPoolExecutor p =
1352 jsr166 1.54 new ThreadPoolExecutor(1, 1,
1353     LONG_DELAY_MS, MILLISECONDS,
1354     new ArrayBlockingQueue<Runnable>(10));
1355 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1356     for (int s = 1; s < 5; s++) {
1357     p.setMaximumPoolSize(s);
1358     p.setCorePoolSize(s);
1359     try {
1360     p.setMaximumPoolSize(s - 1);
1361     shouldThrow();
1362     } catch (IllegalArgumentException success) {}
1363     assertEquals(s, p.getCorePoolSize());
1364     assertEquals(s, p.getMaximumPoolSize());
1365     try {
1366     p.setCorePoolSize(s + 1);
1367     shouldThrow();
1368     } catch (IllegalArgumentException success) {}
1369     assertEquals(s, p.getCorePoolSize());
1370     assertEquals(s, p.getMaximumPoolSize());
1371     }
1372 jsr166 1.54 }
1373     }
1374    
1375     /**
1376 jsr166 1.35 * setKeepAliveTime throws IllegalArgumentException
1377     * when given a negative value
1378 dl 1.1 */
1379 dl 1.5 public void testKeepAliveTimeIllegalArgumentException() {
1380 jsr166 1.96 final ThreadPoolExecutor p =
1381 jsr166 1.27 new ThreadPoolExecutor(2, 3,
1382     LONG_DELAY_MS, MILLISECONDS,
1383     new ArrayBlockingQueue<Runnable>(10));
1384 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1385     try {
1386     p.setKeepAliveTime(-1, MILLISECONDS);
1387     shouldThrow();
1388     } catch (IllegalArgumentException success) {}
1389 dl 1.1 }
1390     }
1391 dl 1.8
1392     /**
1393     * terminated() is called on termination
1394     */
1395     public void testTerminated() {
1396 jsr166 1.37 ExtendedTPE p = new ExtendedTPE();
1397 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1398     try { p.shutdown(); } catch (SecurityException ok) { return; }
1399     assertTrue(p.terminatedCalled());
1400     assertTrue(p.isShutdown());
1401     }
1402 dl 1.8 }
1403    
1404     /**
1405     * beforeExecute and afterExecute are called when executing task
1406     */
1407 jsr166 1.27 public void testBeforeAfter() throws InterruptedException {
1408 jsr166 1.37 ExtendedTPE p = new ExtendedTPE();
1409 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1410 jsr166 1.45 final CountDownLatch done = new CountDownLatch(1);
1411 jsr166 1.49 p.execute(new CheckedRunnable() {
1412 jsr166 1.45 public void realRun() {
1413     done.countDown();
1414 jsr166 1.49 }});
1415 jsr166 1.45 await(p.afterCalled);
1416     assertEquals(0, done.getCount());
1417     assertTrue(p.afterCalled());
1418     assertTrue(p.beforeCalled());
1419 dl 1.8 }
1420     }
1421 dl 1.12
1422     /**
1423     * completed submit of callable returns result
1424     */
1425 jsr166 1.27 public void testSubmitCallable() throws Exception {
1426 jsr166 1.98 final ExecutorService e =
1427 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1428     LONG_DELAY_MS, MILLISECONDS,
1429     new ArrayBlockingQueue<Runnable>(10));
1430 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1431 dl 1.12 Future<String> future = e.submit(new StringTask());
1432     String result = future.get();
1433     assertSame(TEST_STRING, result);
1434     }
1435     }
1436    
1437     /**
1438     * completed submit of runnable returns successfully
1439     */
1440 jsr166 1.27 public void testSubmitRunnable() throws Exception {
1441 jsr166 1.98 final ExecutorService e =
1442 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1443     LONG_DELAY_MS, MILLISECONDS,
1444     new ArrayBlockingQueue<Runnable>(10));
1445 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1446 dl 1.12 Future<?> future = e.submit(new NoOpRunnable());
1447     future.get();
1448     assertTrue(future.isDone());
1449     }
1450     }
1451    
1452     /**
1453     * completed submit of (runnable, result) returns result
1454     */
1455 jsr166 1.27 public void testSubmitRunnable2() throws Exception {
1456 jsr166 1.98 final ExecutorService e =
1457 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1458     LONG_DELAY_MS, MILLISECONDS,
1459     new ArrayBlockingQueue<Runnable>(10));
1460 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1461 dl 1.12 Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1462     String result = future.get();
1463     assertSame(TEST_STRING, result);
1464     }
1465     }
1466    
1467     /**
1468     * invokeAny(null) throws NPE
1469     */
1470 jsr166 1.27 public void testInvokeAny1() throws Exception {
1471 jsr166 1.98 final ExecutorService e =
1472 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1473     LONG_DELAY_MS, MILLISECONDS,
1474     new ArrayBlockingQueue<Runnable>(10));
1475 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1476     try {
1477     e.invokeAny(null);
1478     shouldThrow();
1479     } catch (NullPointerException success) {}
1480 dl 1.12 }
1481     }
1482    
1483     /**
1484     * invokeAny(empty collection) throws IAE
1485     */
1486 jsr166 1.27 public void testInvokeAny2() throws Exception {
1487 jsr166 1.98 final ExecutorService e =
1488 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1489     LONG_DELAY_MS, MILLISECONDS,
1490     new ArrayBlockingQueue<Runnable>(10));
1491 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1492     try {
1493     e.invokeAny(new ArrayList<Callable<String>>());
1494     shouldThrow();
1495     } catch (IllegalArgumentException success) {}
1496 dl 1.12 }
1497     }
1498    
1499     /**
1500     * invokeAny(c) throws NPE if c has null elements
1501     */
1502 jsr166 1.27 public void testInvokeAny3() throws Exception {
1503 jsr166 1.37 final CountDownLatch latch = new CountDownLatch(1);
1504     final ExecutorService e =
1505     new ThreadPoolExecutor(2, 2,
1506     LONG_DELAY_MS, MILLISECONDS,
1507     new ArrayBlockingQueue<Runnable>(10));
1508 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1509 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1510 jsr166 1.98 l.add(latchAwaitingStringTask(latch));
1511     l.add(null);
1512     try {
1513     e.invokeAny(l);
1514     shouldThrow();
1515     } catch (NullPointerException success) {}
1516 jsr166 1.27 latch.countDown();
1517 dl 1.12 }
1518     }
1519    
1520     /**
1521     * invokeAny(c) throws ExecutionException if no task completes
1522     */
1523 jsr166 1.27 public void testInvokeAny4() throws Exception {
1524 jsr166 1.98 final ExecutorService e =
1525 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1526     LONG_DELAY_MS, MILLISECONDS,
1527     new ArrayBlockingQueue<Runnable>(10));
1528 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1529 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1530 jsr166 1.98 l.add(new NPETask());
1531     try {
1532     e.invokeAny(l);
1533     shouldThrow();
1534     } catch (ExecutionException success) {
1535     assertTrue(success.getCause() instanceof NullPointerException);
1536     }
1537 dl 1.12 }
1538     }
1539    
1540     /**
1541     * invokeAny(c) returns result of some task
1542     */
1543 jsr166 1.27 public void testInvokeAny5() throws Exception {
1544 jsr166 1.98 final ExecutorService e =
1545 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1546     LONG_DELAY_MS, MILLISECONDS,
1547     new ArrayBlockingQueue<Runnable>(10));
1548 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1549 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1550 dl 1.12 l.add(new StringTask());
1551     l.add(new StringTask());
1552     String result = e.invokeAny(l);
1553     assertSame(TEST_STRING, result);
1554     }
1555     }
1556    
1557     /**
1558     * invokeAll(null) throws NPE
1559     */
1560 jsr166 1.27 public void testInvokeAll1() throws Exception {
1561 jsr166 1.98 final ExecutorService e =
1562 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1563     LONG_DELAY_MS, MILLISECONDS,
1564     new ArrayBlockingQueue<Runnable>(10));
1565 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1566     try {
1567     e.invokeAll(null);
1568     shouldThrow();
1569     } catch (NullPointerException success) {}
1570 dl 1.12 }
1571     }
1572    
1573     /**
1574     * invokeAll(empty collection) returns empty collection
1575     */
1576 jsr166 1.27 public void testInvokeAll2() throws InterruptedException {
1577 jsr166 1.98 final ExecutorService e =
1578 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1579     LONG_DELAY_MS, MILLISECONDS,
1580     new ArrayBlockingQueue<Runnable>(10));
1581 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1582 dl 1.12 List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1583     assertTrue(r.isEmpty());
1584     }
1585     }
1586    
1587     /**
1588     * invokeAll(c) throws NPE if c has null elements
1589     */
1590 jsr166 1.27 public void testInvokeAll3() throws Exception {
1591 jsr166 1.98 final ExecutorService e =
1592 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1593     LONG_DELAY_MS, MILLISECONDS,
1594     new ArrayBlockingQueue<Runnable>(10));
1595 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1596 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1597 jsr166 1.98 l.add(new StringTask());
1598     l.add(null);
1599     try {
1600     e.invokeAll(l);
1601     shouldThrow();
1602     } catch (NullPointerException success) {}
1603 dl 1.12 }
1604     }
1605    
1606     /**
1607     * get of element of invokeAll(c) throws exception on failed task
1608     */
1609 jsr166 1.27 public void testInvokeAll4() throws Exception {
1610 jsr166 1.98 final ExecutorService e =
1611 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1612     LONG_DELAY_MS, MILLISECONDS,
1613     new ArrayBlockingQueue<Runnable>(10));
1614 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1615 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1616 dl 1.12 l.add(new NPETask());
1617 jsr166 1.33 List<Future<String>> futures = e.invokeAll(l);
1618     assertEquals(1, futures.size());
1619     try {
1620     futures.get(0).get();
1621     shouldThrow();
1622     } catch (ExecutionException success) {
1623     assertTrue(success.getCause() instanceof NullPointerException);
1624 jsr166 1.27 }
1625 dl 1.12 }
1626     }
1627    
1628     /**
1629     * invokeAll(c) returns results of all completed tasks
1630     */
1631 jsr166 1.27 public void testInvokeAll5() throws Exception {
1632 jsr166 1.98 final ExecutorService e =
1633 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1634     LONG_DELAY_MS, MILLISECONDS,
1635     new ArrayBlockingQueue<Runnable>(10));
1636 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1637 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1638 dl 1.12 l.add(new StringTask());
1639     l.add(new StringTask());
1640 jsr166 1.33 List<Future<String>> futures = e.invokeAll(l);
1641     assertEquals(2, futures.size());
1642     for (Future<String> future : futures)
1643 jsr166 1.27 assertSame(TEST_STRING, future.get());
1644 dl 1.12 }
1645     }
1646    
1647 dl 1.13 /**
1648     * timed invokeAny(null) throws NPE
1649     */
1650 jsr166 1.27 public void testTimedInvokeAny1() throws Exception {
1651 jsr166 1.98 final ExecutorService e =
1652 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1653     LONG_DELAY_MS, MILLISECONDS,
1654     new ArrayBlockingQueue<Runnable>(10));
1655 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1656     try {
1657     e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1658     shouldThrow();
1659     } catch (NullPointerException success) {}
1660 dl 1.13 }
1661     }
1662    
1663     /**
1664     * timed invokeAny(,,null) throws NPE
1665     */
1666 jsr166 1.27 public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1667 jsr166 1.98 final ExecutorService e =
1668 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1669     LONG_DELAY_MS, MILLISECONDS,
1670     new ArrayBlockingQueue<Runnable>(10));
1671 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1672 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1673 jsr166 1.98 l.add(new StringTask());
1674     try {
1675     e.invokeAny(l, MEDIUM_DELAY_MS, null);
1676     shouldThrow();
1677     } catch (NullPointerException success) {}
1678 dl 1.13 }
1679     }
1680    
1681     /**
1682     * timed invokeAny(empty collection) throws IAE
1683     */
1684 jsr166 1.27 public void testTimedInvokeAny2() throws Exception {
1685 jsr166 1.98 final ExecutorService e =
1686 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1687     LONG_DELAY_MS, MILLISECONDS,
1688     new ArrayBlockingQueue<Runnable>(10));
1689 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1690     try {
1691     e.invokeAny(new ArrayList<Callable<String>>(),
1692     MEDIUM_DELAY_MS, MILLISECONDS);
1693     shouldThrow();
1694     } catch (IllegalArgumentException success) {}
1695 dl 1.13 }
1696     }
1697    
1698     /**
1699     * timed invokeAny(c) throws NPE if c has null elements
1700     */
1701 jsr166 1.27 public void testTimedInvokeAny3() throws Exception {
1702 jsr166 1.37 final CountDownLatch latch = new CountDownLatch(1);
1703     final ExecutorService e =
1704     new ThreadPoolExecutor(2, 2,
1705     LONG_DELAY_MS, MILLISECONDS,
1706     new ArrayBlockingQueue<Runnable>(10));
1707 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1708 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1709 jsr166 1.98 l.add(latchAwaitingStringTask(latch));
1710     l.add(null);
1711     try {
1712     e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1713     shouldThrow();
1714     } catch (NullPointerException success) {}
1715 jsr166 1.30 latch.countDown();
1716 dl 1.13 }
1717     }
1718    
1719     /**
1720     * timed invokeAny(c) throws ExecutionException if no task completes
1721     */
1722 jsr166 1.27 public void testTimedInvokeAny4() throws Exception {
1723 jsr166 1.98 final ExecutorService e =
1724 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1725     LONG_DELAY_MS, MILLISECONDS,
1726     new ArrayBlockingQueue<Runnable>(10));
1727 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1728 jsr166 1.111 long startTime = System.nanoTime();
1729 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1730 jsr166 1.98 l.add(new NPETask());
1731     try {
1732 jsr166 1.111 e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1733 jsr166 1.98 shouldThrow();
1734     } catch (ExecutionException success) {
1735     assertTrue(success.getCause() instanceof NullPointerException);
1736     }
1737 jsr166 1.111 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1738 dl 1.13 }
1739     }
1740    
1741     /**
1742     * timed invokeAny(c) returns result of some task
1743     */
1744 jsr166 1.27 public void testTimedInvokeAny5() throws Exception {
1745 jsr166 1.98 final ExecutorService e =
1746 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1747     LONG_DELAY_MS, MILLISECONDS,
1748     new ArrayBlockingQueue<Runnable>(10));
1749 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1750 jsr166 1.112 long startTime = System.nanoTime();
1751 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1752 dl 1.13 l.add(new StringTask());
1753     l.add(new StringTask());
1754 jsr166 1.112 String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS);
1755 dl 1.13 assertSame(TEST_STRING, result);
1756 jsr166 1.112 assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1757 dl 1.13 }
1758     }
1759    
1760     /**
1761     * timed invokeAll(null) throws NPE
1762     */
1763 jsr166 1.27 public void testTimedInvokeAll1() throws Exception {
1764 jsr166 1.98 final ExecutorService e =
1765 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1766     LONG_DELAY_MS, MILLISECONDS,
1767     new ArrayBlockingQueue<Runnable>(10));
1768 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1769     try {
1770     e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1771     shouldThrow();
1772     } catch (NullPointerException success) {}
1773 dl 1.13 }
1774     }
1775    
1776     /**
1777     * timed invokeAll(,,null) throws NPE
1778     */
1779 jsr166 1.27 public void testTimedInvokeAllNullTimeUnit() throws Exception {
1780 jsr166 1.98 final ExecutorService e =
1781 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1782     LONG_DELAY_MS, MILLISECONDS,
1783     new ArrayBlockingQueue<Runnable>(10));
1784 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1785 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1786 jsr166 1.98 l.add(new StringTask());
1787     try {
1788     e.invokeAll(l, MEDIUM_DELAY_MS, null);
1789     shouldThrow();
1790     } catch (NullPointerException success) {}
1791 dl 1.13 }
1792     }
1793    
1794     /**
1795     * timed invokeAll(empty collection) returns empty collection
1796     */
1797 jsr166 1.27 public void testTimedInvokeAll2() throws InterruptedException {
1798 jsr166 1.98 final ExecutorService e =
1799 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1800     LONG_DELAY_MS, MILLISECONDS,
1801     new ArrayBlockingQueue<Runnable>(10));
1802 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1803     List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(),
1804     MEDIUM_DELAY_MS, MILLISECONDS);
1805 dl 1.13 assertTrue(r.isEmpty());
1806     }
1807     }
1808    
1809     /**
1810     * timed invokeAll(c) throws NPE if c has null elements
1811     */
1812 jsr166 1.27 public void testTimedInvokeAll3() throws Exception {
1813 jsr166 1.98 final ExecutorService e =
1814 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1815     LONG_DELAY_MS, MILLISECONDS,
1816     new ArrayBlockingQueue<Runnable>(10));
1817 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1818 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1819 jsr166 1.98 l.add(new StringTask());
1820     l.add(null);
1821     try {
1822     e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1823     shouldThrow();
1824     } catch (NullPointerException success) {}
1825 dl 1.13 }
1826     }
1827    
1828     /**
1829     * get of element of invokeAll(c) throws exception on failed task
1830     */
1831 jsr166 1.27 public void testTimedInvokeAll4() throws Exception {
1832 jsr166 1.98 final ExecutorService e =
1833 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1834     LONG_DELAY_MS, MILLISECONDS,
1835     new ArrayBlockingQueue<Runnable>(10));
1836 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1837 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1838 jsr166 1.98 l.add(new NPETask());
1839     List<Future<String>> futures =
1840 jsr166 1.105 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1841 jsr166 1.98 assertEquals(1, futures.size());
1842     try {
1843     futures.get(0).get();
1844     shouldThrow();
1845     } catch (ExecutionException success) {
1846     assertTrue(success.getCause() instanceof NullPointerException);
1847     }
1848 dl 1.13 }
1849     }
1850    
1851     /**
1852     * timed invokeAll(c) returns results of all completed tasks
1853     */
1854 jsr166 1.27 public void testTimedInvokeAll5() throws Exception {
1855 jsr166 1.98 final ExecutorService e =
1856 jsr166 1.37 new ThreadPoolExecutor(2, 2,
1857     LONG_DELAY_MS, MILLISECONDS,
1858     new ArrayBlockingQueue<Runnable>(10));
1859 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1860 jsr166 1.114 List<Callable<String>> l = new ArrayList<>();
1861 dl 1.13 l.add(new StringTask());
1862     l.add(new StringTask());
1863 jsr166 1.33 List<Future<String>> futures =
1864 jsr166 1.65 e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1865 jsr166 1.33 assertEquals(2, futures.size());
1866     for (Future<String> future : futures)
1867     assertSame(TEST_STRING, future.get());
1868 dl 1.13 }
1869     }
1870    
1871     /**
1872     * timed invokeAll(c) cancels tasks not completed by timeout
1873     */
1874 jsr166 1.27 public void testTimedInvokeAll6() throws Exception {
1875 jsr166 1.110 for (long timeout = timeoutMillis();;) {
1876     final CountDownLatch done = new CountDownLatch(1);
1877     final Callable<String> waiter = new CheckedCallable<String>() {
1878     public String realCall() {
1879     try { done.await(LONG_DELAY_MS, MILLISECONDS); }
1880     catch (InterruptedException ok) {}
1881     return "1"; }};
1882     final ExecutorService p =
1883     new ThreadPoolExecutor(2, 2,
1884     LONG_DELAY_MS, MILLISECONDS,
1885     new ArrayBlockingQueue<Runnable>(10));
1886     try (PoolCleaner cleaner = cleaner(p, done)) {
1887 jsr166 1.56 List<Callable<String>> tasks = new ArrayList<>();
1888 jsr166 1.59 tasks.add(new StringTask("0"));
1889 jsr166 1.110 tasks.add(waiter);
1890 jsr166 1.59 tasks.add(new StringTask("2"));
1891 jsr166 1.56 long startTime = System.nanoTime();
1892     List<Future<String>> futures =
1893 jsr166 1.110 p.invokeAll(tasks, timeout, MILLISECONDS);
1894 jsr166 1.56 assertEquals(tasks.size(), futures.size());
1895     assertTrue(millisElapsedSince(startTime) >= timeout);
1896     for (Future future : futures)
1897     assertTrue(future.isDone());
1898     assertTrue(futures.get(1).isCancelled());
1899     try {
1900 jsr166 1.59 assertEquals("0", futures.get(0).get());
1901     assertEquals("2", futures.get(2).get());
1902 jsr166 1.56 break;
1903     } catch (CancellationException retryWithLongerTimeout) {
1904     timeout *= 2;
1905     if (timeout >= LONG_DELAY_MS / 2)
1906     fail("expected exactly one task to be cancelled");
1907     }
1908     }
1909 dl 1.13 }
1910     }
1911    
1912 dl 1.19 /**
1913     * Execution continues if there is at least one thread even if
1914     * thread factory fails to create more
1915     */
1916 jsr166 1.27 public void testFailingThreadFactory() throws InterruptedException {
1917 jsr166 1.37 final ExecutorService e =
1918     new ThreadPoolExecutor(100, 100,
1919     LONG_DELAY_MS, MILLISECONDS,
1920     new LinkedBlockingQueue<Runnable>(),
1921     new FailingThreadFactory());
1922 jsr166 1.98 try (PoolCleaner cleaner = cleaner(e)) {
1923 jsr166 1.37 final int TASKS = 100;
1924     final CountDownLatch done = new CountDownLatch(TASKS);
1925     for (int k = 0; k < TASKS; ++k)
1926     e.execute(new CheckedRunnable() {
1927     public void realRun() {
1928     done.countDown();
1929     }});
1930     assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1931 dl 1.19 }
1932     }
1933 dl 1.21
1934     /**
1935     * allowsCoreThreadTimeOut is by default false.
1936     */
1937     public void testAllowsCoreThreadTimeOut() {
1938 jsr166 1.37 final ThreadPoolExecutor p =
1939     new ThreadPoolExecutor(2, 2,
1940     1000, MILLISECONDS,
1941     new ArrayBlockingQueue<Runnable>(10));
1942 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1943     assertFalse(p.allowsCoreThreadTimeOut());
1944     }
1945 dl 1.21 }
1946    
1947     /**
1948     * allowCoreThreadTimeOut(true) causes idle threads to time out
1949     */
1950 jsr166 1.37 public void testAllowCoreThreadTimeOut_true() throws Exception {
1951 jsr166 1.57 long keepAliveTime = timeoutMillis();
1952 jsr166 1.37 final ThreadPoolExecutor p =
1953     new ThreadPoolExecutor(2, 10,
1954 jsr166 1.57 keepAliveTime, MILLISECONDS,
1955 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
1956 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1957     final CountDownLatch threadStarted = new CountDownLatch(1);
1958 jsr166 1.37 p.allowCoreThreadTimeOut(true);
1959     p.execute(new CheckedRunnable() {
1960 jsr166 1.44 public void realRun() {
1961 jsr166 1.37 threadStarted.countDown();
1962     assertEquals(1, p.getPoolSize());
1963     }});
1964 jsr166 1.44 await(threadStarted);
1965 jsr166 1.57 delay(keepAliveTime);
1966 jsr166 1.44 long startTime = System.nanoTime();
1967     while (p.getPoolSize() > 0
1968     && millisElapsedSince(startTime) < LONG_DELAY_MS)
1969     Thread.yield();
1970     assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
1971 jsr166 1.37 assertEquals(0, p.getPoolSize());
1972 dl 1.21 }
1973     }
1974    
1975     /**
1976     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1977     */
1978 jsr166 1.37 public void testAllowCoreThreadTimeOut_false() throws Exception {
1979 jsr166 1.57 long keepAliveTime = timeoutMillis();
1980 jsr166 1.37 final ThreadPoolExecutor p =
1981     new ThreadPoolExecutor(2, 10,
1982 jsr166 1.57 keepAliveTime, MILLISECONDS,
1983 jsr166 1.37 new ArrayBlockingQueue<Runnable>(10));
1984 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
1985     final CountDownLatch threadStarted = new CountDownLatch(1);
1986 jsr166 1.37 p.allowCoreThreadTimeOut(false);
1987     p.execute(new CheckedRunnable() {
1988     public void realRun() throws InterruptedException {
1989     threadStarted.countDown();
1990     assertTrue(p.getPoolSize() >= 1);
1991     }});
1992 jsr166 1.57 delay(2 * keepAliveTime);
1993 jsr166 1.37 assertTrue(p.getPoolSize() >= 1);
1994 dl 1.21 }
1995     }
1996    
1997 dl 1.23 /**
1998     * execute allows the same task to be submitted multiple times, even
1999     * if rejected
2000     */
2001 jsr166 1.27 public void testRejectedRecycledTask() throws InterruptedException {
2002 dl 1.23 final int nTasks = 1000;
2003 jsr166 1.37 final CountDownLatch done = new CountDownLatch(nTasks);
2004 dl 1.23 final Runnable recycledTask = new Runnable() {
2005 jsr166 1.37 public void run() {
2006     done.countDown();
2007     }};
2008 jsr166 1.24 final ThreadPoolExecutor p =
2009 jsr166 1.55 new ThreadPoolExecutor(1, 30,
2010     60, SECONDS,
2011 dl 1.23 new ArrayBlockingQueue(30));
2012 jsr166 1.98 try (PoolCleaner cleaner = cleaner(p)) {
2013 dl 1.23 for (int i = 0; i < nTasks; ++i) {
2014     for (;;) {
2015     try {
2016     p.execute(recycledTask);
2017     break;
2018     }
2019 jsr166 1.37 catch (RejectedExecutionException ignore) {}
2020 dl 1.23 }
2021     }
2022 jsr166 1.37 // enough time to run all tasks
2023     assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2024 dl 1.23 }
2025     }
2026 jsr166 1.24
2027 jsr166 1.64 /**
2028     * get(cancelled task) throws CancellationException
2029     */
2030     public void testGet_cancelled() throws Exception {
2031 jsr166 1.109 final CountDownLatch done = new CountDownLatch(1);
2032 jsr166 1.64 final ExecutorService e =
2033     new ThreadPoolExecutor(1, 1,
2034     LONG_DELAY_MS, MILLISECONDS,
2035     new LinkedBlockingQueue<Runnable>());
2036 jsr166 1.109 try (PoolCleaner cleaner = cleaner(e, done)) {
2037 jsr166 1.64 final CountDownLatch blockerStarted = new CountDownLatch(1);
2038     final List<Future<?>> futures = new ArrayList<>();
2039     for (int i = 0; i < 2; i++) {
2040     Runnable r = new CheckedRunnable() { public void realRun()
2041     throws Throwable {
2042     blockerStarted.countDown();
2043     assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2044     }};
2045     futures.add(e.submit(r));
2046     }
2047 jsr166 1.108 await(blockerStarted);
2048 jsr166 1.64 for (Future<?> future : futures) future.cancel(false);
2049     for (Future<?> future : futures) {
2050     try {
2051     future.get();
2052     shouldThrow();
2053     } catch (CancellationException success) {}
2054     try {
2055     future.get(LONG_DELAY_MS, MILLISECONDS);
2056     shouldThrow();
2057     } catch (CancellationException success) {}
2058     assertTrue(future.isCancelled());
2059     assertTrue(future.isDone());
2060     }
2061     }
2062     }
2063    
2064 dl 1.1 }