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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines