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.31 by jsr166, Sat Nov 21 02:07:27 2009 UTC vs.
Revision 1.60 by jsr166, Sun Sep 27 18:50:50 2015 UTC

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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines