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.13 by dl, Tue Dec 23 19:40:24 2003 UTC vs.
Revision 1.50 by jsr166, Wed Dec 31 19:05:43 2014 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines