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.35 by jsr166, Sat Oct 9 19:30:35 2010 UTC vs.
Revision 1.69 by jsr166, Sun Oct 4 01:29:09 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
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  
# Line 65 | Line 105 | public class ThreadPoolExecutorTest exte
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
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, 6,
136 >                                   LONG_DELAY_MS, MILLISECONDS,
137 >                                   new ArrayBlockingQueue<Runnable>(10));
138 >        try (PoolCleaner cleaner = cleaner(p)) {
139 >            assertEquals(0, p.getPoolSize());
140 >            assertTrue(p.prestartCoreThread());
141 >            assertEquals(1, p.getPoolSize());
142 >            assertTrue(p.prestartCoreThread());
143 >            assertEquals(2, p.getPoolSize());
144 >            assertFalse(p.prestartCoreThread());
145 >            assertEquals(2, p.getPoolSize());
146 >            p.setCorePoolSize(4);
147 >            assertTrue(p.prestartCoreThread());
148 >            assertEquals(3, p.getPoolSize());
149 >            assertTrue(p.prestartCoreThread());
150 >            assertEquals(4, p.getPoolSize());
151 >            assertFalse(p.prestartCoreThread());
152 >            assertEquals(4, p.getPoolSize());
153 >        }
154      }
155  
156      /**
157       * prestartAllCoreThreads starts all corePoolSize threads
158       */
159      public void testPrestartAllCoreThreads() {
160 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
161 <        assertEquals(0, p2.getPoolSize());
162 <        p2.prestartAllCoreThreads();
163 <        assertEquals(2, p2.getPoolSize());
164 <        p2.prestartAllCoreThreads();
165 <        assertEquals(2, p2.getPoolSize());
166 <        joinPool(p2);
160 >        final ThreadPoolExecutor p =
161 >            new ThreadPoolExecutor(2, 6,
162 >                                   LONG_DELAY_MS, MILLISECONDS,
163 >                                   new ArrayBlockingQueue<Runnable>(10));
164 >        try (PoolCleaner cleaner = cleaner(p)) {
165 >            assertEquals(0, p.getPoolSize());
166 >            p.prestartAllCoreThreads();
167 >            assertEquals(2, p.getPoolSize());
168 >            p.prestartAllCoreThreads();
169 >            assertEquals(2, p.getPoolSize());
170 >            p.setCorePoolSize(4);
171 >            p.prestartAllCoreThreads();
172 >            assertEquals(4, p.getPoolSize());
173 >            p.prestartAllCoreThreads();
174 >            assertEquals(4, p.getPoolSize());
175 >        }
176      }
177  
178      /**
# Line 106 | Line 180 | public class ThreadPoolExecutorTest exte
180       * when tasks complete
181       */
182      public void testGetCompletedTaskCount() throws InterruptedException {
183 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
184 <        assertEquals(0, p2.getCompletedTaskCount());
185 <        p2.execute(new ShortRunnable());
186 <        Thread.sleep(SMALL_DELAY_MS);
187 <        assertEquals(1, p2.getCompletedTaskCount());
188 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
189 <        joinPool(p2);
183 >        final ThreadPoolExecutor p =
184 >            new ThreadPoolExecutor(2, 2,
185 >                                   LONG_DELAY_MS, MILLISECONDS,
186 >                                   new ArrayBlockingQueue<Runnable>(10));
187 >        try (PoolCleaner cleaner = cleaner(p)) {
188 >            final CountDownLatch threadStarted = new CountDownLatch(1);
189 >            final CountDownLatch threadProceed = new CountDownLatch(1);
190 >            final CountDownLatch threadDone = new CountDownLatch(1);
191 >            assertEquals(0, p.getCompletedTaskCount());
192 >            p.execute(new CheckedRunnable() {
193 >                public void realRun() throws InterruptedException {
194 >                    threadStarted.countDown();
195 >                    assertEquals(0, p.getCompletedTaskCount());
196 >                    threadProceed.await();
197 >                    threadDone.countDown();
198 >                }});
199 >            await(threadStarted);
200 >            assertEquals(0, p.getCompletedTaskCount());
201 >            threadProceed.countDown();
202 >            threadDone.await();
203 >            long startTime = System.nanoTime();
204 >            while (p.getCompletedTaskCount() != 1) {
205 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
206 >                    fail("timed out");
207 >                Thread.yield();
208 >            }
209 >        }
210      }
211  
212      /**
213       * getCorePoolSize returns size given in constructor if not otherwise set
214       */
215      public void testGetCorePoolSize() {
216 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
217 <        assertEquals(1, p1.getCorePoolSize());
218 <        joinPool(p1);
216 >        final ThreadPoolExecutor p =
217 >            new ThreadPoolExecutor(1, 1,
218 >                                   LONG_DELAY_MS, MILLISECONDS,
219 >                                   new ArrayBlockingQueue<Runnable>(10));
220 >        try (PoolCleaner cleaner = cleaner(p)) {
221 >            assertEquals(1, p.getCorePoolSize());
222 >        }
223      }
224  
225      /**
226       * getKeepAliveTime returns value given in constructor if not otherwise set
227       */
228      public void testGetKeepAliveTime() {
229 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
230 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
231 <        joinPool(p2);
229 >        final ThreadPoolExecutor p =
230 >            new ThreadPoolExecutor(2, 2,
231 >                                   1000, MILLISECONDS,
232 >                                   new ArrayBlockingQueue<Runnable>(10));
233 >        assertEquals(1, p.getKeepAliveTime(SECONDS));
234 >        joinPool(p);
235      }
236  
136
237      /**
238       * getThreadFactory returns factory in constructor if not set
239       */
240      public void testGetThreadFactory() {
241          ThreadFactory tf = new SimpleThreadFactory();
242 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
242 >        final ThreadPoolExecutor p =
243 >            new ThreadPoolExecutor(1, 2,
244 >                                   LONG_DELAY_MS, MILLISECONDS,
245 >                                   new ArrayBlockingQueue<Runnable>(10),
246 >                                   tf,
247 >                                   new NoOpREHandler());
248          assertSame(tf, p.getThreadFactory());
249          joinPool(p);
250      }
# Line 148 | Line 253 | public class ThreadPoolExecutorTest exte
253       * setThreadFactory sets the thread factory returned by getThreadFactory
254       */
255      public void testSetThreadFactory() {
256 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
256 >        final ThreadPoolExecutor p =
257 >            new ThreadPoolExecutor(1, 2,
258 >                                   LONG_DELAY_MS, MILLISECONDS,
259 >                                   new ArrayBlockingQueue<Runnable>(10));
260          ThreadFactory tf = new SimpleThreadFactory();
261          p.setThreadFactory(tf);
262          assertSame(tf, p.getThreadFactory());
263          joinPool(p);
264      }
265  
158
266      /**
267       * setThreadFactory(null) throws NPE
268       */
269      public void testSetThreadFactoryNull() {
270 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
270 >        final ThreadPoolExecutor p =
271 >            new ThreadPoolExecutor(1, 2,
272 >                                   LONG_DELAY_MS, MILLISECONDS,
273 >                                   new ArrayBlockingQueue<Runnable>(10));
274          try {
275              p.setThreadFactory(null);
276              shouldThrow();
# Line 174 | Line 284 | public class ThreadPoolExecutorTest exte
284       * getRejectedExecutionHandler returns handler in constructor if not set
285       */
286      public void testGetRejectedExecutionHandler() {
287 <        RejectedExecutionHandler h = new NoOpREHandler();
288 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
287 >        final RejectedExecutionHandler h = new NoOpREHandler();
288 >        final ThreadPoolExecutor p =
289 >            new ThreadPoolExecutor(1, 2,
290 >                                   LONG_DELAY_MS, MILLISECONDS,
291 >                                   new ArrayBlockingQueue<Runnable>(10),
292 >                                   h);
293          assertSame(h, p.getRejectedExecutionHandler());
294          joinPool(p);
295      }
# Line 185 | Line 299 | public class ThreadPoolExecutorTest exte
299       * getRejectedExecutionHandler
300       */
301      public void testSetRejectedExecutionHandler() {
302 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
302 >        final ThreadPoolExecutor p =
303 >            new ThreadPoolExecutor(1, 2,
304 >                                   LONG_DELAY_MS, MILLISECONDS,
305 >                                   new ArrayBlockingQueue<Runnable>(10));
306          RejectedExecutionHandler h = new NoOpREHandler();
307          p.setRejectedExecutionHandler(h);
308          assertSame(h, p.getRejectedExecutionHandler());
309          joinPool(p);
310      }
311  
195
312      /**
313       * setRejectedExecutionHandler(null) throws NPE
314       */
315      public void testSetRejectedExecutionHandlerNull() {
316 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
316 >        final ThreadPoolExecutor p =
317 >            new ThreadPoolExecutor(1, 2,
318 >                                   LONG_DELAY_MS, MILLISECONDS,
319 >                                   new ArrayBlockingQueue<Runnable>(10));
320          try {
321              p.setRejectedExecutionHandler(null);
322              shouldThrow();
# Line 207 | Line 326 | public class ThreadPoolExecutorTest exte
326          }
327      }
328  
210
329      /**
330       * getLargestPoolSize increases, but doesn't overestimate, when
331       * multiple threads active
332       */
333      public void testGetLargestPoolSize() throws InterruptedException {
334 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
335 <        assertEquals(0, p2.getLargestPoolSize());
336 <        p2.execute(new MediumRunnable());
337 <        p2.execute(new MediumRunnable());
338 <        Thread.sleep(SHORT_DELAY_MS);
339 <        assertEquals(2, p2.getLargestPoolSize());
340 <        joinPool(p2);
334 >        final int THREADS = 3;
335 >        final ThreadPoolExecutor p =
336 >            new ThreadPoolExecutor(THREADS, THREADS,
337 >                                   LONG_DELAY_MS, MILLISECONDS,
338 >                                   new ArrayBlockingQueue<Runnable>(10));
339 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
340 >        final CountDownLatch done = new CountDownLatch(1);
341 >        try {
342 >            assertEquals(0, p.getLargestPoolSize());
343 >            for (int i = 0; i < THREADS; i++)
344 >                p.execute(new CheckedRunnable() {
345 >                    public void realRun() throws InterruptedException {
346 >                        threadsStarted.countDown();
347 >                        done.await();
348 >                        assertEquals(THREADS, p.getLargestPoolSize());
349 >                    }});
350 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
351 >            assertEquals(THREADS, p.getLargestPoolSize());
352 >        } finally {
353 >            done.countDown();
354 >            joinPool(p);
355 >            assertEquals(THREADS, p.getLargestPoolSize());
356 >        }
357      }
358  
359      /**
# Line 227 | Line 361 | public class ThreadPoolExecutorTest exte
361       * otherwise set
362       */
363      public void testGetMaximumPoolSize() {
364 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
365 <        assertEquals(2, p2.getMaximumPoolSize());
366 <        joinPool(p2);
364 >        final ThreadPoolExecutor p =
365 >            new ThreadPoolExecutor(2, 3,
366 >                                   LONG_DELAY_MS, MILLISECONDS,
367 >                                   new ArrayBlockingQueue<Runnable>(10));
368 >        assertEquals(3, p.getMaximumPoolSize());
369 >        joinPool(p);
370      }
371  
372      /**
373       * getPoolSize increases, but doesn't overestimate, when threads
374       * become active
375       */
376 <    public void testGetPoolSize() {
377 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
378 <        assertEquals(0, p1.getPoolSize());
379 <        p1.execute(new MediumRunnable());
380 <        assertEquals(1, p1.getPoolSize());
381 <        joinPool(p1);
376 >    public void testGetPoolSize() throws InterruptedException {
377 >        final ThreadPoolExecutor p =
378 >            new ThreadPoolExecutor(1, 1,
379 >                                   LONG_DELAY_MS, MILLISECONDS,
380 >                                   new ArrayBlockingQueue<Runnable>(10));
381 >        final CountDownLatch threadStarted = new CountDownLatch(1);
382 >        final CountDownLatch done = new CountDownLatch(1);
383 >        try {
384 >            assertEquals(0, p.getPoolSize());
385 >            p.execute(new CheckedRunnable() {
386 >                public void realRun() throws InterruptedException {
387 >                    threadStarted.countDown();
388 >                    assertEquals(1, p.getPoolSize());
389 >                    done.await();
390 >                }});
391 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
392 >            assertEquals(1, p.getPoolSize());
393 >        } finally {
394 >            done.countDown();
395 >            joinPool(p);
396 >        }
397      }
398  
399      /**
400       * getTaskCount increases, but doesn't overestimate, when tasks submitted
401       */
402      public void testGetTaskCount() throws InterruptedException {
403 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
404 <        assertEquals(0, p1.getTaskCount());
405 <        p1.execute(new MediumRunnable());
406 <        Thread.sleep(SHORT_DELAY_MS);
407 <        assertEquals(1, p1.getTaskCount());
408 <        joinPool(p1);
403 >        final ThreadPoolExecutor p =
404 >            new ThreadPoolExecutor(1, 1,
405 >                                   LONG_DELAY_MS, MILLISECONDS,
406 >                                   new ArrayBlockingQueue<Runnable>(10));
407 >        final CountDownLatch threadStarted = new CountDownLatch(1);
408 >        final CountDownLatch done = new CountDownLatch(1);
409 >        try {
410 >            assertEquals(0, p.getTaskCount());
411 >            p.execute(new CheckedRunnable() {
412 >                public void realRun() throws InterruptedException {
413 >                    threadStarted.countDown();
414 >                    assertEquals(1, p.getTaskCount());
415 >                    done.await();
416 >                }});
417 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
418 >            assertEquals(1, p.getTaskCount());
419 >        } finally {
420 >            done.countDown();
421 >            joinPool(p);
422 >        }
423      }
424  
425      /**
426 <     * isShutDown is false before shutdown, true after
426 >     * isShutdown is false before shutdown, true after
427       */
428      public void testIsShutdown() {
429 <
430 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
431 <        assertFalse(p1.isShutdown());
432 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
433 <        assertTrue(p1.isShutdown());
434 <        joinPool(p1);
429 >        final ThreadPoolExecutor p =
430 >            new ThreadPoolExecutor(1, 1,
431 >                                   LONG_DELAY_MS, MILLISECONDS,
432 >                                   new ArrayBlockingQueue<Runnable>(10));
433 >        assertFalse(p.isShutdown());
434 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
435 >        assertTrue(p.isShutdown());
436 >        joinPool(p);
437      }
438  
439 +    /**
440 +     * awaitTermination on a non-shutdown pool times out
441 +     */
442 +    public void testAwaitTermination_timesOut() throws InterruptedException {
443 +        final ThreadPoolExecutor p =
444 +            new ThreadPoolExecutor(1, 1,
445 +                                   LONG_DELAY_MS, MILLISECONDS,
446 +                                   new ArrayBlockingQueue<Runnable>(10));
447 +        assertFalse(p.isTerminated());
448 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
449 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
450 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
451 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
452 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
453 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
454 +        long timeoutNanos = 999999L;
455 +        long startTime = System.nanoTime();
456 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
457 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
458 +        assertFalse(p.isTerminated());
459 +        startTime = System.nanoTime();
460 +        long timeoutMillis = timeoutMillis();
461 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
462 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
463 +        assertFalse(p.isTerminated());
464 +        p.shutdown();
465 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
466 +        assertTrue(p.isTerminated());
467 +    }
468  
469      /**
470       * isTerminated is false before termination, true after
471       */
472      public void testIsTerminated() throws InterruptedException {
473 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
474 <        assertFalse(p1.isTerminated());
475 <        try {
476 <            p1.execute(new MediumRunnable());
473 >        final ThreadPoolExecutor p =
474 >            new ThreadPoolExecutor(1, 1,
475 >                                   LONG_DELAY_MS, MILLISECONDS,
476 >                                   new ArrayBlockingQueue<Runnable>(10));
477 >        final CountDownLatch threadStarted = new CountDownLatch(1);
478 >        final CountDownLatch done = new CountDownLatch(1);
479 >        assertFalse(p.isTerminated());
480 >        try {
481 >            p.execute(new CheckedRunnable() {
482 >                public void realRun() throws InterruptedException {
483 >                    assertFalse(p.isTerminated());
484 >                    threadStarted.countDown();
485 >                    done.await();
486 >                }});
487 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
488 >            assertFalse(p.isTerminating());
489 >            done.countDown();
490          } finally {
491 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
491 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
492          }
493 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
494 <        assertTrue(p1.isTerminated());
493 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
494 >        assertTrue(p.isTerminated());
495      }
496  
497      /**
498       * isTerminating is not true when running or when terminated
499       */
500      public void testIsTerminating() throws InterruptedException {
501 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
502 <        assertFalse(p1.isTerminating());
501 >        final ThreadPoolExecutor p =
502 >            new ThreadPoolExecutor(1, 1,
503 >                                   LONG_DELAY_MS, MILLISECONDS,
504 >                                   new ArrayBlockingQueue<Runnable>(10));
505 >        final CountDownLatch threadStarted = new CountDownLatch(1);
506 >        final CountDownLatch done = new CountDownLatch(1);
507          try {
508 <            p1.execute(new SmallRunnable());
509 <            assertFalse(p1.isTerminating());
510 <        } finally {
511 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
512 <        }
513 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
514 <        assertTrue(p1.isTerminated());
515 <        assertFalse(p1.isTerminating());
508 >            assertFalse(p.isTerminating());
509 >            p.execute(new CheckedRunnable() {
510 >                public void realRun() throws InterruptedException {
511 >                    assertFalse(p.isTerminating());
512 >                    threadStarted.countDown();
513 >                    done.await();
514 >                }});
515 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
516 >            assertFalse(p.isTerminating());
517 >            done.countDown();
518 >        } finally {
519 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
520 >        }
521 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
522 >        assertTrue(p.isTerminated());
523 >        assertFalse(p.isTerminating());
524      }
525  
526      /**
527       * getQueue returns the work queue, which contains queued tasks
528       */
529      public void testGetQueue() throws InterruptedException {
530 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
531 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
532 <        FutureTask[] tasks = new FutureTask[5];
533 <        for (int i = 0; i < 5; i++) {
534 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
535 <            p1.execute(tasks[i]);
536 <        }
537 <        try {
538 <            Thread.sleep(SHORT_DELAY_MS);
539 <            BlockingQueue<Runnable> wq = p1.getQueue();
540 <            assertSame(q, wq);
541 <            assertFalse(wq.contains(tasks[0]));
542 <            assertTrue(wq.contains(tasks[4]));
543 <            for (int i = 1; i < 5; ++i)
544 <                tasks[i].cancel(true);
545 <            p1.shutdownNow();
530 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
531 >        final ThreadPoolExecutor p =
532 >            new ThreadPoolExecutor(1, 1,
533 >                                   LONG_DELAY_MS, MILLISECONDS,
534 >                                   q);
535 >        final CountDownLatch threadStarted = new CountDownLatch(1);
536 >        final CountDownLatch done = new CountDownLatch(1);
537 >        try {
538 >            FutureTask[] tasks = new FutureTask[5];
539 >            for (int i = 0; i < tasks.length; i++) {
540 >                Callable task = new CheckedCallable<Boolean>() {
541 >                    public Boolean realCall() throws InterruptedException {
542 >                        threadStarted.countDown();
543 >                        assertSame(q, p.getQueue());
544 >                        done.await();
545 >                        return Boolean.TRUE;
546 >                    }};
547 >                tasks[i] = new FutureTask(task);
548 >                p.execute(tasks[i]);
549 >            }
550 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
551 >            assertSame(q, p.getQueue());
552 >            assertFalse(q.contains(tasks[0]));
553 >            assertTrue(q.contains(tasks[tasks.length - 1]));
554 >            assertEquals(tasks.length - 1, q.size());
555          } finally {
556 <            joinPool(p1);
556 >            done.countDown();
557 >            joinPool(p);
558          }
559      }
560  
# Line 331 | Line 563 | public class ThreadPoolExecutorTest exte
563       */
564      public void testRemove() throws InterruptedException {
565          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
566 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
567 <        FutureTask[] tasks = new FutureTask[5];
568 <        for (int i = 0; i < 5; i++) {
569 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
570 <            p1.execute(tasks[i]);
571 <        }
572 <        try {
573 <            Thread.sleep(SHORT_DELAY_MS);
574 <            assertFalse(p1.remove(tasks[0]));
566 >        final ThreadPoolExecutor p =
567 >            new ThreadPoolExecutor(1, 1,
568 >                                   LONG_DELAY_MS, MILLISECONDS,
569 >                                   q);
570 >        Runnable[] tasks = new Runnable[5];
571 >        final CountDownLatch threadStarted = new CountDownLatch(1);
572 >        final CountDownLatch done = new CountDownLatch(1);
573 >        try {
574 >            for (int i = 0; i < tasks.length; i++) {
575 >                tasks[i] = new CheckedRunnable() {
576 >                    public void realRun() throws InterruptedException {
577 >                        threadStarted.countDown();
578 >                        done.await();
579 >                    }};
580 >                p.execute(tasks[i]);
581 >            }
582 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
583 >            assertFalse(p.remove(tasks[0]));
584              assertTrue(q.contains(tasks[4]));
585              assertTrue(q.contains(tasks[3]));
586 <            assertTrue(p1.remove(tasks[4]));
587 <            assertFalse(p1.remove(tasks[4]));
586 >            assertTrue(p.remove(tasks[4]));
587 >            assertFalse(p.remove(tasks[4]));
588              assertFalse(q.contains(tasks[4]));
589              assertTrue(q.contains(tasks[3]));
590 <            assertTrue(p1.remove(tasks[3]));
590 >            assertTrue(p.remove(tasks[3]));
591              assertFalse(q.contains(tasks[3]));
592          } finally {
593 <            joinPool(p1);
593 >            done.countDown();
594 >            joinPool(p);
595          }
596      }
597  
598      /**
599       * purge removes cancelled tasks from the queue
600       */
601 <    public void testPurge() {
602 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
601 >    public void testPurge() throws InterruptedException {
602 >        final CountDownLatch threadStarted = new CountDownLatch(1);
603 >        final CountDownLatch done = new CountDownLatch(1);
604 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
605 >        final ThreadPoolExecutor p =
606 >            new ThreadPoolExecutor(1, 1,
607 >                                   LONG_DELAY_MS, MILLISECONDS,
608 >                                   q);
609          FutureTask[] tasks = new FutureTask[5];
610 <        for (int i = 0; i < 5; i++) {
611 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
612 <            p1.execute(tasks[i]);
610 >        try {
611 >            for (int i = 0; i < tasks.length; i++) {
612 >                Callable task = new CheckedCallable<Boolean>() {
613 >                    public Boolean realCall() throws InterruptedException {
614 >                        threadStarted.countDown();
615 >                        done.await();
616 >                        return Boolean.TRUE;
617 >                    }};
618 >                tasks[i] = new FutureTask(task);
619 >                p.execute(tasks[i]);
620 >            }
621 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
622 >            assertEquals(tasks.length, p.getTaskCount());
623 >            assertEquals(tasks.length - 1, q.size());
624 >            assertEquals(1L, p.getActiveCount());
625 >            assertEquals(0L, p.getCompletedTaskCount());
626 >            tasks[4].cancel(true);
627 >            tasks[3].cancel(false);
628 >            p.purge();
629 >            assertEquals(tasks.length - 3, q.size());
630 >            assertEquals(tasks.length - 2, p.getTaskCount());
631 >            p.purge();         // Nothing to do
632 >            assertEquals(tasks.length - 3, q.size());
633 >            assertEquals(tasks.length - 2, p.getTaskCount());
634 >        } finally {
635 >            done.countDown();
636 >            joinPool(p);
637          }
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);
638      }
639  
640      /**
641 <     * shutDownNow returns a list containing tasks that were not run
641 >     * shutdownNow returns a list containing tasks that were not run,
642 >     * and those tasks are drained from the queue
643       */
644 <    public void testShutDownNow() {
645 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
646 <        List l;
647 <        try {
648 <            for (int i = 0; i < 5; i++)
649 <                p1.execute(new MediumPossiblyInterruptedRunnable());
650 <        }
651 <        finally {
644 >    public void testShutdownNow() throws InterruptedException {
645 >        final int poolSize = 2;
646 >        final int count = 5;
647 >        final AtomicInteger ran = new AtomicInteger(0);
648 >        final ThreadPoolExecutor p =
649 >            new ThreadPoolExecutor(poolSize, poolSize,
650 >                                   LONG_DELAY_MS, MILLISECONDS,
651 >                                   new ArrayBlockingQueue<Runnable>(10));
652 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
653 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
654 >            threadsStarted.countDown();
655              try {
656 <                l = p1.shutdownNow();
657 <            } catch (SecurityException ok) { return; }
658 <        }
659 <        assertTrue(p1.isShutdown());
660 <        assertTrue(l.size() <= 4);
656 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
657 >            } catch (InterruptedException success) {}
658 >            ran.getAndIncrement();
659 >        }};
660 >        for (int i = 0; i < count; i++)
661 >            p.execute(waiter);
662 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
663 >        assertEquals(poolSize, p.getActiveCount());
664 >        assertEquals(0, p.getCompletedTaskCount());
665 >        final List<Runnable> queuedTasks;
666 >        try {
667 >            queuedTasks = p.shutdownNow();
668 >        } catch (SecurityException ok) {
669 >            return; // Allowed in case test doesn't have privs
670 >        }
671 >        assertTrue(p.isShutdown());
672 >        assertTrue(p.getQueue().isEmpty());
673 >        assertEquals(count - poolSize, queuedTasks.size());
674 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
675 >        assertTrue(p.isTerminated());
676 >        assertEquals(poolSize, ran.get());
677 >        assertEquals(poolSize, p.getCompletedTaskCount());
678      }
679  
680      // Exception Tests
681  
395
682      /**
683       * Constructor throws if corePoolSize argument is less than zero
684       */
685      public void testConstructor1() {
686          try {
687 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
687 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
688 >                                   new ArrayBlockingQueue<Runnable>(10));
689              shouldThrow();
690          } catch (IllegalArgumentException success) {}
691      }
# Line 408 | Line 695 | public class ThreadPoolExecutorTest exte
695       */
696      public void testConstructor2() {
697          try {
698 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
698 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
699 >                                   new ArrayBlockingQueue<Runnable>(10));
700              shouldThrow();
701          } catch (IllegalArgumentException success) {}
702      }
# Line 418 | Line 706 | public class ThreadPoolExecutorTest exte
706       */
707      public void testConstructor3() {
708          try {
709 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
709 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
710 >                                   new ArrayBlockingQueue<Runnable>(10));
711              shouldThrow();
712          } catch (IllegalArgumentException success) {}
713      }
# Line 428 | Line 717 | public class ThreadPoolExecutorTest exte
717       */
718      public void testConstructor4() {
719          try {
720 <            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
720 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
721 >                                   new ArrayBlockingQueue<Runnable>(10));
722              shouldThrow();
723          } catch (IllegalArgumentException success) {}
724      }
# Line 438 | Line 728 | public class ThreadPoolExecutorTest exte
728       */
729      public void testConstructor5() {
730          try {
731 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
731 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
732 >                                   new ArrayBlockingQueue<Runnable>(10));
733              shouldThrow();
734          } catch (IllegalArgumentException success) {}
735      }
# Line 448 | Line 739 | public class ThreadPoolExecutorTest exte
739       */
740      public void testConstructorNullPointerException() {
741          try {
742 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null);
742 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
743 >                                   (BlockingQueue) null);
744              shouldThrow();
745          } catch (NullPointerException success) {}
746      }
747  
456
457
748      /**
749       * Constructor throws if corePoolSize argument is less than zero
750       */
751      public void testConstructor6() {
752          try {
753 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
753 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
754 >                                   new ArrayBlockingQueue<Runnable>(10),
755 >                                   new SimpleThreadFactory());
756              shouldThrow();
757          } catch (IllegalArgumentException success) {}
758      }
# Line 470 | Line 762 | public class ThreadPoolExecutorTest exte
762       */
763      public void testConstructor7() {
764          try {
765 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
765 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
766 >                                   new ArrayBlockingQueue<Runnable>(10),
767 >                                   new SimpleThreadFactory());
768              shouldThrow();
769          } catch (IllegalArgumentException success) {}
770      }
# Line 480 | Line 774 | public class ThreadPoolExecutorTest exte
774       */
775      public void testConstructor8() {
776          try {
777 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
777 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
778 >                                   new ArrayBlockingQueue<Runnable>(10),
779 >                                   new SimpleThreadFactory());
780              shouldThrow();
781          } catch (IllegalArgumentException success) {}
782      }
# Line 490 | Line 786 | public class ThreadPoolExecutorTest exte
786       */
787      public void testConstructor9() {
788          try {
789 <            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
789 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
790 >                                   new ArrayBlockingQueue<Runnable>(10),
791 >                                   new SimpleThreadFactory());
792              shouldThrow();
793          } catch (IllegalArgumentException success) {}
794      }
# Line 500 | Line 798 | public class ThreadPoolExecutorTest exte
798       */
799      public void testConstructor10() {
800          try {
801 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
801 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
802 >                                   new ArrayBlockingQueue<Runnable>(10),
803 >                                   new SimpleThreadFactory());
804              shouldThrow();
805          } catch (IllegalArgumentException success) {}
806      }
# Line 510 | Line 810 | public class ThreadPoolExecutorTest exte
810       */
811      public void testConstructorNullPointerException2() {
812          try {
813 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
813 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
814 >                                   (BlockingQueue) null,
815 >                                   new SimpleThreadFactory());
816              shouldThrow();
817          } catch (NullPointerException success) {}
818      }
# Line 520 | Line 822 | public class ThreadPoolExecutorTest exte
822       */
823      public void testConstructorNullPointerException3() {
824          try {
825 <            ThreadFactory f = null;
826 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
825 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
826 >                                   new ArrayBlockingQueue<Runnable>(10),
827 >                                   (ThreadFactory) null);
828              shouldThrow();
829          } catch (NullPointerException success) {}
830      }
831  
529
832      /**
833       * Constructor throws if corePoolSize argument is less than zero
834       */
835      public void testConstructor11() {
836          try {
837 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
837 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
838 >                                   new ArrayBlockingQueue<Runnable>(10),
839 >                                   new NoOpREHandler());
840              shouldThrow();
841          } catch (IllegalArgumentException success) {}
842      }
# Line 542 | Line 846 | public class ThreadPoolExecutorTest exte
846       */
847      public void testConstructor12() {
848          try {
849 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
849 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
850 >                                   new ArrayBlockingQueue<Runnable>(10),
851 >                                   new NoOpREHandler());
852              shouldThrow();
853          } catch (IllegalArgumentException success) {}
854      }
# Line 552 | Line 858 | public class ThreadPoolExecutorTest exte
858       */
859      public void testConstructor13() {
860          try {
861 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
861 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
862 >                                   new ArrayBlockingQueue<Runnable>(10),
863 >                                   new NoOpREHandler());
864              shouldThrow();
865          } catch (IllegalArgumentException success) {}
866      }
# Line 562 | Line 870 | public class ThreadPoolExecutorTest exte
870       */
871      public void testConstructor14() {
872          try {
873 <            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
873 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
874 >                                   new ArrayBlockingQueue<Runnable>(10),
875 >                                   new NoOpREHandler());
876              shouldThrow();
877          } catch (IllegalArgumentException success) {}
878      }
# Line 572 | Line 882 | public class ThreadPoolExecutorTest exte
882       */
883      public void testConstructor15() {
884          try {
885 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
885 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
886 >                                   new ArrayBlockingQueue<Runnable>(10),
887 >                                   new NoOpREHandler());
888              shouldThrow();
889          } catch (IllegalArgumentException success) {}
890      }
# Line 582 | Line 894 | public class ThreadPoolExecutorTest exte
894       */
895      public void testConstructorNullPointerException4() {
896          try {
897 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
897 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
898 >                                   (BlockingQueue) null,
899 >                                   new NoOpREHandler());
900              shouldThrow();
901          } catch (NullPointerException success) {}
902      }
# Line 592 | Line 906 | public class ThreadPoolExecutorTest exte
906       */
907      public void testConstructorNullPointerException5() {
908          try {
909 <            RejectedExecutionHandler r = null;
910 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
909 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
910 >                                   new ArrayBlockingQueue<Runnable>(10),
911 >                                   (RejectedExecutionHandler) null);
912              shouldThrow();
913          } catch (NullPointerException success) {}
914      }
915  
601
916      /**
917       * Constructor throws if corePoolSize argument is less than zero
918       */
919      public void testConstructor16() {
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 614 | Line 931 | public class ThreadPoolExecutorTest exte
931       */
932      public void testConstructor17() {
933          try {
934 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
934 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
935 >                                   new ArrayBlockingQueue<Runnable>(10),
936 >                                   new SimpleThreadFactory(),
937 >                                   new NoOpREHandler());
938              shouldThrow();
939          } catch (IllegalArgumentException success) {}
940      }
# Line 624 | Line 944 | public class ThreadPoolExecutorTest exte
944       */
945      public void testConstructor18() {
946          try {
947 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
947 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
948 >                                   new ArrayBlockingQueue<Runnable>(10),
949 >                                   new SimpleThreadFactory(),
950 >                                   new NoOpREHandler());
951              shouldThrow();
952          } catch (IllegalArgumentException success) {}
953      }
# Line 634 | Line 957 | public class ThreadPoolExecutorTest exte
957       */
958      public void testConstructor19() {
959          try {
960 <            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
960 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
961 >                                   new ArrayBlockingQueue<Runnable>(10),
962 >                                   new SimpleThreadFactory(),
963 >                                   new NoOpREHandler());
964              shouldThrow();
965          } catch (IllegalArgumentException success) {}
966      }
# Line 644 | Line 970 | public class ThreadPoolExecutorTest exte
970       */
971      public void testConstructor20() {
972          try {
973 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
973 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
974 >                                   new ArrayBlockingQueue<Runnable>(10),
975 >                                   new SimpleThreadFactory(),
976 >                                   new NoOpREHandler());
977              shouldThrow();
978          } catch (IllegalArgumentException success) {}
979      }
980  
981      /**
982 <     * Constructor throws if workQueue is set to null
982 >     * Constructor throws if workQueue is null
983       */
984      public void testConstructorNullPointerException6() {
985          try {
986 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
986 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
987 >                                   (BlockingQueue) null,
988 >                                   new SimpleThreadFactory(),
989 >                                   new NoOpREHandler());
990              shouldThrow();
991          } catch (NullPointerException success) {}
992      }
993  
994      /**
995 <     * Constructor throws if handler is set to null
995 >     * Constructor throws if handler is null
996       */
997      public void testConstructorNullPointerException7() {
998          try {
999 <            RejectedExecutionHandler r = null;
1000 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
999 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1000 >                                   new ArrayBlockingQueue<Runnable>(10),
1001 >                                   new SimpleThreadFactory(),
1002 >                                   (RejectedExecutionHandler) null);
1003              shouldThrow();
1004          } catch (NullPointerException success) {}
1005      }
1006  
1007      /**
1008 <     * Constructor throws if ThreadFactory is set top null
1008 >     * Constructor throws if ThreadFactory is null
1009       */
1010      public void testConstructorNullPointerException8() {
1011          try {
1012 <            ThreadFactory f = null;
1013 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
1012 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1013 >                                   new ArrayBlockingQueue<Runnable>(10),
1014 >                                   (ThreadFactory) null,
1015 >                                   new NoOpREHandler());
1016              shouldThrow();
1017          } catch (NullPointerException success) {}
1018      }
1019  
1020 +    /**
1021 +     * get of submitted callable throws InterruptedException if interrupted
1022 +     */
1023 +    public void testInterruptedSubmit() throws InterruptedException {
1024 +        final ThreadPoolExecutor p =
1025 +            new ThreadPoolExecutor(1, 1,
1026 +                                   60, SECONDS,
1027 +                                   new ArrayBlockingQueue<Runnable>(10));
1028 +
1029 +        final CountDownLatch threadStarted = new CountDownLatch(1);
1030 +        final CountDownLatch done = new CountDownLatch(1);
1031 +        try {
1032 +            Thread t = newStartedThread(new CheckedInterruptedRunnable() {
1033 +                public void realRun() throws Exception {
1034 +                    Callable task = new CheckedCallable<Boolean>() {
1035 +                        public Boolean realCall() throws InterruptedException {
1036 +                            threadStarted.countDown();
1037 +                            done.await();
1038 +                            return Boolean.TRUE;
1039 +                        }};
1040 +                    p.submit(task).get();
1041 +                }});
1042 +
1043 +            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1044 +            t.interrupt();
1045 +            awaitTermination(t, MEDIUM_DELAY_MS);
1046 +        } finally {
1047 +            done.countDown();
1048 +            joinPool(p);
1049 +        }
1050 +    }
1051  
1052      /**
1053       * execute throws RejectedExecutionException if saturated.
# Line 690 | Line 1057 | public class ThreadPoolExecutorTest exte
1057              new ThreadPoolExecutor(1, 1,
1058                                     LONG_DELAY_MS, MILLISECONDS,
1059                                     new ArrayBlockingQueue<Runnable>(1));
1060 +        final CountDownLatch done = new CountDownLatch(1);
1061          try {
1062 +            Runnable task = new CheckedRunnable() {
1063 +                public void realRun() throws InterruptedException {
1064 +                    done.await();
1065 +                }};
1066              for (int i = 0; i < 2; ++i)
1067 <                p.execute(new MediumRunnable());
1067 >                p.execute(task);
1068              for (int i = 0; i < 2; ++i) {
1069                  try {
1070 <                    p.execute(new MediumRunnable());
1070 >                    p.execute(task);
1071                      shouldThrow();
1072                  } catch (RejectedExecutionException success) {}
1073 +                assertTrue(p.getTaskCount() <= 2);
1074              }
1075          } finally {
1076 +            done.countDown();
1077 +            joinPool(p);
1078 +        }
1079 +    }
1080 +
1081 +    /**
1082 +     * submit(runnable) throws RejectedExecutionException if saturated.
1083 +     */
1084 +    public void testSaturatedSubmitRunnable() {
1085 +        ThreadPoolExecutor p =
1086 +            new ThreadPoolExecutor(1, 1,
1087 +                                   LONG_DELAY_MS, MILLISECONDS,
1088 +                                   new ArrayBlockingQueue<Runnable>(1));
1089 +        final CountDownLatch done = new CountDownLatch(1);
1090 +        try {
1091 +            Runnable task = new CheckedRunnable() {
1092 +                public void realRun() throws InterruptedException {
1093 +                    done.await();
1094 +                }};
1095 +            for (int i = 0; i < 2; ++i)
1096 +                p.submit(task);
1097 +            for (int i = 0; i < 2; ++i) {
1098 +                try {
1099 +                    p.execute(task);
1100 +                    shouldThrow();
1101 +                } catch (RejectedExecutionException success) {}
1102 +                assertTrue(p.getTaskCount() <= 2);
1103 +            }
1104 +        } finally {
1105 +            done.countDown();
1106 +            joinPool(p);
1107 +        }
1108 +    }
1109 +
1110 +    /**
1111 +     * submit(callable) throws RejectedExecutionException if saturated.
1112 +     */
1113 +    public void testSaturatedSubmitCallable() {
1114 +        ThreadPoolExecutor p =
1115 +            new ThreadPoolExecutor(1, 1,
1116 +                                   LONG_DELAY_MS, MILLISECONDS,
1117 +                                   new ArrayBlockingQueue<Runnable>(1));
1118 +        final CountDownLatch done = new CountDownLatch(1);
1119 +        try {
1120 +            Runnable task = new CheckedRunnable() {
1121 +                public void realRun() throws InterruptedException {
1122 +                    done.await();
1123 +                }};
1124 +            for (int i = 0; i < 2; ++i)
1125 +                p.submit(Executors.callable(task));
1126 +            for (int i = 0; i < 2; ++i) {
1127 +                try {
1128 +                    p.execute(task);
1129 +                    shouldThrow();
1130 +                } catch (RejectedExecutionException success) {}
1131 +                assertTrue(p.getTaskCount() <= 2);
1132 +            }
1133 +        } finally {
1134 +            done.countDown();
1135              joinPool(p);
1136          }
1137      }
# Line 709 | Line 1141 | public class ThreadPoolExecutorTest exte
1141       */
1142      public void testSaturatedExecute2() {
1143          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1144 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1144 >        final ThreadPoolExecutor p =
1145 >            new ThreadPoolExecutor(1, 1,
1146 >                                   LONG_DELAY_MS,
1147 >                                   MILLISECONDS,
1148 >                                   new ArrayBlockingQueue<Runnable>(1),
1149 >                                   h);
1150          try {
714
1151              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1152 <            for (int i = 0; i < 5; ++i) {
1152 >            for (int i = 0; i < tasks.length; ++i)
1153                  tasks[i] = new TrackedNoOpRunnable();
718            }
1154              TrackedLongRunnable mr = new TrackedLongRunnable();
1155              p.execute(mr);
1156 <            for (int i = 0; i < 5; ++i) {
1156 >            for (int i = 0; i < tasks.length; ++i)
1157                  p.execute(tasks[i]);
1158 <            }
724 <            for (int i = 1; i < 5; ++i) {
1158 >            for (int i = 1; i < tasks.length; ++i)
1159                  assertTrue(tasks[i].done);
726            }
1160              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1161          } finally {
1162              joinPool(p);
# Line 735 | Line 1168 | public class ThreadPoolExecutorTest exte
1168       */
1169      public void testSaturatedExecute3() {
1170          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1171 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1171 >        final ThreadPoolExecutor p =
1172 >            new ThreadPoolExecutor(1, 1,
1173 >                                   LONG_DELAY_MS, MILLISECONDS,
1174 >                                   new ArrayBlockingQueue<Runnable>(1),
1175 >                                   h);
1176          try {
740
1177              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1178 <            for (int i = 0; i < 5; ++i) {
1178 >            for (int i = 0; i < tasks.length; ++i)
1179                  tasks[i] = new TrackedNoOpRunnable();
744            }
1180              p.execute(new TrackedLongRunnable());
1181 <            for (int i = 0; i < 5; ++i) {
1182 <                p.execute(tasks[i]);
1183 <            }
1184 <            for (int i = 0; i < 5; ++i) {
750 <                assertFalse(tasks[i].done);
751 <            }
1181 >            for (TrackedNoOpRunnable task : tasks)
1182 >                p.execute(task);
1183 >            for (TrackedNoOpRunnable task : tasks)
1184 >                assertFalse(task.done);
1185              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1186          } finally {
1187              joinPool(p);
# Line 760 | Line 1193 | public class ThreadPoolExecutorTest exte
1193       */
1194      public void testSaturatedExecute4() {
1195          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1196 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1196 >        final ThreadPoolExecutor p =
1197 >            new ThreadPoolExecutor(1, 1,
1198 >                                   LONG_DELAY_MS, MILLISECONDS,
1199 >                                   new ArrayBlockingQueue<Runnable>(1),
1200 >                                   h);
1201          try {
1202              p.execute(new TrackedLongRunnable());
1203              TrackedLongRunnable r2 = new TrackedLongRunnable();
# Line 780 | Line 1217 | public class ThreadPoolExecutorTest exte
1217       * execute throws RejectedExecutionException if shutdown
1218       */
1219      public void testRejectedExecutionExceptionOnShutdown() {
1220 <        ThreadPoolExecutor tpe =
1221 <            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1222 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1220 >        ThreadPoolExecutor p =
1221 >            new ThreadPoolExecutor(1, 1,
1222 >                                   LONG_DELAY_MS, MILLISECONDS,
1223 >                                   new ArrayBlockingQueue<Runnable>(1));
1224 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1225          try {
1226 <            tpe.execute(new NoOpRunnable());
1226 >            p.execute(new NoOpRunnable());
1227              shouldThrow();
1228          } catch (RejectedExecutionException success) {}
1229  
1230 <        joinPool(tpe);
1230 >        joinPool(p);
1231      }
1232  
1233      /**
# Line 796 | Line 1235 | public class ThreadPoolExecutorTest exte
1235       */
1236      public void testCallerRunsOnShutdown() {
1237          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1238 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1238 >        final ThreadPoolExecutor p =
1239 >            new ThreadPoolExecutor(1, 1,
1240 >                                   LONG_DELAY_MS, MILLISECONDS,
1241 >                                   new ArrayBlockingQueue<Runnable>(1), h);
1242  
1243          try { p.shutdown(); } catch (SecurityException ok) { return; }
1244          try {
# Line 813 | Line 1255 | public class ThreadPoolExecutorTest exte
1255       */
1256      public void testDiscardOnShutdown() {
1257          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1258 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1258 >        ThreadPoolExecutor p =
1259 >            new ThreadPoolExecutor(1, 1,
1260 >                                   LONG_DELAY_MS, MILLISECONDS,
1261 >                                   new ArrayBlockingQueue<Runnable>(1),
1262 >                                   h);
1263  
1264          try { p.shutdown(); } catch (SecurityException ok) { return; }
1265          try {
# Line 825 | Line 1271 | public class ThreadPoolExecutorTest exte
1271          }
1272      }
1273  
828
1274      /**
1275       * execute using DiscardOldestPolicy drops task on shutdown
1276       */
1277      public void testDiscardOldestOnShutdown() {
1278          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1279 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1279 >        ThreadPoolExecutor p =
1280 >            new ThreadPoolExecutor(1, 1,
1281 >                                   LONG_DELAY_MS, MILLISECONDS,
1282 >                                   new ArrayBlockingQueue<Runnable>(1),
1283 >                                   h);
1284  
1285          try { p.shutdown(); } catch (SecurityException ok) { return; }
1286          try {
# Line 843 | Line 1292 | public class ThreadPoolExecutorTest exte
1292          }
1293      }
1294  
846
1295      /**
1296       * execute(null) throws NPE
1297       */
1298      public void testExecuteNull() {
1299 <        ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1299 >        ThreadPoolExecutor p =
1300 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1301 >                                   new ArrayBlockingQueue<Runnable>(10));
1302          try {
1303 <            tpe.execute(null);
1303 >            p.execute(null);
1304              shouldThrow();
1305          } catch (NullPointerException success) {}
1306  
1307 <        joinPool(tpe);
1307 >        joinPool(p);
1308      }
1309  
1310      /**
1311       * setCorePoolSize of negative value throws IllegalArgumentException
1312       */
1313      public void testCorePoolSizeIllegalArgumentException() {
1314 <        ThreadPoolExecutor tpe =
1314 >        ThreadPoolExecutor p =
1315              new ThreadPoolExecutor(1, 2,
1316                                     LONG_DELAY_MS, MILLISECONDS,
1317                                     new ArrayBlockingQueue<Runnable>(10));
1318          try {
1319 <            tpe.setCorePoolSize(-1);
1319 >            p.setCorePoolSize(-1);
1320              shouldThrow();
1321          } catch (IllegalArgumentException success) {
1322          } finally {
1323 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1323 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1324          }
1325 <        joinPool(tpe);
1325 >        joinPool(p);
1326      }
1327  
1328      /**
# Line 880 | Line 1330 | public class ThreadPoolExecutorTest exte
1330       * given a value less the core pool size
1331       */
1332      public void testMaximumPoolSizeIllegalArgumentException() {
1333 <        ThreadPoolExecutor tpe =
1333 >        ThreadPoolExecutor p =
1334              new ThreadPoolExecutor(2, 3,
1335                                     LONG_DELAY_MS, MILLISECONDS,
1336                                     new ArrayBlockingQueue<Runnable>(10));
1337          try {
1338 <            tpe.setMaximumPoolSize(1);
1338 >            p.setMaximumPoolSize(1);
1339              shouldThrow();
1340          } catch (IllegalArgumentException success) {
1341          } finally {
1342 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1342 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1343          }
1344 <        joinPool(tpe);
1344 >        joinPool(p);
1345      }
1346  
1347      /**
# Line 899 | Line 1349 | public class ThreadPoolExecutorTest exte
1349       * if given a negative value
1350       */
1351      public void testMaximumPoolSizeIllegalArgumentException2() {
1352 <        ThreadPoolExecutor tpe =
1352 >        ThreadPoolExecutor p =
1353              new ThreadPoolExecutor(2, 3,
1354                                     LONG_DELAY_MS, MILLISECONDS,
1355                                     new ArrayBlockingQueue<Runnable>(10));
1356          try {
1357 <            tpe.setMaximumPoolSize(-1);
1357 >            p.setMaximumPoolSize(-1);
1358              shouldThrow();
1359          } catch (IllegalArgumentException success) {
1360          } finally {
1361 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1361 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1362          }
1363 <        joinPool(tpe);
1363 >        joinPool(p);
1364      }
1365  
1366 +    /**
1367 +     * Configuration changes that allow core pool size greater than
1368 +     * max pool size result in IllegalArgumentException.
1369 +     */
1370 +    public void testPoolSizeInvariants() {
1371 +        ThreadPoolExecutor p =
1372 +            new ThreadPoolExecutor(1, 1,
1373 +                                   LONG_DELAY_MS, MILLISECONDS,
1374 +                                   new ArrayBlockingQueue<Runnable>(10));
1375 +        for (int s = 1; s < 5; s++) {
1376 +            p.setMaximumPoolSize(s);
1377 +            p.setCorePoolSize(s);
1378 +            try {
1379 +                p.setMaximumPoolSize(s - 1);
1380 +                shouldThrow();
1381 +            } catch (IllegalArgumentException success) {}
1382 +            assertEquals(s, p.getCorePoolSize());
1383 +            assertEquals(s, p.getMaximumPoolSize());
1384 +            try {
1385 +                p.setCorePoolSize(s + 1);
1386 +                shouldThrow();
1387 +            } catch (IllegalArgumentException success) {}
1388 +            assertEquals(s, p.getCorePoolSize());
1389 +            assertEquals(s, p.getMaximumPoolSize());
1390 +        }
1391 +        joinPool(p);
1392 +    }
1393  
1394      /**
1395       * setKeepAliveTime throws IllegalArgumentException
1396       * when given a negative value
1397       */
1398      public void testKeepAliveTimeIllegalArgumentException() {
1399 <        ThreadPoolExecutor tpe =
1399 >        ThreadPoolExecutor p =
1400              new ThreadPoolExecutor(2, 3,
1401                                     LONG_DELAY_MS, MILLISECONDS,
1402                                     new ArrayBlockingQueue<Runnable>(10));
1403          try {
1404 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1404 >            p.setKeepAliveTime(-1,MILLISECONDS);
1405              shouldThrow();
1406          } catch (IllegalArgumentException success) {
1407          } finally {
1408 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1408 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1409          }
1410 <        joinPool(tpe);
1410 >        joinPool(p);
1411      }
1412  
1413      /**
1414       * terminated() is called on termination
1415       */
1416      public void testTerminated() {
1417 <        ExtendedTPE tpe = new ExtendedTPE();
1418 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1419 <        assertTrue(tpe.terminatedCalled);
1420 <        joinPool(tpe);
1417 >        ExtendedTPE p = new ExtendedTPE();
1418 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1419 >        assertTrue(p.terminatedCalled());
1420 >        joinPool(p);
1421      }
1422  
1423      /**
1424       * beforeExecute and afterExecute are called when executing task
1425       */
1426      public void testBeforeAfter() throws InterruptedException {
1427 <        ExtendedTPE tpe = new ExtendedTPE();
1427 >        ExtendedTPE p = new ExtendedTPE();
1428          try {
1429 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1430 <            tpe.execute(r);
1431 <            Thread.sleep(SHORT_DELAY_MS);
1432 <            assertTrue(r.done);
1433 <            assertTrue(tpe.beforeCalled);
1434 <            assertTrue(tpe.afterCalled);
1435 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1429 >            final CountDownLatch done = new CountDownLatch(1);
1430 >            p.execute(new CheckedRunnable() {
1431 >                public void realRun() {
1432 >                    done.countDown();
1433 >                }});
1434 >            await(p.afterCalled);
1435 >            assertEquals(0, done.getCount());
1436 >            assertTrue(p.afterCalled());
1437 >            assertTrue(p.beforeCalled());
1438 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1439          } finally {
1440 <            joinPool(tpe);
1440 >            joinPool(p);
1441          }
1442      }
1443  
# Line 965 | Line 1445 | public class ThreadPoolExecutorTest exte
1445       * completed submit of callable returns result
1446       */
1447      public void testSubmitCallable() throws Exception {
1448 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1448 >        ExecutorService e =
1449 >            new ThreadPoolExecutor(2, 2,
1450 >                                   LONG_DELAY_MS, MILLISECONDS,
1451 >                                   new ArrayBlockingQueue<Runnable>(10));
1452          try {
1453              Future<String> future = e.submit(new StringTask());
1454              String result = future.get();
# Line 979 | Line 1462 | public class ThreadPoolExecutorTest exte
1462       * completed submit of runnable returns successfully
1463       */
1464      public void testSubmitRunnable() throws Exception {
1465 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1465 >        ExecutorService e =
1466 >            new ThreadPoolExecutor(2, 2,
1467 >                                   LONG_DELAY_MS, MILLISECONDS,
1468 >                                   new ArrayBlockingQueue<Runnable>(10));
1469          try {
1470              Future<?> future = e.submit(new NoOpRunnable());
1471              future.get();
# Line 993 | Line 1479 | public class ThreadPoolExecutorTest exte
1479       * completed submit of (runnable, result) returns result
1480       */
1481      public void testSubmitRunnable2() throws Exception {
1482 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1482 >        ExecutorService e =
1483 >            new ThreadPoolExecutor(2, 2,
1484 >                                   LONG_DELAY_MS, MILLISECONDS,
1485 >                                   new ArrayBlockingQueue<Runnable>(10));
1486          try {
1487              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1488              String result = future.get();
# Line 1003 | Line 1492 | public class ThreadPoolExecutorTest exte
1492          }
1493      }
1494  
1006
1495      /**
1496       * invokeAny(null) throws NPE
1497       */
1498      public void testInvokeAny1() throws Exception {
1499 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1499 >        ExecutorService e =
1500 >            new ThreadPoolExecutor(2, 2,
1501 >                                   LONG_DELAY_MS, MILLISECONDS,
1502 >                                   new ArrayBlockingQueue<Runnable>(10));
1503          try {
1504              e.invokeAny(null);
1505              shouldThrow();
# Line 1022 | Line 1513 | public class ThreadPoolExecutorTest exte
1513       * invokeAny(empty collection) throws IAE
1514       */
1515      public void testInvokeAny2() throws Exception {
1516 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1516 >        ExecutorService e =
1517 >            new ThreadPoolExecutor(2, 2,
1518 >                                   LONG_DELAY_MS, MILLISECONDS,
1519 >                                   new ArrayBlockingQueue<Runnable>(10));
1520          try {
1521              e.invokeAny(new ArrayList<Callable<String>>());
1522              shouldThrow();
# Line 1036 | Line 1530 | public class ThreadPoolExecutorTest exte
1530       * invokeAny(c) throws NPE if c has null elements
1531       */
1532      public void testInvokeAny3() throws Exception {
1533 <        CountDownLatch latch = new CountDownLatch(1);
1534 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1533 >        final CountDownLatch latch = new CountDownLatch(1);
1534 >        final ExecutorService e =
1535 >            new ThreadPoolExecutor(2, 2,
1536 >                                   LONG_DELAY_MS, MILLISECONDS,
1537 >                                   new ArrayBlockingQueue<Runnable>(10));
1538          List<Callable<String>> l = new ArrayList<Callable<String>>();
1539          l.add(latchAwaitingStringTask(latch));
1540          l.add(null);
# Line 1055 | Line 1552 | public class ThreadPoolExecutorTest exte
1552       * invokeAny(c) throws ExecutionException if no task completes
1553       */
1554      public void testInvokeAny4() throws Exception {
1555 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1555 >        ExecutorService e =
1556 >            new ThreadPoolExecutor(2, 2,
1557 >                                   LONG_DELAY_MS, MILLISECONDS,
1558 >                                   new ArrayBlockingQueue<Runnable>(10));
1559          List<Callable<String>> l = new ArrayList<Callable<String>>();
1560          l.add(new NPETask());
1561          try {
# Line 1072 | Line 1572 | public class ThreadPoolExecutorTest exte
1572       * invokeAny(c) returns result of some task
1573       */
1574      public void testInvokeAny5() throws Exception {
1575 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1575 >        ExecutorService e =
1576 >            new ThreadPoolExecutor(2, 2,
1577 >                                   LONG_DELAY_MS, MILLISECONDS,
1578 >                                   new ArrayBlockingQueue<Runnable>(10));
1579          try {
1580              List<Callable<String>> l = new ArrayList<Callable<String>>();
1581              l.add(new StringTask());
# Line 1088 | Line 1591 | public class ThreadPoolExecutorTest exte
1591       * invokeAll(null) throws NPE
1592       */
1593      public void testInvokeAll1() throws Exception {
1594 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1594 >        ExecutorService e =
1595 >            new ThreadPoolExecutor(2, 2,
1596 >                                   LONG_DELAY_MS, MILLISECONDS,
1597 >                                   new ArrayBlockingQueue<Runnable>(10));
1598          try {
1599              e.invokeAll(null);
1600              shouldThrow();
# Line 1102 | Line 1608 | public class ThreadPoolExecutorTest exte
1608       * invokeAll(empty collection) returns empty collection
1609       */
1610      public void testInvokeAll2() throws InterruptedException {
1611 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1611 >        ExecutorService e =
1612 >            new ThreadPoolExecutor(2, 2,
1613 >                                   LONG_DELAY_MS, MILLISECONDS,
1614 >                                   new ArrayBlockingQueue<Runnable>(10));
1615          try {
1616              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1617              assertTrue(r.isEmpty());
# Line 1115 | Line 1624 | public class ThreadPoolExecutorTest exte
1624       * invokeAll(c) throws NPE if c has null elements
1625       */
1626      public void testInvokeAll3() throws Exception {
1627 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1627 >        ExecutorService e =
1628 >            new ThreadPoolExecutor(2, 2,
1629 >                                   LONG_DELAY_MS, MILLISECONDS,
1630 >                                   new ArrayBlockingQueue<Runnable>(10));
1631          List<Callable<String>> l = new ArrayList<Callable<String>>();
1632          l.add(new StringTask());
1633          l.add(null);
# Line 1132 | Line 1644 | public class ThreadPoolExecutorTest exte
1644       * get of element of invokeAll(c) throws exception on failed task
1645       */
1646      public void testInvokeAll4() throws Exception {
1647 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1647 >        ExecutorService e =
1648 >            new ThreadPoolExecutor(2, 2,
1649 >                                   LONG_DELAY_MS, MILLISECONDS,
1650 >                                   new ArrayBlockingQueue<Runnable>(10));
1651          try {
1652              List<Callable<String>> l = new ArrayList<Callable<String>>();
1653              l.add(new NPETask());
# Line 1153 | Line 1668 | public class ThreadPoolExecutorTest exte
1668       * invokeAll(c) returns results of all completed tasks
1669       */
1670      public void testInvokeAll5() throws Exception {
1671 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1671 >        ExecutorService e =
1672 >            new ThreadPoolExecutor(2, 2,
1673 >                                   LONG_DELAY_MS, MILLISECONDS,
1674 >                                   new ArrayBlockingQueue<Runnable>(10));
1675          try {
1676              List<Callable<String>> l = new ArrayList<Callable<String>>();
1677              l.add(new StringTask());
# Line 1167 | Line 1685 | public class ThreadPoolExecutorTest exte
1685          }
1686      }
1687  
1170
1171
1688      /**
1689       * timed invokeAny(null) throws NPE
1690       */
1691      public void testTimedInvokeAny1() throws Exception {
1692 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1692 >        ExecutorService e =
1693 >            new ThreadPoolExecutor(2, 2,
1694 >                                   LONG_DELAY_MS, MILLISECONDS,
1695 >                                   new ArrayBlockingQueue<Runnable>(10));
1696          try {
1697              e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1698              shouldThrow();
# Line 1187 | Line 1706 | public class ThreadPoolExecutorTest exte
1706       * timed invokeAny(,,null) throws NPE
1707       */
1708      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1709 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1709 >        ExecutorService e =
1710 >            new ThreadPoolExecutor(2, 2,
1711 >                                   LONG_DELAY_MS, MILLISECONDS,
1712 >                                   new ArrayBlockingQueue<Runnable>(10));
1713          List<Callable<String>> l = new ArrayList<Callable<String>>();
1714          l.add(new StringTask());
1715          try {
# Line 1203 | Line 1725 | public class ThreadPoolExecutorTest exte
1725       * timed invokeAny(empty collection) throws IAE
1726       */
1727      public void testTimedInvokeAny2() throws Exception {
1728 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1728 >        ExecutorService e =
1729 >            new ThreadPoolExecutor(2, 2,
1730 >                                   LONG_DELAY_MS, MILLISECONDS,
1731 >                                   new ArrayBlockingQueue<Runnable>(10));
1732          try {
1733              e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1734              shouldThrow();
# Line 1217 | Line 1742 | public class ThreadPoolExecutorTest exte
1742       * timed invokeAny(c) throws NPE if c has null elements
1743       */
1744      public void testTimedInvokeAny3() throws Exception {
1745 <        CountDownLatch latch = new CountDownLatch(1);
1746 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1745 >        final CountDownLatch latch = new CountDownLatch(1);
1746 >        final ExecutorService e =
1747 >            new ThreadPoolExecutor(2, 2,
1748 >                                   LONG_DELAY_MS, MILLISECONDS,
1749 >                                   new ArrayBlockingQueue<Runnable>(10));
1750          List<Callable<String>> l = new ArrayList<Callable<String>>();
1751          l.add(latchAwaitingStringTask(latch));
1752          l.add(null);
# Line 1236 | Line 1764 | public class ThreadPoolExecutorTest exte
1764       * timed invokeAny(c) throws ExecutionException if no task completes
1765       */
1766      public void testTimedInvokeAny4() throws Exception {
1767 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1767 >        ExecutorService e =
1768 >            new ThreadPoolExecutor(2, 2,
1769 >                                   LONG_DELAY_MS, MILLISECONDS,
1770 >                                   new ArrayBlockingQueue<Runnable>(10));
1771          List<Callable<String>> l = new ArrayList<Callable<String>>();
1772          l.add(new NPETask());
1773          try {
# Line 1253 | Line 1784 | public class ThreadPoolExecutorTest exte
1784       * timed invokeAny(c) returns result of some task
1785       */
1786      public void testTimedInvokeAny5() throws Exception {
1787 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1787 >        ExecutorService e =
1788 >            new ThreadPoolExecutor(2, 2,
1789 >                                   LONG_DELAY_MS, MILLISECONDS,
1790 >                                   new ArrayBlockingQueue<Runnable>(10));
1791          try {
1792              List<Callable<String>> l = new ArrayList<Callable<String>>();
1793              l.add(new StringTask());
# Line 1269 | Line 1803 | public class ThreadPoolExecutorTest exte
1803       * timed invokeAll(null) throws NPE
1804       */
1805      public void testTimedInvokeAll1() throws Exception {
1806 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1806 >        ExecutorService e =
1807 >            new ThreadPoolExecutor(2, 2,
1808 >                                   LONG_DELAY_MS, MILLISECONDS,
1809 >                                   new ArrayBlockingQueue<Runnable>(10));
1810          try {
1811              e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1812              shouldThrow();
# Line 1283 | Line 1820 | public class ThreadPoolExecutorTest exte
1820       * timed invokeAll(,,null) throws NPE
1821       */
1822      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1823 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1823 >        ExecutorService e =
1824 >            new ThreadPoolExecutor(2, 2,
1825 >                                   LONG_DELAY_MS, MILLISECONDS,
1826 >                                   new ArrayBlockingQueue<Runnable>(10));
1827          List<Callable<String>> l = new ArrayList<Callable<String>>();
1828          l.add(new StringTask());
1829          try {
# Line 1299 | Line 1839 | public class ThreadPoolExecutorTest exte
1839       * timed invokeAll(empty collection) returns empty collection
1840       */
1841      public void testTimedInvokeAll2() throws InterruptedException {
1842 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1842 >        ExecutorService e =
1843 >            new ThreadPoolExecutor(2, 2,
1844 >                                   LONG_DELAY_MS, MILLISECONDS,
1845 >                                   new ArrayBlockingQueue<Runnable>(10));
1846          try {
1847              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1848              assertTrue(r.isEmpty());
# Line 1312 | Line 1855 | public class ThreadPoolExecutorTest exte
1855       * timed invokeAll(c) throws NPE if c has null elements
1856       */
1857      public void testTimedInvokeAll3() throws Exception {
1858 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1858 >        ExecutorService e =
1859 >            new ThreadPoolExecutor(2, 2,
1860 >                                   LONG_DELAY_MS, MILLISECONDS,
1861 >                                   new ArrayBlockingQueue<Runnable>(10));
1862          List<Callable<String>> l = new ArrayList<Callable<String>>();
1863          l.add(new StringTask());
1864          l.add(null);
# Line 1329 | Line 1875 | public class ThreadPoolExecutorTest exte
1875       * get of element of invokeAll(c) throws exception on failed task
1876       */
1877      public void testTimedInvokeAll4() throws Exception {
1878 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1878 >        ExecutorService e =
1879 >            new ThreadPoolExecutor(2, 2,
1880 >                                   LONG_DELAY_MS, MILLISECONDS,
1881 >                                   new ArrayBlockingQueue<Runnable>(10));
1882          List<Callable<String>> l = new ArrayList<Callable<String>>();
1883          l.add(new NPETask());
1884          List<Future<String>> futures =
# Line 1349 | Line 1898 | public class ThreadPoolExecutorTest exte
1898       * timed invokeAll(c) returns results of all completed tasks
1899       */
1900      public void testTimedInvokeAll5() throws Exception {
1901 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1901 >        ExecutorService e =
1902 >            new ThreadPoolExecutor(2, 2,
1903 >                                   LONG_DELAY_MS, MILLISECONDS,
1904 >                                   new ArrayBlockingQueue<Runnable>(10));
1905          try {
1906              List<Callable<String>> l = new ArrayList<Callable<String>>();
1907              l.add(new StringTask());
1908              l.add(new StringTask());
1909              List<Future<String>> futures =
1910 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1910 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1911              assertEquals(2, futures.size());
1912              for (Future<String> future : futures)
1913                  assertSame(TEST_STRING, future.get());
# Line 1368 | Line 1920 | public class ThreadPoolExecutorTest exte
1920       * timed invokeAll(c) cancels tasks not completed by timeout
1921       */
1922      public void testTimedInvokeAll6() throws Exception {
1923 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1923 >        ExecutorService e =
1924 >            new ThreadPoolExecutor(2, 2,
1925 >                                   LONG_DELAY_MS, MILLISECONDS,
1926 >                                   new ArrayBlockingQueue<Runnable>(10));
1927          try {
1928 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1929 <            l.add(new StringTask());
1930 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1931 <            l.add(new StringTask());
1932 <            List<Future<String>> futures =
1933 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1934 <            assertEquals(3, futures.size());
1935 <            Iterator<Future<String>> it = futures.iterator();
1936 <            Future<String> f1 = it.next();
1937 <            Future<String> f2 = it.next();
1938 <            Future<String> f3 = it.next();
1939 <            assertTrue(f1.isDone());
1940 <            assertTrue(f2.isDone());
1941 <            assertTrue(f3.isDone());
1942 <            assertFalse(f1.isCancelled());
1943 <            assertTrue(f2.isCancelled());
1928 >            for (long timeout = timeoutMillis();;) {
1929 >                List<Callable<String>> tasks = new ArrayList<>();
1930 >                tasks.add(new StringTask("0"));
1931 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1932 >                tasks.add(new StringTask("2"));
1933 >                long startTime = System.nanoTime();
1934 >                List<Future<String>> futures =
1935 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1936 >                assertEquals(tasks.size(), futures.size());
1937 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1938 >                for (Future future : futures)
1939 >                    assertTrue(future.isDone());
1940 >                assertTrue(futures.get(1).isCancelled());
1941 >                try {
1942 >                    assertEquals("0", futures.get(0).get());
1943 >                    assertEquals("2", futures.get(2).get());
1944 >                    break;
1945 >                } catch (CancellationException retryWithLongerTimeout) {
1946 >                    timeout *= 2;
1947 >                    if (timeout >= LONG_DELAY_MS / 2)
1948 >                        fail("expected exactly one task to be cancelled");
1949 >                }
1950 >            }
1951          } finally {
1952              joinPool(e);
1953          }
# Line 1396 | Line 1958 | public class ThreadPoolExecutorTest exte
1958       * thread factory fails to create more
1959       */
1960      public void testFailingThreadFactory() throws InterruptedException {
1961 <        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1961 >        final ExecutorService e =
1962 >            new ThreadPoolExecutor(100, 100,
1963 >                                   LONG_DELAY_MS, MILLISECONDS,
1964 >                                   new LinkedBlockingQueue<Runnable>(),
1965 >                                   new FailingThreadFactory());
1966          try {
1967 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1968 <            for (int k = 0; k < 100; ++k) {
1969 <                e.execute(new NoOpRunnable());
1970 <            }
1971 <            Thread.sleep(LONG_DELAY_MS);
1967 >            final int TASKS = 100;
1968 >            final CountDownLatch done = new CountDownLatch(TASKS);
1969 >            for (int k = 0; k < TASKS; ++k)
1970 >                e.execute(new CheckedRunnable() {
1971 >                    public void realRun() {
1972 >                        done.countDown();
1973 >                    }});
1974 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1975          } finally {
1976              joinPool(e);
1977          }
# Line 1412 | Line 1981 | public class ThreadPoolExecutorTest exte
1981       * allowsCoreThreadTimeOut is by default false.
1982       */
1983      public void testAllowsCoreThreadTimeOut() {
1984 <        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1985 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1986 <        joinPool(tpe);
1984 >        final ThreadPoolExecutor p =
1985 >            new ThreadPoolExecutor(2, 2,
1986 >                                   1000, MILLISECONDS,
1987 >                                   new ArrayBlockingQueue<Runnable>(10));
1988 >        assertFalse(p.allowsCoreThreadTimeOut());
1989 >        joinPool(p);
1990      }
1991  
1992      /**
1993       * allowCoreThreadTimeOut(true) causes idle threads to time out
1994       */
1995 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1996 <        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1997 <        tpe.allowCoreThreadTimeOut(true);
1998 <        tpe.execute(new NoOpRunnable());
1995 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1996 >        long keepAliveTime = timeoutMillis();
1997 >        final ThreadPoolExecutor p =
1998 >            new ThreadPoolExecutor(2, 10,
1999 >                                   keepAliveTime, MILLISECONDS,
2000 >                                   new ArrayBlockingQueue<Runnable>(10));
2001 >        final CountDownLatch threadStarted = new CountDownLatch(1);
2002          try {
2003 <            Thread.sleep(MEDIUM_DELAY_MS);
2004 <            assertEquals(0, tpe.getPoolSize());
2003 >            p.allowCoreThreadTimeOut(true);
2004 >            p.execute(new CheckedRunnable() {
2005 >                public void realRun() {
2006 >                    threadStarted.countDown();
2007 >                    assertEquals(1, p.getPoolSize());
2008 >                }});
2009 >            await(threadStarted);
2010 >            delay(keepAliveTime);
2011 >            long startTime = System.nanoTime();
2012 >            while (p.getPoolSize() > 0
2013 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
2014 >                Thread.yield();
2015 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
2016 >            assertEquals(0, p.getPoolSize());
2017          } finally {
2018 <            joinPool(tpe);
2018 >            joinPool(p);
2019          }
2020      }
2021  
2022      /**
2023       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2024       */
2025 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
2026 <        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
2027 <        tpe.allowCoreThreadTimeOut(false);
2028 <        tpe.execute(new NoOpRunnable());
2025 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
2026 >        long keepAliveTime = timeoutMillis();
2027 >        final ThreadPoolExecutor p =
2028 >            new ThreadPoolExecutor(2, 10,
2029 >                                   keepAliveTime, MILLISECONDS,
2030 >                                   new ArrayBlockingQueue<Runnable>(10));
2031 >        final CountDownLatch threadStarted = new CountDownLatch(1);
2032          try {
2033 <            Thread.sleep(MEDIUM_DELAY_MS);
2034 <            assertTrue(tpe.getPoolSize() >= 1);
2033 >            p.allowCoreThreadTimeOut(false);
2034 >            p.execute(new CheckedRunnable() {
2035 >                public void realRun() throws InterruptedException {
2036 >                    threadStarted.countDown();
2037 >                    assertTrue(p.getPoolSize() >= 1);
2038 >                }});
2039 >            delay(2 * keepAliveTime);
2040 >            assertTrue(p.getPoolSize() >= 1);
2041          } finally {
2042 <            joinPool(tpe);
2042 >            joinPool(p);
2043          }
2044      }
2045  
# Line 1453 | Line 2049 | public class ThreadPoolExecutorTest exte
2049       */
2050      public void testRejectedRecycledTask() throws InterruptedException {
2051          final int nTasks = 1000;
2052 <        final AtomicInteger nRun = new AtomicInteger(0);
2052 >        final CountDownLatch done = new CountDownLatch(nTasks);
2053          final Runnable recycledTask = new Runnable() {
2054 <                public void run() {
2055 <                    nRun.getAndIncrement();
2056 <                } };
2054 >            public void run() {
2055 >                done.countDown();
2056 >            }};
2057          final ThreadPoolExecutor p =
2058 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2058 >            new ThreadPoolExecutor(1, 30,
2059 >                                   60, SECONDS,
2060                                     new ArrayBlockingQueue(30));
2061          try {
2062              for (int i = 0; i < nTasks; ++i) {
# Line 1468 | Line 2065 | public class ThreadPoolExecutorTest exte
2065                          p.execute(recycledTask);
2066                          break;
2067                      }
2068 <                    catch (RejectedExecutionException ignore) {
1472 <                    }
2068 >                    catch (RejectedExecutionException ignore) {}
2069                  }
2070              }
2071 <            Thread.sleep(5000); // enough time to run all tasks
2072 <            assertEquals(nRun.get(), nTasks);
2071 >            // enough time to run all tasks
2072 >            assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2073 >        } finally {
2074 >            joinPool(p);
2075 >        }
2076 >    }
2077 >
2078 >    /**
2079 >     * get(cancelled task) throws CancellationException
2080 >     */
2081 >    public void testGet_cancelled() throws Exception {
2082 >        final ExecutorService e =
2083 >            new ThreadPoolExecutor(1, 1,
2084 >                                   LONG_DELAY_MS, MILLISECONDS,
2085 >                                   new LinkedBlockingQueue<Runnable>());
2086 >        try {
2087 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2088 >            final CountDownLatch done = new CountDownLatch(1);
2089 >            final List<Future<?>> futures = new ArrayList<>();
2090 >            for (int i = 0; i < 2; i++) {
2091 >                Runnable r = new CheckedRunnable() { public void realRun()
2092 >                                                         throws Throwable {
2093 >                    blockerStarted.countDown();
2094 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2095 >                }};
2096 >                futures.add(e.submit(r));
2097 >            }
2098 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2099 >            for (Future<?> future : futures) future.cancel(false);
2100 >            for (Future<?> future : futures) {
2101 >                try {
2102 >                    future.get();
2103 >                    shouldThrow();
2104 >                } catch (CancellationException success) {}
2105 >                try {
2106 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2107 >                    shouldThrow();
2108 >                } catch (CancellationException success) {}
2109 >                assertTrue(future.isCancelled());
2110 >                assertTrue(future.isDone());
2111 >            }
2112 >            done.countDown();
2113          } finally {
2114 <            p.shutdown();
2114 >            joinPool(e);
2115          }
2116      }
2117  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines