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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines