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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines