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

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.14 by dl, Thu Dec 25 19:48:57 2003 UTC vs.
Revision 1.44 by jsr166, Fri May 27 19:35:24 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines