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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines