ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.114
Committed: Wed Jan 4 06:09:58 2017 UTC (7 years, 4 months ago) by jsr166
Branch: MAIN
Changes since 1.113: +17 -17 lines
Log Message:
convert to Diamond

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