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.21 by dl, Fri Dec 31 14:52:40 2004 UTC vs.
Revision 1.73 by jsr166, Sun Oct 4 01:56:51 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines