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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines