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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines