ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
(Generate patch)

Comparing jsr166/src/test/tck/ThreadPoolExecutorTest.java (file contents):
Revision 1.21 by dl, Fri Dec 31 14:52:40 2004 UTC vs.
Revision 1.107 by jsr166, Tue Oct 6 05:22:25 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines