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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines