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.1 by dl, Sun Aug 31 19:24:56 2003 UTC vs.
Revision 1.44 by jsr166, Fri May 27 19:35:24 2011 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines