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.36 by jsr166, Sat Oct 9 22:27:16 2010 UTC vs.
Revision 1.37 by jsr166, Mon Oct 11 07:21:32 2010 UTC

# Line 51 | Line 51 | public class ThreadPoolExecutorTest exte
51       * execute successfully executes a runnable
52       */
53      public void testExecute() throws InterruptedException {
54 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
54 >        final ThreadPoolExecutor p =
55 >            new ThreadPoolExecutor(1, 1,
56 >                                   LONG_DELAY_MS, MILLISECONDS,
57 >                                   new ArrayBlockingQueue<Runnable>(10));
58 >        final CountDownLatch done = new CountDownLatch(1);
59 >        final Runnable task = new CheckedRunnable() {
60 >            public void realRun() {
61 >                done.countDown();
62 >            }};
63          try {
64 <            p1.execute(new ShortRunnable());
65 <            Thread.sleep(SMALL_DELAY_MS);
64 >            p.execute(task);
65 >            assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS));
66          } finally {
67 <            joinPool(p1);
67 >            joinPool(p);
68          }
69      }
70  
# Line 65 | Line 73 | public class ThreadPoolExecutorTest exte
73       * thread becomes active
74       */
75      public void testGetActiveCount() throws InterruptedException {
76 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
77 <        assertEquals(0, p2.getActiveCount());
78 <        p2.execute(new MediumRunnable());
79 <        Thread.sleep(SHORT_DELAY_MS);
80 <        assertEquals(1, p2.getActiveCount());
81 <        joinPool(p2);
76 >        final ThreadPoolExecutor p =
77 >            new ThreadPoolExecutor(2, 2,
78 >                                   LONG_DELAY_MS, MILLISECONDS,
79 >                                   new ArrayBlockingQueue<Runnable>(10));
80 >        final CountDownLatch threadStarted = new CountDownLatch(1);
81 >        final CountDownLatch done = new CountDownLatch(1);
82 >        try {
83 >            assertEquals(0, p.getActiveCount());
84 >            p.execute(new CheckedRunnable() {
85 >                public void realRun() throws InterruptedException {
86 >                    threadStarted.countDown();
87 >                    assertEquals(1, p.getActiveCount());
88 >                    done.await();
89 >                }});
90 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
91 >            assertEquals(1, p.getActiveCount());
92 >        } finally {
93 >            done.countDown();
94 >            joinPool(p);
95 >        }
96      }
97  
98      /**
99       * prestartCoreThread starts a thread if under corePoolSize, else doesn't
100       */
101      public void testPrestartCoreThread() {
102 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
103 <        assertEquals(0, p2.getPoolSize());
104 <        assertTrue(p2.prestartCoreThread());
105 <        assertEquals(1, p2.getPoolSize());
106 <        assertTrue(p2.prestartCoreThread());
107 <        assertEquals(2, p2.getPoolSize());
108 <        assertFalse(p2.prestartCoreThread());
109 <        assertEquals(2, p2.getPoolSize());
110 <        joinPool(p2);
102 >        final ThreadPoolExecutor p =
103 >            new ThreadPoolExecutor(2, 2,
104 >                                   LONG_DELAY_MS, MILLISECONDS,
105 >                                   new ArrayBlockingQueue<Runnable>(10));
106 >        assertEquals(0, p.getPoolSize());
107 >        assertTrue(p.prestartCoreThread());
108 >        assertEquals(1, p.getPoolSize());
109 >        assertTrue(p.prestartCoreThread());
110 >        assertEquals(2, p.getPoolSize());
111 >        assertFalse(p.prestartCoreThread());
112 >        assertEquals(2, p.getPoolSize());
113 >        joinPool(p);
114      }
115  
116      /**
117       * prestartAllCoreThreads starts all corePoolSize threads
118       */
119      public void testPrestartAllCoreThreads() {
120 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
121 <        assertEquals(0, p2.getPoolSize());
122 <        p2.prestartAllCoreThreads();
123 <        assertEquals(2, p2.getPoolSize());
124 <        p2.prestartAllCoreThreads();
125 <        assertEquals(2, p2.getPoolSize());
126 <        joinPool(p2);
120 >        final ThreadPoolExecutor p =
121 >            new ThreadPoolExecutor(2, 2,
122 >                                   LONG_DELAY_MS, MILLISECONDS,
123 >                                   new ArrayBlockingQueue<Runnable>(10));
124 >        assertEquals(0, p.getPoolSize());
125 >        p.prestartAllCoreThreads();
126 >        assertEquals(2, p.getPoolSize());
127 >        p.prestartAllCoreThreads();
128 >        assertEquals(2, p.getPoolSize());
129 >        joinPool(p);
130      }
131  
132      /**
# Line 106 | Line 134 | public class ThreadPoolExecutorTest exte
134       * when tasks complete
135       */
136      public void testGetCompletedTaskCount() throws InterruptedException {
137 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
138 <        assertEquals(0, p2.getCompletedTaskCount());
139 <        p2.execute(new ShortRunnable());
140 <        Thread.sleep(SMALL_DELAY_MS);
141 <        assertEquals(1, p2.getCompletedTaskCount());
142 <        try { p2.shutdown(); } catch (SecurityException ok) { return; }
143 <        joinPool(p2);
137 >        final ThreadPoolExecutor p =
138 >            new ThreadPoolExecutor(2, 2,
139 >                                   LONG_DELAY_MS, MILLISECONDS,
140 >                                   new ArrayBlockingQueue<Runnable>(10));
141 >        final CountDownLatch threadStarted = new CountDownLatch(1);
142 >        final CountDownLatch threadProceed = new CountDownLatch(1);
143 >        final CountDownLatch threadDone = new CountDownLatch(1);
144 >        try {
145 >            assertEquals(0, p.getCompletedTaskCount());
146 >            p.execute(new CheckedRunnable() {
147 >                public void realRun() throws InterruptedException {
148 >                    threadStarted.countDown();
149 >                    assertEquals(0, p.getCompletedTaskCount());
150 >                    threadProceed.await();
151 >                    threadDone.countDown();
152 >                }});
153 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
154 >            assertEquals(0, p.getCompletedTaskCount());
155 >            threadProceed.countDown();
156 >            threadDone.await();
157 >            Thread.sleep(SHORT_DELAY_MS);
158 >            assertEquals(1, p.getCompletedTaskCount());
159 >        } finally {
160 >            joinPool(p);
161 >        }
162      }
163  
164      /**
165       * getCorePoolSize returns size given in constructor if not otherwise set
166       */
167      public void testGetCorePoolSize() {
168 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
169 <        assertEquals(1, p1.getCorePoolSize());
170 <        joinPool(p1);
168 >        final ThreadPoolExecutor p =
169 >            new ThreadPoolExecutor(1, 1,
170 >                                   LONG_DELAY_MS, MILLISECONDS,
171 >                                   new ArrayBlockingQueue<Runnable>(10));
172 >        assertEquals(1, p.getCorePoolSize());
173 >        joinPool(p);
174      }
175  
176      /**
177       * getKeepAliveTime returns value given in constructor if not otherwise set
178       */
179      public void testGetKeepAliveTime() {
180 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
181 <        assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
182 <        joinPool(p2);
180 >        final ThreadPoolExecutor p =
181 >            new ThreadPoolExecutor(2, 2,
182 >                                   1000, MILLISECONDS,
183 >                                   new ArrayBlockingQueue<Runnable>(10));
184 >        assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS));
185 >        joinPool(p);
186      }
187  
188  
# Line 139 | Line 191 | public class ThreadPoolExecutorTest exte
191       */
192      public void testGetThreadFactory() {
193          ThreadFactory tf = new SimpleThreadFactory();
194 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
194 >        final ThreadPoolExecutor p =
195 >            new ThreadPoolExecutor(1, 2,
196 >                                   LONG_DELAY_MS, MILLISECONDS,
197 >                                   new ArrayBlockingQueue<Runnable>(10),
198 >                                   tf,
199 >                                   new NoOpREHandler());
200          assertSame(tf, p.getThreadFactory());
201          joinPool(p);
202      }
# Line 148 | Line 205 | public class ThreadPoolExecutorTest exte
205       * setThreadFactory sets the thread factory returned by getThreadFactory
206       */
207      public void testSetThreadFactory() {
208 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
208 >        final ThreadPoolExecutor p =
209 >            new ThreadPoolExecutor(1, 2,
210 >                                   LONG_DELAY_MS, MILLISECONDS,
211 >                                   new ArrayBlockingQueue<Runnable>(10));
212          ThreadFactory tf = new SimpleThreadFactory();
213          p.setThreadFactory(tf);
214          assertSame(tf, p.getThreadFactory());
# Line 160 | Line 220 | public class ThreadPoolExecutorTest exte
220       * setThreadFactory(null) throws NPE
221       */
222      public void testSetThreadFactoryNull() {
223 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
223 >        final ThreadPoolExecutor p =
224 >            new ThreadPoolExecutor(1, 2,
225 >                                   LONG_DELAY_MS, MILLISECONDS,
226 >                                   new ArrayBlockingQueue<Runnable>(10));
227          try {
228              p.setThreadFactory(null);
229              shouldThrow();
# Line 174 | Line 237 | public class ThreadPoolExecutorTest exte
237       * getRejectedExecutionHandler returns handler in constructor if not set
238       */
239      public void testGetRejectedExecutionHandler() {
240 <        RejectedExecutionHandler h = new NoOpREHandler();
241 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
240 >        final RejectedExecutionHandler h = new NoOpREHandler();
241 >        final ThreadPoolExecutor p =
242 >            new ThreadPoolExecutor(1, 2,
243 >                                   LONG_DELAY_MS, MILLISECONDS,
244 >                                   new ArrayBlockingQueue<Runnable>(10),
245 >                                   h);
246          assertSame(h, p.getRejectedExecutionHandler());
247          joinPool(p);
248      }
# Line 185 | Line 252 | public class ThreadPoolExecutorTest exte
252       * getRejectedExecutionHandler
253       */
254      public void testSetRejectedExecutionHandler() {
255 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
255 >        final ThreadPoolExecutor p =
256 >            new ThreadPoolExecutor(1, 2,
257 >                                   LONG_DELAY_MS, MILLISECONDS,
258 >                                   new ArrayBlockingQueue<Runnable>(10));
259          RejectedExecutionHandler h = new NoOpREHandler();
260          p.setRejectedExecutionHandler(h);
261          assertSame(h, p.getRejectedExecutionHandler());
# Line 197 | Line 267 | public class ThreadPoolExecutorTest exte
267       * setRejectedExecutionHandler(null) throws NPE
268       */
269      public void testSetRejectedExecutionHandlerNull() {
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.setRejectedExecutionHandler(null);
276              shouldThrow();
# Line 213 | Line 286 | public class ThreadPoolExecutorTest exte
286       * multiple threads active
287       */
288      public void testGetLargestPoolSize() throws InterruptedException {
289 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
290 <        assertEquals(0, p2.getLargestPoolSize());
291 <        p2.execute(new MediumRunnable());
292 <        p2.execute(new MediumRunnable());
293 <        Thread.sleep(SHORT_DELAY_MS);
294 <        assertEquals(2, p2.getLargestPoolSize());
295 <        joinPool(p2);
289 >        final int THREADS = 3;
290 >        final ThreadPoolExecutor p =
291 >            new ThreadPoolExecutor(THREADS, THREADS,
292 >                                   LONG_DELAY_MS, MILLISECONDS,
293 >                                   new ArrayBlockingQueue<Runnable>(10));
294 >        final CountDownLatch threadsStarted = new CountDownLatch(THREADS);
295 >        final CountDownLatch done = new CountDownLatch(1);
296 >        try {
297 >            assertEquals(0, p.getLargestPoolSize());
298 >            for (int i = 0; i < THREADS; i++)
299 >                p.execute(new CheckedRunnable() {
300 >                    public void realRun() throws InterruptedException {
301 >                        threadsStarted.countDown();
302 >                        done.await();
303 >                        assertEquals(THREADS, p.getLargestPoolSize());
304 >                    }});
305 >            assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS));
306 >            assertEquals(THREADS, p.getLargestPoolSize());
307 >        } finally {
308 >            done.countDown();
309 >            joinPool(p);
310 >            assertEquals(THREADS, p.getLargestPoolSize());
311 >        }
312      }
313  
314      /**
# Line 227 | Line 316 | public class ThreadPoolExecutorTest exte
316       * otherwise set
317       */
318      public void testGetMaximumPoolSize() {
319 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
320 <        assertEquals(2, p2.getMaximumPoolSize());
321 <        joinPool(p2);
319 >        final ThreadPoolExecutor p =
320 >            new ThreadPoolExecutor(2, 3,
321 >                                   LONG_DELAY_MS, MILLISECONDS,
322 >                                   new ArrayBlockingQueue<Runnable>(10));
323 >        assertEquals(3, p.getMaximumPoolSize());
324 >        joinPool(p);
325      }
326  
327      /**
328       * getPoolSize increases, but doesn't overestimate, when threads
329       * become active
330       */
331 <    public void testGetPoolSize() {
332 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
333 <        assertEquals(0, p1.getPoolSize());
334 <        p1.execute(new MediumRunnable());
335 <        assertEquals(1, p1.getPoolSize());
336 <        joinPool(p1);
331 >    public void testGetPoolSize() throws InterruptedException {
332 >        final ThreadPoolExecutor p =
333 >            new ThreadPoolExecutor(1, 1,
334 >                                   LONG_DELAY_MS, MILLISECONDS,
335 >                                   new ArrayBlockingQueue<Runnable>(10));
336 >        final CountDownLatch threadStarted = new CountDownLatch(1);
337 >        final CountDownLatch done = new CountDownLatch(1);
338 >        try {
339 >            assertEquals(0, p.getPoolSize());
340 >            p.execute(new CheckedRunnable() {
341 >                public void realRun() throws InterruptedException {
342 >                    threadStarted.countDown();
343 >                    assertEquals(1, p.getPoolSize());
344 >                    done.await();
345 >                }});
346 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
347 >            assertEquals(1, p.getPoolSize());
348 >        } finally {
349 >            done.countDown();
350 >            joinPool(p);
351 >        }
352      }
353  
354      /**
355       * getTaskCount increases, but doesn't overestimate, when tasks submitted
356       */
357      public void testGetTaskCount() throws InterruptedException {
358 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
359 <        assertEquals(0, p1.getTaskCount());
360 <        p1.execute(new MediumRunnable());
361 <        Thread.sleep(SHORT_DELAY_MS);
362 <        assertEquals(1, p1.getTaskCount());
363 <        joinPool(p1);
358 >        final ThreadPoolExecutor p =
359 >            new ThreadPoolExecutor(1, 1,
360 >                                   LONG_DELAY_MS, MILLISECONDS,
361 >                                   new ArrayBlockingQueue<Runnable>(10));
362 >        final CountDownLatch threadStarted = new CountDownLatch(1);
363 >        final CountDownLatch done = new CountDownLatch(1);
364 >        try {
365 >            assertEquals(0, p.getTaskCount());
366 >            p.execute(new CheckedRunnable() {
367 >                public void realRun() throws InterruptedException {
368 >                    threadStarted.countDown();
369 >                    assertEquals(1, p.getTaskCount());
370 >                    done.await();
371 >                }});
372 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
373 >            assertEquals(1, p.getTaskCount());
374 >        } finally {
375 >            done.countDown();
376 >            joinPool(p);
377 >        }
378      }
379  
380      /**
381       * isShutDown is false before shutdown, true after
382       */
383      public void testIsShutdown() {
384 <
385 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
386 <        assertFalse(p1.isShutdown());
387 <        try { p1.shutdown(); } catch (SecurityException ok) { return; }
388 <        assertTrue(p1.isShutdown());
389 <        joinPool(p1);
384 >        final ThreadPoolExecutor p =
385 >            new ThreadPoolExecutor(1, 1,
386 >                                   LONG_DELAY_MS, MILLISECONDS,
387 >                                   new ArrayBlockingQueue<Runnable>(10));
388 >        assertFalse(p.isShutdown());
389 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
390 >        assertTrue(p.isShutdown());
391 >        joinPool(p);
392      }
393  
394  
# Line 273 | Line 396 | public class ThreadPoolExecutorTest exte
396       * isTerminated is false before termination, true after
397       */
398      public void testIsTerminated() throws InterruptedException {
399 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
400 <        assertFalse(p1.isTerminated());
401 <        try {
402 <            p1.execute(new MediumRunnable());
399 >        final ThreadPoolExecutor p =
400 >            new ThreadPoolExecutor(1, 1,
401 >                                   LONG_DELAY_MS, MILLISECONDS,
402 >                                   new ArrayBlockingQueue<Runnable>(10));
403 >        final CountDownLatch threadStarted = new CountDownLatch(1);
404 >        final CountDownLatch done = new CountDownLatch(1);
405 >        assertFalse(p.isTerminated());
406 >        try {
407 >            p.execute(new CheckedRunnable() {
408 >                public void realRun() throws InterruptedException {
409 >                    threadStarted.countDown();
410 >                    assertFalse(p.isTerminated());
411 >                    done.await();
412 >                }});
413 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
414 >            done.countDown();
415          } finally {
416 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
416 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
417          }
418 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
419 <        assertTrue(p1.isTerminated());
418 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
419 >        assertTrue(p.isTerminated());
420      }
421  
422      /**
423       * isTerminating is not true when running or when terminated
424       */
425      public void testIsTerminating() throws InterruptedException {
426 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
427 <        assertFalse(p1.isTerminating());
426 >        final ThreadPoolExecutor p =
427 >            new ThreadPoolExecutor(1, 1,
428 >                                   LONG_DELAY_MS, MILLISECONDS,
429 >                                   new ArrayBlockingQueue<Runnable>(10));
430 >        final CountDownLatch threadStarted = new CountDownLatch(1);
431 >        final CountDownLatch done = new CountDownLatch(1);
432          try {
433 <            p1.execute(new SmallRunnable());
434 <            assertFalse(p1.isTerminating());
435 <        } finally {
436 <            try { p1.shutdown(); } catch (SecurityException ok) { return; }
437 <        }
438 <        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
439 <        assertTrue(p1.isTerminated());
440 <        assertFalse(p1.isTerminating());
433 >            assertFalse(p.isTerminating());
434 >            p.execute(new CheckedRunnable() {
435 >                public void realRun() throws InterruptedException {
436 >                    threadStarted.countDown();
437 >                    assertFalse(p.isTerminating());
438 >                    done.await();
439 >                }});
440 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
441 >            assertFalse(p.isTerminating());
442 >            done.countDown();
443 >        } finally {
444 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
445 >        }
446 >        assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
447 >        assertTrue(p.isTerminated());
448 >        assertFalse(p.isTerminating());
449      }
450  
451      /**
452       * getQueue returns the work queue, which contains queued tasks
453       */
454      public void testGetQueue() throws InterruptedException {
455 <        BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
456 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
457 <        FutureTask[] tasks = new FutureTask[5];
458 <        for (int i = 0; i < 5; i++) {
459 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
460 <            p1.execute(tasks[i]);
461 <        }
462 <        try {
463 <            Thread.sleep(SHORT_DELAY_MS);
464 <            BlockingQueue<Runnable> wq = p1.getQueue();
465 <            assertSame(q, wq);
466 <            assertFalse(wq.contains(tasks[0]));
467 <            assertTrue(wq.contains(tasks[4]));
468 <            for (int i = 1; i < 5; ++i)
469 <                tasks[i].cancel(true);
470 <            p1.shutdownNow();
455 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
456 >        final ThreadPoolExecutor p =
457 >            new ThreadPoolExecutor(1, 1,
458 >                                   LONG_DELAY_MS, MILLISECONDS,
459 >                                   q);
460 >        final CountDownLatch threadStarted = new CountDownLatch(1);
461 >        final CountDownLatch done = new CountDownLatch(1);
462 >        try {
463 >            FutureTask[] tasks = new FutureTask[5];
464 >            for (int i = 0; i < tasks.length; i++) {
465 >                Callable task = new CheckedCallable<Boolean>() {
466 >                    public Boolean realCall() throws InterruptedException {
467 >                        threadStarted.countDown();
468 >                        assertSame(q, p.getQueue());
469 >                        done.await();
470 >                        return Boolean.TRUE;
471 >                    }};
472 >                tasks[i] = new FutureTask(task);
473 >                p.execute(tasks[i]);
474 >            }
475 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
476 >            assertSame(q, p.getQueue());
477 >            assertFalse(q.contains(tasks[0]));
478 >            assertTrue(q.contains(tasks[tasks.length - 1]));
479 >            assertEquals(tasks.length - 1, q.size());
480          } finally {
481 <            joinPool(p1);
481 >            done.countDown();
482 >            joinPool(p);
483          }
484      }
485  
# Line 331 | Line 488 | public class ThreadPoolExecutorTest exte
488       */
489      public void testRemove() throws InterruptedException {
490          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
491 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
492 <        FutureTask[] tasks = new FutureTask[5];
493 <        for (int i = 0; i < 5; i++) {
494 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
495 <            p1.execute(tasks[i]);
496 <        }
497 <        try {
498 <            Thread.sleep(SHORT_DELAY_MS);
499 <            assertFalse(p1.remove(tasks[0]));
491 >        final ThreadPoolExecutor p =
492 >            new ThreadPoolExecutor(1, 1,
493 >                                   LONG_DELAY_MS, MILLISECONDS,
494 >                                   q);
495 >        Runnable[] tasks = new Runnable[5];
496 >        final CountDownLatch threadStarted = new CountDownLatch(1);
497 >        final CountDownLatch done = new CountDownLatch(1);
498 >        try {
499 >            for (int i = 0; i < tasks.length; i++) {
500 >                tasks[i] = new CheckedRunnable() {
501 >                    public void realRun() throws InterruptedException {
502 >                        threadStarted.countDown();
503 >                        done.await();
504 >                    }};
505 >                p.execute(tasks[i]);
506 >            }
507 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
508 >            assertFalse(p.remove(tasks[0]));
509              assertTrue(q.contains(tasks[4]));
510              assertTrue(q.contains(tasks[3]));
511 <            assertTrue(p1.remove(tasks[4]));
512 <            assertFalse(p1.remove(tasks[4]));
511 >            assertTrue(p.remove(tasks[4]));
512 >            assertFalse(p.remove(tasks[4]));
513              assertFalse(q.contains(tasks[4]));
514              assertTrue(q.contains(tasks[3]));
515 <            assertTrue(p1.remove(tasks[3]));
515 >            assertTrue(p.remove(tasks[3]));
516              assertFalse(q.contains(tasks[3]));
517          } finally {
518 <            joinPool(p1);
518 >            done.countDown();
519 >            joinPool(p);
520          }
521      }
522  
523      /**
524       * purge removes cancelled tasks from the queue
525       */
526 <    public void testPurge() {
527 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
526 >    public void testPurge() throws InterruptedException {
527 >        final CountDownLatch threadStarted = new CountDownLatch(1);
528 >        final CountDownLatch done = new CountDownLatch(1);
529 >        final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
530 >        final ThreadPoolExecutor p =
531 >            new ThreadPoolExecutor(1, 1,
532 >                                   LONG_DELAY_MS, MILLISECONDS,
533 >                                   q);
534          FutureTask[] tasks = new FutureTask[5];
535 <        for (int i = 0; i < 5; i++) {
536 <            tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
537 <            p1.execute(tasks[i]);
538 <        }
539 <        tasks[4].cancel(true);
540 <        tasks[3].cancel(true);
541 <        p1.purge();
542 <        long count = p1.getTaskCount();
543 <        assertTrue(count >= 2 && count < 5);
544 <        joinPool(p1);
535 >        try {
536 >            for (int i = 0; i < tasks.length; i++) {
537 >                Callable task = new CheckedCallable<Boolean>() {
538 >                    public Boolean realCall() throws InterruptedException {
539 >                        threadStarted.countDown();
540 >                        done.await();
541 >                        return Boolean.TRUE;
542 >                    }};
543 >                tasks[i] = new FutureTask(task);
544 >                p.execute(tasks[i]);
545 >            }
546 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
547 >            assertEquals(tasks.length, p.getTaskCount());
548 >            assertEquals(tasks.length - 1, q.size());
549 >            assertEquals(1L, p.getActiveCount());
550 >            assertEquals(0L, p.getCompletedTaskCount());
551 >            tasks[4].cancel(true);
552 >            tasks[3].cancel(false);
553 >            p.purge();
554 >            assertEquals(tasks.length - 3, q.size());
555 >            assertEquals(tasks.length - 2, p.getTaskCount());
556 >            p.purge();         // Nothing to do
557 >            assertEquals(tasks.length - 3, q.size());
558 >            assertEquals(tasks.length - 2, p.getTaskCount());
559 >        } finally {
560 >            done.countDown();
561 >            joinPool(p);
562 >        }
563      }
564  
565      /**
566       * shutDownNow returns a list containing tasks that were not run
567       */
568      public void testShutDownNow() {
569 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
569 >        final ThreadPoolExecutor p =
570 >            new ThreadPoolExecutor(1, 1,
571 >                                   LONG_DELAY_MS, MILLISECONDS,
572 >                                   new ArrayBlockingQueue<Runnable>(10));
573          List l;
574          try {
575              for (int i = 0; i < 5; i++)
576 <                p1.execute(new MediumPossiblyInterruptedRunnable());
576 >                p.execute(new MediumPossiblyInterruptedRunnable());
577          }
578          finally {
579              try {
580 <                l = p1.shutdownNow();
580 >                l = p.shutdownNow();
581              } catch (SecurityException ok) { return; }
582          }
583 <        assertTrue(p1.isShutdown());
583 >        assertTrue(p.isShutdown());
584          assertTrue(l.size() <= 4);
585      }
586  
# Line 398 | Line 592 | public class ThreadPoolExecutorTest exte
592       */
593      public void testConstructor1() {
594          try {
595 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
595 >            new ThreadPoolExecutor(-1, 1,
596 >                                   LONG_DELAY_MS, MILLISECONDS,
597 >                                   new ArrayBlockingQueue<Runnable>(10));
598              shouldThrow();
599          } catch (IllegalArgumentException success) {}
600      }
# Line 408 | Line 604 | public class ThreadPoolExecutorTest exte
604       */
605      public void testConstructor2() {
606          try {
607 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
607 >            new ThreadPoolExecutor(1, -1,
608 >                                   LONG_DELAY_MS, MILLISECONDS,
609 >                                   new ArrayBlockingQueue<Runnable>(10));
610              shouldThrow();
611          } catch (IllegalArgumentException success) {}
612      }
# Line 418 | Line 616 | public class ThreadPoolExecutorTest exte
616       */
617      public void testConstructor3() {
618          try {
619 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
619 >            new ThreadPoolExecutor(1, 0,
620 >                                   LONG_DELAY_MS, MILLISECONDS,
621 >                                   new ArrayBlockingQueue<Runnable>(10));
622              shouldThrow();
623          } catch (IllegalArgumentException success) {}
624      }
# Line 428 | Line 628 | public class ThreadPoolExecutorTest exte
628       */
629      public void testConstructor4() {
630          try {
631 <            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
631 >            new ThreadPoolExecutor(1, 2,
632 >                                   -1L, MILLISECONDS,
633 >                                   new ArrayBlockingQueue<Runnable>(10));
634              shouldThrow();
635          } catch (IllegalArgumentException success) {}
636      }
# Line 438 | Line 640 | public class ThreadPoolExecutorTest exte
640       */
641      public void testConstructor5() {
642          try {
643 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
643 >            new ThreadPoolExecutor(2, 1,
644 >                                   LONG_DELAY_MS, MILLISECONDS,
645 >                                   new ArrayBlockingQueue<Runnable>(10));
646              shouldThrow();
647          } catch (IllegalArgumentException success) {}
648      }
# Line 448 | Line 652 | public class ThreadPoolExecutorTest exte
652       */
653      public void testConstructorNullPointerException() {
654          try {
655 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null);
655 >            new ThreadPoolExecutor(1, 2,
656 >                                   LONG_DELAY_MS, MILLISECONDS,
657 >                                   (BlockingQueue) null);
658              shouldThrow();
659          } catch (NullPointerException success) {}
660      }
# Line 460 | Line 666 | public class ThreadPoolExecutorTest exte
666       */
667      public void testConstructor6() {
668          try {
669 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
669 >            new ThreadPoolExecutor(-1, 1,
670 >                                   LONG_DELAY_MS, MILLISECONDS,
671 >                                   new ArrayBlockingQueue<Runnable>(10),
672 >                                   new SimpleThreadFactory());
673              shouldThrow();
674          } catch (IllegalArgumentException success) {}
675      }
# Line 470 | Line 679 | public class ThreadPoolExecutorTest exte
679       */
680      public void testConstructor7() {
681          try {
682 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
682 >            new ThreadPoolExecutor(1, -1,
683 >                                   LONG_DELAY_MS, MILLISECONDS,
684 >                                   new ArrayBlockingQueue<Runnable>(10),
685 >                                   new SimpleThreadFactory());
686              shouldThrow();
687          } catch (IllegalArgumentException success) {}
688      }
# Line 480 | Line 692 | public class ThreadPoolExecutorTest exte
692       */
693      public void testConstructor8() {
694          try {
695 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
695 >            new ThreadPoolExecutor(1, 0,
696 >                                   LONG_DELAY_MS, MILLISECONDS,
697 >                                   new ArrayBlockingQueue<Runnable>(10),
698 >                                   new SimpleThreadFactory());
699              shouldThrow();
700          } catch (IllegalArgumentException success) {}
701      }
# Line 490 | Line 705 | public class ThreadPoolExecutorTest exte
705       */
706      public void testConstructor9() {
707          try {
708 <            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
708 >            new ThreadPoolExecutor(1, 2,
709 >                                   -1L, MILLISECONDS,
710 >                                   new ArrayBlockingQueue<Runnable>(10),
711 >                                   new SimpleThreadFactory());
712              shouldThrow();
713          } catch (IllegalArgumentException success) {}
714      }
# Line 500 | Line 718 | public class ThreadPoolExecutorTest exte
718       */
719      public void testConstructor10() {
720          try {
721 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
721 >            new ThreadPoolExecutor(2, 1,
722 >                                   LONG_DELAY_MS, MILLISECONDS,
723 >                                   new ArrayBlockingQueue<Runnable>(10),
724 >                                   new SimpleThreadFactory());
725              shouldThrow();
726          } catch (IllegalArgumentException success) {}
727      }
# Line 510 | Line 731 | public class ThreadPoolExecutorTest exte
731       */
732      public void testConstructorNullPointerException2() {
733          try {
734 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
734 >            new ThreadPoolExecutor(1, 2,
735 >                                   LONG_DELAY_MS, MILLISECONDS,
736 >                                   (BlockingQueue) null,
737 >                                   new SimpleThreadFactory());
738              shouldThrow();
739          } catch (NullPointerException success) {}
740      }
# Line 520 | Line 744 | public class ThreadPoolExecutorTest exte
744       */
745      public void testConstructorNullPointerException3() {
746          try {
747 <            ThreadFactory f = null;
748 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
747 >            new ThreadPoolExecutor(1, 2,
748 >                                   LONG_DELAY_MS, MILLISECONDS,
749 >                                   new ArrayBlockingQueue<Runnable>(10),
750 >                                   (ThreadFactory) null);
751              shouldThrow();
752          } catch (NullPointerException success) {}
753      }
# Line 532 | Line 758 | public class ThreadPoolExecutorTest exte
758       */
759      public void testConstructor11() {
760          try {
761 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
761 >            new ThreadPoolExecutor(-1, 1,
762 >                                   LONG_DELAY_MS, MILLISECONDS,
763 >                                   new ArrayBlockingQueue<Runnable>(10),
764 >                                   new NoOpREHandler());
765              shouldThrow();
766          } catch (IllegalArgumentException success) {}
767      }
# Line 542 | Line 771 | public class ThreadPoolExecutorTest exte
771       */
772      public void testConstructor12() {
773          try {
774 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
774 >            new ThreadPoolExecutor(1, -1,
775 >                                   LONG_DELAY_MS, MILLISECONDS,
776 >                                   new ArrayBlockingQueue<Runnable>(10),
777 >                                   new NoOpREHandler());
778              shouldThrow();
779          } catch (IllegalArgumentException success) {}
780      }
# Line 552 | Line 784 | public class ThreadPoolExecutorTest exte
784       */
785      public void testConstructor13() {
786          try {
787 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
787 >            new ThreadPoolExecutor(1, 0,
788 >                                   LONG_DELAY_MS, MILLISECONDS,
789 >                                   new ArrayBlockingQueue<Runnable>(10),
790 >                                   new NoOpREHandler());
791              shouldThrow();
792          } catch (IllegalArgumentException success) {}
793      }
# Line 562 | Line 797 | public class ThreadPoolExecutorTest exte
797       */
798      public void testConstructor14() {
799          try {
800 <            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
800 >            new ThreadPoolExecutor(1, 2,
801 >                                   -1L, MILLISECONDS,
802 >                                   new ArrayBlockingQueue<Runnable>(10),
803 >                                   new NoOpREHandler());
804              shouldThrow();
805          } catch (IllegalArgumentException success) {}
806      }
# Line 572 | Line 810 | public class ThreadPoolExecutorTest exte
810       */
811      public void testConstructor15() {
812          try {
813 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
813 >            new ThreadPoolExecutor(2, 1,
814 >                                   LONG_DELAY_MS, MILLISECONDS,
815 >                                   new ArrayBlockingQueue<Runnable>(10),
816 >                                   new NoOpREHandler());
817              shouldThrow();
818          } catch (IllegalArgumentException success) {}
819      }
# Line 582 | Line 823 | public class ThreadPoolExecutorTest exte
823       */
824      public void testConstructorNullPointerException4() {
825          try {
826 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
826 >            new ThreadPoolExecutor(1, 2,
827 >                                   LONG_DELAY_MS, MILLISECONDS,
828 >                                   (BlockingQueue) null,
829 >                                   new NoOpREHandler());
830              shouldThrow();
831          } catch (NullPointerException success) {}
832      }
# Line 592 | Line 836 | public class ThreadPoolExecutorTest exte
836       */
837      public void testConstructorNullPointerException5() {
838          try {
839 <            RejectedExecutionHandler r = null;
840 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
839 >            new ThreadPoolExecutor(1, 2,
840 >                                   LONG_DELAY_MS, MILLISECONDS,
841 >                                   new ArrayBlockingQueue<Runnable>(10),
842 >                                   (RejectedExecutionHandler) null);
843              shouldThrow();
844          } catch (NullPointerException success) {}
845      }
# Line 604 | Line 850 | public class ThreadPoolExecutorTest exte
850       */
851      public void testConstructor16() {
852          try {
853 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
853 >            new ThreadPoolExecutor(-1, 1,
854 >                                   LONG_DELAY_MS, MILLISECONDS,
855 >                                   new ArrayBlockingQueue<Runnable>(10),
856 >                                   new SimpleThreadFactory(),
857 >                                   new NoOpREHandler());
858              shouldThrow();
859          } catch (IllegalArgumentException success) {}
860      }
# Line 614 | Line 864 | public class ThreadPoolExecutorTest exte
864       */
865      public void testConstructor17() {
866          try {
867 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
867 >            new ThreadPoolExecutor(1, -1,
868 >                                   LONG_DELAY_MS, MILLISECONDS,
869 >                                   new ArrayBlockingQueue<Runnable>(10),
870 >                                   new SimpleThreadFactory(),
871 >                                   new NoOpREHandler());
872              shouldThrow();
873          } catch (IllegalArgumentException success) {}
874      }
# Line 624 | Line 878 | public class ThreadPoolExecutorTest exte
878       */
879      public void testConstructor18() {
880          try {
881 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
881 >            new ThreadPoolExecutor(1, 0,
882 >                                   LONG_DELAY_MS, MILLISECONDS,
883 >                                   new ArrayBlockingQueue<Runnable>(10),
884 >                                   new SimpleThreadFactory(),
885 >                                   new NoOpREHandler());
886              shouldThrow();
887          } catch (IllegalArgumentException success) {}
888      }
# Line 634 | Line 892 | public class ThreadPoolExecutorTest exte
892       */
893      public void testConstructor19() {
894          try {
895 <            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
895 >            new ThreadPoolExecutor(1, 2,
896 >                                   -1L, MILLISECONDS,
897 >                                   new ArrayBlockingQueue<Runnable>(10),
898 >                                   new SimpleThreadFactory(),
899 >                                   new NoOpREHandler());
900              shouldThrow();
901          } catch (IllegalArgumentException success) {}
902      }
# Line 644 | Line 906 | public class ThreadPoolExecutorTest exte
906       */
907      public void testConstructor20() {
908          try {
909 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
909 >            new ThreadPoolExecutor(2, 1,
910 >                                   LONG_DELAY_MS, MILLISECONDS,
911 >                                   new ArrayBlockingQueue<Runnable>(10),
912 >                                   new SimpleThreadFactory(),
913 >                                   new NoOpREHandler());
914              shouldThrow();
915          } catch (IllegalArgumentException success) {}
916      }
# Line 654 | Line 920 | public class ThreadPoolExecutorTest exte
920       */
921      public void testConstructorNullPointerException6() {
922          try {
923 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
923 >            new ThreadPoolExecutor(1, 2,
924 >                                   LONG_DELAY_MS, MILLISECONDS,
925 >                                   (BlockingQueue) null,
926 >                                   new SimpleThreadFactory(),
927 >                                   new NoOpREHandler());
928              shouldThrow();
929          } catch (NullPointerException success) {}
930      }
# Line 664 | Line 934 | public class ThreadPoolExecutorTest exte
934       */
935      public void testConstructorNullPointerException7() {
936          try {
937 <            RejectedExecutionHandler r = null;
938 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
937 >            new ThreadPoolExecutor(1, 2,
938 >                                   LONG_DELAY_MS, MILLISECONDS,
939 >                                   new ArrayBlockingQueue<Runnable>(10),
940 >                                   new SimpleThreadFactory(),
941 >                                   (RejectedExecutionHandler) null);
942              shouldThrow();
943          } catch (NullPointerException success) {}
944      }
# Line 675 | Line 948 | public class ThreadPoolExecutorTest exte
948       */
949      public void testConstructorNullPointerException8() {
950          try {
951 <            ThreadFactory f = null;
952 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
951 >            new ThreadPoolExecutor(1, 2,
952 >                                   LONG_DELAY_MS, MILLISECONDS,
953 >                                   new ArrayBlockingQueue<Runnable>(10),
954 >                                   (ThreadFactory) null,
955 >                                   new NoOpREHandler());
956              shouldThrow();
957          } catch (NullPointerException success) {}
958      }
959  
960 +    /**
961 +     * get of submitted callable throws InterruptedException if interrupted
962 +     */
963 +    public void testInterruptedSubmit() throws InterruptedException {
964 +        final ThreadPoolExecutor p =
965 +            new ThreadPoolExecutor(1, 1,
966 +                                   60, TimeUnit.SECONDS,
967 +                                   new ArrayBlockingQueue<Runnable>(10));
968 +
969 +        final CountDownLatch threadStarted = new CountDownLatch(1);
970 +        final CountDownLatch done = new CountDownLatch(1);
971 +        try {
972 +            Thread t = newStartedThread(new CheckedInterruptedRunnable() {
973 +                public void realRun() throws Exception {
974 +                    Callable task = new CheckedCallable<Boolean>() {
975 +                        public Boolean realCall() throws InterruptedException {
976 +                            threadStarted.countDown();
977 +                            done.await();
978 +                            return Boolean.TRUE;
979 +                        }};
980 +                    p.submit(task).get();
981 +                }});
982 +
983 +            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
984 +            t.interrupt();
985 +            awaitTermination(t, MEDIUM_DELAY_MS);
986 +        } finally {
987 +            done.countDown();
988 +            joinPool(p);
989 +        }
990 +    }
991  
992      /**
993       * execute throws RejectedExecutionException if saturated.
# Line 690 | Line 997 | public class ThreadPoolExecutorTest exte
997              new ThreadPoolExecutor(1, 1,
998                                     LONG_DELAY_MS, MILLISECONDS,
999                                     new ArrayBlockingQueue<Runnable>(1));
1000 +        final CountDownLatch done = new CountDownLatch(1);
1001 +        try {
1002 +            Runnable task = new CheckedRunnable() {
1003 +                public void realRun() throws InterruptedException {
1004 +                    done.await();
1005 +                }};
1006 +            for (int i = 0; i < 2; ++i)
1007 +                p.execute(task);
1008 +            for (int i = 0; i < 2; ++i) {
1009 +                try {
1010 +                    p.execute(task);
1011 +                    shouldThrow();
1012 +                } catch (RejectedExecutionException success) {}
1013 +                assertTrue(p.getTaskCount() <= 2);
1014 +            }
1015 +        } finally {
1016 +            done.countDown();
1017 +            joinPool(p);
1018 +        }
1019 +    }
1020 +
1021 +    /**
1022 +     * submit(runnable) throws RejectedExecutionException if saturated.
1023 +     */
1024 +    public void testSaturatedSubmitRunnable() {
1025 +        ThreadPoolExecutor p =
1026 +            new ThreadPoolExecutor(1, 1,
1027 +                                   LONG_DELAY_MS, MILLISECONDS,
1028 +                                   new ArrayBlockingQueue<Runnable>(1));
1029 +        final CountDownLatch done = new CountDownLatch(1);
1030 +        try {
1031 +            Runnable task = new CheckedRunnable() {
1032 +                public void realRun() throws InterruptedException {
1033 +                    done.await();
1034 +                }};
1035 +            for (int i = 0; i < 2; ++i)
1036 +                p.submit(task);
1037 +            for (int i = 0; i < 2; ++i) {
1038 +                try {
1039 +                    p.execute(task);
1040 +                    shouldThrow();
1041 +                } catch (RejectedExecutionException success) {}
1042 +                assertTrue(p.getTaskCount() <= 2);
1043 +            }
1044 +        } finally {
1045 +            done.countDown();
1046 +            joinPool(p);
1047 +        }
1048 +    }
1049 +
1050 +    /**
1051 +     * submit(callable) throws RejectedExecutionException if saturated.
1052 +     */
1053 +    public void testSaturatedSubmitCallable() {
1054 +        ThreadPoolExecutor p =
1055 +            new ThreadPoolExecutor(1, 1,
1056 +                                   LONG_DELAY_MS, MILLISECONDS,
1057 +                                   new ArrayBlockingQueue<Runnable>(1));
1058 +        final CountDownLatch done = new CountDownLatch(1);
1059          try {
1060 +            Runnable task = new CheckedRunnable() {
1061 +                public void realRun() throws InterruptedException {
1062 +                    done.await();
1063 +                }};
1064              for (int i = 0; i < 2; ++i)
1065 <                p.execute(new MediumRunnable());
1065 >                p.submit(Executors.callable(task));
1066              for (int i = 0; i < 2; ++i) {
1067                  try {
1068 <                    p.execute(new MediumRunnable());
1068 >                    p.execute(task);
1069                      shouldThrow();
1070                  } catch (RejectedExecutionException success) {}
1071 +                assertTrue(p.getTaskCount() <= 2);
1072              }
1073          } finally {
1074 +            done.countDown();
1075              joinPool(p);
1076          }
1077      }
# Line 709 | Line 1081 | public class ThreadPoolExecutorTest exte
1081       */
1082      public void testSaturatedExecute2() {
1083          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1084 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1084 >        final ThreadPoolExecutor p =
1085 >            new ThreadPoolExecutor(1, 1,
1086 >                                   LONG_DELAY_MS,
1087 >                                   MILLISECONDS,
1088 >                                   new ArrayBlockingQueue<Runnable>(1),
1089 >                                   h);
1090          try {
714
1091              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1092 <            for (int i = 0; i < 5; ++i) {
1092 >            for (int i = 0; i < tasks.length; ++i)
1093                  tasks[i] = new TrackedNoOpRunnable();
718            }
1094              TrackedLongRunnable mr = new TrackedLongRunnable();
1095              p.execute(mr);
1096 <            for (int i = 0; i < 5; ++i) {
1096 >            for (int i = 0; i < tasks.length; ++i)
1097                  p.execute(tasks[i]);
1098 <            }
724 <            for (int i = 1; i < 5; ++i) {
1098 >            for (int i = 1; i < tasks.length; ++i)
1099                  assertTrue(tasks[i].done);
726            }
1100              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1101          } finally {
1102              joinPool(p);
# Line 735 | Line 1108 | public class ThreadPoolExecutorTest exte
1108       */
1109      public void testSaturatedExecute3() {
1110          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1111 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1111 >        final ThreadPoolExecutor p =
1112 >            new ThreadPoolExecutor(1, 1,
1113 >                                   LONG_DELAY_MS, MILLISECONDS,
1114 >                                   new ArrayBlockingQueue<Runnable>(1),
1115 >                                   h);
1116          try {
740
1117              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
1118 <            for (int i = 0; i < 5; ++i) {
1118 >            for (int i = 0; i < tasks.length; ++i)
1119                  tasks[i] = new TrackedNoOpRunnable();
744            }
1120              p.execute(new TrackedLongRunnable());
1121 <            for (int i = 0; i < 5; ++i) {
1122 <                p.execute(tasks[i]);
1123 <            }
1124 <            for (int i = 0; i < 5; ++i) {
750 <                assertFalse(tasks[i].done);
751 <            }
1121 >            for (TrackedNoOpRunnable task : tasks)
1122 >                p.execute(task);
1123 >            for (TrackedNoOpRunnable task : tasks)
1124 >                assertFalse(task.done);
1125              try { p.shutdownNow(); } catch (SecurityException ok) { return; }
1126          } finally {
1127              joinPool(p);
# Line 760 | Line 1133 | public class ThreadPoolExecutorTest exte
1133       */
1134      public void testSaturatedExecute4() {
1135          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1136 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1136 >        final ThreadPoolExecutor p =
1137 >            new ThreadPoolExecutor(1, 1,
1138 >                                   LONG_DELAY_MS, MILLISECONDS,
1139 >                                   new ArrayBlockingQueue<Runnable>(1),
1140 >                                   h);
1141          try {
1142              p.execute(new TrackedLongRunnable());
1143              TrackedLongRunnable r2 = new TrackedLongRunnable();
# Line 780 | Line 1157 | public class ThreadPoolExecutorTest exte
1157       * execute throws RejectedExecutionException if shutdown
1158       */
1159      public void testRejectedExecutionExceptionOnShutdown() {
1160 <        ThreadPoolExecutor tpe =
1161 <            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
1162 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1160 >        ThreadPoolExecutor p =
1161 >            new ThreadPoolExecutor(1, 1,
1162 >                                   LONG_DELAY_MS, MILLISECONDS,
1163 >                                   new ArrayBlockingQueue<Runnable>(1));
1164 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1165          try {
1166 <            tpe.execute(new NoOpRunnable());
1166 >            p.execute(new NoOpRunnable());
1167              shouldThrow();
1168          } catch (RejectedExecutionException success) {}
1169  
1170 <        joinPool(tpe);
1170 >        joinPool(p);
1171      }
1172  
1173      /**
# Line 796 | Line 1175 | public class ThreadPoolExecutorTest exte
1175       */
1176      public void testCallerRunsOnShutdown() {
1177          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
1178 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1178 >        final ThreadPoolExecutor p =
1179 >            new ThreadPoolExecutor(1, 1,
1180 >                                   LONG_DELAY_MS, MILLISECONDS,
1181 >                                   new ArrayBlockingQueue<Runnable>(1), h);
1182  
1183          try { p.shutdown(); } catch (SecurityException ok) { return; }
1184          try {
# Line 813 | Line 1195 | public class ThreadPoolExecutorTest exte
1195       */
1196      public void testDiscardOnShutdown() {
1197          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
1198 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1198 >        ThreadPoolExecutor p =
1199 >            new ThreadPoolExecutor(1, 1,
1200 >                                   LONG_DELAY_MS, MILLISECONDS,
1201 >                                   new ArrayBlockingQueue<Runnable>(1),
1202 >                                   h);
1203  
1204          try { p.shutdown(); } catch (SecurityException ok) { return; }
1205          try {
# Line 831 | Line 1217 | public class ThreadPoolExecutorTest exte
1217       */
1218      public void testDiscardOldestOnShutdown() {
1219          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
1220 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
1220 >        ThreadPoolExecutor p =
1221 >            new ThreadPoolExecutor(1, 1,
1222 >                                   LONG_DELAY_MS, MILLISECONDS,
1223 >                                   new ArrayBlockingQueue<Runnable>(1),
1224 >                                   h);
1225  
1226          try { p.shutdown(); } catch (SecurityException ok) { return; }
1227          try {
# Line 848 | Line 1238 | public class ThreadPoolExecutorTest exte
1238       * execute(null) throws NPE
1239       */
1240      public void testExecuteNull() {
1241 <        ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
1241 >        ThreadPoolExecutor p =
1242 >            new ThreadPoolExecutor(1, 2,
1243 >                                   LONG_DELAY_MS, MILLISECONDS,
1244 >                                   new ArrayBlockingQueue<Runnable>(10));
1245          try {
1246 <            tpe.execute(null);
1246 >            p.execute(null);
1247              shouldThrow();
1248          } catch (NullPointerException success) {}
1249  
1250 <        joinPool(tpe);
1250 >        joinPool(p);
1251      }
1252  
1253      /**
1254       * setCorePoolSize of negative value throws IllegalArgumentException
1255       */
1256      public void testCorePoolSizeIllegalArgumentException() {
1257 <        ThreadPoolExecutor tpe =
1257 >        ThreadPoolExecutor p =
1258              new ThreadPoolExecutor(1, 2,
1259                                     LONG_DELAY_MS, MILLISECONDS,
1260                                     new ArrayBlockingQueue<Runnable>(10));
1261          try {
1262 <            tpe.setCorePoolSize(-1);
1262 >            p.setCorePoolSize(-1);
1263              shouldThrow();
1264          } catch (IllegalArgumentException success) {
1265          } finally {
1266 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1266 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1267          }
1268 <        joinPool(tpe);
1268 >        joinPool(p);
1269      }
1270  
1271      /**
# Line 880 | Line 1273 | public class ThreadPoolExecutorTest exte
1273       * given a value less the core pool size
1274       */
1275      public void testMaximumPoolSizeIllegalArgumentException() {
1276 <        ThreadPoolExecutor tpe =
1276 >        ThreadPoolExecutor p =
1277              new ThreadPoolExecutor(2, 3,
1278                                     LONG_DELAY_MS, MILLISECONDS,
1279                                     new ArrayBlockingQueue<Runnable>(10));
1280          try {
1281 <            tpe.setMaximumPoolSize(1);
1281 >            p.setMaximumPoolSize(1);
1282              shouldThrow();
1283          } catch (IllegalArgumentException success) {
1284          } finally {
1285 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1285 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1286          }
1287 <        joinPool(tpe);
1287 >        joinPool(p);
1288      }
1289  
1290      /**
# Line 899 | Line 1292 | public class ThreadPoolExecutorTest exte
1292       * if given a negative value
1293       */
1294      public void testMaximumPoolSizeIllegalArgumentException2() {
1295 <        ThreadPoolExecutor tpe =
1295 >        ThreadPoolExecutor p =
1296              new ThreadPoolExecutor(2, 3,
1297                                     LONG_DELAY_MS, MILLISECONDS,
1298                                     new ArrayBlockingQueue<Runnable>(10));
1299          try {
1300 <            tpe.setMaximumPoolSize(-1);
1300 >            p.setMaximumPoolSize(-1);
1301              shouldThrow();
1302          } catch (IllegalArgumentException success) {
1303          } finally {
1304 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1304 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1305          }
1306 <        joinPool(tpe);
1306 >        joinPool(p);
1307      }
1308  
1309  
# Line 919 | Line 1312 | public class ThreadPoolExecutorTest exte
1312       * when given a negative value
1313       */
1314      public void testKeepAliveTimeIllegalArgumentException() {
1315 <        ThreadPoolExecutor tpe =
1315 >        ThreadPoolExecutor p =
1316              new ThreadPoolExecutor(2, 3,
1317                                     LONG_DELAY_MS, MILLISECONDS,
1318                                     new ArrayBlockingQueue<Runnable>(10));
1319          try {
1320 <            tpe.setKeepAliveTime(-1,MILLISECONDS);
1320 >            p.setKeepAliveTime(-1,MILLISECONDS);
1321              shouldThrow();
1322          } catch (IllegalArgumentException success) {
1323          } finally {
1324 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1324 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1325          }
1326 <        joinPool(tpe);
1326 >        joinPool(p);
1327      }
1328  
1329      /**
1330       * terminated() is called on termination
1331       */
1332      public void testTerminated() {
1333 <        ExtendedTPE tpe = new ExtendedTPE();
1334 <        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1335 <        assertTrue(tpe.terminatedCalled);
1336 <        joinPool(tpe);
1333 >        ExtendedTPE p = new ExtendedTPE();
1334 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
1335 >        assertTrue(p.terminatedCalled);
1336 >        joinPool(p);
1337      }
1338  
1339      /**
1340       * beforeExecute and afterExecute are called when executing task
1341       */
1342      public void testBeforeAfter() throws InterruptedException {
1343 <        ExtendedTPE tpe = new ExtendedTPE();
1343 >        ExtendedTPE p = new ExtendedTPE();
1344          try {
1345              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
1346 <            tpe.execute(r);
1346 >            p.execute(r);
1347              Thread.sleep(SHORT_DELAY_MS);
1348              assertTrue(r.done);
1349 <            assertTrue(tpe.beforeCalled);
1350 <            assertTrue(tpe.afterCalled);
1351 <            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
1349 >            assertTrue(p.beforeCalled);
1350 >            assertTrue(p.afterCalled);
1351 >            try { p.shutdown(); } catch (SecurityException ok) { return; }
1352          } finally {
1353 <            joinPool(tpe);
1353 >            joinPool(p);
1354          }
1355      }
1356  
# Line 965 | Line 1358 | public class ThreadPoolExecutorTest exte
1358       * completed submit of callable returns result
1359       */
1360      public void testSubmitCallable() throws Exception {
1361 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1361 >        ExecutorService e =
1362 >            new ThreadPoolExecutor(2, 2,
1363 >                                   LONG_DELAY_MS, MILLISECONDS,
1364 >                                   new ArrayBlockingQueue<Runnable>(10));
1365          try {
1366              Future<String> future = e.submit(new StringTask());
1367              String result = future.get();
# Line 979 | Line 1375 | public class ThreadPoolExecutorTest exte
1375       * completed submit of runnable returns successfully
1376       */
1377      public void testSubmitRunnable() throws Exception {
1378 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1378 >        ExecutorService e =
1379 >            new ThreadPoolExecutor(2, 2,
1380 >                                   LONG_DELAY_MS, MILLISECONDS,
1381 >                                   new ArrayBlockingQueue<Runnable>(10));
1382          try {
1383              Future<?> future = e.submit(new NoOpRunnable());
1384              future.get();
# Line 993 | Line 1392 | public class ThreadPoolExecutorTest exte
1392       * completed submit of (runnable, result) returns result
1393       */
1394      public void testSubmitRunnable2() throws Exception {
1395 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1395 >        ExecutorService e =
1396 >            new ThreadPoolExecutor(2, 2,
1397 >                                   LONG_DELAY_MS, MILLISECONDS,
1398 >                                   new ArrayBlockingQueue<Runnable>(10));
1399          try {
1400              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1401              String result = future.get();
# Line 1008 | Line 1410 | public class ThreadPoolExecutorTest exte
1410       * invokeAny(null) throws NPE
1411       */
1412      public void testInvokeAny1() throws Exception {
1413 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1413 >        ExecutorService e =
1414 >            new ThreadPoolExecutor(2, 2,
1415 >                                   LONG_DELAY_MS, MILLISECONDS,
1416 >                                   new ArrayBlockingQueue<Runnable>(10));
1417          try {
1418              e.invokeAny(null);
1419              shouldThrow();
# Line 1022 | Line 1427 | public class ThreadPoolExecutorTest exte
1427       * invokeAny(empty collection) throws IAE
1428       */
1429      public void testInvokeAny2() throws Exception {
1430 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1430 >        ExecutorService e =
1431 >            new ThreadPoolExecutor(2, 2,
1432 >                                   LONG_DELAY_MS, MILLISECONDS,
1433 >                                   new ArrayBlockingQueue<Runnable>(10));
1434          try {
1435              e.invokeAny(new ArrayList<Callable<String>>());
1436              shouldThrow();
# Line 1036 | Line 1444 | public class ThreadPoolExecutorTest exte
1444       * invokeAny(c) throws NPE if c has null elements
1445       */
1446      public void testInvokeAny3() throws Exception {
1447 <        CountDownLatch latch = new CountDownLatch(1);
1448 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1447 >        final CountDownLatch latch = new CountDownLatch(1);
1448 >        final ExecutorService e =
1449 >            new ThreadPoolExecutor(2, 2,
1450 >                                   LONG_DELAY_MS, MILLISECONDS,
1451 >                                   new ArrayBlockingQueue<Runnable>(10));
1452          List<Callable<String>> l = new ArrayList<Callable<String>>();
1453          l.add(latchAwaitingStringTask(latch));
1454          l.add(null);
# Line 1055 | Line 1466 | public class ThreadPoolExecutorTest exte
1466       * invokeAny(c) throws ExecutionException if no task completes
1467       */
1468      public void testInvokeAny4() throws Exception {
1469 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1469 >        ExecutorService e =
1470 >            new ThreadPoolExecutor(2, 2,
1471 >                                   LONG_DELAY_MS, MILLISECONDS,
1472 >                                   new ArrayBlockingQueue<Runnable>(10));
1473          List<Callable<String>> l = new ArrayList<Callable<String>>();
1474          l.add(new NPETask());
1475          try {
# Line 1072 | Line 1486 | public class ThreadPoolExecutorTest exte
1486       * invokeAny(c) returns result of some task
1487       */
1488      public void testInvokeAny5() throws Exception {
1489 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1489 >        ExecutorService e =
1490 >            new ThreadPoolExecutor(2, 2,
1491 >                                   LONG_DELAY_MS, MILLISECONDS,
1492 >                                   new ArrayBlockingQueue<Runnable>(10));
1493          try {
1494              List<Callable<String>> l = new ArrayList<Callable<String>>();
1495              l.add(new StringTask());
# Line 1088 | Line 1505 | public class ThreadPoolExecutorTest exte
1505       * invokeAll(null) throws NPE
1506       */
1507      public void testInvokeAll1() throws Exception {
1508 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1508 >        ExecutorService e =
1509 >            new ThreadPoolExecutor(2, 2,
1510 >                                   LONG_DELAY_MS, MILLISECONDS,
1511 >                                   new ArrayBlockingQueue<Runnable>(10));
1512          try {
1513              e.invokeAll(null);
1514              shouldThrow();
# Line 1102 | Line 1522 | public class ThreadPoolExecutorTest exte
1522       * invokeAll(empty collection) returns empty collection
1523       */
1524      public void testInvokeAll2() throws InterruptedException {
1525 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1525 >        ExecutorService e =
1526 >            new ThreadPoolExecutor(2, 2,
1527 >                                   LONG_DELAY_MS, MILLISECONDS,
1528 >                                   new ArrayBlockingQueue<Runnable>(10));
1529          try {
1530              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1531              assertTrue(r.isEmpty());
# Line 1115 | Line 1538 | public class ThreadPoolExecutorTest exte
1538       * invokeAll(c) throws NPE if c has null elements
1539       */
1540      public void testInvokeAll3() throws Exception {
1541 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1541 >        ExecutorService e =
1542 >            new ThreadPoolExecutor(2, 2,
1543 >                                   LONG_DELAY_MS, MILLISECONDS,
1544 >                                   new ArrayBlockingQueue<Runnable>(10));
1545          List<Callable<String>> l = new ArrayList<Callable<String>>();
1546          l.add(new StringTask());
1547          l.add(null);
# Line 1132 | Line 1558 | public class ThreadPoolExecutorTest exte
1558       * get of element of invokeAll(c) throws exception on failed task
1559       */
1560      public void testInvokeAll4() throws Exception {
1561 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1561 >        ExecutorService e =
1562 >            new ThreadPoolExecutor(2, 2,
1563 >                                   LONG_DELAY_MS, MILLISECONDS,
1564 >                                   new ArrayBlockingQueue<Runnable>(10));
1565          try {
1566              List<Callable<String>> l = new ArrayList<Callable<String>>();
1567              l.add(new NPETask());
# Line 1153 | Line 1582 | public class ThreadPoolExecutorTest exte
1582       * invokeAll(c) returns results of all completed tasks
1583       */
1584      public void testInvokeAll5() throws Exception {
1585 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1585 >        ExecutorService e =
1586 >            new ThreadPoolExecutor(2, 2,
1587 >                                   LONG_DELAY_MS, MILLISECONDS,
1588 >                                   new ArrayBlockingQueue<Runnable>(10));
1589          try {
1590              List<Callable<String>> l = new ArrayList<Callable<String>>();
1591              l.add(new StringTask());
# Line 1173 | Line 1605 | public class ThreadPoolExecutorTest exte
1605       * timed invokeAny(null) throws NPE
1606       */
1607      public void testTimedInvokeAny1() throws Exception {
1608 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1608 >        ExecutorService e =
1609 >            new ThreadPoolExecutor(2, 2,
1610 >                                   LONG_DELAY_MS, MILLISECONDS,
1611 >                                   new ArrayBlockingQueue<Runnable>(10));
1612          try {
1613              e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1614              shouldThrow();
# Line 1187 | Line 1622 | public class ThreadPoolExecutorTest exte
1622       * timed invokeAny(,,null) throws NPE
1623       */
1624      public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1625 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1625 >        ExecutorService e =
1626 >            new ThreadPoolExecutor(2, 2,
1627 >                                   LONG_DELAY_MS, MILLISECONDS,
1628 >                                   new ArrayBlockingQueue<Runnable>(10));
1629          List<Callable<String>> l = new ArrayList<Callable<String>>();
1630          l.add(new StringTask());
1631          try {
# Line 1203 | Line 1641 | public class ThreadPoolExecutorTest exte
1641       * timed invokeAny(empty collection) throws IAE
1642       */
1643      public void testTimedInvokeAny2() throws Exception {
1644 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1644 >        ExecutorService e =
1645 >            new ThreadPoolExecutor(2, 2,
1646 >                                   LONG_DELAY_MS, MILLISECONDS,
1647 >                                   new ArrayBlockingQueue<Runnable>(10));
1648          try {
1649              e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1650              shouldThrow();
# Line 1217 | Line 1658 | public class ThreadPoolExecutorTest exte
1658       * timed invokeAny(c) throws NPE if c has null elements
1659       */
1660      public void testTimedInvokeAny3() throws Exception {
1661 <        CountDownLatch latch = new CountDownLatch(1);
1662 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1661 >        final CountDownLatch latch = new CountDownLatch(1);
1662 >        final ExecutorService e =
1663 >            new ThreadPoolExecutor(2, 2,
1664 >                                   LONG_DELAY_MS, MILLISECONDS,
1665 >                                   new ArrayBlockingQueue<Runnable>(10));
1666          List<Callable<String>> l = new ArrayList<Callable<String>>();
1667          l.add(latchAwaitingStringTask(latch));
1668          l.add(null);
# Line 1236 | Line 1680 | public class ThreadPoolExecutorTest exte
1680       * timed invokeAny(c) throws ExecutionException if no task completes
1681       */
1682      public void testTimedInvokeAny4() throws Exception {
1683 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1683 >        ExecutorService e =
1684 >            new ThreadPoolExecutor(2, 2,
1685 >                                   LONG_DELAY_MS, MILLISECONDS,
1686 >                                   new ArrayBlockingQueue<Runnable>(10));
1687          List<Callable<String>> l = new ArrayList<Callable<String>>();
1688          l.add(new NPETask());
1689          try {
# Line 1253 | Line 1700 | public class ThreadPoolExecutorTest exte
1700       * timed invokeAny(c) returns result of some task
1701       */
1702      public void testTimedInvokeAny5() throws Exception {
1703 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1703 >        ExecutorService e =
1704 >            new ThreadPoolExecutor(2, 2,
1705 >                                   LONG_DELAY_MS, MILLISECONDS,
1706 >                                   new ArrayBlockingQueue<Runnable>(10));
1707          try {
1708              List<Callable<String>> l = new ArrayList<Callable<String>>();
1709              l.add(new StringTask());
# Line 1269 | Line 1719 | public class ThreadPoolExecutorTest exte
1719       * timed invokeAll(null) throws NPE
1720       */
1721      public void testTimedInvokeAll1() throws Exception {
1722 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1722 >        ExecutorService e =
1723 >            new ThreadPoolExecutor(2, 2,
1724 >                                   LONG_DELAY_MS, MILLISECONDS,
1725 >                                   new ArrayBlockingQueue<Runnable>(10));
1726          try {
1727              e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1728              shouldThrow();
# Line 1283 | Line 1736 | public class ThreadPoolExecutorTest exte
1736       * timed invokeAll(,,null) throws NPE
1737       */
1738      public void testTimedInvokeAllNullTimeUnit() throws Exception {
1739 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1739 >        ExecutorService e =
1740 >            new ThreadPoolExecutor(2, 2,
1741 >                                   LONG_DELAY_MS, MILLISECONDS,
1742 >                                   new ArrayBlockingQueue<Runnable>(10));
1743          List<Callable<String>> l = new ArrayList<Callable<String>>();
1744          l.add(new StringTask());
1745          try {
# Line 1299 | Line 1755 | public class ThreadPoolExecutorTest exte
1755       * timed invokeAll(empty collection) returns empty collection
1756       */
1757      public void testTimedInvokeAll2() throws InterruptedException {
1758 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1758 >        ExecutorService e =
1759 >            new ThreadPoolExecutor(2, 2,
1760 >                                   LONG_DELAY_MS, MILLISECONDS,
1761 >                                   new ArrayBlockingQueue<Runnable>(10));
1762          try {
1763              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1764              assertTrue(r.isEmpty());
# Line 1312 | Line 1771 | public class ThreadPoolExecutorTest exte
1771       * timed invokeAll(c) throws NPE if c has null elements
1772       */
1773      public void testTimedInvokeAll3() throws Exception {
1774 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1774 >        ExecutorService e =
1775 >            new ThreadPoolExecutor(2, 2,
1776 >                                   LONG_DELAY_MS, MILLISECONDS,
1777 >                                   new ArrayBlockingQueue<Runnable>(10));
1778          List<Callable<String>> l = new ArrayList<Callable<String>>();
1779          l.add(new StringTask());
1780          l.add(null);
# Line 1329 | Line 1791 | public class ThreadPoolExecutorTest exte
1791       * get of element of invokeAll(c) throws exception on failed task
1792       */
1793      public void testTimedInvokeAll4() throws Exception {
1794 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1794 >        ExecutorService e =
1795 >            new ThreadPoolExecutor(2, 2,
1796 >                                   LONG_DELAY_MS, MILLISECONDS,
1797 >                                   new ArrayBlockingQueue<Runnable>(10));
1798          List<Callable<String>> l = new ArrayList<Callable<String>>();
1799          l.add(new NPETask());
1800          List<Future<String>> futures =
# Line 1349 | Line 1814 | public class ThreadPoolExecutorTest exte
1814       * timed invokeAll(c) returns results of all completed tasks
1815       */
1816      public void testTimedInvokeAll5() throws Exception {
1817 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1817 >        ExecutorService e =
1818 >            new ThreadPoolExecutor(2, 2,
1819 >                                   LONG_DELAY_MS, MILLISECONDS,
1820 >                                   new ArrayBlockingQueue<Runnable>(10));
1821          try {
1822              List<Callable<String>> l = new ArrayList<Callable<String>>();
1823              l.add(new StringTask());
# Line 1368 | Line 1836 | public class ThreadPoolExecutorTest exte
1836       * timed invokeAll(c) cancels tasks not completed by timeout
1837       */
1838      public void testTimedInvokeAll6() throws Exception {
1839 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1839 >        ExecutorService e =
1840 >            new ThreadPoolExecutor(2, 2,
1841 >                                   LONG_DELAY_MS, MILLISECONDS,
1842 >                                   new ArrayBlockingQueue<Runnable>(10));
1843          try {
1844              List<Callable<String>> l = new ArrayList<Callable<String>>();
1845              l.add(new StringTask());
# Line 1396 | Line 1867 | public class ThreadPoolExecutorTest exte
1867       * thread factory fails to create more
1868       */
1869      public void testFailingThreadFactory() throws InterruptedException {
1870 <        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1870 >        final ExecutorService e =
1871 >            new ThreadPoolExecutor(100, 100,
1872 >                                   LONG_DELAY_MS, MILLISECONDS,
1873 >                                   new LinkedBlockingQueue<Runnable>(),
1874 >                                   new FailingThreadFactory());
1875          try {
1876 <            List<Callable<String>> l = new ArrayList<Callable<String>>();
1877 <            for (int k = 0; k < 100; ++k) {
1878 <                e.execute(new NoOpRunnable());
1879 <            }
1880 <            Thread.sleep(LONG_DELAY_MS);
1876 >            final int TASKS = 100;
1877 >            final CountDownLatch done = new CountDownLatch(TASKS);
1878 >            for (int k = 0; k < TASKS; ++k)
1879 >                e.execute(new CheckedRunnable() {
1880 >                    public void realRun() {
1881 >                        done.countDown();
1882 >                    }});
1883 >            assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS));
1884          } finally {
1885              joinPool(e);
1886          }
# Line 1412 | Line 1890 | public class ThreadPoolExecutorTest exte
1890       * allowsCoreThreadTimeOut is by default false.
1891       */
1892      public void testAllowsCoreThreadTimeOut() {
1893 <        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1894 <        assertFalse(tpe.allowsCoreThreadTimeOut());
1895 <        joinPool(tpe);
1893 >        final ThreadPoolExecutor p =
1894 >            new ThreadPoolExecutor(2, 2,
1895 >                                   1000, MILLISECONDS,
1896 >                                   new ArrayBlockingQueue<Runnable>(10));
1897 >        assertFalse(p.allowsCoreThreadTimeOut());
1898 >        joinPool(p);
1899      }
1900  
1901      /**
1902       * allowCoreThreadTimeOut(true) causes idle threads to time out
1903       */
1904 <    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1905 <        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1906 <        tpe.allowCoreThreadTimeOut(true);
1907 <        tpe.execute(new NoOpRunnable());
1904 >    public void testAllowCoreThreadTimeOut_true() throws Exception {
1905 >        final ThreadPoolExecutor p =
1906 >            new ThreadPoolExecutor(2, 10,
1907 >                                   SHORT_DELAY_MS, MILLISECONDS,
1908 >                                   new ArrayBlockingQueue<Runnable>(10));
1909 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1910          try {
1911 <            Thread.sleep(MEDIUM_DELAY_MS);
1912 <            assertEquals(0, tpe.getPoolSize());
1911 >            p.allowCoreThreadTimeOut(true);
1912 >            p.execute(new CheckedRunnable() {
1913 >                public void realRun() throws InterruptedException {
1914 >                    threadStarted.countDown();
1915 >                    assertEquals(1, p.getPoolSize());
1916 >                }});
1917 >            assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS));
1918 >            for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) {
1919 >                if (p.getPoolSize() == 0)
1920 >                    break;
1921 >                Thread.sleep(10);
1922 >            }
1923 >            assertEquals(0, p.getPoolSize());
1924          } finally {
1925 <            joinPool(tpe);
1925 >            joinPool(p);
1926          }
1927      }
1928  
1929      /**
1930       * allowCoreThreadTimeOut(false) causes idle threads not to time out
1931       */
1932 <    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1933 <        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1934 <        tpe.allowCoreThreadTimeOut(false);
1935 <        tpe.execute(new NoOpRunnable());
1932 >    public void testAllowCoreThreadTimeOut_false() throws Exception {
1933 >        final ThreadPoolExecutor p =
1934 >            new ThreadPoolExecutor(2, 10,
1935 >                                   SHORT_DELAY_MS, MILLISECONDS,
1936 >                                   new ArrayBlockingQueue<Runnable>(10));
1937 >        final CountDownLatch threadStarted = new CountDownLatch(1);
1938          try {
1939 <            Thread.sleep(MEDIUM_DELAY_MS);
1940 <            assertTrue(tpe.getPoolSize() >= 1);
1939 >            p.allowCoreThreadTimeOut(false);
1940 >            p.execute(new CheckedRunnable() {
1941 >                public void realRun() throws InterruptedException {
1942 >                    threadStarted.countDown();
1943 >                    assertTrue(p.getPoolSize() >= 1);
1944 >                }});
1945 >            Thread.sleep(SMALL_DELAY_MS);
1946 >            assertTrue(p.getPoolSize() >= 1);
1947          } finally {
1948 <            joinPool(tpe);
1948 >            joinPool(p);
1949          }
1950      }
1951  
# Line 1453 | Line 1955 | public class ThreadPoolExecutorTest exte
1955       */
1956      public void testRejectedRecycledTask() throws InterruptedException {
1957          final int nTasks = 1000;
1958 <        final AtomicInteger nRun = new AtomicInteger(0);
1958 >        final CountDownLatch done = new CountDownLatch(nTasks);
1959          final Runnable recycledTask = new Runnable() {
1960 <                public void run() {
1961 <                    nRun.getAndIncrement();
1962 <                } };
1960 >            public void run() {
1961 >                done.countDown();
1962 >            }};
1963          final ThreadPoolExecutor p =
1964              new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
1965                                     new ArrayBlockingQueue(30));
# Line 1468 | Line 1970 | public class ThreadPoolExecutorTest exte
1970                          p.execute(recycledTask);
1971                          break;
1972                      }
1973 <                    catch (RejectedExecutionException ignore) {
1472 <                    }
1973 >                    catch (RejectedExecutionException ignore) {}
1974                  }
1975              }
1976 <            Thread.sleep(5000); // enough time to run all tasks
1977 <            assertEquals(nRun.get(), nTasks);
1976 >            // enough time to run all tasks
1977 >            assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS));
1978          } finally {
1979              p.shutdown();
1980          }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines