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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines