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.15 by dl, Sat Dec 27 20:42:48 2003 UTC vs.
Revision 1.29 by jsr166, Fri Nov 20 16:02:10 2009 UTC

# Line 2 | Line 2
2   * Written by Doug Lea with assistance from members of JCP JSR-166
3   * Expert Group and released to the public domain, as explained at
4   * http://creativecommons.org/licenses/publicdomain
5 < * Other contributors include Andrew Wright, Jeffrey Hayes,
6 < * Pat Fisher, Mike Judd.
5 > * Other contributors include Andrew Wright, Jeffrey Hayes,
6 > * Pat Fisher, Mike Judd.
7   */
8  
9   import java.util.concurrent.*;
10 + import static java.util.concurrent.TimeUnit.MILLISECONDS;
11 + import java.util.concurrent.atomic.*;
12   import junit.framework.*;
13   import java.util.*;
14  
15   public class ThreadPoolExecutorTest extends JSR166TestCase {
16      public static void main(String[] args) {
17 <        junit.textui.TestRunner.run (suite());  
17 >        junit.textui.TestRunner.run (suite());
18      }
19      public static Test suite() {
20          return new TestSuite(ThreadPoolExecutorTest.class);
21      }
22 <    
22 >
23      static class ExtendedTPE extends ThreadPoolExecutor {
24          volatile boolean beforeCalled = false;
25          volatile boolean afterCalled = false;
26          volatile boolean terminatedCalled = false;
27          public ExtendedTPE() {
28 <            super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>());
28 >            super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>());
29          }
30          protected void beforeExecute(Thread t, Runnable r) {
31              beforeCalled = true;
# Line 36 | Line 38 | public class ThreadPoolExecutorTest exte
38          }
39      }
40  
41 +    static class FailingThreadFactory implements ThreadFactory {
42 +        int calls = 0;
43 +        public Thread newThread(Runnable r) {
44 +            if (++calls > 1) return null;
45 +            return new Thread(r);
46 +        }
47 +    }
48 +
49 +
50      /**
51       *  execute successfully executes a runnable
52       */
53 <    public void testExecute() {
54 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
53 >    public void testExecute() throws InterruptedException {
54 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
55          try {
56 <            p1.execute(new Runnable() {
46 <                    public void run() {
47 <                        try {
48 <                            Thread.sleep(SHORT_DELAY_MS);
49 <                        } catch(InterruptedException e){
50 <                            threadUnexpectedException();
51 <                        }
52 <                    }
53 <                });
56 >            p1.execute(new ShortRunnable());
57              Thread.sleep(SMALL_DELAY_MS);
58 <        } catch(InterruptedException e){
59 <            unexpectedException();
60 <        }
58 <        joinPool(p1);
58 >        } finally {
59 >            joinPool(p1);
60 >        }
61      }
62  
63      /**
64       *  getActiveCount increases but doesn't overestimate, when a
65       *  thread becomes active
66       */
67 <    public void testGetActiveCount() {
68 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
67 >    public void testGetActiveCount() throws InterruptedException {
68 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
69          assertEquals(0, p2.getActiveCount());
70          p2.execute(new MediumRunnable());
71 <        try {
70 <            Thread.sleep(SHORT_DELAY_MS);
71 <        } catch(Exception e){
72 <            unexpectedException();
73 <        }
71 >        Thread.sleep(SHORT_DELAY_MS);
72          assertEquals(1, p2.getActiveCount());
73          joinPool(p2);
74      }
# Line 79 | Line 77 | public class ThreadPoolExecutorTest exte
77       *  prestartCoreThread starts a thread if under corePoolSize, else doesn't
78       */
79      public void testPrestartCoreThread() {
80 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
80 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
81          assertEquals(0, p2.getPoolSize());
82          assertTrue(p2.prestartCoreThread());
83          assertEquals(1, p2.getPoolSize());
# Line 94 | Line 92 | public class ThreadPoolExecutorTest exte
92       *  prestartAllCoreThreads starts all corePoolSize threads
93       */
94      public void testPrestartAllCoreThreads() {
95 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
95 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
96          assertEquals(0, p2.getPoolSize());
97          p2.prestartAllCoreThreads();
98          assertEquals(2, p2.getPoolSize());
# Line 102 | Line 100 | public class ThreadPoolExecutorTest exte
100          assertEquals(2, p2.getPoolSize());
101          joinPool(p2);
102      }
103 <    
103 >
104      /**
105       *   getCompletedTaskCount increases, but doesn't overestimate,
106       *   when tasks complete
107       */
108 <    public void testGetCompletedTaskCount() {
109 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
108 >    public void testGetCompletedTaskCount() throws InterruptedException {
109 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
110          assertEquals(0, p2.getCompletedTaskCount());
111          p2.execute(new ShortRunnable());
112 <        try {
115 <            Thread.sleep(SMALL_DELAY_MS);
116 <        } catch(Exception e){
117 <            unexpectedException();
118 <        }
112 >        Thread.sleep(SMALL_DELAY_MS);
113          assertEquals(1, p2.getCompletedTaskCount());
114 <        p2.shutdown();
114 >        try { p2.shutdown(); } catch (SecurityException ok) { return; }
115          joinPool(p2);
116      }
117 <    
117 >
118      /**
119       *   getCorePoolSize returns size given in constructor if not otherwise set
120       */
121      public void testGetCorePoolSize() {
122 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
122 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
123          assertEquals(1, p1.getCorePoolSize());
124          joinPool(p1);
125      }
126 <    
126 >
127      /**
128       *   getKeepAliveTime returns value given in constructor if not otherwise set
129       */
130      public void testGetKeepAliveTime() {
131 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
131 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
132          assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS));
133          joinPool(p2);
134      }
135  
136  
137 <    /**
137 >    /**
138       * getThreadFactory returns factory in constructor if not set
139       */
140      public void testGetThreadFactory() {
141          ThreadFactory tf = new SimpleThreadFactory();
142 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
142 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler());
143          assertSame(tf, p.getThreadFactory());
150        p.shutdown();
144          joinPool(p);
145      }
146  
147 <    /**
147 >    /**
148       * setThreadFactory sets the thread factory returned by getThreadFactory
149       */
150      public void testSetThreadFactory() {
151 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
151 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
152          ThreadFactory tf = new SimpleThreadFactory();
153          p.setThreadFactory(tf);
154          assertSame(tf, p.getThreadFactory());
162        p.shutdown();
155          joinPool(p);
156      }
157  
158  
159 <    /**
159 >    /**
160       * setThreadFactory(null) throws NPE
161       */
162      public void testSetThreadFactoryNull() {
163 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
163 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
164          try {
165              p.setThreadFactory(null);
166              shouldThrow();
# Line 178 | Line 170 | public class ThreadPoolExecutorTest exte
170          }
171      }
172  
173 <    /**
173 >    /**
174       * getRejectedExecutionHandler returns handler in constructor if not set
175       */
176      public void testGetRejectedExecutionHandler() {
177          RejectedExecutionHandler h = new NoOpREHandler();
178 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
178 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h);
179          assertSame(h, p.getRejectedExecutionHandler());
188        p.shutdown();
180          joinPool(p);
181      }
182  
183 <    /**
183 >    /**
184       * setRejectedExecutionHandler sets the handler returned by
185       * getRejectedExecutionHandler
186       */
187      public void testSetRejectedExecutionHandler() {
188 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
188 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
189          RejectedExecutionHandler h = new NoOpREHandler();
190          p.setRejectedExecutionHandler(h);
191          assertSame(h, p.getRejectedExecutionHandler());
201        p.shutdown();
192          joinPool(p);
193      }
194  
195  
196 <    /**
196 >    /**
197       * setRejectedExecutionHandler(null) throws NPE
198       */
199      public void testSetRejectedExecutionHandlerNull() {
200 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
200 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
201          try {
202              p.setRejectedExecutionHandler(null);
203              shouldThrow();
# Line 217 | Line 207 | public class ThreadPoolExecutorTest exte
207          }
208      }
209  
210 <    
210 >
211      /**
212       *   getLargestPoolSize increases, but doesn't overestimate, when
213       *   multiple threads active
214       */
215 <    public void testGetLargestPoolSize() {
216 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
217 <        try {
218 <            assertEquals(0, p2.getLargestPoolSize());
219 <            p2.execute(new MediumRunnable());
220 <            p2.execute(new MediumRunnable());
221 <            Thread.sleep(SHORT_DELAY_MS);
232 <            assertEquals(2, p2.getLargestPoolSize());
233 <        } catch(Exception e){
234 <            unexpectedException();
235 <        }
215 >    public void testGetLargestPoolSize() throws InterruptedException {
216 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
217 >        assertEquals(0, p2.getLargestPoolSize());
218 >        p2.execute(new MediumRunnable());
219 >        p2.execute(new MediumRunnable());
220 >        Thread.sleep(SHORT_DELAY_MS);
221 >        assertEquals(2, p2.getLargestPoolSize());
222          joinPool(p2);
223      }
224 <    
224 >
225      /**
226       *   getMaximumPoolSize returns value given in constructor if not
227       *   otherwise set
228       */
229      public void testGetMaximumPoolSize() {
230 <        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
230 >        ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
231          assertEquals(2, p2.getMaximumPoolSize());
232          joinPool(p2);
233      }
234 <    
234 >
235      /**
236       *   getPoolSize increases, but doesn't overestimate, when threads
237       *   become active
238       */
239      public void testGetPoolSize() {
240 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
240 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
241          assertEquals(0, p1.getPoolSize());
242          p1.execute(new MediumRunnable());
243          assertEquals(1, p1.getPoolSize());
244          joinPool(p1);
245      }
246 <    
246 >
247      /**
248       *  getTaskCount increases, but doesn't overestimate, when tasks submitted
249       */
250 <    public void testGetTaskCount() {
251 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
252 <        try {
253 <            assertEquals(0, p1.getTaskCount());
254 <            p1.execute(new MediumRunnable());
255 <            Thread.sleep(SHORT_DELAY_MS);
270 <            assertEquals(1, p1.getTaskCount());
271 <        } catch(Exception e){
272 <            unexpectedException();
273 <        }
250 >    public void testGetTaskCount() throws InterruptedException {
251 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
252 >        assertEquals(0, p1.getTaskCount());
253 >        p1.execute(new MediumRunnable());
254 >        Thread.sleep(SHORT_DELAY_MS);
255 >        assertEquals(1, p1.getTaskCount());
256          joinPool(p1);
257      }
258 <    
258 >
259      /**
260       *   isShutDown is false before shutdown, true after
261       */
262      public void testIsShutdown() {
263 <        
264 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
263 >
264 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
265          assertFalse(p1.isShutdown());
266 <        p1.shutdown();
266 >        try { p1.shutdown(); } catch (SecurityException ok) { return; }
267          assertTrue(p1.isShutdown());
268          joinPool(p1);
269      }
270  
271 <        
271 >
272      /**
273       *  isTerminated is false before termination, true after
274       */
275 <    public void testIsTerminated() {
276 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
275 >    public void testIsTerminated() throws InterruptedException {
276 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
277          assertFalse(p1.isTerminated());
278          try {
279              p1.execute(new MediumRunnable());
280          } finally {
281 <            p1.shutdown();
281 >            try { p1.shutdown(); } catch (SecurityException ok) { return; }
282          }
283 <        try {
284 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
303 <            assertTrue(p1.isTerminated());
304 <        } catch(Exception e){
305 <            unexpectedException();
306 <        }      
283 >        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
284 >        assertTrue(p1.isTerminated());
285      }
286  
287      /**
288       *  isTerminating is not true when running or when terminated
289       */
290 <    public void testIsTerminating() {
291 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
290 >    public void testIsTerminating() throws InterruptedException {
291 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
292          assertFalse(p1.isTerminating());
293          try {
294              p1.execute(new SmallRunnable());
295              assertFalse(p1.isTerminating());
296          } finally {
297 <            p1.shutdown();
297 >            try { p1.shutdown(); } catch (SecurityException ok) { return; }
298          }
299 <        try {
300 <            assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS));
301 <            assertTrue(p1.isTerminated());
324 <            assertFalse(p1.isTerminating());
325 <        } catch(Exception e){
326 <            unexpectedException();
327 <        }      
299 >        assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS));
300 >        assertTrue(p1.isTerminated());
301 >        assertFalse(p1.isTerminating());
302      }
303  
304      /**
305       * getQueue returns the work queue, which contains queued tasks
306       */
307 <    public void testGetQueue() {
307 >    public void testGetQueue() throws InterruptedException {
308          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
309 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
309 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
310          FutureTask[] tasks = new FutureTask[5];
311 <        for(int i = 0; i < 5; i++){
311 >        for (int i = 0; i < 5; i++) {
312              tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
313              p1.execute(tasks[i]);
314          }
# Line 344 | Line 318 | public class ThreadPoolExecutorTest exte
318              assertSame(q, wq);
319              assertFalse(wq.contains(tasks[0]));
320              assertTrue(wq.contains(tasks[4]));
321 +            for (int i = 1; i < 5; ++i)
322 +                tasks[i].cancel(true);
323              p1.shutdownNow();
348        } catch(Exception e) {
349            unexpectedException();
324          } finally {
325              joinPool(p1);
326          }
# Line 355 | Line 329 | public class ThreadPoolExecutorTest exte
329      /**
330       * remove(task) removes queued task, and fails to remove active task
331       */
332 <    public void testRemove() {
332 >    public void testRemove() throws InterruptedException {
333          BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10);
334 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q);
334 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q);
335          FutureTask[] tasks = new FutureTask[5];
336 <        for(int i = 0; i < 5; i++){
336 >        for (int i = 0; i < 5; i++) {
337              tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
338              p1.execute(tasks[i]);
339          }
# Line 374 | Line 348 | public class ThreadPoolExecutorTest exte
348              assertTrue(q.contains(tasks[3]));
349              assertTrue(p1.remove(tasks[3]));
350              assertFalse(q.contains(tasks[3]));
377            p1.shutdownNow();
378        } catch(Exception e) {
379            unexpectedException();
351          } finally {
352              joinPool(p1);
353          }
# Line 386 | Line 357 | public class ThreadPoolExecutorTest exte
357       *   purge removes cancelled tasks from the queue
358       */
359      public void testPurge() {
360 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
360 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
361          FutureTask[] tasks = new FutureTask[5];
362 <        for(int i = 0; i < 5; i++){
362 >        for (int i = 0; i < 5; i++) {
363              tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE);
364              p1.execute(tasks[i]);
365          }
# Line 397 | Line 368 | public class ThreadPoolExecutorTest exte
368          p1.purge();
369          long count = p1.getTaskCount();
370          assertTrue(count >= 2 && count < 5);
400        p1.shutdownNow();
371          joinPool(p1);
372      }
373  
# Line 405 | Line 375 | public class ThreadPoolExecutorTest exte
375       *  shutDownNow returns a list containing tasks that were not run
376       */
377      public void testShutDownNow() {
378 <        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
378 >        ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
379          List l;
380          try {
381 <            for(int i = 0; i < 5; i++)
381 >            for (int i = 0; i < 5; i++)
382                  p1.execute(new MediumPossiblyInterruptedRunnable());
383          }
384          finally {
385 <            l = p1.shutdownNow();
385 >            try {
386 >                l = p1.shutdownNow();
387 >            } catch (SecurityException ok) { return; }
388 >
389          }
390          assertTrue(p1.isShutdown());
391          assertTrue(l.size() <= 4);
392      }
393  
394      // Exception Tests
422    
395  
396 <    /**
397 <     * Constructor throws if corePoolSize argument is less than zero
396 >
397 >    /**
398 >     * Constructor throws if corePoolSize argument is less than zero
399       */
400      public void testConstructor1() {
401          try {
402 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
402 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
403              shouldThrow();
404 <        }
432 <        catch (IllegalArgumentException success){}
404 >        } catch (IllegalArgumentException success) {}
405      }
406 <    
407 <    /**
408 <     * Constructor throws if maximumPoolSize is less than zero
406 >
407 >    /**
408 >     * Constructor throws if maximumPoolSize is less than zero
409       */
410      public void testConstructor2() {
411          try {
412 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
412 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
413              shouldThrow();
414 <        }
443 <        catch (IllegalArgumentException success){}
414 >        } catch (IllegalArgumentException success) {}
415      }
416 <    
417 <    /**
418 <     * Constructor throws if maximumPoolSize is equal to zero
416 >
417 >    /**
418 >     * Constructor throws if maximumPoolSize is equal to zero
419       */
420      public void testConstructor3() {
421          try {
422 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
422 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
423              shouldThrow();
424 <        }
454 <        catch (IllegalArgumentException success){}
424 >        } catch (IllegalArgumentException success) {}
425      }
426  
427 <    /**
428 <     * Constructor throws if keepAliveTime is less than zero
427 >    /**
428 >     * Constructor throws if keepAliveTime is less than zero
429       */
430      public void testConstructor4() {
431          try {
432 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
432 >            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
433              shouldThrow();
434 <        }
465 <        catch (IllegalArgumentException success){}
434 >        } catch (IllegalArgumentException success) {}
435      }
436  
437 <    /**
438 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
437 >    /**
438 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
439       */
440      public void testConstructor5() {
441          try {
442 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
442 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
443              shouldThrow();
444 <        }
476 <        catch (IllegalArgumentException success){}
444 >        } catch (IllegalArgumentException success) {}
445      }
446 <        
447 <    /**
448 <     * Constructor throws if workQueue is set to null
446 >
447 >    /**
448 >     * Constructor throws if workQueue is set to null
449       */
450      public void testConstructorNullPointerException() {
451          try {
452 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
452 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null);
453              shouldThrow();
454 <        }
487 <        catch (NullPointerException success){}  
454 >        } catch (NullPointerException success) {}
455      }
489    
456  
457 <    
458 <    /**
459 <     * Constructor throws if corePoolSize argument is less than zero
457 >
458 >
459 >    /**
460 >     * Constructor throws if corePoolSize argument is less than zero
461       */
462      public void testConstructor6() {
463          try {
464 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
464 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
465              shouldThrow();
466 <        } catch (IllegalArgumentException success){}
466 >        } catch (IllegalArgumentException success) {}
467      }
468 <    
469 <    /**
470 <     * Constructor throws if maximumPoolSize is less than zero
468 >
469 >    /**
470 >     * Constructor throws if maximumPoolSize is less than zero
471       */
472      public void testConstructor7() {
473          try {
474 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
474 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
475              shouldThrow();
476 <        }
510 <        catch (IllegalArgumentException success){}
476 >        } catch (IllegalArgumentException success) {}
477      }
478  
479 <    /**
480 <     * Constructor throws if maximumPoolSize is equal to zero
479 >    /**
480 >     * Constructor throws if maximumPoolSize is equal to zero
481       */
482      public void testConstructor8() {
483          try {
484 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
484 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
485              shouldThrow();
486 <        }
521 <        catch (IllegalArgumentException success){}
486 >        } catch (IllegalArgumentException success) {}
487      }
488  
489 <    /**
490 <     * Constructor throws if keepAliveTime is less than zero
489 >    /**
490 >     * Constructor throws if keepAliveTime is less than zero
491       */
492      public void testConstructor9() {
493          try {
494 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
494 >            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
495              shouldThrow();
496 <        }
532 <        catch (IllegalArgumentException success){}
496 >        } catch (IllegalArgumentException success) {}
497      }
498  
499 <    /**
500 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
499 >    /**
500 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
501       */
502      public void testConstructor10() {
503          try {
504 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
504 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
505              shouldThrow();
506 <        }
543 <        catch (IllegalArgumentException success){}
506 >        } catch (IllegalArgumentException success) {}
507      }
508  
509 <    /**
510 <     * Constructor throws if workQueue is set to null
509 >    /**
510 >     * Constructor throws if workQueue is set to null
511       */
512      public void testConstructorNullPointerException2() {
513          try {
514 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
514 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
515              shouldThrow();
516 <        }
554 <        catch (NullPointerException success){}  
516 >        } catch (NullPointerException success) {}
517      }
518  
519 <    /**
520 <     * Constructor throws if threadFactory is set to null
519 >    /**
520 >     * Constructor throws if threadFactory is set to null
521       */
522      public void testConstructorNullPointerException3() {
523          try {
524              ThreadFactory f = null;
525 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
525 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
526              shouldThrow();
527 <        }
566 <        catch (NullPointerException success){}  
527 >        } catch (NullPointerException success) {}
528      }
529 <
530 <    
531 <    /**
532 <     * Constructor throws if corePoolSize argument is less than zero
529 >
530 >
531 >    /**
532 >     * Constructor throws if corePoolSize argument is less than zero
533       */
534      public void testConstructor11() {
535          try {
536 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
536 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
537              shouldThrow();
538 <        }
578 <        catch (IllegalArgumentException success){}
538 >        } catch (IllegalArgumentException success) {}
539      }
540  
541 <    /**
542 <     * Constructor throws if maximumPoolSize is less than zero
541 >    /**
542 >     * Constructor throws if maximumPoolSize is less than zero
543       */
544      public void testConstructor12() {
545          try {
546 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
546 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
547              shouldThrow();
548 <        }
589 <        catch (IllegalArgumentException success){}
548 >        } catch (IllegalArgumentException success) {}
549      }
550  
551 <    /**
552 <     * Constructor throws if maximumPoolSize is equal to zero
551 >    /**
552 >     * Constructor throws if maximumPoolSize is equal to zero
553       */
554      public void testConstructor13() {
555          try {
556 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
556 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
557              shouldThrow();
558 <        }
600 <        catch (IllegalArgumentException success){}
558 >        } catch (IllegalArgumentException success) {}
559      }
560  
561 <    /**
562 <     * Constructor throws if keepAliveTime is less than zero
561 >    /**
562 >     * Constructor throws if keepAliveTime is less than zero
563       */
564      public void testConstructor14() {
565          try {
566 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
566 >            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
567              shouldThrow();
568 <        }
611 <        catch (IllegalArgumentException success){}
568 >        } catch (IllegalArgumentException success) {}
569      }
570  
571 <    /**
572 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
571 >    /**
572 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
573       */
574      public void testConstructor15() {
575          try {
576 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
576 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
577              shouldThrow();
578 <        }
622 <        catch (IllegalArgumentException success){}
578 >        } catch (IllegalArgumentException success) {}
579      }
580  
581 <    /**
582 <     * Constructor throws if workQueue is set to null
581 >    /**
582 >     * Constructor throws if workQueue is set to null
583       */
584      public void testConstructorNullPointerException4() {
585          try {
586 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
586 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
587              shouldThrow();
588 <        }
633 <        catch (NullPointerException success){}  
588 >        } catch (NullPointerException success) {}
589      }
590  
591 <    /**
592 <     * Constructor throws if handler is set to null
591 >    /**
592 >     * Constructor throws if handler is set to null
593       */
594      public void testConstructorNullPointerException5() {
595          try {
596              RejectedExecutionHandler r = null;
597 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
597 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
598              shouldThrow();
599 <        }
645 <        catch (NullPointerException success){}  
599 >        } catch (NullPointerException success) {}
600      }
601  
602 <    
603 <    /**
604 <     * Constructor throws if corePoolSize argument is less than zero
602 >
603 >    /**
604 >     * Constructor throws if corePoolSize argument is less than zero
605       */
606      public void testConstructor16() {
607          try {
608 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
608 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
609              shouldThrow();
610 <        }
657 <        catch (IllegalArgumentException success){}
610 >        } catch (IllegalArgumentException success) {}
611      }
612  
613 <    /**
614 <     * Constructor throws if maximumPoolSize is less than zero
613 >    /**
614 >     * Constructor throws if maximumPoolSize is less than zero
615       */
616      public void testConstructor17() {
617          try {
618 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
618 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
619              shouldThrow();
620 <        }
668 <        catch (IllegalArgumentException success){}
620 >        } catch (IllegalArgumentException success) {}
621      }
622  
623 <    /**
624 <     * Constructor throws if maximumPoolSize is equal to zero
623 >    /**
624 >     * Constructor throws if maximumPoolSize is equal to zero
625       */
626      public void testConstructor18() {
627          try {
628 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
628 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
629              shouldThrow();
630 <        }
679 <        catch (IllegalArgumentException success){}
630 >        } catch (IllegalArgumentException success) {}
631      }
632  
633 <    /**
634 <     * Constructor throws if keepAliveTime is less than zero
633 >    /**
634 >     * Constructor throws if keepAliveTime is less than zero
635       */
636      public void testConstructor19() {
637          try {
638 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
638 >            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
639              shouldThrow();
640 <        }
690 <        catch (IllegalArgumentException success){}
640 >        } catch (IllegalArgumentException success) {}
641      }
642  
643 <    /**
644 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
643 >    /**
644 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
645       */
646      public void testConstructor20() {
647          try {
648 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
648 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
649              shouldThrow();
650 <        }
701 <        catch (IllegalArgumentException success){}
650 >        } catch (IllegalArgumentException success) {}
651      }
652  
653 <    /**
654 <     * Constructor throws if workQueue is set to null
653 >    /**
654 >     * Constructor throws if workQueue is set to null
655       */
656      public void testConstructorNullPointerException6() {
657          try {
658 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
658 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
659              shouldThrow();
660 <        }
712 <        catch (NullPointerException success){}  
660 >        } catch (NullPointerException success) {}
661      }
662  
663 <    /**
664 <     * Constructor throws if handler is set to null
663 >    /**
664 >     * Constructor throws if handler is set to null
665       */
666      public void testConstructorNullPointerException7() {
667          try {
668              RejectedExecutionHandler r = null;
669 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
669 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
670              shouldThrow();
671 <        }
724 <        catch (NullPointerException success){}  
671 >        } catch (NullPointerException success) {}
672      }
673  
674 <    /**
675 <     * Constructor throws if ThreadFactory is set top null
674 >    /**
675 >     * Constructor throws if ThreadFactory is set top null
676       */
677      public void testConstructorNullPointerException8() {
678          try {
679              ThreadFactory f = null;
680 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
680 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
681              shouldThrow();
682 <        }
736 <        catch (NullPointerException successdn8){}  
682 >        } catch (NullPointerException success) {}
683      }
684 <    
684 >
685  
686      /**
687       *  execute throws RejectedExecutionException
688       *  if saturated.
689       */
690      public void testSaturatedExecute() {
691 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
691 >        ThreadPoolExecutor p =
692 >            new ThreadPoolExecutor(1, 1,
693 >                                   LONG_DELAY_MS, MILLISECONDS,
694 >                                   new ArrayBlockingQueue<Runnable>(1));
695          try {
696 <            
748 <            for(int i = 0; i < 5; ++i){
696 >            for (int i = 0; i < 2; ++i)
697                  p.execute(new MediumRunnable());
698 +            for (int i = 0; i < 2; ++i) {
699 +                try {
700 +                    p.execute(new MediumRunnable());
701 +                    shouldThrow();
702 +                } catch (RejectedExecutionException success) {}
703              }
704 <            shouldThrow();
705 <        } catch(RejectedExecutionException success){}
706 <        joinPool(p);
704 >        } finally {
705 >            joinPool(p);
706 >        }
707      }
708  
709      /**
# Line 758 | Line 711 | public class ThreadPoolExecutorTest exte
711       */
712      public void testSaturatedExecute2() {
713          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
714 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
714 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
715          try {
716 <            
716 >
717              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
718 <            for(int i = 0; i < 5; ++i){
718 >            for (int i = 0; i < 5; ++i) {
719                  tasks[i] = new TrackedNoOpRunnable();
720              }
721              TrackedLongRunnable mr = new TrackedLongRunnable();
722              p.execute(mr);
723 <            for(int i = 0; i < 5; ++i){
723 >            for (int i = 0; i < 5; ++i) {
724                  p.execute(tasks[i]);
725              }
726 <            for(int i = 1; i < 5; ++i) {
726 >            for (int i = 1; i < 5; ++i) {
727                  assertTrue(tasks[i].done);
728              }
729 <            p.shutdownNow();
777 <        } catch(RejectedExecutionException ex){
778 <            unexpectedException();
729 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
730          } finally {
731              joinPool(p);
732          }
# Line 786 | Line 737 | public class ThreadPoolExecutorTest exte
737       */
738      public void testSaturatedExecute3() {
739          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
740 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
740 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
741          try {
742 <            
742 >
743              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
744 <            for(int i = 0; i < 5; ++i){
744 >            for (int i = 0; i < 5; ++i) {
745                  tasks[i] = new TrackedNoOpRunnable();
746              }
747              p.execute(new TrackedLongRunnable());
748 <            for(int i = 0; i < 5; ++i){
748 >            for (int i = 0; i < 5; ++i) {
749                  p.execute(tasks[i]);
750              }
751 <            for(int i = 0; i < 5; ++i){
751 >            for (int i = 0; i < 5; ++i) {
752                  assertFalse(tasks[i].done);
753              }
754 <            p.shutdownNow();
804 <        } catch(RejectedExecutionException ex){
805 <            unexpectedException();
754 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
755          } finally {
756              joinPool(p);
757          }
# Line 813 | Line 762 | public class ThreadPoolExecutorTest exte
762       */
763      public void testSaturatedExecute4() {
764          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
765 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
765 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
766          try {
767              p.execute(new TrackedLongRunnable());
768              TrackedLongRunnable r2 = new TrackedLongRunnable();
# Line 823 | Line 772 | public class ThreadPoolExecutorTest exte
772              p.execute(r3);
773              assertFalse(p.getQueue().contains(r2));
774              assertTrue(p.getQueue().contains(r3));
775 <            p.shutdownNow();
827 <        } catch(RejectedExecutionException ex){
828 <            unexpectedException();
775 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
776          } finally {
777              joinPool(p);
778          }
# Line 835 | Line 782 | public class ThreadPoolExecutorTest exte
782       *  execute throws RejectedExecutionException if shutdown
783       */
784      public void testRejectedExecutionExceptionOnShutdown() {
785 <        ThreadPoolExecutor tpe =
786 <            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
787 <        tpe.shutdown();
785 >        ThreadPoolExecutor tpe =
786 >            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
787 >        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
788          try {
789              tpe.execute(new NoOpRunnable());
790              shouldThrow();
791 <        } catch(RejectedExecutionException success){}
792 <        
791 >        } catch (RejectedExecutionException success) {}
792 >
793          joinPool(tpe);
794      }
795  
# Line 851 | Line 798 | public class ThreadPoolExecutorTest exte
798       */
799      public void testCallerRunsOnShutdown() {
800          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
801 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
801 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
802  
803 <        p.shutdown();
803 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
804          try {
805              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
806              p.execute(r);
807              assertFalse(r.done);
861        } catch(RejectedExecutionException success){
862            unexpectedException();
808          } finally {
809              joinPool(p);
810          }
# Line 870 | Line 815 | public class ThreadPoolExecutorTest exte
815       */
816      public void testDiscardOnShutdown() {
817          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
818 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
818 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
819  
820 <        p.shutdown();
820 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
821          try {
822              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
823              p.execute(r);
824              assertFalse(r.done);
880        } catch(RejectedExecutionException success){
881            unexpectedException();
825          } finally {
826              joinPool(p);
827          }
# Line 890 | Line 833 | public class ThreadPoolExecutorTest exte
833       */
834      public void testDiscardOldestOnShutdown() {
835          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
836 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
836 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
837  
838 <        p.shutdown();
838 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
839          try {
840              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
841              p.execute(r);
842              assertFalse(r.done);
900        } catch(RejectedExecutionException success){
901            unexpectedException();
843          } finally {
844              joinPool(p);
845          }
# Line 909 | Line 850 | public class ThreadPoolExecutorTest exte
850       *  execute (null) throws NPE
851       */
852      public void testExecuteNull() {
853 <        ThreadPoolExecutor tpe = null;
853 >        ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
854          try {
914            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
855              tpe.execute(null);
856              shouldThrow();
857 <        } catch(NullPointerException success){}
858 <        
857 >        } catch (NullPointerException success) {}
858 >
859          joinPool(tpe);
860      }
861 <    
861 >
862      /**
863       *  setCorePoolSize of negative value throws IllegalArgumentException
864       */
865      public void testCorePoolSizeIllegalArgumentException() {
866 <        ThreadPoolExecutor tpe = null;
867 <        try {
868 <            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
869 <        } catch(Exception e){}
866 >        ThreadPoolExecutor tpe =
867 >            new ThreadPoolExecutor(1, 2,
868 >                                   LONG_DELAY_MS, MILLISECONDS,
869 >                                   new ArrayBlockingQueue<Runnable>(10));
870          try {
871              tpe.setCorePoolSize(-1);
872              shouldThrow();
873 <        } catch(IllegalArgumentException success){
873 >        } catch (IllegalArgumentException success) {
874          } finally {
875 <            tpe.shutdown();
875 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
876          }
877          joinPool(tpe);
878 <    }  
878 >    }
879  
880      /**
881       *  setMaximumPoolSize(int) throws IllegalArgumentException if
882       *  given a value less the core pool size
883 <     */  
883 >     */
884      public void testMaximumPoolSizeIllegalArgumentException() {
885 <        ThreadPoolExecutor tpe = null;
886 <        try {
887 <            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
888 <        } catch(Exception e){}
885 >        ThreadPoolExecutor tpe =
886 >            new ThreadPoolExecutor(2, 3,
887 >                                   LONG_DELAY_MS, MILLISECONDS,
888 >                                   new ArrayBlockingQueue<Runnable>(10));
889          try {
890              tpe.setMaximumPoolSize(1);
891              shouldThrow();
892 <        } catch(IllegalArgumentException success){
892 >        } catch (IllegalArgumentException success) {
893          } finally {
894 <            tpe.shutdown();
894 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
895          }
896          joinPool(tpe);
897      }
898 <    
898 >
899      /**
900       *  setMaximumPoolSize throws IllegalArgumentException
901       *  if given a negative value
902       */
903      public void testMaximumPoolSizeIllegalArgumentException2() {
904 <        ThreadPoolExecutor tpe = null;
905 <        try {
906 <            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
907 <        } catch(Exception e){}
904 >        ThreadPoolExecutor tpe =
905 >            new ThreadPoolExecutor(2, 3,
906 >                                   LONG_DELAY_MS, MILLISECONDS,
907 >                                   new ArrayBlockingQueue<Runnable>(10));
908          try {
909              tpe.setMaximumPoolSize(-1);
910              shouldThrow();
911 <        } catch(IllegalArgumentException success){
911 >        } catch (IllegalArgumentException success) {
912          } finally {
913 <            tpe.shutdown();
913 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
914          }
915          joinPool(tpe);
916      }
917 <    
917 >
918  
919      /**
920       *  setKeepAliveTime  throws IllegalArgumentException
921       *  when given a negative value
922       */
923      public void testKeepAliveTimeIllegalArgumentException() {
924 <        ThreadPoolExecutor tpe = null;
925 <        try {
926 <            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
927 <        } catch(Exception e){}
988 <        
924 >        ThreadPoolExecutor tpe =
925 >            new ThreadPoolExecutor(2, 3,
926 >                                   LONG_DELAY_MS, MILLISECONDS,
927 >                                   new ArrayBlockingQueue<Runnable>(10));
928          try {
929 <            tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
929 >            tpe.setKeepAliveTime(-1,MILLISECONDS);
930              shouldThrow();
931 <        } catch(IllegalArgumentException success){
931 >        } catch (IllegalArgumentException success) {
932          } finally {
933 <            tpe.shutdown();
933 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
934          }
935          joinPool(tpe);
936      }
# Line 1001 | Line 940 | public class ThreadPoolExecutorTest exte
940       */
941      public void testTerminated() {
942          ExtendedTPE tpe = new ExtendedTPE();
943 <        tpe.shutdown();
943 >        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
944          assertTrue(tpe.terminatedCalled);
945          joinPool(tpe);
946      }
# Line 1009 | Line 948 | public class ThreadPoolExecutorTest exte
948      /**
949       * beforeExecute and afterExecute are called when executing task
950       */
951 <    public void testBeforeAfter() {
951 >    public void testBeforeAfter() throws InterruptedException {
952          ExtendedTPE tpe = new ExtendedTPE();
953          try {
954              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
# Line 1018 | Line 957 | public class ThreadPoolExecutorTest exte
957              assertTrue(r.done);
958              assertTrue(tpe.beforeCalled);
959              assertTrue(tpe.afterCalled);
960 <            tpe.shutdown();
1022 <        }
1023 <        catch(Exception ex) {
1024 <            unexpectedException();
960 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
961          } finally {
962              joinPool(tpe);
963          }
# Line 1030 | Line 966 | public class ThreadPoolExecutorTest exte
966      /**
967       * completed submit of callable returns result
968       */
969 <    public void testSubmitCallable() {
970 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
969 >    public void testSubmitCallable() throws Exception {
970 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
971          try {
972              Future<String> future = e.submit(new StringTask());
973              String result = future.get();
974              assertSame(TEST_STRING, result);
1039        }
1040        catch (ExecutionException ex) {
1041            unexpectedException();
1042        }
1043        catch (InterruptedException ex) {
1044            unexpectedException();
975          } finally {
976              joinPool(e);
977          }
# Line 1050 | Line 980 | public class ThreadPoolExecutorTest exte
980      /**
981       * completed submit of runnable returns successfully
982       */
983 <    public void testSubmitRunnable() {
984 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
983 >    public void testSubmitRunnable() throws Exception {
984 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
985          try {
986              Future<?> future = e.submit(new NoOpRunnable());
987              future.get();
988              assertTrue(future.isDone());
1059        }
1060        catch (ExecutionException ex) {
1061            unexpectedException();
1062        }
1063        catch (InterruptedException ex) {
1064            unexpectedException();
989          } finally {
990              joinPool(e);
991          }
# Line 1070 | Line 994 | public class ThreadPoolExecutorTest exte
994      /**
995       * completed submit of (runnable, result) returns result
996       */
997 <    public void testSubmitRunnable2() {
998 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
997 >    public void testSubmitRunnable2() throws Exception {
998 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
999          try {
1000              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1001              String result = future.get();
1002              assertSame(TEST_STRING, result);
1079        }
1080        catch (ExecutionException ex) {
1081            unexpectedException();
1082        }
1083        catch (InterruptedException ex) {
1084            unexpectedException();
1003          } finally {
1004              joinPool(e);
1005          }
1006      }
1007  
1008  
1091
1092
1093
1009      /**
1010       * invokeAny(null) throws NPE
1011       */
1012 <    public void testInvokeAny1() {
1013 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1012 >    public void testInvokeAny1() throws Exception {
1013 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1014          try {
1015              e.invokeAny(null);
1016 +            shouldThrow();
1017          } catch (NullPointerException success) {
1102        } catch(Exception ex) {
1103            unexpectedException();
1018          } finally {
1019              joinPool(e);
1020          }
# Line 1109 | Line 1023 | public class ThreadPoolExecutorTest exte
1023      /**
1024       * invokeAny(empty collection) throws IAE
1025       */
1026 <    public void testInvokeAny2() {
1027 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1026 >    public void testInvokeAny2() throws Exception {
1027 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1028          try {
1029              e.invokeAny(new ArrayList<Callable<String>>());
1030 +            shouldThrow();
1031          } catch (IllegalArgumentException success) {
1117        } catch(Exception ex) {
1118            unexpectedException();
1032          } finally {
1033              joinPool(e);
1034          }
# Line 1124 | Line 1037 | public class ThreadPoolExecutorTest exte
1037      /**
1038       * invokeAny(c) throws NPE if c has null elements
1039       */
1040 <    public void testInvokeAny3() {
1041 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1040 >    public void testInvokeAny3() throws Exception {
1041 >        final CountDownLatch latch = new CountDownLatch(1);
1042 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1043          try {
1044              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1045 <            l.add(new StringTask());
1045 >            l.add(new Callable<String>() {
1046 >                      public String call() {
1047 >                          try {
1048 >                              latch.await();
1049 >                          } catch (InterruptedException ok) {}
1050 >                          return TEST_STRING;
1051 >                      }});
1052              l.add(null);
1053              e.invokeAny(l);
1054 +            shouldThrow();
1055          } catch (NullPointerException success) {
1135        } catch(Exception ex) {
1136            unexpectedException();
1056          } finally {
1057 +            latch.countDown();
1058              joinPool(e);
1059          }
1060      }
# Line 1142 | Line 1062 | public class ThreadPoolExecutorTest exte
1062      /**
1063       * invokeAny(c) throws ExecutionException if no task completes
1064       */
1065 <    public void testInvokeAny4() {
1066 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1065 >    public void testInvokeAny4() throws Exception {
1066 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1067          try {
1068              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1069              l.add(new NPETask());
1070              e.invokeAny(l);
1071 +            shouldThrow();
1072          } catch (ExecutionException success) {
1073 <        } catch(Exception ex) {
1153 <            unexpectedException();
1073 >            assertTrue(success.getCause() instanceof NullPointerException);
1074          } finally {
1075              joinPool(e);
1076          }
# Line 1159 | Line 1079 | public class ThreadPoolExecutorTest exte
1079      /**
1080       * invokeAny(c) returns result of some task
1081       */
1082 <    public void testInvokeAny5() {
1083 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1082 >    public void testInvokeAny5() throws Exception {
1083 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1084          try {
1085              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1086              l.add(new StringTask());
1087              l.add(new StringTask());
1088              String result = e.invokeAny(l);
1089              assertSame(TEST_STRING, result);
1170        } catch (ExecutionException success) {
1171        } catch(Exception ex) {
1172            unexpectedException();
1090          } finally {
1091              joinPool(e);
1092          }
# Line 1178 | Line 1095 | public class ThreadPoolExecutorTest exte
1095      /**
1096       * invokeAll(null) throws NPE
1097       */
1098 <    public void testInvokeAll1() {
1099 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1098 >    public void testInvokeAll1() throws Exception {
1099 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1100          try {
1101              e.invokeAll(null);
1102 +            shouldThrow();
1103          } catch (NullPointerException success) {
1186        } catch(Exception ex) {
1187            unexpectedException();
1104          } finally {
1105              joinPool(e);
1106          }
# Line 1193 | Line 1109 | public class ThreadPoolExecutorTest exte
1109      /**
1110       * invokeAll(empty collection) returns empty collection
1111       */
1112 <    public void testInvokeAll2() {
1113 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1112 >    public void testInvokeAll2() throws InterruptedException {
1113 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1114          try {
1115              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1116              assertTrue(r.isEmpty());
1201        } catch(Exception ex) {
1202            unexpectedException();
1117          } finally {
1118              joinPool(e);
1119          }
# Line 1208 | Line 1122 | public class ThreadPoolExecutorTest exte
1122      /**
1123       * invokeAll(c) throws NPE if c has null elements
1124       */
1125 <    public void testInvokeAll3() {
1126 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1125 >    public void testInvokeAll3() throws Exception {
1126 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1127          try {
1128              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1129              l.add(new StringTask());
1130              l.add(null);
1131              e.invokeAll(l);
1132 +            shouldThrow();
1133          } catch (NullPointerException success) {
1219        } catch(Exception ex) {
1220            unexpectedException();
1134          } finally {
1135              joinPool(e);
1136          }
# Line 1226 | Line 1139 | public class ThreadPoolExecutorTest exte
1139      /**
1140       * get of element of invokeAll(c) throws exception on failed task
1141       */
1142 <    public void testInvokeAll4() {
1143 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1142 >    public void testInvokeAll4() throws Exception {
1143 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1144          try {
1145              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1146              l.add(new NPETask());
1147              List<Future<String>> result = e.invokeAll(l);
1148              assertEquals(1, result.size());
1149 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1150 <                it.next().get();
1151 <        } catch(ExecutionException success) {
1152 <        } catch(Exception ex) {
1153 <            unexpectedException();
1149 >            for (Future<String> future : result) {
1150 >                try {
1151 >                    future.get();
1152 >                    shouldThrow();
1153 >                } catch (ExecutionException success) {
1154 >                    Throwable cause = success.getCause();
1155 >                    assertTrue(cause instanceof NullPointerException);
1156 >                }
1157 >            }
1158          } finally {
1159              joinPool(e);
1160          }
# Line 1246 | Line 1163 | public class ThreadPoolExecutorTest exte
1163      /**
1164       * invokeAll(c) returns results of all completed tasks
1165       */
1166 <    public void testInvokeAll5() {
1167 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1166 >    public void testInvokeAll5() throws Exception {
1167 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1168          try {
1169              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1170              l.add(new StringTask());
1171              l.add(new StringTask());
1172              List<Future<String>> result = e.invokeAll(l);
1173              assertEquals(2, result.size());
1174 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1175 <                assertSame(TEST_STRING, it.next().get());
1259 <        } catch (ExecutionException success) {
1260 <        } catch(Exception ex) {
1261 <            unexpectedException();
1174 >            for (Future<String> future : result)
1175 >                assertSame(TEST_STRING, future.get());
1176          } finally {
1177              joinPool(e);
1178          }
# Line 1269 | Line 1183 | public class ThreadPoolExecutorTest exte
1183      /**
1184       * timed invokeAny(null) throws NPE
1185       */
1186 <    public void testTimedInvokeAny1() {
1187 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1186 >    public void testTimedInvokeAny1() throws Exception {
1187 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1188          try {
1189 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1189 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1190 >            shouldThrow();
1191          } catch (NullPointerException success) {
1277        } catch(Exception ex) {
1278            unexpectedException();
1192          } finally {
1193              joinPool(e);
1194          }
# Line 1284 | Line 1197 | public class ThreadPoolExecutorTest exte
1197      /**
1198       * timed invokeAny(,,null) throws NPE
1199       */
1200 <    public void testTimedInvokeAnyNullTimeUnit() {
1201 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1200 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1201 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1202          try {
1203              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1204              l.add(new StringTask());
1205              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1206 +            shouldThrow();
1207          } catch (NullPointerException success) {
1294        } catch(Exception ex) {
1295            unexpectedException();
1208          } finally {
1209              joinPool(e);
1210          }
# Line 1301 | Line 1213 | public class ThreadPoolExecutorTest exte
1213      /**
1214       * timed invokeAny(empty collection) throws IAE
1215       */
1216 <    public void testTimedInvokeAny2() {
1217 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1216 >    public void testTimedInvokeAny2() throws Exception {
1217 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1218          try {
1219 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1219 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1220 >            shouldThrow();
1221          } catch (IllegalArgumentException success) {
1309        } catch(Exception ex) {
1310            unexpectedException();
1222          } finally {
1223              joinPool(e);
1224          }
# Line 1316 | Line 1227 | public class ThreadPoolExecutorTest exte
1227      /**
1228       * timed invokeAny(c) throws NPE if c has null elements
1229       */
1230 <    public void testTimedInvokeAny3() {
1231 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1230 >    public void testTimedInvokeAny3() throws Exception {
1231 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1232          try {
1233              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1234              l.add(new StringTask());
1235              l.add(null);
1236 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1236 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1237 >            shouldThrow();
1238          } catch (NullPointerException success) {
1327        } catch(Exception ex) {
1328            ex.printStackTrace();
1329            unexpectedException();
1239          } finally {
1240              joinPool(e);
1241          }
# Line 1335 | Line 1244 | public class ThreadPoolExecutorTest exte
1244      /**
1245       * timed invokeAny(c) throws ExecutionException if no task completes
1246       */
1247 <    public void testTimedInvokeAny4() {
1248 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1247 >    public void testTimedInvokeAny4() throws Exception {
1248 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1249          try {
1250              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1251              l.add(new NPETask());
1252 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1253 <        } catch(ExecutionException success) {
1254 <        } catch(Exception ex) {
1255 <            unexpectedException();
1252 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1253 >            shouldThrow();
1254 >        } catch (ExecutionException success) {
1255 >            assertTrue(success.getCause() instanceof NullPointerException);
1256          } finally {
1257              joinPool(e);
1258          }
# Line 1352 | Line 1261 | public class ThreadPoolExecutorTest exte
1261      /**
1262       * timed invokeAny(c) returns result of some task
1263       */
1264 <    public void testTimedInvokeAny5() {
1265 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1264 >    public void testTimedInvokeAny5() throws Exception {
1265 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1266          try {
1267              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1268              l.add(new StringTask());
1269              l.add(new StringTask());
1270 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1270 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1271              assertSame(TEST_STRING, result);
1363        } catch (ExecutionException success) {
1364        } catch(Exception ex) {
1365            unexpectedException();
1272          } finally {
1273              joinPool(e);
1274          }
# Line 1371 | Line 1277 | public class ThreadPoolExecutorTest exte
1277      /**
1278       * timed invokeAll(null) throws NPE
1279       */
1280 <    public void testTimedInvokeAll1() {
1281 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1280 >    public void testTimedInvokeAll1() throws Exception {
1281 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1282          try {
1283 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1283 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1284 >            shouldThrow();
1285          } catch (NullPointerException success) {
1379        } catch(Exception ex) {
1380            unexpectedException();
1286          } finally {
1287              joinPool(e);
1288          }
# Line 1386 | Line 1291 | public class ThreadPoolExecutorTest exte
1291      /**
1292       * timed invokeAll(,,null) throws NPE
1293       */
1294 <    public void testTimedInvokeAllNullTimeUnit() {
1295 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1294 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1295 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1296          try {
1297              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1298              l.add(new StringTask());
1299              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1300 +            shouldThrow();
1301          } catch (NullPointerException success) {
1396        } catch(Exception ex) {
1397            unexpectedException();
1302          } finally {
1303              joinPool(e);
1304          }
# Line 1403 | Line 1307 | public class ThreadPoolExecutorTest exte
1307      /**
1308       * timed invokeAll(empty collection) returns empty collection
1309       */
1310 <    public void testTimedInvokeAll2() {
1311 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1310 >    public void testTimedInvokeAll2() throws InterruptedException {
1311 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1312          try {
1313 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1313 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1314              assertTrue(r.isEmpty());
1411        } catch(Exception ex) {
1412            unexpectedException();
1315          } finally {
1316              joinPool(e);
1317          }
# Line 1418 | Line 1320 | public class ThreadPoolExecutorTest exte
1320      /**
1321       * timed invokeAll(c) throws NPE if c has null elements
1322       */
1323 <    public void testTimedInvokeAll3() {
1324 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1323 >    public void testTimedInvokeAll3() throws Exception {
1324 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1325          try {
1326              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1327              l.add(new StringTask());
1328              l.add(null);
1329 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1329 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1330 >            shouldThrow();
1331          } catch (NullPointerException success) {
1429        } catch(Exception ex) {
1430            unexpectedException();
1332          } finally {
1333              joinPool(e);
1334          }
# Line 1436 | Line 1337 | public class ThreadPoolExecutorTest exte
1337      /**
1338       * get of element of invokeAll(c) throws exception on failed task
1339       */
1340 <    public void testTimedInvokeAll4() {
1341 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1340 >    public void testTimedInvokeAll4() throws Exception {
1341 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1342          try {
1343              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1344              l.add(new NPETask());
1345 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1345 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1346              assertEquals(1, result.size());
1347 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1348 <                it.next().get();
1349 <        } catch(ExecutionException success) {
1350 <        } catch(Exception ex) {
1351 <            unexpectedException();
1347 >            for (Future<String> future : result)
1348 >                future.get();
1349 >            shouldThrow();
1350 >        } catch (ExecutionException success) {
1351 >            assertTrue(success.getCause() instanceof NullPointerException);
1352          } finally {
1353              joinPool(e);
1354          }
# Line 1456 | Line 1357 | public class ThreadPoolExecutorTest exte
1357      /**
1358       * timed invokeAll(c) returns results of all completed tasks
1359       */
1360 <    public void testTimedInvokeAll5() {
1361 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1360 >    public void testTimedInvokeAll5() throws Exception {
1361 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1362          try {
1363              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1364              l.add(new StringTask());
1365              l.add(new StringTask());
1366 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1366 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1367              assertEquals(2, result.size());
1368 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1368 >            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1369                  assertSame(TEST_STRING, it.next().get());
1469        } catch (ExecutionException success) {
1470        } catch(Exception ex) {
1471            unexpectedException();
1370          } finally {
1371              joinPool(e);
1372          }
# Line 1477 | Line 1375 | public class ThreadPoolExecutorTest exte
1375      /**
1376       * timed invokeAll(c) cancels tasks not completed by timeout
1377       */
1378 <    public void testTimedInvokeAll6() {
1379 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1378 >    public void testTimedInvokeAll6() throws Exception {
1379 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1380          try {
1381              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1382              l.add(new StringTask());
1383              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1384 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1385 <            assertEquals(2, result.size());
1386 <            Iterator<Future<String>> it = result.iterator();
1384 >            l.add(new StringTask());
1385 >            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1386 >            assertEquals(3, result.size());
1387 >            Iterator<Future<String>> it = result.iterator();
1388              Future<String> f1 = it.next();
1389              Future<String> f2 = it.next();
1390 +            Future<String> f3 = it.next();
1391              assertTrue(f1.isDone());
1492            assertFalse(f1.isCancelled());
1392              assertTrue(f2.isDone());
1393 +            assertTrue(f3.isDone());
1394 +            assertFalse(f1.isCancelled());
1395              assertTrue(f2.isCancelled());
1495        } catch(Exception ex) {
1496            unexpectedException();
1396          } finally {
1397              joinPool(e);
1398          }
1399      }
1400  
1401 +    /**
1402 +     * Execution continues if there is at least one thread even if
1403 +     * thread factory fails to create more
1404 +     */
1405 +    public void testFailingThreadFactory() throws InterruptedException {
1406 +        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1407 +        try {
1408 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1409 +            for (int k = 0; k < 100; ++k) {
1410 +                e.execute(new NoOpRunnable());
1411 +            }
1412 +            Thread.sleep(LONG_DELAY_MS);
1413 +        } finally {
1414 +            joinPool(e);
1415 +        }
1416 +    }
1417 +
1418 +    /**
1419 +     * allowsCoreThreadTimeOut is by default false.
1420 +     */
1421 +    public void testAllowsCoreThreadTimeOut() {
1422 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1423 +        assertFalse(tpe.allowsCoreThreadTimeOut());
1424 +        joinPool(tpe);
1425 +    }
1426 +
1427 +    /**
1428 +     * allowCoreThreadTimeOut(true) causes idle threads to time out
1429 +     */
1430 +    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1431 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1432 +        tpe.allowCoreThreadTimeOut(true);
1433 +        tpe.execute(new NoOpRunnable());
1434 +        try {
1435 +            Thread.sleep(MEDIUM_DELAY_MS);
1436 +            assertEquals(0, tpe.getPoolSize());
1437 +        } finally {
1438 +            joinPool(tpe);
1439 +        }
1440 +    }
1441 +
1442 +    /**
1443 +     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1444 +     */
1445 +    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1446 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1447 +        tpe.allowCoreThreadTimeOut(false);
1448 +        tpe.execute(new NoOpRunnable());
1449 +        try {
1450 +            Thread.sleep(MEDIUM_DELAY_MS);
1451 +            assertTrue(tpe.getPoolSize() >= 1);
1452 +        } finally {
1453 +            joinPool(tpe);
1454 +        }
1455 +    }
1456 +
1457 +    /**
1458 +     * execute allows the same task to be submitted multiple times, even
1459 +     * if rejected
1460 +     */
1461 +    public void testRejectedRecycledTask() throws InterruptedException {
1462 +        final int nTasks = 1000;
1463 +        final AtomicInteger nRun = new AtomicInteger(0);
1464 +        final Runnable recycledTask = new Runnable() {
1465 +                public void run() {
1466 +                    nRun.getAndIncrement();
1467 +                } };
1468 +        final ThreadPoolExecutor p =
1469 +            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
1470 +                                   new ArrayBlockingQueue(30));
1471 +        try {
1472 +            for (int i = 0; i < nTasks; ++i) {
1473 +                for (;;) {
1474 +                    try {
1475 +                        p.execute(recycledTask);
1476 +                        break;
1477 +                    }
1478 +                    catch (RejectedExecutionException ignore) {
1479 +                    }
1480 +                }
1481 +            }
1482 +            Thread.sleep(5000); // enough time to run all tasks
1483 +            assertEquals(nRun.get(), nTasks);
1484 +        } finally {
1485 +            p.shutdown();
1486 +        }
1487 +    }
1488  
1489   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines