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.15 by dl, Sat Dec 27 20:42:48 2003 UTC vs.
Revision 1.47 by jsr166, Tue May 31 16:16:24 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines