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.17 by dl, Tue Jan 20 20:20:56 2004 UTC vs.
Revision 1.37 by jsr166, Mon Oct 11 07:21:32 2010 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines