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

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines