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.37 by jsr166, Mon Oct 11 07:21:32 2010 UTC vs.
Revision 1.82 by jsr166, Sun Oct 4 02:26:47 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       */
# Line 55 | Line 87 | public class ThreadPoolExecutorTest exte
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();
62 <            }};
63 <        try {
90 >        try (PoolCleaner cleaner = cleaner(p)) {
91 >            final CountDownLatch done = new CountDownLatch(1);
92 >            final Runnable task = new CheckedRunnable() {
93 >                public void realRun() { done.countDown(); }};
94              p.execute(task);
95 <            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
66 <        } finally {
67 <            joinPool(p);
95 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
96          }
97      }
98  
# Line 77 | Line 105 | public class ThreadPoolExecutorTest exte
105              new ThreadPoolExecutor(2, 2,
106                                     LONG_DELAY_MS, MILLISECONDS,
107                                     new ArrayBlockingQueue<Runnable>(10));
108 <        final CountDownLatch threadStarted = new CountDownLatch(1);
109 <        final CountDownLatch done = new CountDownLatch(1);
110 <        try {
108 >        try (PoolCleaner cleaner = cleaner(p)) {
109 >            final CountDownLatch threadStarted = new CountDownLatch(1);
110 >            final CountDownLatch done = new CountDownLatch(1);
111              assertEquals(0, p.getActiveCount());
112              p.execute(new CheckedRunnable() {
113                  public void realRun() throws InterruptedException {
# Line 87 | Line 115 | public class ThreadPoolExecutorTest exte
115                      assertEquals(1, p.getActiveCount());
116                      done.await();
117                  }});
118 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
118 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
119              assertEquals(1, p.getActiveCount());
92        } finally {
120              done.countDown();
94            joinPool(p);
121          }
122      }
123  
# Line 100 | Line 126 | public class ThreadPoolExecutorTest exte
126       */
127      public void testPrestartCoreThread() {
128          final ThreadPoolExecutor p =
129 <            new ThreadPoolExecutor(2, 2,
129 >            new ThreadPoolExecutor(2, 6,
130                                     LONG_DELAY_MS, MILLISECONDS,
131                                     new ArrayBlockingQueue<Runnable>(10));
132 <        assertEquals(0, p.getPoolSize());
133 <        assertTrue(p.prestartCoreThread());
134 <        assertEquals(1, p.getPoolSize());
135 <        assertTrue(p.prestartCoreThread());
136 <        assertEquals(2, p.getPoolSize());
137 <        assertFalse(p.prestartCoreThread());
138 <        assertEquals(2, p.getPoolSize());
139 <        joinPool(p);
132 >        try (PoolCleaner cleaner = cleaner(p)) {
133 >            assertEquals(0, p.getPoolSize());
134 >            assertTrue(p.prestartCoreThread());
135 >            assertEquals(1, p.getPoolSize());
136 >            assertTrue(p.prestartCoreThread());
137 >            assertEquals(2, p.getPoolSize());
138 >            assertFalse(p.prestartCoreThread());
139 >            assertEquals(2, p.getPoolSize());
140 >            p.setCorePoolSize(4);
141 >            assertTrue(p.prestartCoreThread());
142 >            assertEquals(3, p.getPoolSize());
143 >            assertTrue(p.prestartCoreThread());
144 >            assertEquals(4, p.getPoolSize());
145 >            assertFalse(p.prestartCoreThread());
146 >            assertEquals(4, p.getPoolSize());
147 >        }
148      }
149  
150      /**
# Line 118 | Line 152 | public class ThreadPoolExecutorTest exte
152       */
153      public void testPrestartAllCoreThreads() {
154          final ThreadPoolExecutor p =
155 <            new ThreadPoolExecutor(2, 2,
155 >            new ThreadPoolExecutor(2, 6,
156                                     LONG_DELAY_MS, MILLISECONDS,
157                                     new ArrayBlockingQueue<Runnable>(10));
158 <        assertEquals(0, p.getPoolSize());
159 <        p.prestartAllCoreThreads();
160 <        assertEquals(2, p.getPoolSize());
161 <        p.prestartAllCoreThreads();
162 <        assertEquals(2, p.getPoolSize());
163 <        joinPool(p);
158 >        try (PoolCleaner cleaner = cleaner(p)) {
159 >            assertEquals(0, p.getPoolSize());
160 >            p.prestartAllCoreThreads();
161 >            assertEquals(2, p.getPoolSize());
162 >            p.prestartAllCoreThreads();
163 >            assertEquals(2, p.getPoolSize());
164 >            p.setCorePoolSize(4);
165 >            p.prestartAllCoreThreads();
166 >            assertEquals(4, p.getPoolSize());
167 >            p.prestartAllCoreThreads();
168 >            assertEquals(4, p.getPoolSize());
169 >        }
170      }
171  
172      /**
# Line 138 | Line 178 | public class ThreadPoolExecutorTest exte
178              new ThreadPoolExecutor(2, 2,
179                                     LONG_DELAY_MS, MILLISECONDS,
180                                     new ArrayBlockingQueue<Runnable>(10));
181 <        final CountDownLatch threadStarted = new CountDownLatch(1);
182 <        final CountDownLatch threadProceed = new CountDownLatch(1);
183 <        final CountDownLatch threadDone = new CountDownLatch(1);
184 <        try {
181 >        try (PoolCleaner cleaner = cleaner(p)) {
182 >            final CountDownLatch threadStarted = new CountDownLatch(1);
183 >            final CountDownLatch threadProceed = new CountDownLatch(1);
184 >            final CountDownLatch threadDone = new CountDownLatch(1);
185              assertEquals(0, p.getCompletedTaskCount());
186              p.execute(new CheckedRunnable() {
187                  public void realRun() throws InterruptedException {
# Line 150 | Line 190 | public class ThreadPoolExecutorTest exte
190                      threadProceed.await();
191                      threadDone.countDown();
192                  }});
193 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
193 >            await(threadStarted);
194              assertEquals(0, p.getCompletedTaskCount());
195              threadProceed.countDown();
196              threadDone.await();
197 <            Thread.sleep(SHORT_DELAY_MS);
198 <            assertEquals(1, p.getCompletedTaskCount());
199 <        } finally {
200 <            joinPool(p);
197 >            long startTime = System.nanoTime();
198 >            while (p.getCompletedTaskCount() != 1) {
199 >                if (millisElapsedSince(startTime) > LONG_DELAY_MS)
200 >                    fail("timed out");
201 >                Thread.yield();
202 >            }
203          }
204      }
205  
# Line 169 | Line 211 | public class ThreadPoolExecutorTest exte
211              new ThreadPoolExecutor(1, 1,
212                                     LONG_DELAY_MS, MILLISECONDS,
213                                     new ArrayBlockingQueue<Runnable>(10));
214 <        assertEquals(1, p.getCorePoolSize());
215 <        joinPool(p);
214 >        try (PoolCleaner cleaner = cleaner(p)) {
215 >            assertEquals(1, p.getCorePoolSize());
216 >        }
217      }
218  
219      /**
# Line 181 | Line 224 | public class ThreadPoolExecutorTest exte
224              new ThreadPoolExecutor(2, 2,
225                                     1000, MILLISECONDS,
226                                     new ArrayBlockingQueue<Runnable>(10));
227 <        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
228 <        joinPool(p);
227 >        try (PoolCleaner cleaner = cleaner(p)) {
228 >            assertEquals(1, p.getKeepAliveTime(SECONDS));
229 >        }
230      }
231  
188
232      /**
233       * getThreadFactory returns factory in constructor if not set
234       */
235      public void testGetThreadFactory() {
236 <        ThreadFactory tf = new SimpleThreadFactory();
236 >        ThreadFactory threadFactory = new SimpleThreadFactory();
237          final ThreadPoolExecutor p =
238              new ThreadPoolExecutor(1, 2,
239                                     LONG_DELAY_MS, MILLISECONDS,
240                                     new ArrayBlockingQueue<Runnable>(10),
241 <                                   tf,
241 >                                   threadFactory,
242                                     new NoOpREHandler());
243 <        assertSame(tf, p.getThreadFactory());
244 <        joinPool(p);
243 >        try (PoolCleaner cleaner = cleaner(p)) {
244 >            assertSame(threadFactory, p.getThreadFactory());
245 >        }
246      }
247  
248      /**
# Line 209 | Line 253 | public class ThreadPoolExecutorTest exte
253              new ThreadPoolExecutor(1, 2,
254                                     LONG_DELAY_MS, MILLISECONDS,
255                                     new ArrayBlockingQueue<Runnable>(10));
256 <        ThreadFactory tf = new SimpleThreadFactory();
257 <        p.setThreadFactory(tf);
258 <        assertSame(tf, p.getThreadFactory());
259 <        joinPool(p);
256 >        try (PoolCleaner cleaner = cleaner(p)) {
257 >            ThreadFactory threadFactory = new SimpleThreadFactory();
258 >            p.setThreadFactory(threadFactory);
259 >            assertSame(threadFactory, p.getThreadFactory());
260 >        }
261      }
262  
218
263      /**
264       * setThreadFactory(null) throws NPE
265       */
# Line 224 | Line 268 | public class ThreadPoolExecutorTest exte
268              new ThreadPoolExecutor(1, 2,
269                                     LONG_DELAY_MS, MILLISECONDS,
270                                     new ArrayBlockingQueue<Runnable>(10));
271 <        try {
272 <            p.setThreadFactory(null);
273 <            shouldThrow();
274 <        } catch (NullPointerException success) {
275 <        } finally {
232 <            joinPool(p);
271 >        try (PoolCleaner cleaner = cleaner(p)) {
272 >            try {
273 >                p.setThreadFactory(null);
274 >                shouldThrow();
275 >            } catch (NullPointerException success) {}
276          }
277      }
278  
# Line 237 | Line 280 | public class ThreadPoolExecutorTest exte
280       * getRejectedExecutionHandler returns handler in constructor if not set
281       */
282      public void testGetRejectedExecutionHandler() {
283 <        final RejectedExecutionHandler h = new NoOpREHandler();
283 >        final RejectedExecutionHandler handler = new NoOpREHandler();
284          final ThreadPoolExecutor p =
285              new ThreadPoolExecutor(1, 2,
286                                     LONG_DELAY_MS, MILLISECONDS,
287                                     new ArrayBlockingQueue<Runnable>(10),
288 <                                   h);
289 <        assertSame(h, p.getRejectedExecutionHandler());
290 <        joinPool(p);
288 >                                   handler);
289 >        try (PoolCleaner cleaner = cleaner(p)) {
290 >            assertSame(handler, p.getRejectedExecutionHandler());
291 >        }
292      }
293  
294      /**
# Line 256 | Line 300 | public class ThreadPoolExecutorTest exte
300              new ThreadPoolExecutor(1, 2,
301                                     LONG_DELAY_MS, MILLISECONDS,
302                                     new ArrayBlockingQueue<Runnable>(10));
303 <        RejectedExecutionHandler h = new NoOpREHandler();
304 <        p.setRejectedExecutionHandler(h);
305 <        assertSame(h, p.getRejectedExecutionHandler());
306 <        joinPool(p);
303 >        try (PoolCleaner cleaner = cleaner(p)) {
304 >            RejectedExecutionHandler handler = new NoOpREHandler();
305 >            p.setRejectedExecutionHandler(handler);
306 >            assertSame(handler, p.getRejectedExecutionHandler());
307 >        }
308      }
309  
265
310      /**
311       * setRejectedExecutionHandler(null) throws NPE
312       */
# Line 271 | Line 315 | public class ThreadPoolExecutorTest exte
315              new ThreadPoolExecutor(1, 2,
316                                     LONG_DELAY_MS, MILLISECONDS,
317                                     new ArrayBlockingQueue<Runnable>(10));
318 <        try {
319 <            p.setRejectedExecutionHandler(null);
320 <            shouldThrow();
321 <        } catch (NullPointerException success) {
322 <        } finally {
279 <            joinPool(p);
318 >        try (PoolCleaner cleaner = cleaner(p)) {
319 >            try {
320 >                p.setRejectedExecutionHandler(null);
321 >                shouldThrow();
322 >            } catch (NullPointerException success) {}
323          }
324      }
325  
283
326      /**
327       * getLargestPoolSize increases, but doesn't overestimate, when
328       * multiple threads active
# Line 291 | Line 333 | public class ThreadPoolExecutorTest exte
333              new ThreadPoolExecutor(THREADS, THREADS,
334                                     LONG_DELAY_MS, MILLISECONDS,
335                                     new ArrayBlockingQueue<Runnable>(10));
336 <        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
337 <        final CountDownLatch done = new CountDownLatch(1);
338 <        try {
336 >        try (PoolCleaner cleaner = cleaner(p)) {
337 >            final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
338 >            final CountDownLatch done = new CountDownLatch(1);
339              assertEquals(0, p.getLargestPoolSize());
340              for (int i = 0; i < THREADS; i++)
341                  p.execute(new CheckedRunnable() {
# Line 302 | Line 344 | public class ThreadPoolExecutorTest exte
344                          done.await();
345                          assertEquals(THREADS, p.getLargestPoolSize());
346                      }});
347 <            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
306 <            assertEquals(THREADS, p.getLargestPoolSize());
307 <        } finally {
308 <            done.countDown();
309 <            joinPool(p);
347 >            assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
348              assertEquals(THREADS, p.getLargestPoolSize());
349 +            done.countDown();   // release pool
350          }
351 +        assertEquals(THREADS, p.getLargestPoolSize());
352      }
353  
354      /**
# Line 320 | Line 360 | public class ThreadPoolExecutorTest exte
360              new ThreadPoolExecutor(2, 3,
361                                     LONG_DELAY_MS, MILLISECONDS,
362                                     new ArrayBlockingQueue<Runnable>(10));
363 <        assertEquals(3, p.getMaximumPoolSize());
364 <        joinPool(p);
363 >        try (PoolCleaner cleaner = cleaner(p)) {
364 >            assertEquals(3, p.getMaximumPoolSize());
365 >            p.setMaximumPoolSize(5);
366 >            assertEquals(5, p.getMaximumPoolSize());
367 >            p.setMaximumPoolSize(4);
368 >            assertEquals(4, p.getMaximumPoolSize());
369 >        }
370      }
371  
372      /**
# Line 333 | Line 378 | public class ThreadPoolExecutorTest exte
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 {
381 >        try (PoolCleaner cleaner = cleaner(p)) {
382 >            final CountDownLatch threadStarted = new CountDownLatch(1);
383 >            final CountDownLatch done = new CountDownLatch(1);
384              assertEquals(0, p.getPoolSize());
385              p.execute(new CheckedRunnable() {
386                  public void realRun() throws InterruptedException {
# Line 343 | Line 388 | public class ThreadPoolExecutorTest exte
388                      assertEquals(1, p.getPoolSize());
389                      done.await();
390                  }});
391 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
391 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
392              assertEquals(1, p.getPoolSize());
393 <        } finally {
349 <            done.countDown();
350 <            joinPool(p);
393 >            done.countDown();   // release pool
394          }
395      }
396  
# Line 359 | Line 402 | public class ThreadPoolExecutorTest exte
402              new ThreadPoolExecutor(1, 1,
403                                     LONG_DELAY_MS, MILLISECONDS,
404                                     new ArrayBlockingQueue<Runnable>(10));
405 <        final CountDownLatch threadStarted = new CountDownLatch(1);
406 <        final CountDownLatch done = new CountDownLatch(1);
407 <        try {
405 >        try (PoolCleaner cleaner = cleaner(p)) {
406 >            final CountDownLatch threadStarted = new CountDownLatch(1);
407 >            final CountDownLatch done = new CountDownLatch(1);
408              assertEquals(0, p.getTaskCount());
409              p.execute(new CheckedRunnable() {
410                  public void realRun() throws InterruptedException {
# Line 369 | Line 412 | public class ThreadPoolExecutorTest exte
412                      assertEquals(1, p.getTaskCount());
413                      done.await();
414                  }});
415 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
415 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
416              assertEquals(1, p.getTaskCount());
374        } finally {
417              done.countDown();
376            joinPool(p);
418          }
419      }
420  
421      /**
422 <     * isShutDown is false before shutdown, true after
422 >     * isShutdown is false before shutdown, true after
423       */
424      public void testIsShutdown() {
425          final ThreadPoolExecutor p =
426              new ThreadPoolExecutor(1, 1,
427                                     LONG_DELAY_MS, MILLISECONDS,
428                                     new ArrayBlockingQueue<Runnable>(10));
429 <        assertFalse(p.isShutdown());
430 <        try { p.shutdown(); } catch (SecurityException ok) { return; }
431 <        assertTrue(p.isShutdown());
432 <        joinPool(p);
429 >        try (PoolCleaner cleaner = cleaner(p)) {
430 >            assertFalse(p.isShutdown());
431 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
432 >            assertTrue(p.isShutdown());
433 >        }
434      }
435  
436 +    /**
437 +     * awaitTermination on a non-shutdown pool times out
438 +     */
439 +    public void testAwaitTermination_timesOut() throws InterruptedException {
440 +        final ThreadPoolExecutor p =
441 +            new ThreadPoolExecutor(1, 1,
442 +                                   LONG_DELAY_MS, MILLISECONDS,
443 +                                   new ArrayBlockingQueue<Runnable>(10));
444 +        assertFalse(p.isTerminated());
445 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS));
446 +        assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS));
447 +        assertFalse(p.awaitTermination(-1L, NANOSECONDS));
448 +        assertFalse(p.awaitTermination(-1L, MILLISECONDS));
449 +        assertFalse(p.awaitTermination(0L, NANOSECONDS));
450 +        assertFalse(p.awaitTermination(0L, MILLISECONDS));
451 +        long timeoutNanos = 999999L;
452 +        long startTime = System.nanoTime();
453 +        assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS));
454 +        assertTrue(System.nanoTime() - startTime >= timeoutNanos);
455 +        assertFalse(p.isTerminated());
456 +        startTime = System.nanoTime();
457 +        long timeoutMillis = timeoutMillis();
458 +        assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS));
459 +        assertTrue(millisElapsedSince(startTime) >= timeoutMillis);
460 +        assertFalse(p.isTerminated());
461 +        p.shutdown();
462 +        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
463 +        assertTrue(p.isTerminated());
464 +    }
465  
466      /**
467       * isTerminated is false before termination, true after
# Line 406 | Line 477 | public class ThreadPoolExecutorTest exte
477          try {
478              p.execute(new CheckedRunnable() {
479                  public void realRun() throws InterruptedException {
409                    threadStarted.countDown();
480                      assertFalse(p.isTerminated());
481 +                    threadStarted.countDown();
482                      done.await();
483                  }});
484 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
484 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
485 >            assertFalse(p.isTerminating());
486              done.countDown();
487          } finally {
488              try { p.shutdown(); } catch (SecurityException ok) { return; }
# Line 433 | Line 505 | public class ThreadPoolExecutorTest exte
505              assertFalse(p.isTerminating());
506              p.execute(new CheckedRunnable() {
507                  public void realRun() throws InterruptedException {
436                    threadStarted.countDown();
508                      assertFalse(p.isTerminating());
509 +                    threadStarted.countDown();
510                      done.await();
511                  }});
512 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
512 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
513              assertFalse(p.isTerminating());
514              done.countDown();
515          } finally {
# Line 472 | Line 544 | public class ThreadPoolExecutorTest exte
544                  tasks[i] = new FutureTask(task);
545                  p.execute(tasks[i]);
546              }
547 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
547 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
548              assertSame(q, p.getQueue());
549              assertFalse(q.contains(tasks[0]));
550              assertTrue(q.contains(tasks[tasks.length - 1]));
# Line 504 | Line 576 | public class ThreadPoolExecutorTest exte
576                      }};
577                  p.execute(tasks[i]);
578              }
579 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
579 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
580              assertFalse(p.remove(tasks[0]));
581              assertTrue(q.contains(tasks[4]));
582              assertTrue(q.contains(tasks[3]));
# Line 543 | Line 615 | public class ThreadPoolExecutorTest exte
615                  tasks[i] = new FutureTask(task);
616                  p.execute(tasks[i]);
617              }
618 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
618 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
619              assertEquals(tasks.length, p.getTaskCount());
620              assertEquals(tasks.length - 1, q.size());
621              assertEquals(1L, p.getActiveCount());
# Line 563 | Line 635 | public class ThreadPoolExecutorTest exte
635      }
636  
637      /**
638 <     * shutDownNow returns a list containing tasks that were not run
638 >     * shutdownNow returns a list containing tasks that were not run,
639 >     * and those tasks are drained from the queue
640       */
641 <    public void testShutDownNow() {
641 >    public void testShutdownNow() throws InterruptedException {
642 >        final int poolSize = 2;
643 >        final int count = 5;
644 >        final AtomicInteger ran = new AtomicInteger(0);
645          final ThreadPoolExecutor p =
646 <            new ThreadPoolExecutor(1, 1,
646 >            new ThreadPoolExecutor(poolSize, poolSize,
647                                     LONG_DELAY_MS, MILLISECONDS,
648                                     new ArrayBlockingQueue<Runnable>(10));
649 <        List l;
650 <        try {
651 <            for (int i = 0; i < 5; i++)
576 <                p.execute(new MediumPossiblyInterruptedRunnable());
577 <        }
578 <        finally {
649 >        CountDownLatch threadsStarted = new CountDownLatch(poolSize);
650 >        Runnable waiter = new CheckedRunnable() { public void realRun() {
651 >            threadsStarted.countDown();
652              try {
653 <                l = p.shutdownNow();
654 <            } catch (SecurityException ok) { return; }
653 >                MILLISECONDS.sleep(2 * LONG_DELAY_MS);
654 >            } catch (InterruptedException success) {}
655 >            ran.getAndIncrement();
656 >        }};
657 >        for (int i = 0; i < count; i++)
658 >            p.execute(waiter);
659 >        assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS));
660 >        assertEquals(poolSize, p.getActiveCount());
661 >        assertEquals(0, p.getCompletedTaskCount());
662 >        final List<Runnable> queuedTasks;
663 >        try {
664 >            queuedTasks = p.shutdownNow();
665 >        } catch (SecurityException ok) {
666 >            return; // Allowed in case test doesn't have privs
667          }
668          assertTrue(p.isShutdown());
669 <        assertTrue(l.size() <= 4);
669 >        assertTrue(p.getQueue().isEmpty());
670 >        assertEquals(count - poolSize, queuedTasks.size());
671 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
672 >        assertTrue(p.isTerminated());
673 >        assertEquals(poolSize, ran.get());
674 >        assertEquals(poolSize, p.getCompletedTaskCount());
675      }
676  
677      // Exception Tests
678  
589
679      /**
680       * Constructor throws if corePoolSize argument is less than zero
681       */
682      public void testConstructor1() {
683          try {
684 <            new ThreadPoolExecutor(-1, 1,
596 <                                   LONG_DELAY_MS, MILLISECONDS,
684 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
685                                     new ArrayBlockingQueue<Runnable>(10));
686              shouldThrow();
687          } catch (IllegalArgumentException success) {}
# Line 604 | Line 692 | public class ThreadPoolExecutorTest exte
692       */
693      public void testConstructor2() {
694          try {
695 <            new ThreadPoolExecutor(1, -1,
608 <                                   LONG_DELAY_MS, MILLISECONDS,
695 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
696                                     new ArrayBlockingQueue<Runnable>(10));
697              shouldThrow();
698          } catch (IllegalArgumentException success) {}
# Line 616 | Line 703 | public class ThreadPoolExecutorTest exte
703       */
704      public void testConstructor3() {
705          try {
706 <            new ThreadPoolExecutor(1, 0,
620 <                                   LONG_DELAY_MS, MILLISECONDS,
706 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
707                                     new ArrayBlockingQueue<Runnable>(10));
708              shouldThrow();
709          } catch (IllegalArgumentException success) {}
# Line 628 | Line 714 | public class ThreadPoolExecutorTest exte
714       */
715      public void testConstructor4() {
716          try {
717 <            new ThreadPoolExecutor(1, 2,
632 <                                   -1L, MILLISECONDS,
717 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
718                                     new ArrayBlockingQueue<Runnable>(10));
719              shouldThrow();
720          } catch (IllegalArgumentException success) {}
# Line 640 | Line 725 | public class ThreadPoolExecutorTest exte
725       */
726      public void testConstructor5() {
727          try {
728 <            new ThreadPoolExecutor(2, 1,
644 <                                   LONG_DELAY_MS, MILLISECONDS,
728 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
729                                     new ArrayBlockingQueue<Runnable>(10));
730              shouldThrow();
731          } catch (IllegalArgumentException success) {}
# Line 652 | Line 736 | public class ThreadPoolExecutorTest exte
736       */
737      public void testConstructorNullPointerException() {
738          try {
739 <            new ThreadPoolExecutor(1, 2,
656 <                                   LONG_DELAY_MS, MILLISECONDS,
739 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
740                                     (BlockingQueue) null);
741              shouldThrow();
742          } catch (NullPointerException success) {}
743      }
744  
662
663
745      /**
746       * Constructor throws if corePoolSize argument is less than zero
747       */
748      public void testConstructor6() {
749          try {
750 <            new ThreadPoolExecutor(-1, 1,
670 <                                   LONG_DELAY_MS, MILLISECONDS,
750 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
751                                     new ArrayBlockingQueue<Runnable>(10),
752                                     new SimpleThreadFactory());
753              shouldThrow();
# Line 679 | Line 759 | public class ThreadPoolExecutorTest exte
759       */
760      public void testConstructor7() {
761          try {
762 <            new ThreadPoolExecutor(1, -1,
683 <                                   LONG_DELAY_MS, MILLISECONDS,
762 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
763                                     new ArrayBlockingQueue<Runnable>(10),
764                                     new SimpleThreadFactory());
765              shouldThrow();
# Line 692 | Line 771 | public class ThreadPoolExecutorTest exte
771       */
772      public void testConstructor8() {
773          try {
774 <            new ThreadPoolExecutor(1, 0,
696 <                                   LONG_DELAY_MS, MILLISECONDS,
774 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
775                                     new ArrayBlockingQueue<Runnable>(10),
776                                     new SimpleThreadFactory());
777              shouldThrow();
# Line 705 | Line 783 | public class ThreadPoolExecutorTest exte
783       */
784      public void testConstructor9() {
785          try {
786 <            new ThreadPoolExecutor(1, 2,
709 <                                   -1L, MILLISECONDS,
786 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
787                                     new ArrayBlockingQueue<Runnable>(10),
788                                     new SimpleThreadFactory());
789              shouldThrow();
# Line 718 | Line 795 | public class ThreadPoolExecutorTest exte
795       */
796      public void testConstructor10() {
797          try {
798 <            new ThreadPoolExecutor(2, 1,
722 <                                   LONG_DELAY_MS, MILLISECONDS,
798 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
799                                     new ArrayBlockingQueue<Runnable>(10),
800                                     new SimpleThreadFactory());
801              shouldThrow();
# Line 731 | Line 807 | public class ThreadPoolExecutorTest exte
807       */
808      public void testConstructorNullPointerException2() {
809          try {
810 <            new ThreadPoolExecutor(1, 2,
735 <                                   LONG_DELAY_MS, MILLISECONDS,
810 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
811                                     (BlockingQueue) null,
812                                     new SimpleThreadFactory());
813              shouldThrow();
# Line 744 | Line 819 | public class ThreadPoolExecutorTest exte
819       */
820      public void testConstructorNullPointerException3() {
821          try {
822 <            new ThreadPoolExecutor(1, 2,
748 <                                   LONG_DELAY_MS, MILLISECONDS,
822 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
823                                     new ArrayBlockingQueue<Runnable>(10),
824                                     (ThreadFactory) null);
825              shouldThrow();
826          } catch (NullPointerException success) {}
827      }
828  
755
829      /**
830       * Constructor throws if corePoolSize argument is less than zero
831       */
832      public void testConstructor11() {
833          try {
834 <            new ThreadPoolExecutor(-1, 1,
762 <                                   LONG_DELAY_MS, MILLISECONDS,
834 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
835                                     new ArrayBlockingQueue<Runnable>(10),
836                                     new NoOpREHandler());
837              shouldThrow();
# Line 771 | Line 843 | public class ThreadPoolExecutorTest exte
843       */
844      public void testConstructor12() {
845          try {
846 <            new ThreadPoolExecutor(1, -1,
775 <                                   LONG_DELAY_MS, MILLISECONDS,
846 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
847                                     new ArrayBlockingQueue<Runnable>(10),
848                                     new NoOpREHandler());
849              shouldThrow();
# Line 784 | Line 855 | public class ThreadPoolExecutorTest exte
855       */
856      public void testConstructor13() {
857          try {
858 <            new ThreadPoolExecutor(1, 0,
788 <                                   LONG_DELAY_MS, MILLISECONDS,
858 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
859                                     new ArrayBlockingQueue<Runnable>(10),
860                                     new NoOpREHandler());
861              shouldThrow();
# Line 797 | Line 867 | public class ThreadPoolExecutorTest exte
867       */
868      public void testConstructor14() {
869          try {
870 <            new ThreadPoolExecutor(1, 2,
801 <                                   -1L, MILLISECONDS,
870 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
871                                     new ArrayBlockingQueue<Runnable>(10),
872                                     new NoOpREHandler());
873              shouldThrow();
# Line 810 | Line 879 | public class ThreadPoolExecutorTest exte
879       */
880      public void testConstructor15() {
881          try {
882 <            new ThreadPoolExecutor(2, 1,
814 <                                   LONG_DELAY_MS, MILLISECONDS,
882 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
883                                     new ArrayBlockingQueue<Runnable>(10),
884                                     new NoOpREHandler());
885              shouldThrow();
# Line 823 | Line 891 | public class ThreadPoolExecutorTest exte
891       */
892      public void testConstructorNullPointerException4() {
893          try {
894 <            new ThreadPoolExecutor(1, 2,
827 <                                   LONG_DELAY_MS, MILLISECONDS,
894 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
895                                     (BlockingQueue) null,
896                                     new NoOpREHandler());
897              shouldThrow();
# Line 836 | Line 903 | public class ThreadPoolExecutorTest exte
903       */
904      public void testConstructorNullPointerException5() {
905          try {
906 <            new ThreadPoolExecutor(1, 2,
840 <                                   LONG_DELAY_MS, MILLISECONDS,
906 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
907                                     new ArrayBlockingQueue<Runnable>(10),
908                                     (RejectedExecutionHandler) null);
909              shouldThrow();
910          } catch (NullPointerException success) {}
911      }
912  
847
913      /**
914       * Constructor throws if corePoolSize argument is less than zero
915       */
916      public void testConstructor16() {
917          try {
918 <            new ThreadPoolExecutor(-1, 1,
854 <                                   LONG_DELAY_MS, MILLISECONDS,
918 >            new ThreadPoolExecutor(-1, 1, 1L, SECONDS,
919                                     new ArrayBlockingQueue<Runnable>(10),
920                                     new SimpleThreadFactory(),
921                                     new NoOpREHandler());
# Line 864 | Line 928 | public class ThreadPoolExecutorTest exte
928       */
929      public void testConstructor17() {
930          try {
931 <            new ThreadPoolExecutor(1, -1,
868 <                                   LONG_DELAY_MS, MILLISECONDS,
931 >            new ThreadPoolExecutor(1, -1, 1L, SECONDS,
932                                     new ArrayBlockingQueue<Runnable>(10),
933                                     new SimpleThreadFactory(),
934                                     new NoOpREHandler());
# Line 878 | Line 941 | public class ThreadPoolExecutorTest exte
941       */
942      public void testConstructor18() {
943          try {
944 <            new ThreadPoolExecutor(1, 0,
882 <                                   LONG_DELAY_MS, MILLISECONDS,
944 >            new ThreadPoolExecutor(1, 0, 1L, SECONDS,
945                                     new ArrayBlockingQueue<Runnable>(10),
946                                     new SimpleThreadFactory(),
947                                     new NoOpREHandler());
# Line 892 | Line 954 | public class ThreadPoolExecutorTest exte
954       */
955      public void testConstructor19() {
956          try {
957 <            new ThreadPoolExecutor(1, 2,
896 <                                   -1L, MILLISECONDS,
957 >            new ThreadPoolExecutor(1, 2, -1L, SECONDS,
958                                     new ArrayBlockingQueue<Runnable>(10),
959                                     new SimpleThreadFactory(),
960                                     new NoOpREHandler());
# Line 906 | Line 967 | public class ThreadPoolExecutorTest exte
967       */
968      public void testConstructor20() {
969          try {
970 <            new ThreadPoolExecutor(2, 1,
910 <                                   LONG_DELAY_MS, MILLISECONDS,
970 >            new ThreadPoolExecutor(2, 1, 1L, SECONDS,
971                                     new ArrayBlockingQueue<Runnable>(10),
972                                     new SimpleThreadFactory(),
973                                     new NoOpREHandler());
# Line 920 | Line 980 | public class ThreadPoolExecutorTest exte
980       */
981      public void testConstructorNullPointerException6() {
982          try {
983 <            new ThreadPoolExecutor(1, 2,
924 <                                   LONG_DELAY_MS, MILLISECONDS,
983 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
984                                     (BlockingQueue) null,
985                                     new SimpleThreadFactory(),
986                                     new NoOpREHandler());
# Line 934 | Line 993 | public class ThreadPoolExecutorTest exte
993       */
994      public void testConstructorNullPointerException7() {
995          try {
996 <            new ThreadPoolExecutor(1, 2,
938 <                                   LONG_DELAY_MS, MILLISECONDS,
996 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
997                                     new ArrayBlockingQueue<Runnable>(10),
998                                     new SimpleThreadFactory(),
999                                     (RejectedExecutionHandler) null);
# Line 948 | Line 1006 | public class ThreadPoolExecutorTest exte
1006       */
1007      public void testConstructorNullPointerException8() {
1008          try {
1009 <            new ThreadPoolExecutor(1, 2,
952 <                                   LONG_DELAY_MS, MILLISECONDS,
1009 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1010                                     new ArrayBlockingQueue<Runnable>(10),
1011                                     (ThreadFactory) null,
1012                                     new NoOpREHandler());
# Line 963 | Line 1020 | public class ThreadPoolExecutorTest exte
1020      public void testInterruptedSubmit() throws InterruptedException {
1021          final ThreadPoolExecutor p =
1022              new ThreadPoolExecutor(1, 1,
1023 <                                   60, TimeUnit.SECONDS,
1023 >                                   60, SECONDS,
1024                                     new ArrayBlockingQueue<Runnable>(10));
1025  
1026          final CountDownLatch threadStarted = new CountDownLatch(1);
# Line 980 | Line 1037 | public class ThreadPoolExecutorTest exte
1037                      p.submit(task).get();
1038                  }});
1039  
1040 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1040 >            assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS));
1041              t.interrupt();
1042              awaitTermination(t, MEDIUM_DELAY_MS);
1043          } finally {
# Line 1211 | Line 1268 | public class ThreadPoolExecutorTest exte
1268          }
1269      }
1270  
1214
1271      /**
1272       * execute using DiscardOldestPolicy drops task on shutdown
1273       */
# Line 1233 | Line 1289 | public class ThreadPoolExecutorTest exte
1289          }
1290      }
1291  
1236
1292      /**
1293       * execute(null) throws NPE
1294       */
1295      public void testExecuteNull() {
1296          ThreadPoolExecutor p =
1297 <            new ThreadPoolExecutor(1, 2,
1243 <                                   LONG_DELAY_MS, MILLISECONDS,
1297 >            new ThreadPoolExecutor(1, 2, 1L, SECONDS,
1298                                     new ArrayBlockingQueue<Runnable>(10));
1299          try {
1300              p.execute(null);
# Line 1306 | Line 1360 | public class ThreadPoolExecutorTest exte
1360          joinPool(p);
1361      }
1362  
1363 +    /**
1364 +     * Configuration changes that allow core pool size greater than
1365 +     * max pool size result in IllegalArgumentException.
1366 +     */
1367 +    public void testPoolSizeInvariants() {
1368 +        ThreadPoolExecutor p =
1369 +            new ThreadPoolExecutor(1, 1,
1370 +                                   LONG_DELAY_MS, MILLISECONDS,
1371 +                                   new ArrayBlockingQueue<Runnable>(10));
1372 +        for (int s = 1; s < 5; s++) {
1373 +            p.setMaximumPoolSize(s);
1374 +            p.setCorePoolSize(s);
1375 +            try {
1376 +                p.setMaximumPoolSize(s - 1);
1377 +                shouldThrow();
1378 +            } catch (IllegalArgumentException success) {}
1379 +            assertEquals(s, p.getCorePoolSize());
1380 +            assertEquals(s, p.getMaximumPoolSize());
1381 +            try {
1382 +                p.setCorePoolSize(s + 1);
1383 +                shouldThrow();
1384 +            } catch (IllegalArgumentException success) {}
1385 +            assertEquals(s, p.getCorePoolSize());
1386 +            assertEquals(s, p.getMaximumPoolSize());
1387 +        }
1388 +        joinPool(p);
1389 +    }
1390  
1391      /**
1392       * setKeepAliveTime throws IllegalArgumentException
# Line 1332 | Line 1413 | public class ThreadPoolExecutorTest exte
1413      public void testTerminated() {
1414          ExtendedTPE p = new ExtendedTPE();
1415          try { p.shutdown(); } catch (SecurityException ok) { return; }
1416 <        assertTrue(p.terminatedCalled);
1416 >        assertTrue(p.terminatedCalled());
1417          joinPool(p);
1418      }
1419  
# Line 1342 | Line 1423 | public class ThreadPoolExecutorTest exte
1423      public void testBeforeAfter() throws InterruptedException {
1424          ExtendedTPE p = new ExtendedTPE();
1425          try {
1426 <            TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1427 <            p.execute(r);
1428 <            Thread.sleep(SHORT_DELAY_MS);
1429 <            assertTrue(r.done);
1430 <            assertTrue(p.beforeCalled);
1431 <            assertTrue(p.afterCalled);
1426 >            final CountDownLatch done = new CountDownLatch(1);
1427 >            p.execute(new CheckedRunnable() {
1428 >                public void realRun() {
1429 >                    done.countDown();
1430 >                }});
1431 >            await(p.afterCalled);
1432 >            assertEquals(0, done.getCount());
1433 >            assertTrue(p.afterCalled());
1434 >            assertTrue(p.beforeCalled());
1435              try { p.shutdown(); } catch (SecurityException ok) { return; }
1436          } finally {
1437              joinPool(p);
# Line 1405 | Line 1489 | public class ThreadPoolExecutorTest exte
1489          }
1490      }
1491  
1408
1492      /**
1493       * invokeAny(null) throws NPE
1494       */
# Line 1599 | Line 1682 | public class ThreadPoolExecutorTest exte
1682          }
1683      }
1684  
1602
1603
1685      /**
1686       * timed invokeAny(null) throws NPE
1687       */
# Line 1823 | Line 1904 | public class ThreadPoolExecutorTest exte
1904              l.add(new StringTask());
1905              l.add(new StringTask());
1906              List<Future<String>> futures =
1907 <                e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1907 >                e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS);
1908              assertEquals(2, futures.size());
1909              for (Future<String> future : futures)
1910                  assertSame(TEST_STRING, future.get());
# Line 1841 | Line 1922 | public class ThreadPoolExecutorTest exte
1922                                     LONG_DELAY_MS, MILLISECONDS,
1923                                     new ArrayBlockingQueue<Runnable>(10));
1924          try {
1925 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1926 <            l.add(new StringTask());
1927 <            l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1928 <            l.add(new StringTask());
1929 <            List<Future<String>> futures =
1930 <                e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1931 <            assertEquals(3, futures.size());
1932 <            Iterator<Future<String>> it = futures.iterator();
1933 <            Future<String> f1 = it.next();
1934 <            Future<String> f2 = it.next();
1935 <            Future<String> f3 = it.next();
1936 <            assertTrue(f1.isDone());
1937 <            assertTrue(f2.isDone());
1938 <            assertTrue(f3.isDone());
1939 <            assertFalse(f1.isCancelled());
1940 <            assertTrue(f2.isCancelled());
1925 >            for (long timeout = timeoutMillis();;) {
1926 >                List<Callable<String>> tasks = new ArrayList<>();
1927 >                tasks.add(new StringTask("0"));
1928 >                tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING));
1929 >                tasks.add(new StringTask("2"));
1930 >                long startTime = System.nanoTime();
1931 >                List<Future<String>> futures =
1932 >                    e.invokeAll(tasks, timeout, MILLISECONDS);
1933 >                assertEquals(tasks.size(), futures.size());
1934 >                assertTrue(millisElapsedSince(startTime) >= timeout);
1935 >                for (Future future : futures)
1936 >                    assertTrue(future.isDone());
1937 >                assertTrue(futures.get(1).isCancelled());
1938 >                try {
1939 >                    assertEquals("0", futures.get(0).get());
1940 >                    assertEquals("2", futures.get(2).get());
1941 >                    break;
1942 >                } catch (CancellationException retryWithLongerTimeout) {
1943 >                    timeout *= 2;
1944 >                    if (timeout >= LONG_DELAY_MS / 2)
1945 >                        fail("expected exactly one task to be cancelled");
1946 >                }
1947 >            }
1948          } finally {
1949              joinPool(e);
1950          }
# Line 1902 | Line 1990 | public class ThreadPoolExecutorTest exte
1990       * allowCoreThreadTimeOut(true) causes idle threads to time out
1991       */
1992      public void testAllowCoreThreadTimeOut_true() throws Exception {
1993 +        long keepAliveTime = timeoutMillis();
1994          final ThreadPoolExecutor p =
1995              new ThreadPoolExecutor(2, 10,
1996 <                                   SHORT_DELAY_MS, MILLISECONDS,
1996 >                                   keepAliveTime, MILLISECONDS,
1997                                     new ArrayBlockingQueue<Runnable>(10));
1998          final CountDownLatch threadStarted = new CountDownLatch(1);
1999          try {
2000              p.allowCoreThreadTimeOut(true);
2001              p.execute(new CheckedRunnable() {
2002 <                public void realRun() throws InterruptedException {
2002 >                public void realRun() {
2003                      threadStarted.countDown();
2004                      assertEquals(1, p.getPoolSize());
2005                  }});
2006 <            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
2007 <            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
2008 <                if (p.getPoolSize() == 0)
2009 <                    break;
2010 <                Thread.sleep(10);
2011 <            }
2006 >            await(threadStarted);
2007 >            delay(keepAliveTime);
2008 >            long startTime = System.nanoTime();
2009 >            while (p.getPoolSize() > 0
2010 >                   && millisElapsedSince(startTime) < LONG_DELAY_MS)
2011 >                Thread.yield();
2012 >            assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS);
2013              assertEquals(0, p.getPoolSize());
2014          } finally {
2015              joinPool(p);
# Line 1930 | Line 2020 | public class ThreadPoolExecutorTest exte
2020       * allowCoreThreadTimeOut(false) causes idle threads not to time out
2021       */
2022      public void testAllowCoreThreadTimeOut_false() throws Exception {
2023 +        long keepAliveTime = timeoutMillis();
2024          final ThreadPoolExecutor p =
2025              new ThreadPoolExecutor(2, 10,
2026 <                                   SHORT_DELAY_MS, MILLISECONDS,
2026 >                                   keepAliveTime, MILLISECONDS,
2027                                     new ArrayBlockingQueue<Runnable>(10));
2028          final CountDownLatch threadStarted = new CountDownLatch(1);
2029          try {
# Line 1942 | Line 2033 | public class ThreadPoolExecutorTest exte
2033                      threadStarted.countDown();
2034                      assertTrue(p.getPoolSize() >= 1);
2035                  }});
2036 <            Thread.sleep(SMALL_DELAY_MS);
2036 >            delay(2 * keepAliveTime);
2037              assertTrue(p.getPoolSize() >= 1);
2038          } finally {
2039              joinPool(p);
# Line 1961 | Line 2052 | public class ThreadPoolExecutorTest exte
2052                  done.countDown();
2053              }};
2054          final ThreadPoolExecutor p =
2055 <            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
2055 >            new ThreadPoolExecutor(1, 30,
2056 >                                   60, SECONDS,
2057                                     new ArrayBlockingQueue(30));
2058          try {
2059              for (int i = 0; i < nTasks; ++i) {
# Line 1976 | Line 2068 | public class ThreadPoolExecutorTest exte
2068              // enough time to run all tasks
2069              assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
2070          } finally {
2071 <            p.shutdown();
2071 >            joinPool(p);
2072 >        }
2073 >    }
2074 >
2075 >    /**
2076 >     * get(cancelled task) throws CancellationException
2077 >     */
2078 >    public void testGet_cancelled() throws Exception {
2079 >        final ExecutorService e =
2080 >            new ThreadPoolExecutor(1, 1,
2081 >                                   LONG_DELAY_MS, MILLISECONDS,
2082 >                                   new LinkedBlockingQueue<Runnable>());
2083 >        try {
2084 >            final CountDownLatch blockerStarted = new CountDownLatch(1);
2085 >            final CountDownLatch done = new CountDownLatch(1);
2086 >            final List<Future<?>> futures = new ArrayList<>();
2087 >            for (int i = 0; i < 2; i++) {
2088 >                Runnable r = new CheckedRunnable() { public void realRun()
2089 >                                                         throws Throwable {
2090 >                    blockerStarted.countDown();
2091 >                    assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS));
2092 >                }};
2093 >                futures.add(e.submit(r));
2094 >            }
2095 >            assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS));
2096 >            for (Future<?> future : futures) future.cancel(false);
2097 >            for (Future<?> future : futures) {
2098 >                try {
2099 >                    future.get();
2100 >                    shouldThrow();
2101 >                } catch (CancellationException success) {}
2102 >                try {
2103 >                    future.get(LONG_DELAY_MS, MILLISECONDS);
2104 >                    shouldThrow();
2105 >                } catch (CancellationException success) {}
2106 >                assertTrue(future.isCancelled());
2107 >                assertTrue(future.isDone());
2108 >            }
2109 >            done.countDown();
2110 >        } finally {
2111 >            joinPool(e);
2112          }
2113      }
2114  

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines