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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines