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.21 by dl, Fri Dec 31 14:52:40 2004 UTC vs.
Revision 1.46 by jsr166, Sun May 29 13:55:36 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines