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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines