ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.122
Committed: Sat Jul 15 18:42:01 2017 UTC (6 years, 10 months ago) by jsr166
Branch: MAIN
Changes since 1.121: +16 -21 lines
Log Message:
coalesce tests for simple RejectedExecutionHandlers

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