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

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines