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.14 by dl, Thu Dec 25 19:48:57 2003 UTC vs.
Revision 1.41 by dl, Fri May 6 11:22:08 2011 UTC

# Line 1 | Line 1
1   /*
2 < * Written by members of JCP JSR-166 Expert Group and released to the
3 < * public domain. Use, modify, and redistribute this code in any way
4 < * without acknowledgement. Other contributors include Andrew Wright,
5 < * Jeffrey Hayes, Pat Fischer, Mike Judd.
2 > * Written by Doug Lea with assistance from members of JCP JSR-166
3 > * Expert Group and released to the public domain, as explained at
4 > * http://creativecommons.org/publicdomain/zero/1.0/
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 35 | 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){
49 <                            threadUnexpectedException();
50 <                        }
51 <                    }
52 <                });
53 <            Thread.sleep(SMALL_DELAY_MS);
54 <        } catch(InterruptedException e){
55 <            unexpectedException();
56 <        }
57 <        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
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();
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          }
73        assertEquals(1, p2.getActiveCount());
74        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();
141 <        }
142 <        assertEquals(1, p2.getCompletedTaskCount());
143 <        p2.shutdown();
144 <        joinPool(p2);
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 >            delay(SHORT_DELAY_MS);
158 >            assertEquals(1, p.getCompletedTaskCount());
159 >        } finally {
160 >            joinPool(p);
161 >        }
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());
149        p.shutdown();
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());
161        p.shutdown();
215          joinPool(p);
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 177 | 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());
187        p.shutdown();
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());
200        p.shutdown();
262          joinPool(p);
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 216 | 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 <        p1.shutdown();
388 <        assertTrue(p1.isShutdown());
389 <        joinPool(p1);
384 >        final ThreadPoolExecutor p =
385 >            new ThreadPoolExecutor(1, 1,
386 >                                   LONG_DELAY_MS, MILLISECONDS,
387 >                                   new ArrayBlockingQueue<Runnable>(10));
388 >        assertFalse(p.isShutdown());
389 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
390 >        assertTrue(p.isShutdown());
391 >        joinPool(p);
392      }
393  
394 <        
394 >
395      /**
396 <     *  isTerminated is false before termination, true after
396 >     * isTerminated is false before termination, true after
397       */
398 <    public void testIsTerminated() {
399 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
400 <        assertFalse(p1.isTerminated());
401 <        try {
402 <            p1.execute(new MediumRunnable());
403 <        } finally {
404 <            p1.shutdown();
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 >                    assertFalse(p.isTerminated());
410 >                    threadStarted.countDown();
411 >                    done.await();
412 >                }});
413 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
414 >            assertFalse(p.isTerminating());
415 >            done.countDown();
416 >        } finally {
417 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
418 >        }
419 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
420 >        assertTrue(p.isTerminated());
421      }
422  
423      /**
424 <     *  isTerminating is not true when running or when terminated
425 <     */
426 <    public void testIsTerminating() {
427 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
428 <        assertFalse(p1.isTerminating());
429 <        try {
430 <            p1.execute(new SmallRunnable());
431 <            assertFalse(p1.isTerminating());
432 <        } finally {
433 <            p1.shutdown();
434 <        }
435 <        try {
436 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
437 <            assertTrue(p1.isTerminated());
438 <            assertFalse(p1.isTerminating());
439 <        } catch(Exception e){
440 <            unexpectedException();
441 <        }      
424 >     * isTerminating is not true when running or when terminated
425 >     */
426 >    public void testIsTerminating() throws InterruptedException {
427 >        final ThreadPoolExecutor p =
428 >            new ThreadPoolExecutor(1, 1,
429 >                                   LONG_DELAY_MS, MILLISECONDS,
430 >                                   new ArrayBlockingQueue<Runnable>(10));
431 >        final CountDownLatch threadStarted = new CountDownLatch(1);
432 >        final CountDownLatch done = new CountDownLatch(1);
433 >        try {
434 >            assertFalse(p.isTerminating());
435 >            p.execute(new CheckedRunnable() {
436 >                public void realRun() throws InterruptedException {
437 >                    assertFalse(p.isTerminating());
438 >                    threadStarted.countDown();
439 >                    done.await();
440 >                }});
441 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
442 >            assertFalse(p.isTerminating());
443 >            done.countDown();
444 >        } finally {
445 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
446 >        }
447 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
448 >        assertTrue(p.isTerminated());
449 >        assertFalse(p.isTerminating());
450      }
451  
452      /**
453       * getQueue returns the work queue, which contains queued tasks
454       */
455 <    public void testGetQueue() {
456 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
457 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
458 <        FutureTask[] tasks = new FutureTask[5];
459 <        for(int i = 0; i < 5; i++){
460 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
461 <            p1.execute(tasks[i]);
462 <        }
463 <        try {
464 <            Thread.sleep(SHORT_DELAY_MS);
465 <            BlockingQueue<Runnable> wq = p1.getQueue();
466 <            assertSame(q, wq);
467 <            assertFalse(wq.contains(tasks[0]));
468 <            assertTrue(wq.contains(tasks[4]));
469 <            p1.shutdownNow();
470 <        } catch(Exception e) {
471 <            unexpectedException();
455 >    public void testGetQueue() throws InterruptedException {
456 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
457 >        final ThreadPoolExecutor p =
458 >            new ThreadPoolExecutor(1, 1,
459 >                                   LONG_DELAY_MS, MILLISECONDS,
460 >                                   q);
461 >        final CountDownLatch threadStarted = new CountDownLatch(1);
462 >        final CountDownLatch done = new CountDownLatch(1);
463 >        try {
464 >            FutureTask[] tasks = new FutureTask[5];
465 >            for (int i = 0; i < tasks.length; i++) {
466 >                Callable task = new CheckedCallable<Boolean>() {
467 >                    public Boolean realCall() throws InterruptedException {
468 >                        threadStarted.countDown();
469 >                        assertSame(q, p.getQueue());
470 >                        done.await();
471 >                        return Boolean.TRUE;
472 >                    }};
473 >                tasks[i] = new FutureTask(task);
474 >                p.execute(tasks[i]);
475 >            }
476 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
477 >            assertSame(q, p.getQueue());
478 >            assertFalse(q.contains(tasks[0]));
479 >            assertTrue(q.contains(tasks[tasks.length - 1]));
480 >            assertEquals(tasks.length - 1, q.size());
481          } finally {
482 <            joinPool(p1);
482 >            done.countDown();
483 >            joinPool(p);
484          }
485      }
486  
487      /**
488       * remove(task) removes queued task, and fails to remove active task
489       */
490 <    public void testRemove() {
490 >    public void testRemove() throws InterruptedException {
491          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
492 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
493 <        FutureTask[] tasks = new FutureTask[5];
494 <        for(int i = 0; i < 5; i++){
495 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
496 <            p1.execute(tasks[i]);
497 <        }
498 <        try {
499 <            Thread.sleep(SHORT_DELAY_MS);
500 <            assertFalse(p1.remove(tasks[0]));
492 >        final ThreadPoolExecutor p =
493 >            new ThreadPoolExecutor(1, 1,
494 >                                   LONG_DELAY_MS, MILLISECONDS,
495 >                                   q);
496 >        Runnable[] tasks = new Runnable[5];
497 >        final CountDownLatch threadStarted = new CountDownLatch(1);
498 >        final CountDownLatch done = new CountDownLatch(1);
499 >        try {
500 >            for (int i = 0; i < tasks.length; i++) {
501 >                tasks[i] = new CheckedRunnable() {
502 >                    public void realRun() throws InterruptedException {
503 >                        threadStarted.countDown();
504 >                        done.await();
505 >                    }};
506 >                p.execute(tasks[i]);
507 >            }
508 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
509 >            assertFalse(p.remove(tasks[0]));
510              assertTrue(q.contains(tasks[4]));
511              assertTrue(q.contains(tasks[3]));
512 <            assertTrue(p1.remove(tasks[4]));
513 <            assertFalse(p1.remove(tasks[4]));
512 >            assertTrue(p.remove(tasks[4]));
513 >            assertFalse(p.remove(tasks[4]));
514              assertFalse(q.contains(tasks[4]));
515              assertTrue(q.contains(tasks[3]));
516 <            assertTrue(p1.remove(tasks[3]));
516 >            assertTrue(p.remove(tasks[3]));
517              assertFalse(q.contains(tasks[3]));
376            p1.shutdownNow();
377        } catch(Exception e) {
378            unexpectedException();
518          } finally {
519 <            joinPool(p1);
519 >            done.countDown();
520 >            joinPool(p);
521          }
522      }
523  
524      /**
525 <     *   purge removes cancelled tasks from the queue
525 >     * purge removes cancelled tasks from the queue
526       */
527 <    public void testPurge() {
528 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
527 >    public void testPurge() throws InterruptedException {
528 >        final CountDownLatch threadStarted = new CountDownLatch(1);
529 >        final CountDownLatch done = new CountDownLatch(1);
530 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
531 >        final ThreadPoolExecutor p =
532 >            new ThreadPoolExecutor(1, 1,
533 >                                   LONG_DELAY_MS, MILLISECONDS,
534 >                                   q);
535          FutureTask[] tasks = new FutureTask[5];
536 <        for(int i = 0; i < 5; i++){
537 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
538 <            p1.execute(tasks[i]);
536 >        try {
537 >            for (int i = 0; i < tasks.length; i++) {
538 >                Callable task = new CheckedCallable<Boolean>() {
539 >                    public Boolean realCall() throws InterruptedException {
540 >                        threadStarted.countDown();
541 >                        done.await();
542 >                        return Boolean.TRUE;
543 >                    }};
544 >                tasks[i] = new FutureTask(task);
545 >                p.execute(tasks[i]);
546 >            }
547 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
548 >            assertEquals(tasks.length, p.getTaskCount());
549 >            assertEquals(tasks.length - 1, q.size());
550 >            assertEquals(1L, p.getActiveCount());
551 >            assertEquals(0L, p.getCompletedTaskCount());
552 >            tasks[4].cancel(true);
553 >            tasks[3].cancel(false);
554 >            p.purge();
555 >            assertEquals(tasks.length - 3, q.size());
556 >            assertEquals(tasks.length - 2, p.getTaskCount());
557 >            p.purge();         // Nothing to do
558 >            assertEquals(tasks.length - 3, q.size());
559 >            assertEquals(tasks.length - 2, p.getTaskCount());
560 >        } finally {
561 >            done.countDown();
562 >            joinPool(p);
563          }
394        tasks[4].cancel(true);
395        tasks[3].cancel(true);
396        p1.purge();
397        long count = p1.getTaskCount();
398        assertTrue(count >= 2 && count < 5);
399        p1.shutdownNow();
400        joinPool(p1);
564      }
565  
566      /**
567 <     *  shutDownNow returns a list containing tasks that were not run
567 >     * shutDownNow returns a list containing tasks that were not run
568       */
569      public void testShutDownNow() {
570 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
570 >        final ThreadPoolExecutor p =
571 >            new ThreadPoolExecutor(1, 1,
572 >                                   LONG_DELAY_MS, MILLISECONDS,
573 >                                   new ArrayBlockingQueue<Runnable>(10));
574          List l;
575          try {
576 <            for(int i = 0; i < 5; i++)
577 <                p1.execute(new MediumPossiblyInterruptedRunnable());
576 >            for (int i = 0; i < 5; i++)
577 >                p.execute(new MediumPossiblyInterruptedRunnable());
578          }
579          finally {
580 <            l = p1.shutdownNow();
580 >            try {
581 >                l = p.shutdownNow();
582 >            } catch (SecurityException ok) { return; }
583          }
584 <        assertTrue(p1.isShutdown());
585 <        assertTrue(l.size() <= 4);
584 >        assertTrue(p.isShutdown());
585 >        assertTrue(l.size() <= 4);
586      }
587  
588      // Exception Tests
421    
589  
590 <    /**
591 <     * Constructor throws if corePoolSize argument is less than zero
590 >
591 >    /**
592 >     * Constructor throws if corePoolSize argument is less than zero
593       */
594      public void testConstructor1() {
595          try {
596 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
596 >            new ThreadPoolExecutor(-1, 1,
597 >                                   LONG_DELAY_MS, MILLISECONDS,
598 >                                   new ArrayBlockingQueue<Runnable>(10));
599              shouldThrow();
600 <        }
431 <        catch (IllegalArgumentException success){}
600 >        } catch (IllegalArgumentException success) {}
601      }
602 <    
603 <    /**
604 <     * Constructor throws if maximumPoolSize is less than zero
602 >
603 >    /**
604 >     * Constructor throws if maximumPoolSize is less than zero
605       */
606      public void testConstructor2() {
607          try {
608 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
608 >            new ThreadPoolExecutor(1, -1,
609 >                                   LONG_DELAY_MS, MILLISECONDS,
610 >                                   new ArrayBlockingQueue<Runnable>(10));
611              shouldThrow();
612 <        }
442 <        catch (IllegalArgumentException success){}
612 >        } catch (IllegalArgumentException success) {}
613      }
614 <    
615 <    /**
616 <     * Constructor throws if maximumPoolSize is equal to zero
614 >
615 >    /**
616 >     * Constructor throws if maximumPoolSize is equal to zero
617       */
618      public void testConstructor3() {
619          try {
620 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
620 >            new ThreadPoolExecutor(1, 0,
621 >                                   LONG_DELAY_MS, MILLISECONDS,
622 >                                   new ArrayBlockingQueue<Runnable>(10));
623              shouldThrow();
624 <        }
453 <        catch (IllegalArgumentException success){}
624 >        } catch (IllegalArgumentException success) {}
625      }
626  
627 <    /**
628 <     * Constructor throws if keepAliveTime is less than zero
627 >    /**
628 >     * Constructor throws if keepAliveTime is less than zero
629       */
630      public void testConstructor4() {
631          try {
632 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
632 >            new ThreadPoolExecutor(1, 2,
633 >                                   -1L, MILLISECONDS,
634 >                                   new ArrayBlockingQueue<Runnable>(10));
635              shouldThrow();
636 <        }
464 <        catch (IllegalArgumentException success){}
636 >        } catch (IllegalArgumentException success) {}
637      }
638  
639 <    /**
640 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
639 >    /**
640 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
641       */
642      public void testConstructor5() {
643          try {
644 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
644 >            new ThreadPoolExecutor(2, 1,
645 >                                   LONG_DELAY_MS, MILLISECONDS,
646 >                                   new ArrayBlockingQueue<Runnable>(10));
647              shouldThrow();
648 <        }
475 <        catch (IllegalArgumentException success){}
648 >        } catch (IllegalArgumentException success) {}
649      }
650 <        
651 <    /**
652 <     * Constructor throws if workQueue is set to null
650 >
651 >    /**
652 >     * Constructor throws if workQueue is set to null
653       */
654      public void testConstructorNullPointerException() {
655          try {
656 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
656 >            new ThreadPoolExecutor(1, 2,
657 >                                   LONG_DELAY_MS, MILLISECONDS,
658 >                                   (BlockingQueue) null);
659              shouldThrow();
660 <        }
486 <        catch (NullPointerException success){}  
660 >        } catch (NullPointerException success) {}
661      }
488    
662  
663 <    
664 <    /**
665 <     * Constructor throws if corePoolSize argument is less than zero
663 >
664 >
665 >    /**
666 >     * Constructor throws if corePoolSize argument is less than zero
667       */
668      public void testConstructor6() {
669          try {
670 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
670 >            new ThreadPoolExecutor(-1, 1,
671 >                                   LONG_DELAY_MS, MILLISECONDS,
672 >                                   new ArrayBlockingQueue<Runnable>(10),
673 >                                   new SimpleThreadFactory());
674              shouldThrow();
675 <        } catch (IllegalArgumentException success){}
675 >        } catch (IllegalArgumentException success) {}
676      }
677 <    
678 <    /**
679 <     * Constructor throws if maximumPoolSize is less than zero
677 >
678 >    /**
679 >     * Constructor throws if maximumPoolSize is less than zero
680       */
681      public void testConstructor7() {
682          try {
683 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
683 >            new ThreadPoolExecutor(1, -1,
684 >                                   LONG_DELAY_MS, MILLISECONDS,
685 >                                   new ArrayBlockingQueue<Runnable>(10),
686 >                                   new SimpleThreadFactory());
687              shouldThrow();
688 <        }
509 <        catch (IllegalArgumentException success){}
688 >        } catch (IllegalArgumentException success) {}
689      }
690  
691 <    /**
692 <     * Constructor throws if maximumPoolSize is equal to zero
691 >    /**
692 >     * Constructor throws if maximumPoolSize is equal to zero
693       */
694      public void testConstructor8() {
695          try {
696 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
696 >            new ThreadPoolExecutor(1, 0,
697 >                                   LONG_DELAY_MS, MILLISECONDS,
698 >                                   new ArrayBlockingQueue<Runnable>(10),
699 >                                   new SimpleThreadFactory());
700              shouldThrow();
701 <        }
520 <        catch (IllegalArgumentException success){}
701 >        } catch (IllegalArgumentException success) {}
702      }
703  
704 <    /**
705 <     * Constructor throws if keepAliveTime is less than zero
704 >    /**
705 >     * Constructor throws if keepAliveTime is less than zero
706       */
707      public void testConstructor9() {
708          try {
709 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
709 >            new ThreadPoolExecutor(1, 2,
710 >                                   -1L, MILLISECONDS,
711 >                                   new ArrayBlockingQueue<Runnable>(10),
712 >                                   new SimpleThreadFactory());
713              shouldThrow();
714 <        }
531 <        catch (IllegalArgumentException success){}
714 >        } catch (IllegalArgumentException success) {}
715      }
716  
717 <    /**
718 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
717 >    /**
718 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
719       */
720      public void testConstructor10() {
721          try {
722 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
722 >            new ThreadPoolExecutor(2, 1,
723 >                                   LONG_DELAY_MS, MILLISECONDS,
724 >                                   new ArrayBlockingQueue<Runnable>(10),
725 >                                   new SimpleThreadFactory());
726              shouldThrow();
727 <        }
542 <        catch (IllegalArgumentException success){}
727 >        } catch (IllegalArgumentException success) {}
728      }
729  
730 <    /**
731 <     * Constructor throws if workQueue is set to null
730 >    /**
731 >     * Constructor throws if workQueue is set to null
732       */
733      public void testConstructorNullPointerException2() {
734          try {
735 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
735 >            new ThreadPoolExecutor(1, 2,
736 >                                   LONG_DELAY_MS, MILLISECONDS,
737 >                                   (BlockingQueue) null,
738 >                                   new SimpleThreadFactory());
739              shouldThrow();
740 <        }
553 <        catch (NullPointerException success){}  
740 >        } catch (NullPointerException success) {}
741      }
742  
743 <    /**
744 <     * Constructor throws if threadFactory is set to null
743 >    /**
744 >     * Constructor throws if threadFactory is set to null
745       */
746      public void testConstructorNullPointerException3() {
747          try {
748 <            ThreadFactory f = null;
749 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
748 >            new ThreadPoolExecutor(1, 2,
749 >                                   LONG_DELAY_MS, MILLISECONDS,
750 >                                   new ArrayBlockingQueue<Runnable>(10),
751 >                                   (ThreadFactory) null);
752              shouldThrow();
753 <        }
565 <        catch (NullPointerException success){}  
753 >        } catch (NullPointerException success) {}
754      }
755 <
756 <    
757 <    /**
758 <     * Constructor throws if corePoolSize argument is less than zero
755 >
756 >
757 >    /**
758 >     * Constructor throws if corePoolSize argument is less than zero
759       */
760      public void testConstructor11() {
761          try {
762 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
762 >            new ThreadPoolExecutor(-1, 1,
763 >                                   LONG_DELAY_MS, MILLISECONDS,
764 >                                   new ArrayBlockingQueue<Runnable>(10),
765 >                                   new NoOpREHandler());
766              shouldThrow();
767 <        }
577 <        catch (IllegalArgumentException success){}
767 >        } catch (IllegalArgumentException success) {}
768      }
769  
770 <    /**
771 <     * Constructor throws if maximumPoolSize is less than zero
770 >    /**
771 >     * Constructor throws if maximumPoolSize is less than zero
772       */
773      public void testConstructor12() {
774          try {
775 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
775 >            new ThreadPoolExecutor(1, -1,
776 >                                   LONG_DELAY_MS, MILLISECONDS,
777 >                                   new ArrayBlockingQueue<Runnable>(10),
778 >                                   new NoOpREHandler());
779              shouldThrow();
780 <        }
588 <        catch (IllegalArgumentException success){}
780 >        } catch (IllegalArgumentException success) {}
781      }
782  
783 <    /**
784 <     * Constructor throws if maximumPoolSize is equal to zero
783 >    /**
784 >     * Constructor throws if maximumPoolSize is equal to zero
785       */
786      public void testConstructor13() {
787          try {
788 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
788 >            new ThreadPoolExecutor(1, 0,
789 >                                   LONG_DELAY_MS, MILLISECONDS,
790 >                                   new ArrayBlockingQueue<Runnable>(10),
791 >                                   new NoOpREHandler());
792              shouldThrow();
793 <        }
599 <        catch (IllegalArgumentException success){}
793 >        } catch (IllegalArgumentException success) {}
794      }
795  
796 <    /**
797 <     * Constructor throws if keepAliveTime is less than zero
796 >    /**
797 >     * Constructor throws if keepAliveTime is less than zero
798       */
799      public void testConstructor14() {
800          try {
801 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
801 >            new ThreadPoolExecutor(1, 2,
802 >                                   -1L, MILLISECONDS,
803 >                                   new ArrayBlockingQueue<Runnable>(10),
804 >                                   new NoOpREHandler());
805              shouldThrow();
806 <        }
610 <        catch (IllegalArgumentException success){}
806 >        } catch (IllegalArgumentException success) {}
807      }
808  
809 <    /**
810 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
809 >    /**
810 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
811       */
812      public void testConstructor15() {
813          try {
814 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
814 >            new ThreadPoolExecutor(2, 1,
815 >                                   LONG_DELAY_MS, MILLISECONDS,
816 >                                   new ArrayBlockingQueue<Runnable>(10),
817 >                                   new NoOpREHandler());
818              shouldThrow();
819 <        }
621 <        catch (IllegalArgumentException success){}
819 >        } catch (IllegalArgumentException success) {}
820      }
821  
822 <    /**
823 <     * Constructor throws if workQueue is set to null
822 >    /**
823 >     * Constructor throws if workQueue is set to null
824       */
825      public void testConstructorNullPointerException4() {
826          try {
827 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
827 >            new ThreadPoolExecutor(1, 2,
828 >                                   LONG_DELAY_MS, MILLISECONDS,
829 >                                   (BlockingQueue) null,
830 >                                   new NoOpREHandler());
831              shouldThrow();
832 <        }
632 <        catch (NullPointerException success){}  
832 >        } catch (NullPointerException success) {}
833      }
834  
835 <    /**
836 <     * Constructor throws if handler is set to null
835 >    /**
836 >     * Constructor throws if handler is set to null
837       */
838      public void testConstructorNullPointerException5() {
839          try {
840 <            RejectedExecutionHandler r = null;
841 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
840 >            new ThreadPoolExecutor(1, 2,
841 >                                   LONG_DELAY_MS, MILLISECONDS,
842 >                                   new ArrayBlockingQueue<Runnable>(10),
843 >                                   (RejectedExecutionHandler) null);
844              shouldThrow();
845 <        }
644 <        catch (NullPointerException success){}  
845 >        } catch (NullPointerException success) {}
846      }
847  
848 <    
849 <    /**
850 <     * Constructor throws if corePoolSize argument is less than zero
848 >
849 >    /**
850 >     * Constructor throws if corePoolSize argument is less than zero
851       */
852      public void testConstructor16() {
853          try {
854 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
854 >            new ThreadPoolExecutor(-1, 1,
855 >                                   LONG_DELAY_MS, MILLISECONDS,
856 >                                   new ArrayBlockingQueue<Runnable>(10),
857 >                                   new SimpleThreadFactory(),
858 >                                   new NoOpREHandler());
859              shouldThrow();
860 <        }
656 <        catch (IllegalArgumentException success){}
860 >        } catch (IllegalArgumentException success) {}
861      }
862  
863 <    /**
864 <     * Constructor throws if maximumPoolSize is less than zero
863 >    /**
864 >     * Constructor throws if maximumPoolSize is less than zero
865       */
866      public void testConstructor17() {
867          try {
868 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
868 >            new ThreadPoolExecutor(1, -1,
869 >                                   LONG_DELAY_MS, MILLISECONDS,
870 >                                   new ArrayBlockingQueue<Runnable>(10),
871 >                                   new SimpleThreadFactory(),
872 >                                   new NoOpREHandler());
873              shouldThrow();
874 <        }
667 <        catch (IllegalArgumentException success){}
874 >        } catch (IllegalArgumentException success) {}
875      }
876  
877 <    /**
878 <     * Constructor throws if maximumPoolSize is equal to zero
877 >    /**
878 >     * Constructor throws if maximumPoolSize is equal to zero
879       */
880      public void testConstructor18() {
881          try {
882 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
882 >            new ThreadPoolExecutor(1, 0,
883 >                                   LONG_DELAY_MS, MILLISECONDS,
884 >                                   new ArrayBlockingQueue<Runnable>(10),
885 >                                   new SimpleThreadFactory(),
886 >                                   new NoOpREHandler());
887              shouldThrow();
888 <        }
678 <        catch (IllegalArgumentException success){}
888 >        } catch (IllegalArgumentException success) {}
889      }
890  
891 <    /**
892 <     * Constructor throws if keepAliveTime is less than zero
891 >    /**
892 >     * Constructor throws if keepAliveTime is less than zero
893       */
894      public void testConstructor19() {
895          try {
896 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
896 >            new ThreadPoolExecutor(1, 2,
897 >                                   -1L, MILLISECONDS,
898 >                                   new ArrayBlockingQueue<Runnable>(10),
899 >                                   new SimpleThreadFactory(),
900 >                                   new NoOpREHandler());
901              shouldThrow();
902 <        }
689 <        catch (IllegalArgumentException success){}
902 >        } catch (IllegalArgumentException success) {}
903      }
904  
905 <    /**
906 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
905 >    /**
906 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
907       */
908      public void testConstructor20() {
909          try {
910 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
910 >            new ThreadPoolExecutor(2, 1,
911 >                                   LONG_DELAY_MS, MILLISECONDS,
912 >                                   new ArrayBlockingQueue<Runnable>(10),
913 >                                   new SimpleThreadFactory(),
914 >                                   new NoOpREHandler());
915              shouldThrow();
916 <        }
700 <        catch (IllegalArgumentException success){}
916 >        } catch (IllegalArgumentException success) {}
917      }
918  
919 <    /**
920 <     * Constructor throws if workQueue is set to null
919 >    /**
920 >     * Constructor throws if workQueue is null
921       */
922      public void testConstructorNullPointerException6() {
923          try {
924 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
924 >            new ThreadPoolExecutor(1, 2,
925 >                                   LONG_DELAY_MS, MILLISECONDS,
926 >                                   (BlockingQueue) null,
927 >                                   new SimpleThreadFactory(),
928 >                                   new NoOpREHandler());
929              shouldThrow();
930 <        }
711 <        catch (NullPointerException success){}  
930 >        } catch (NullPointerException success) {}
931      }
932  
933 <    /**
934 <     * Constructor throws if handler is set to null
933 >    /**
934 >     * Constructor throws if handler is null
935       */
936      public void testConstructorNullPointerException7() {
937          try {
938 <            RejectedExecutionHandler r = null;
939 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
938 >            new ThreadPoolExecutor(1, 2,
939 >                                   LONG_DELAY_MS, MILLISECONDS,
940 >                                   new ArrayBlockingQueue<Runnable>(10),
941 >                                   new SimpleThreadFactory(),
942 >                                   (RejectedExecutionHandler) null);
943              shouldThrow();
944 <        }
723 <        catch (NullPointerException success){}  
944 >        } catch (NullPointerException success) {}
945      }
946  
947 <    /**
948 <     * Constructor throws if ThreadFactory is set top null
947 >    /**
948 >     * Constructor throws if ThreadFactory is null
949       */
950      public void testConstructorNullPointerException8() {
951          try {
952 <            ThreadFactory f = null;
953 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
954 <            shouldThrow();
952 >            new ThreadPoolExecutor(1, 2,
953 >                                   LONG_DELAY_MS, MILLISECONDS,
954 >                                   new ArrayBlockingQueue<Runnable>(10),
955 >                                   (ThreadFactory) null,
956 >                                   new NoOpREHandler());
957 >            shouldThrow();
958 >        } catch (NullPointerException success) {}
959 >    }
960 >
961 >    /**
962 >     * get of submitted callable throws InterruptedException if interrupted
963 >     */
964 >    public void testInterruptedSubmit() throws InterruptedException {
965 >        final ThreadPoolExecutor p =
966 >            new ThreadPoolExecutor(1, 1,
967 >                                   60, TimeUnit.SECONDS,
968 >                                   new ArrayBlockingQueue<Runnable>(10));
969 >
970 >        final CountDownLatch threadStarted = new CountDownLatch(1);
971 >        final CountDownLatch done = new CountDownLatch(1);
972 >        try {
973 >            Thread t = newStartedThread(new CheckedInterruptedRunnable() {
974 >                public void realRun() throws Exception {
975 >                    Callable task = new CheckedCallable<Boolean>() {
976 >                        public Boolean realCall() throws InterruptedException {
977 >                            threadStarted.countDown();
978 >                            done.await();
979 >                            return Boolean.TRUE;
980 >                        }};
981 >                    p.submit(task).get();
982 >                }});
983 >
984 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
985 >            t.interrupt();
986 >            awaitTermination(t, MEDIUM_DELAY_MS);
987 >        } finally {
988 >            done.countDown();
989 >            joinPool(p);
990          }
735        catch (NullPointerException successdn8){}  
991      }
737    
992  
993      /**
994 <     *  execute throws RejectedExecutionException
741 <     *  if saturated.
994 >     * execute throws RejectedExecutionException if saturated.
995       */
996      public void testSaturatedExecute() {
997 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
998 <        try {
999 <            
1000 <            for(int i = 0; i < 5; ++i){
1001 <                p.execute(new MediumRunnable());
997 >        ThreadPoolExecutor p =
998 >            new ThreadPoolExecutor(1, 1,
999 >                                   LONG_DELAY_MS, MILLISECONDS,
1000 >                                   new ArrayBlockingQueue<Runnable>(1));
1001 >        final CountDownLatch done = new CountDownLatch(1);
1002 >        try {
1003 >            Runnable task = new CheckedRunnable() {
1004 >                public void realRun() throws InterruptedException {
1005 >                    done.await();
1006 >                }};
1007 >            for (int i = 0; i < 2; ++i)
1008 >                p.execute(task);
1009 >            for (int i = 0; i < 2; ++i) {
1010 >                try {
1011 >                    p.execute(task);
1012 >                    shouldThrow();
1013 >                } catch (RejectedExecutionException success) {}
1014 >                assertTrue(p.getTaskCount() <= 2);
1015              }
1016 <            shouldThrow();
1017 <        } catch(RejectedExecutionException success){}
1018 <        joinPool(p);
1016 >        } finally {
1017 >            done.countDown();
1018 >            joinPool(p);
1019 >        }
1020 >    }
1021 >
1022 >    /**
1023 >     * submit(runnable) throws RejectedExecutionException if saturated.
1024 >     */
1025 >    public void testSaturatedSubmitRunnable() {
1026 >        ThreadPoolExecutor p =
1027 >            new ThreadPoolExecutor(1, 1,
1028 >                                   LONG_DELAY_MS, MILLISECONDS,
1029 >                                   new ArrayBlockingQueue<Runnable>(1));
1030 >        final CountDownLatch done = new CountDownLatch(1);
1031 >        try {
1032 >            Runnable task = new CheckedRunnable() {
1033 >                public void realRun() throws InterruptedException {
1034 >                    done.await();
1035 >                }};
1036 >            for (int i = 0; i < 2; ++i)
1037 >                p.submit(task);
1038 >            for (int i = 0; i < 2; ++i) {
1039 >                try {
1040 >                    p.execute(task);
1041 >                    shouldThrow();
1042 >                } catch (RejectedExecutionException success) {}
1043 >                assertTrue(p.getTaskCount() <= 2);
1044 >            }
1045 >        } finally {
1046 >            done.countDown();
1047 >            joinPool(p);
1048 >        }
1049 >    }
1050 >
1051 >    /**
1052 >     * submit(callable) throws RejectedExecutionException if saturated.
1053 >     */
1054 >    public void testSaturatedSubmitCallable() {
1055 >        ThreadPoolExecutor p =
1056 >            new ThreadPoolExecutor(1, 1,
1057 >                                   LONG_DELAY_MS, MILLISECONDS,
1058 >                                   new ArrayBlockingQueue<Runnable>(1));
1059 >        final CountDownLatch done = new CountDownLatch(1);
1060 >        try {
1061 >            Runnable task = new CheckedRunnable() {
1062 >                public void realRun() throws InterruptedException {
1063 >                    done.await();
1064 >                }};
1065 >            for (int i = 0; i < 2; ++i)
1066 >                p.submit(Executors.callable(task));
1067 >            for (int i = 0; i < 2; ++i) {
1068 >                try {
1069 >                    p.execute(task);
1070 >                    shouldThrow();
1071 >                } catch (RejectedExecutionException success) {}
1072 >                assertTrue(p.getTaskCount() <= 2);
1073 >            }
1074 >        } finally {
1075 >            done.countDown();
1076 >            joinPool(p);
1077 >        }
1078      }
1079  
1080      /**
1081 <     *  executor using CallerRunsPolicy runs task if saturated.
1081 >     * executor using CallerRunsPolicy runs task if saturated.
1082       */
1083      public void testSaturatedExecute2() {
1084          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1085 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1085 >        final ThreadPoolExecutor p =
1086 >            new ThreadPoolExecutor(1, 1,
1087 >                                   LONG_DELAY_MS,
1088 >                                   MILLISECONDS,
1089 >                                   new ArrayBlockingQueue<Runnable>(1),
1090 >                                   h);
1091          try {
762            
1092              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1093 <            for(int i = 0; i < 5; ++i){
1093 >            for (int i = 0; i < tasks.length; ++i)
1094                  tasks[i] = new TrackedNoOpRunnable();
766            }
1095              TrackedLongRunnable mr = new TrackedLongRunnable();
1096              p.execute(mr);
1097 <            for(int i = 0; i < 5; ++i){
1097 >            for (int i = 0; i < tasks.length; ++i)
1098                  p.execute(tasks[i]);
1099 <            }
772 <            for(int i = 1; i < 5; ++i) {
1099 >            for (int i = 1; i < tasks.length; ++i)
1100                  assertTrue(tasks[i].done);
1101 <            }
775 <            p.shutdownNow();
776 <        } catch(RejectedExecutionException ex){
777 <            unexpectedException();
1101 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1102          } finally {
1103              joinPool(p);
1104          }
1105      }
1106  
1107      /**
1108 <     *  executor using DiscardPolicy drops task if saturated.
1108 >     * executor using DiscardPolicy drops task if saturated.
1109       */
1110      public void testSaturatedExecute3() {
1111          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1112 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1112 >        final ThreadPoolExecutor p =
1113 >            new ThreadPoolExecutor(1, 1,
1114 >                                   LONG_DELAY_MS, MILLISECONDS,
1115 >                                   new ArrayBlockingQueue<Runnable>(1),
1116 >                                   h);
1117          try {
790            
1118              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1119 <            for(int i = 0; i < 5; ++i){
1119 >            for (int i = 0; i < tasks.length; ++i)
1120                  tasks[i] = new TrackedNoOpRunnable();
794            }
1121              p.execute(new TrackedLongRunnable());
1122 <            for(int i = 0; i < 5; ++i){
1123 <                p.execute(tasks[i]);
1124 <            }
1125 <            for(int i = 0; i < 5; ++i){
1126 <                assertFalse(tasks[i].done);
801 <            }
802 <            p.shutdownNow();
803 <        } catch(RejectedExecutionException ex){
804 <            unexpectedException();
1122 >            for (TrackedNoOpRunnable task : tasks)
1123 >                p.execute(task);
1124 >            for (TrackedNoOpRunnable task : tasks)
1125 >                assertFalse(task.done);
1126 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1127          } finally {
1128              joinPool(p);
1129          }
1130      }
1131  
1132      /**
1133 <     *  executor using DiscardOldestPolicy drops oldest task if saturated.
1133 >     * executor using DiscardOldestPolicy drops oldest task if saturated.
1134       */
1135      public void testSaturatedExecute4() {
1136          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1137 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1137 >        final ThreadPoolExecutor p =
1138 >            new ThreadPoolExecutor(1, 1,
1139 >                                   LONG_DELAY_MS, MILLISECONDS,
1140 >                                   new ArrayBlockingQueue<Runnable>(1),
1141 >                                   h);
1142          try {
1143              p.execute(new TrackedLongRunnable());
1144              TrackedLongRunnable r2 = new TrackedLongRunnable();
# Line 822 | Line 1148 | public class ThreadPoolExecutorTest exte
1148              p.execute(r3);
1149              assertFalse(p.getQueue().contains(r2));
1150              assertTrue(p.getQueue().contains(r3));
1151 <            p.shutdownNow();
826 <        } catch(RejectedExecutionException ex){
827 <            unexpectedException();
1151 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1152          } finally {
1153              joinPool(p);
1154          }
1155      }
1156  
1157      /**
1158 <     *  execute throws RejectedExecutionException if shutdown
1158 >     * execute throws RejectedExecutionException if shutdown
1159       */
1160      public void testRejectedExecutionExceptionOnShutdown() {
1161 <        ThreadPoolExecutor tpe =
1162 <            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1163 <        tpe.shutdown();
1164 <        try {
1165 <            tpe.execute(new NoOpRunnable());
1166 <            shouldThrow();
1167 <        } catch(RejectedExecutionException success){}
1168 <        
1169 <        joinPool(tpe);
1161 >        ThreadPoolExecutor p =
1162 >            new ThreadPoolExecutor(1, 1,
1163 >                                   LONG_DELAY_MS, MILLISECONDS,
1164 >                                   new ArrayBlockingQueue<Runnable>(1));
1165 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1166 >        try {
1167 >            p.execute(new NoOpRunnable());
1168 >            shouldThrow();
1169 >        } catch (RejectedExecutionException success) {}
1170 >
1171 >        joinPool(p);
1172      }
1173  
1174      /**
1175 <     *  execute using CallerRunsPolicy drops task on shutdown
1175 >     * execute using CallerRunsPolicy drops task on shutdown
1176       */
1177      public void testCallerRunsOnShutdown() {
1178          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1179 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1179 >        final ThreadPoolExecutor p =
1180 >            new ThreadPoolExecutor(1, 1,
1181 >                                   LONG_DELAY_MS, MILLISECONDS,
1182 >                                   new ArrayBlockingQueue<Runnable>(1), h);
1183  
1184 <        p.shutdown();
1185 <        try {
1184 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1185 >        try {
1186              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1187 <            p.execute(r);
1187 >            p.execute(r);
1188              assertFalse(r.done);
860        } catch(RejectedExecutionException success){
861            unexpectedException();
1189          } finally {
1190              joinPool(p);
1191          }
1192      }
1193  
1194      /**
1195 <     *  execute using DiscardPolicy drops task on shutdown
1195 >     * execute using DiscardPolicy drops task on shutdown
1196       */
1197      public void testDiscardOnShutdown() {
1198          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1199 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1199 >        ThreadPoolExecutor p =
1200 >            new ThreadPoolExecutor(1, 1,
1201 >                                   LONG_DELAY_MS, MILLISECONDS,
1202 >                                   new ArrayBlockingQueue<Runnable>(1),
1203 >                                   h);
1204  
1205 <        p.shutdown();
1206 <        try {
1205 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1206 >        try {
1207              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1208 <            p.execute(r);
1208 >            p.execute(r);
1209              assertFalse(r.done);
879        } catch(RejectedExecutionException success){
880            unexpectedException();
1210          } finally {
1211              joinPool(p);
1212          }
# Line 885 | Line 1214 | public class ThreadPoolExecutorTest exte
1214  
1215  
1216      /**
1217 <     *  execute using DiscardOldestPolicy drops task on shutdown
1217 >     * execute using DiscardOldestPolicy drops task on shutdown
1218       */
1219      public void testDiscardOldestOnShutdown() {
1220          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1221 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1221 >        ThreadPoolExecutor p =
1222 >            new ThreadPoolExecutor(1, 1,
1223 >                                   LONG_DELAY_MS, MILLISECONDS,
1224 >                                   new ArrayBlockingQueue<Runnable>(1),
1225 >                                   h);
1226  
1227 <        p.shutdown();
1228 <        try {
1227 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1228 >        try {
1229              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1230 <            p.execute(r);
1230 >            p.execute(r);
1231              assertFalse(r.done);
899        } catch(RejectedExecutionException success){
900            unexpectedException();
1232          } finally {
1233              joinPool(p);
1234          }
# Line 905 | Line 1236 | public class ThreadPoolExecutorTest exte
1236  
1237  
1238      /**
1239 <     *  execute (null) throws NPE
1239 >     * execute(null) throws NPE
1240       */
1241      public void testExecuteNull() {
1242 <        ThreadPoolExecutor tpe = null;
1242 >        ThreadPoolExecutor p =
1243 >            new ThreadPoolExecutor(1, 2,
1244 >                                   LONG_DELAY_MS, MILLISECONDS,
1245 >                                   new ArrayBlockingQueue<Runnable>(10));
1246          try {
1247 <            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
914 <            tpe.execute(null);
1247 >            p.execute(null);
1248              shouldThrow();
1249 <        } catch(NullPointerException success){}
1250 <        
1251 <        joinPool(tpe);
1249 >        } catch (NullPointerException success) {}
1250 >
1251 >        joinPool(p);
1252      }
1253 <    
1253 >
1254      /**
1255 <     *  setCorePoolSize of negative value throws IllegalArgumentException
1255 >     * setCorePoolSize of negative value throws IllegalArgumentException
1256       */
1257      public void testCorePoolSizeIllegalArgumentException() {
1258 <        ThreadPoolExecutor tpe = null;
1259 <        try {
1260 <            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1261 <        } catch(Exception e){}
929 <        try {
930 <            tpe.setCorePoolSize(-1);
931 <            shouldThrow();
932 <        } catch(IllegalArgumentException success){
933 <        } finally {
934 <            tpe.shutdown();
935 <        }
936 <        joinPool(tpe);
937 <    }  
938 <
939 <    /**
940 <     *  setMaximumPoolSize(int) throws IllegalArgumentException if
941 <     *  given a value less the core pool size
942 <     */  
943 <    public void testMaximumPoolSizeIllegalArgumentException() {
944 <        ThreadPoolExecutor tpe = null;
1258 >        ThreadPoolExecutor p =
1259 >            new ThreadPoolExecutor(1, 2,
1260 >                                   LONG_DELAY_MS, MILLISECONDS,
1261 >                                   new ArrayBlockingQueue<Runnable>(10));
1262          try {
1263 <            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1264 <        } catch(Exception e){}
1263 >            p.setCorePoolSize(-1);
1264 >            shouldThrow();
1265 >        } catch (IllegalArgumentException success) {
1266 >        } finally {
1267 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1268 >        }
1269 >        joinPool(p);
1270 >    }
1271 >
1272 >    /**
1273 >     * setMaximumPoolSize(int) throws IllegalArgumentException if
1274 >     * given a value less the core pool size
1275 >     */
1276 >    public void testMaximumPoolSizeIllegalArgumentException() {
1277 >        ThreadPoolExecutor p =
1278 >            new ThreadPoolExecutor(2, 3,
1279 >                                   LONG_DELAY_MS, MILLISECONDS,
1280 >                                   new ArrayBlockingQueue<Runnable>(10));
1281          try {
1282 <            tpe.setMaximumPoolSize(1);
1282 >            p.setMaximumPoolSize(1);
1283              shouldThrow();
1284 <        } catch(IllegalArgumentException success){
1284 >        } catch (IllegalArgumentException success) {
1285          } finally {
1286 <            tpe.shutdown();
1286 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1287          }
1288 <        joinPool(tpe);
1288 >        joinPool(p);
1289      }
1290 <    
1290 >
1291      /**
1292 <     *  setMaximumPoolSize throws IllegalArgumentException
1293 <     *  if given a negative value
1292 >     * setMaximumPoolSize throws IllegalArgumentException
1293 >     * if given a negative value
1294       */
1295      public void testMaximumPoolSizeIllegalArgumentException2() {
1296 <        ThreadPoolExecutor tpe = null;
1297 <        try {
1298 <            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1299 <        } catch(Exception e){}
1296 >        ThreadPoolExecutor p =
1297 >            new ThreadPoolExecutor(2, 3,
1298 >                                   LONG_DELAY_MS, MILLISECONDS,
1299 >                                   new ArrayBlockingQueue<Runnable>(10));
1300          try {
1301 <            tpe.setMaximumPoolSize(-1);
1301 >            p.setMaximumPoolSize(-1);
1302              shouldThrow();
1303 <        } catch(IllegalArgumentException success){
1303 >        } catch (IllegalArgumentException success) {
1304          } finally {
1305 <            tpe.shutdown();
1305 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1306          }
1307 <        joinPool(tpe);
1307 >        joinPool(p);
1308      }
1309 <    
1309 >
1310  
1311      /**
1312 <     *  setKeepAliveTime  throws IllegalArgumentException
1313 <     *  when given a negative value
1312 >     * setKeepAliveTime throws IllegalArgumentException
1313 >     * when given a negative value
1314       */
1315      public void testKeepAliveTimeIllegalArgumentException() {
1316 <        ThreadPoolExecutor tpe = null;
1316 >        ThreadPoolExecutor p =
1317 >            new ThreadPoolExecutor(2, 3,
1318 >                                   LONG_DELAY_MS, MILLISECONDS,
1319 >                                   new ArrayBlockingQueue<Runnable>(10));
1320          try {
1321 <            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
986 <        } catch(Exception e){}
987 <        
988 <        try {
989 <            tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
1321 >            p.setKeepAliveTime(-1,MILLISECONDS);
1322              shouldThrow();
1323 <        } catch(IllegalArgumentException success){
1323 >        } catch (IllegalArgumentException success) {
1324          } finally {
1325 <            tpe.shutdown();
1325 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1326          }
1327 <        joinPool(tpe);
1327 >        joinPool(p);
1328      }
1329  
1330      /**
1331       * terminated() is called on termination
1332       */
1333      public void testTerminated() {
1334 <        ExtendedTPE tpe = new ExtendedTPE();
1335 <        tpe.shutdown();
1336 <        assertTrue(tpe.terminatedCalled);
1337 <        joinPool(tpe);
1334 >        ExtendedTPE p = new ExtendedTPE();
1335 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1336 >        assertTrue(p.terminatedCalled);
1337 >        joinPool(p);
1338      }
1339  
1340      /**
1341       * beforeExecute and afterExecute are called when executing task
1342       */
1343 <    public void testBeforeAfter() {
1344 <        ExtendedTPE tpe = new ExtendedTPE();
1343 >    public void testBeforeAfter() throws InterruptedException {
1344 >        ExtendedTPE p = new ExtendedTPE();
1345          try {
1346              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1347 <            tpe.execute(r);
1348 <            Thread.sleep(SHORT_DELAY_MS);
1347 >            p.execute(r);
1348 >            delay(SHORT_DELAY_MS);
1349              assertTrue(r.done);
1350 <            assertTrue(tpe.beforeCalled);
1351 <            assertTrue(tpe.afterCalled);
1352 <            tpe.shutdown();
1021 <        }
1022 <        catch(Exception ex) {
1023 <            unexpectedException();
1350 >            assertTrue(p.beforeCalled);
1351 >            assertTrue(p.afterCalled);
1352 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1353          } finally {
1354 <            joinPool(tpe);
1354 >            joinPool(p);
1355          }
1356      }
1357  
1358      /**
1359       * completed submit of callable returns result
1360       */
1361 <    public void testSubmitCallable() {
1362 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1361 >    public void testSubmitCallable() throws Exception {
1362 >        ExecutorService e =
1363 >            new ThreadPoolExecutor(2, 2,
1364 >                                   LONG_DELAY_MS, MILLISECONDS,
1365 >                                   new ArrayBlockingQueue<Runnable>(10));
1366          try {
1367              Future<String> future = e.submit(new StringTask());
1368              String result = future.get();
1369              assertSame(TEST_STRING, result);
1038        }
1039        catch (ExecutionException ex) {
1040            unexpectedException();
1041        }
1042        catch (InterruptedException ex) {
1043            unexpectedException();
1370          } finally {
1371              joinPool(e);
1372          }
# Line 1049 | Line 1375 | public class ThreadPoolExecutorTest exte
1375      /**
1376       * completed submit of runnable returns successfully
1377       */
1378 <    public void testSubmitRunnable() {
1379 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1378 >    public void testSubmitRunnable() throws Exception {
1379 >        ExecutorService e =
1380 >            new ThreadPoolExecutor(2, 2,
1381 >                                   LONG_DELAY_MS, MILLISECONDS,
1382 >                                   new ArrayBlockingQueue<Runnable>(10));
1383          try {
1384              Future<?> future = e.submit(new NoOpRunnable());
1385              future.get();
1386              assertTrue(future.isDone());
1058        }
1059        catch (ExecutionException ex) {
1060            unexpectedException();
1061        }
1062        catch (InterruptedException ex) {
1063            unexpectedException();
1387          } finally {
1388              joinPool(e);
1389          }
# Line 1069 | Line 1392 | public class ThreadPoolExecutorTest exte
1392      /**
1393       * completed submit of (runnable, result) returns result
1394       */
1395 <    public void testSubmitRunnable2() {
1396 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1395 >    public void testSubmitRunnable2() throws Exception {
1396 >        ExecutorService e =
1397 >            new ThreadPoolExecutor(2, 2,
1398 >                                   LONG_DELAY_MS, MILLISECONDS,
1399 >                                   new ArrayBlockingQueue<Runnable>(10));
1400          try {
1401              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1402              String result = future.get();
1403              assertSame(TEST_STRING, result);
1078        }
1079        catch (ExecutionException ex) {
1080            unexpectedException();
1081        }
1082        catch (InterruptedException ex) {
1083            unexpectedException();
1404          } finally {
1405              joinPool(e);
1406          }
1407      }
1408  
1409  
1090
1091
1092
1410      /**
1411       * invokeAny(null) throws NPE
1412       */
1413 <    public void testInvokeAny1() {
1414 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1413 >    public void testInvokeAny1() throws Exception {
1414 >        ExecutorService e =
1415 >            new ThreadPoolExecutor(2, 2,
1416 >                                   LONG_DELAY_MS, MILLISECONDS,
1417 >                                   new ArrayBlockingQueue<Runnable>(10));
1418          try {
1419              e.invokeAny(null);
1420 +            shouldThrow();
1421          } catch (NullPointerException success) {
1101        } catch(Exception ex) {
1102            unexpectedException();
1422          } finally {
1423              joinPool(e);
1424          }
# Line 1108 | Line 1427 | public class ThreadPoolExecutorTest exte
1427      /**
1428       * invokeAny(empty collection) throws IAE
1429       */
1430 <    public void testInvokeAny2() {
1431 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1430 >    public void testInvokeAny2() throws Exception {
1431 >        ExecutorService e =
1432 >            new ThreadPoolExecutor(2, 2,
1433 >                                   LONG_DELAY_MS, MILLISECONDS,
1434 >                                   new ArrayBlockingQueue<Runnable>(10));
1435          try {
1436              e.invokeAny(new ArrayList<Callable<String>>());
1437 +            shouldThrow();
1438          } catch (IllegalArgumentException success) {
1116        } catch(Exception ex) {
1117            unexpectedException();
1439          } finally {
1440              joinPool(e);
1441          }
# Line 1123 | Line 1444 | public class ThreadPoolExecutorTest exte
1444      /**
1445       * invokeAny(c) throws NPE if c has null elements
1446       */
1447 <    public void testInvokeAny3() {
1448 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1447 >    public void testInvokeAny3() throws Exception {
1448 >        final CountDownLatch latch = new CountDownLatch(1);
1449 >        final ExecutorService e =
1450 >            new ThreadPoolExecutor(2, 2,
1451 >                                   LONG_DELAY_MS, MILLISECONDS,
1452 >                                   new ArrayBlockingQueue<Runnable>(10));
1453 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1454 >        l.add(latchAwaitingStringTask(latch));
1455 >        l.add(null);
1456          try {
1129            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1130            l.add(new StringTask());
1131            l.add(null);
1457              e.invokeAny(l);
1458 +            shouldThrow();
1459          } catch (NullPointerException success) {
1134        } catch(Exception ex) {
1135            unexpectedException();
1460          } finally {
1461 +            latch.countDown();
1462              joinPool(e);
1463          }
1464      }
# Line 1141 | Line 1466 | public class ThreadPoolExecutorTest exte
1466      /**
1467       * invokeAny(c) throws ExecutionException if no task completes
1468       */
1469 <    public void testInvokeAny4() {
1470 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1469 >    public void testInvokeAny4() throws Exception {
1470 >        ExecutorService e =
1471 >            new ThreadPoolExecutor(2, 2,
1472 >                                   LONG_DELAY_MS, MILLISECONDS,
1473 >                                   new ArrayBlockingQueue<Runnable>(10));
1474 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1475 >        l.add(new NPETask());
1476          try {
1147            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1148            l.add(new NPETask());
1477              e.invokeAny(l);
1478 +            shouldThrow();
1479          } catch (ExecutionException success) {
1480 <        } catch(Exception ex) {
1152 <            unexpectedException();
1480 >            assertTrue(success.getCause() instanceof NullPointerException);
1481          } finally {
1482              joinPool(e);
1483          }
# Line 1158 | Line 1486 | public class ThreadPoolExecutorTest exte
1486      /**
1487       * invokeAny(c) returns result of some task
1488       */
1489 <    public void testInvokeAny5() {
1490 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1489 >    public void testInvokeAny5() throws Exception {
1490 >        ExecutorService e =
1491 >            new ThreadPoolExecutor(2, 2,
1492 >                                   LONG_DELAY_MS, MILLISECONDS,
1493 >                                   new ArrayBlockingQueue<Runnable>(10));
1494          try {
1495 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1495 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1496              l.add(new StringTask());
1497              l.add(new StringTask());
1498              String result = e.invokeAny(l);
1499              assertSame(TEST_STRING, result);
1169        } catch (ExecutionException success) {
1170        } catch(Exception ex) {
1171            unexpectedException();
1500          } finally {
1501              joinPool(e);
1502          }
# Line 1177 | Line 1505 | public class ThreadPoolExecutorTest exte
1505      /**
1506       * invokeAll(null) throws NPE
1507       */
1508 <    public void testInvokeAll1() {
1509 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1508 >    public void testInvokeAll1() throws Exception {
1509 >        ExecutorService e =
1510 >            new ThreadPoolExecutor(2, 2,
1511 >                                   LONG_DELAY_MS, MILLISECONDS,
1512 >                                   new ArrayBlockingQueue<Runnable>(10));
1513          try {
1514              e.invokeAll(null);
1515 +            shouldThrow();
1516          } catch (NullPointerException success) {
1185        } catch(Exception ex) {
1186            unexpectedException();
1517          } finally {
1518              joinPool(e);
1519          }
# Line 1192 | Line 1522 | public class ThreadPoolExecutorTest exte
1522      /**
1523       * invokeAll(empty collection) returns empty collection
1524       */
1525 <    public void testInvokeAll2() {
1526 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1525 >    public void testInvokeAll2() throws InterruptedException {
1526 >        ExecutorService e =
1527 >            new ThreadPoolExecutor(2, 2,
1528 >                                   LONG_DELAY_MS, MILLISECONDS,
1529 >                                   new ArrayBlockingQueue<Runnable>(10));
1530          try {
1531              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1532              assertTrue(r.isEmpty());
1200        } catch(Exception ex) {
1201            unexpectedException();
1533          } finally {
1534              joinPool(e);
1535          }
# Line 1207 | Line 1538 | public class ThreadPoolExecutorTest exte
1538      /**
1539       * invokeAll(c) throws NPE if c has null elements
1540       */
1541 <    public void testInvokeAll3() {
1542 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1541 >    public void testInvokeAll3() throws Exception {
1542 >        ExecutorService e =
1543 >            new ThreadPoolExecutor(2, 2,
1544 >                                   LONG_DELAY_MS, MILLISECONDS,
1545 >                                   new ArrayBlockingQueue<Runnable>(10));
1546 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1547 >        l.add(new StringTask());
1548 >        l.add(null);
1549          try {
1213            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1214            l.add(new StringTask());
1215            l.add(null);
1550              e.invokeAll(l);
1551 +            shouldThrow();
1552          } catch (NullPointerException success) {
1218        } catch(Exception ex) {
1219            unexpectedException();
1553          } finally {
1554              joinPool(e);
1555          }
# Line 1225 | Line 1558 | public class ThreadPoolExecutorTest exte
1558      /**
1559       * get of element of invokeAll(c) throws exception on failed task
1560       */
1561 <    public void testInvokeAll4() {
1562 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1561 >    public void testInvokeAll4() throws Exception {
1562 >        ExecutorService e =
1563 >            new ThreadPoolExecutor(2, 2,
1564 >                                   LONG_DELAY_MS, MILLISECONDS,
1565 >                                   new ArrayBlockingQueue<Runnable>(10));
1566          try {
1567 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1567 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1568              l.add(new NPETask());
1569 <            List<Future<String>> result = e.invokeAll(l);
1570 <            assertEquals(1, result.size());
1571 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1572 <                it.next().get();
1573 <        } catch(ExecutionException success) {
1574 <        } catch(Exception ex) {
1575 <            unexpectedException();
1569 >            List<Future<String>> futures = e.invokeAll(l);
1570 >            assertEquals(1, futures.size());
1571 >            try {
1572 >                futures.get(0).get();
1573 >                shouldThrow();
1574 >            } catch (ExecutionException success) {
1575 >                assertTrue(success.getCause() instanceof NullPointerException);
1576 >            }
1577          } finally {
1578              joinPool(e);
1579          }
# Line 1245 | Line 1582 | public class ThreadPoolExecutorTest exte
1582      /**
1583       * invokeAll(c) returns results of all completed tasks
1584       */
1585 <    public void testInvokeAll5() {
1586 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1585 >    public void testInvokeAll5() throws Exception {
1586 >        ExecutorService e =
1587 >            new ThreadPoolExecutor(2, 2,
1588 >                                   LONG_DELAY_MS, MILLISECONDS,
1589 >                                   new ArrayBlockingQueue<Runnable>(10));
1590          try {
1591 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1591 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1592              l.add(new StringTask());
1593              l.add(new StringTask());
1594 <            List<Future<String>> result = e.invokeAll(l);
1595 <            assertEquals(2, result.size());
1596 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1597 <                assertSame(TEST_STRING, it.next().get());
1258 <        } catch (ExecutionException success) {
1259 <        } catch(Exception ex) {
1260 <            unexpectedException();
1594 >            List<Future<String>> futures = e.invokeAll(l);
1595 >            assertEquals(2, futures.size());
1596 >            for (Future<String> future : futures)
1597 >                assertSame(TEST_STRING, future.get());
1598          } finally {
1599              joinPool(e);
1600          }
# Line 1268 | Line 1605 | public class ThreadPoolExecutorTest exte
1605      /**
1606       * timed invokeAny(null) throws NPE
1607       */
1608 <    public void testTimedInvokeAny1() {
1609 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1608 >    public void testTimedInvokeAny1() throws Exception {
1609 >        ExecutorService e =
1610 >            new ThreadPoolExecutor(2, 2,
1611 >                                   LONG_DELAY_MS, MILLISECONDS,
1612 >                                   new ArrayBlockingQueue<Runnable>(10));
1613          try {
1614 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1614 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1615 >            shouldThrow();
1616          } catch (NullPointerException success) {
1276        } catch(Exception ex) {
1277            unexpectedException();
1617          } finally {
1618              joinPool(e);
1619          }
# Line 1283 | Line 1622 | public class ThreadPoolExecutorTest exte
1622      /**
1623       * timed invokeAny(,,null) throws NPE
1624       */
1625 <    public void testTimedInvokeAnyNullTimeUnit() {
1626 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1625 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1626 >        ExecutorService e =
1627 >            new ThreadPoolExecutor(2, 2,
1628 >                                   LONG_DELAY_MS, MILLISECONDS,
1629 >                                   new ArrayBlockingQueue<Runnable>(10));
1630 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1631 >        l.add(new StringTask());
1632          try {
1289            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1290            l.add(new StringTask());
1633              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1634 +            shouldThrow();
1635          } catch (NullPointerException success) {
1293        } catch(Exception ex) {
1294            unexpectedException();
1636          } finally {
1637              joinPool(e);
1638          }
# Line 1300 | Line 1641 | public class ThreadPoolExecutorTest exte
1641      /**
1642       * timed invokeAny(empty collection) throws IAE
1643       */
1644 <    public void testTimedInvokeAny2() {
1645 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1644 >    public void testTimedInvokeAny2() throws Exception {
1645 >        ExecutorService e =
1646 >            new ThreadPoolExecutor(2, 2,
1647 >                                   LONG_DELAY_MS, MILLISECONDS,
1648 >                                   new ArrayBlockingQueue<Runnable>(10));
1649          try {
1650 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1650 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1651 >            shouldThrow();
1652          } catch (IllegalArgumentException success) {
1308        } catch(Exception ex) {
1309            unexpectedException();
1653          } finally {
1654              joinPool(e);
1655          }
# Line 1315 | Line 1658 | public class ThreadPoolExecutorTest exte
1658      /**
1659       * timed invokeAny(c) throws NPE if c has null elements
1660       */
1661 <    public void testTimedInvokeAny3() {
1662 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1661 >    public void testTimedInvokeAny3() throws Exception {
1662 >        final CountDownLatch latch = new CountDownLatch(1);
1663 >        final ExecutorService e =
1664 >            new ThreadPoolExecutor(2, 2,
1665 >                                   LONG_DELAY_MS, MILLISECONDS,
1666 >                                   new ArrayBlockingQueue<Runnable>(10));
1667 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1668 >        l.add(latchAwaitingStringTask(latch));
1669 >        l.add(null);
1670          try {
1671 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1672 <            l.add(new StringTask());
1323 <            l.add(null);
1324 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1671 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1672 >            shouldThrow();
1673          } catch (NullPointerException success) {
1326        } catch(Exception ex) {
1327            ex.printStackTrace();
1328            unexpectedException();
1674          } finally {
1675 +            latch.countDown();
1676              joinPool(e);
1677          }
1678      }
# Line 1334 | Line 1680 | public class ThreadPoolExecutorTest exte
1680      /**
1681       * timed invokeAny(c) throws ExecutionException if no task completes
1682       */
1683 <    public void testTimedInvokeAny4() {
1684 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1683 >    public void testTimedInvokeAny4() throws Exception {
1684 >        ExecutorService e =
1685 >            new ThreadPoolExecutor(2, 2,
1686 >                                   LONG_DELAY_MS, MILLISECONDS,
1687 >                                   new ArrayBlockingQueue<Runnable>(10));
1688 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1689 >        l.add(new NPETask());
1690          try {
1691 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1692 <            l.add(new NPETask());
1693 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1694 <        } catch(ExecutionException success) {
1344 <        } catch(Exception ex) {
1345 <            unexpectedException();
1691 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1692 >            shouldThrow();
1693 >        } catch (ExecutionException success) {
1694 >            assertTrue(success.getCause() instanceof NullPointerException);
1695          } finally {
1696              joinPool(e);
1697          }
# Line 1351 | Line 1700 | public class ThreadPoolExecutorTest exte
1700      /**
1701       * timed invokeAny(c) returns result of some task
1702       */
1703 <    public void testTimedInvokeAny5() {
1704 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1703 >    public void testTimedInvokeAny5() throws Exception {
1704 >        ExecutorService e =
1705 >            new ThreadPoolExecutor(2, 2,
1706 >                                   LONG_DELAY_MS, MILLISECONDS,
1707 >                                   new ArrayBlockingQueue<Runnable>(10));
1708          try {
1709 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1709 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1710              l.add(new StringTask());
1711              l.add(new StringTask());
1712 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1712 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1713              assertSame(TEST_STRING, result);
1362        } catch (ExecutionException success) {
1363        } catch(Exception ex) {
1364            unexpectedException();
1714          } finally {
1715              joinPool(e);
1716          }
# Line 1370 | Line 1719 | public class ThreadPoolExecutorTest exte
1719      /**
1720       * timed invokeAll(null) throws NPE
1721       */
1722 <    public void testTimedInvokeAll1() {
1723 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1722 >    public void testTimedInvokeAll1() throws Exception {
1723 >        ExecutorService e =
1724 >            new ThreadPoolExecutor(2, 2,
1725 >                                   LONG_DELAY_MS, MILLISECONDS,
1726 >                                   new ArrayBlockingQueue<Runnable>(10));
1727          try {
1728 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1728 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1729 >            shouldThrow();
1730          } catch (NullPointerException success) {
1378        } catch(Exception ex) {
1379            unexpectedException();
1731          } finally {
1732              joinPool(e);
1733          }
# Line 1385 | Line 1736 | public class ThreadPoolExecutorTest exte
1736      /**
1737       * timed invokeAll(,,null) throws NPE
1738       */
1739 <    public void testTimedInvokeAllNullTimeUnit() {
1740 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1739 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1740 >        ExecutorService e =
1741 >            new ThreadPoolExecutor(2, 2,
1742 >                                   LONG_DELAY_MS, MILLISECONDS,
1743 >                                   new ArrayBlockingQueue<Runnable>(10));
1744 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1745 >        l.add(new StringTask());
1746          try {
1391            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1392            l.add(new StringTask());
1747              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1748 +            shouldThrow();
1749          } catch (NullPointerException success) {
1395        } catch(Exception ex) {
1396            unexpectedException();
1750          } finally {
1751              joinPool(e);
1752          }
# Line 1402 | Line 1755 | public class ThreadPoolExecutorTest exte
1755      /**
1756       * timed invokeAll(empty collection) returns empty collection
1757       */
1758 <    public void testTimedInvokeAll2() {
1759 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1758 >    public void testTimedInvokeAll2() throws InterruptedException {
1759 >        ExecutorService e =
1760 >            new ThreadPoolExecutor(2, 2,
1761 >                                   LONG_DELAY_MS, MILLISECONDS,
1762 >                                   new ArrayBlockingQueue<Runnable>(10));
1763          try {
1764 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1764 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1765              assertTrue(r.isEmpty());
1410        } catch(Exception ex) {
1411            unexpectedException();
1766          } finally {
1767              joinPool(e);
1768          }
# Line 1417 | Line 1771 | public class ThreadPoolExecutorTest exte
1771      /**
1772       * timed invokeAll(c) throws NPE if c has null elements
1773       */
1774 <    public void testTimedInvokeAll3() {
1775 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1774 >    public void testTimedInvokeAll3() throws Exception {
1775 >        ExecutorService e =
1776 >            new ThreadPoolExecutor(2, 2,
1777 >                                   LONG_DELAY_MS, MILLISECONDS,
1778 >                                   new ArrayBlockingQueue<Runnable>(10));
1779 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1780 >        l.add(new StringTask());
1781 >        l.add(null);
1782          try {
1783 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1784 <            l.add(new StringTask());
1425 <            l.add(null);
1426 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1783 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1784 >            shouldThrow();
1785          } catch (NullPointerException success) {
1428        } catch(Exception ex) {
1429            unexpectedException();
1786          } finally {
1787              joinPool(e);
1788          }
# Line 1435 | Line 1791 | public class ThreadPoolExecutorTest exte
1791      /**
1792       * get of element of invokeAll(c) throws exception on failed task
1793       */
1794 <    public void testTimedInvokeAll4() {
1795 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1794 >    public void testTimedInvokeAll4() throws Exception {
1795 >        ExecutorService e =
1796 >            new ThreadPoolExecutor(2, 2,
1797 >                                   LONG_DELAY_MS, MILLISECONDS,
1798 >                                   new ArrayBlockingQueue<Runnable>(10));
1799 >        List<Callable<String>> l = new ArrayList<Callable<String>>();
1800 >        l.add(new NPETask());
1801 >        List<Future<String>> futures =
1802 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1803 >        assertEquals(1, futures.size());
1804          try {
1805 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1806 <            l.add(new NPETask());
1807 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1808 <            assertEquals(1, result.size());
1445 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1446 <                it.next().get();
1447 <        } catch(ExecutionException success) {
1448 <        } catch(Exception ex) {
1449 <            unexpectedException();
1805 >            futures.get(0).get();
1806 >            shouldThrow();
1807 >        } catch (ExecutionException success) {
1808 >            assertTrue(success.getCause() instanceof NullPointerException);
1809          } finally {
1810              joinPool(e);
1811          }
# Line 1455 | Line 1814 | public class ThreadPoolExecutorTest exte
1814      /**
1815       * timed invokeAll(c) returns results of all completed tasks
1816       */
1817 <    public void testTimedInvokeAll5() {
1818 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1817 >    public void testTimedInvokeAll5() throws Exception {
1818 >        ExecutorService e =
1819 >            new ThreadPoolExecutor(2, 2,
1820 >                                   LONG_DELAY_MS, MILLISECONDS,
1821 >                                   new ArrayBlockingQueue<Runnable>(10));
1822          try {
1823 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1823 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1824              l.add(new StringTask());
1825              l.add(new StringTask());
1826 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1827 <            assertEquals(2, result.size());
1828 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1829 <                assertSame(TEST_STRING, it.next().get());
1830 <        } catch (ExecutionException success) {
1469 <        } catch(Exception ex) {
1470 <            unexpectedException();
1826 >            List<Future<String>> futures =
1827 >                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1828 >            assertEquals(2, futures.size());
1829 >            for (Future<String> future : futures)
1830 >                assertSame(TEST_STRING, future.get());
1831          } finally {
1832              joinPool(e);
1833          }
# Line 1476 | Line 1836 | public class ThreadPoolExecutorTest exte
1836      /**
1837       * timed invokeAll(c) cancels tasks not completed by timeout
1838       */
1839 <    public void testTimedInvokeAll6() {
1840 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1839 >    public void testTimedInvokeAll6() throws Exception {
1840 >        ExecutorService e =
1841 >            new ThreadPoolExecutor(2, 2,
1842 >                                   LONG_DELAY_MS, MILLISECONDS,
1843 >                                   new ArrayBlockingQueue<Runnable>(10));
1844          try {
1845 <            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1845 >            List<Callable<String>> l = new ArrayList<Callable<String>>();
1846              l.add(new StringTask());
1847              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1848 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1849 <            assertEquals(2, result.size());
1850 <            Iterator<Future<String>> it = result.iterator();
1848 >            l.add(new StringTask());
1849 >            List<Future<String>> futures =
1850 >                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1851 >            assertEquals(3, futures.size());
1852 >            Iterator<Future<String>> it = futures.iterator();
1853              Future<String> f1 = it.next();
1854              Future<String> f2 = it.next();
1855 +            Future<String> f3 = it.next();
1856              assertTrue(f1.isDone());
1491            assertFalse(f1.isCancelled());
1857              assertTrue(f2.isDone());
1858 +            assertTrue(f3.isDone());
1859 +            assertFalse(f1.isCancelled());
1860              assertTrue(f2.isCancelled());
1494        } catch(Exception ex) {
1495            unexpectedException();
1861          } finally {
1862              joinPool(e);
1863          }
1864      }
1865  
1866 +    /**
1867 +     * Execution continues if there is at least one thread even if
1868 +     * thread factory fails to create more
1869 +     */
1870 +    public void testFailingThreadFactory() throws InterruptedException {
1871 +        final ExecutorService e =
1872 +            new ThreadPoolExecutor(100, 100,
1873 +                                   LONG_DELAY_MS, MILLISECONDS,
1874 +                                   new LinkedBlockingQueue<Runnable>(),
1875 +                                   new FailingThreadFactory());
1876 +        try {
1877 +            final int TASKS = 100;
1878 +            final CountDownLatch done = new CountDownLatch(TASKS);
1879 +            for (int k = 0; k < TASKS; ++k)
1880 +                e.execute(new CheckedRunnable() {
1881 +                    public void realRun() {
1882 +                        done.countDown();
1883 +                    }});
1884 +            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1885 +        } finally {
1886 +            joinPool(e);
1887 +        }
1888 +    }
1889 +
1890 +    /**
1891 +     * allowsCoreThreadTimeOut is by default false.
1892 +     */
1893 +    public void testAllowsCoreThreadTimeOut() {
1894 +        final ThreadPoolExecutor p =
1895 +            new ThreadPoolExecutor(2, 2,
1896 +                                   1000, MILLISECONDS,
1897 +                                   new ArrayBlockingQueue<Runnable>(10));
1898 +        assertFalse(p.allowsCoreThreadTimeOut());
1899 +        joinPool(p);
1900 +    }
1901 +
1902 +    /**
1903 +     * allowCoreThreadTimeOut(true) causes idle threads to time out
1904 +     */
1905 +    public void testAllowCoreThreadTimeOut_true() throws Exception {
1906 +        final ThreadPoolExecutor p =
1907 +            new ThreadPoolExecutor(2, 10,
1908 +                                   SHORT_DELAY_MS, MILLISECONDS,
1909 +                                   new ArrayBlockingQueue<Runnable>(10));
1910 +        final CountDownLatch threadStarted = new CountDownLatch(1);
1911 +        try {
1912 +            p.allowCoreThreadTimeOut(true);
1913 +            p.execute(new CheckedRunnable() {
1914 +                public void realRun() throws InterruptedException {
1915 +                    threadStarted.countDown();
1916 +                    assertEquals(1, p.getPoolSize());
1917 +                }});
1918 +            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1919 +            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1920 +                if (p.getPoolSize() == 0)
1921 +                    break;
1922 +                delay(10);
1923 +            }
1924 +            assertEquals(0, p.getPoolSize());
1925 +        } finally {
1926 +            joinPool(p);
1927 +        }
1928 +    }
1929 +
1930 +    /**
1931 +     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1932 +     */
1933 +    public void testAllowCoreThreadTimeOut_false() throws Exception {
1934 +        final ThreadPoolExecutor p =
1935 +            new ThreadPoolExecutor(2, 10,
1936 +                                   SHORT_DELAY_MS, MILLISECONDS,
1937 +                                   new ArrayBlockingQueue<Runnable>(10));
1938 +        final CountDownLatch threadStarted = new CountDownLatch(1);
1939 +        try {
1940 +            p.allowCoreThreadTimeOut(false);
1941 +            p.execute(new CheckedRunnable() {
1942 +                public void realRun() throws InterruptedException {
1943 +                    threadStarted.countDown();
1944 +                    assertTrue(p.getPoolSize() >= 1);
1945 +                }});
1946 +            delay(SMALL_DELAY_MS);
1947 +            assertTrue(p.getPoolSize() >= 1);
1948 +        } finally {
1949 +            joinPool(p);
1950 +        }
1951 +    }
1952 +
1953 +    /**
1954 +     * execute allows the same task to be submitted multiple times, even
1955 +     * if rejected
1956 +     */
1957 +    public void testRejectedRecycledTask() throws InterruptedException {
1958 +        final int nTasks = 1000;
1959 +        final CountDownLatch done = new CountDownLatch(nTasks);
1960 +        final Runnable recycledTask = new Runnable() {
1961 +            public void run() {
1962 +                done.countDown();
1963 +            }};
1964 +        final ThreadPoolExecutor p =
1965 +            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
1966 +                                   new ArrayBlockingQueue(30));
1967 +        try {
1968 +            for (int i = 0; i < nTasks; ++i) {
1969 +                for (;;) {
1970 +                    try {
1971 +                        p.execute(recycledTask);
1972 +                        break;
1973 +                    }
1974 +                    catch (RejectedExecutionException ignore) {}
1975 +                }
1976 +            }
1977 +            // enough time to run all tasks
1978 +            assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
1979 +        } finally {
1980 +            p.shutdown();
1981 +        }
1982 +    }
1983  
1984   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines