ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.6 by dl, Thu Sep 25 11:02:41 2003 UTC vs.
Revision 1.37 by jsr166, Mon Oct 11 07:21:32 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines