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.25 by jsr166, Mon Nov 16 04:57:10 2009 UTC vs.
Revision 1.49 by jsr166, Wed Sep 25 07:39:17 2013 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines