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.27 by jsr166, Wed Nov 18 05:39:51 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          }
405 <        catch (IllegalArgumentException success){}
405 >        catch (IllegalArgumentException success) {}
406      }
407 <    
408 <    /**
409 <     * Constructor throws if maximumPoolSize is less than zero
407 >
408 >    /**
409 >     * Constructor throws if maximumPoolSize is less than zero
410       */
411      public void testConstructor2() {
412          try {
413 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
413 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
414              shouldThrow();
415          }
416 <        catch (IllegalArgumentException success){}
416 >        catch (IllegalArgumentException success) {}
417      }
418 <    
419 <    /**
420 <     * Constructor throws if maximumPoolSize is equal to zero
418 >
419 >    /**
420 >     * Constructor throws if maximumPoolSize is equal to zero
421       */
422      public void testConstructor3() {
423          try {
424 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
424 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
425              shouldThrow();
426          }
427 <        catch (IllegalArgumentException success){}
427 >        catch (IllegalArgumentException success) {}
428      }
429  
430 <    /**
431 <     * Constructor throws if keepAliveTime is less than zero
430 >    /**
431 >     * Constructor throws if keepAliveTime is less than zero
432       */
433      public void testConstructor4() {
434          try {
435 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
435 >            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
436              shouldThrow();
437          }
438 <        catch (IllegalArgumentException success){}
438 >        catch (IllegalArgumentException success) {}
439      }
440  
441 <    /**
442 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
441 >    /**
442 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
443       */
444      public void testConstructor5() {
445          try {
446 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
446 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
447              shouldThrow();
448          }
449 <        catch (IllegalArgumentException success){}
449 >        catch (IllegalArgumentException success) {}
450      }
451 <        
452 <    /**
453 <     * Constructor throws if workQueue is set to null
451 >
452 >    /**
453 >     * Constructor throws if workQueue is set to null
454       */
455      public void testConstructorNullPointerException() {
456          try {
457 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null);
457 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null);
458              shouldThrow();
459          }
460 <        catch (NullPointerException success){}  
460 >        catch (NullPointerException success) {}
461      }
489    
462  
463 <    
464 <    /**
465 <     * Constructor throws if corePoolSize argument is less than zero
463 >
464 >
465 >    /**
466 >     * Constructor throws if corePoolSize argument is less than zero
467       */
468      public void testConstructor6() {
469          try {
470 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
470 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
471              shouldThrow();
472 <        } catch (IllegalArgumentException success){}
472 >        }
473 >        catch (IllegalArgumentException success) {}
474      }
475 <    
476 <    /**
477 <     * Constructor throws if maximumPoolSize is less than zero
475 >
476 >    /**
477 >     * Constructor throws if maximumPoolSize is less than zero
478       */
479      public void testConstructor7() {
480          try {
481 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
481 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
482              shouldThrow();
483          }
484 <        catch (IllegalArgumentException success){}
484 >        catch (IllegalArgumentException success) {}
485      }
486  
487 <    /**
488 <     * Constructor throws if maximumPoolSize is equal to zero
487 >    /**
488 >     * Constructor throws if maximumPoolSize is equal to zero
489       */
490      public void testConstructor8() {
491          try {
492 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
492 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
493              shouldThrow();
494          }
495 <        catch (IllegalArgumentException success){}
495 >        catch (IllegalArgumentException success) {}
496      }
497  
498 <    /**
499 <     * Constructor throws if keepAliveTime is less than zero
498 >    /**
499 >     * Constructor throws if keepAliveTime is less than zero
500       */
501      public void testConstructor9() {
502          try {
503 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
503 >            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
504              shouldThrow();
505          }
506 <        catch (IllegalArgumentException success){}
506 >        catch (IllegalArgumentException success) {}
507      }
508  
509 <    /**
510 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
509 >    /**
510 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
511       */
512      public void testConstructor10() {
513          try {
514 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
514 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory());
515              shouldThrow();
516          }
517 <        catch (IllegalArgumentException success){}
517 >        catch (IllegalArgumentException success) {}
518      }
519  
520 <    /**
521 <     * Constructor throws if workQueue is set to null
520 >    /**
521 >     * Constructor throws if workQueue is set to null
522       */
523      public void testConstructorNullPointerException2() {
524          try {
525 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory());
525 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory());
526              shouldThrow();
527          }
528 <        catch (NullPointerException success){}  
528 >        catch (NullPointerException success) {}
529      }
530  
531 <    /**
532 <     * Constructor throws if threadFactory is set to null
531 >    /**
532 >     * Constructor throws if threadFactory is set to null
533       */
534      public void testConstructorNullPointerException3() {
535          try {
536              ThreadFactory f = null;
537 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
537 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f);
538              shouldThrow();
539          }
540 <        catch (NullPointerException success){}  
540 >        catch (NullPointerException success) {}
541      }
542 <
543 <    
544 <    /**
545 <     * Constructor throws if corePoolSize argument is less than zero
542 >
543 >
544 >    /**
545 >     * Constructor throws if corePoolSize argument is less than zero
546       */
547      public void testConstructor11() {
548          try {
549 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
549 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
550              shouldThrow();
551          }
552 <        catch (IllegalArgumentException success){}
552 >        catch (IllegalArgumentException success) {}
553      }
554  
555 <    /**
556 <     * Constructor throws if maximumPoolSize is less than zero
555 >    /**
556 >     * Constructor throws if maximumPoolSize is less than zero
557       */
558      public void testConstructor12() {
559          try {
560 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
560 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
561              shouldThrow();
562          }
563 <        catch (IllegalArgumentException success){}
563 >        catch (IllegalArgumentException success) {}
564      }
565  
566 <    /**
567 <     * Constructor throws if maximumPoolSize is equal to zero
566 >    /**
567 >     * Constructor throws if maximumPoolSize is equal to zero
568       */
569      public void testConstructor13() {
570          try {
571 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
571 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
572              shouldThrow();
573          }
574 <        catch (IllegalArgumentException success){}
574 >        catch (IllegalArgumentException success) {}
575      }
576  
577 <    /**
578 <     * Constructor throws if keepAliveTime is less than zero
577 >    /**
578 >     * Constructor throws if keepAliveTime is less than zero
579       */
580      public void testConstructor14() {
581          try {
582 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
582 >            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
583              shouldThrow();
584          }
585 <        catch (IllegalArgumentException success){}
585 >        catch (IllegalArgumentException success) {}
586      }
587  
588 <    /**
589 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
588 >    /**
589 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
590       */
591      public void testConstructor15() {
592          try {
593 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
593 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler());
594              shouldThrow();
595          }
596 <        catch (IllegalArgumentException success){}
596 >        catch (IllegalArgumentException success) {}
597      }
598  
599 <    /**
600 <     * Constructor throws if workQueue is set to null
599 >    /**
600 >     * Constructor throws if workQueue is set to null
601       */
602      public void testConstructorNullPointerException4() {
603          try {
604 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler());
604 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler());
605              shouldThrow();
606          }
607 <        catch (NullPointerException success){}  
607 >        catch (NullPointerException success) {}
608      }
609  
610 <    /**
611 <     * Constructor throws if handler is set to null
610 >    /**
611 >     * Constructor throws if handler is set to null
612       */
613      public void testConstructorNullPointerException5() {
614          try {
615              RejectedExecutionHandler r = null;
616 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
616 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r);
617              shouldThrow();
618          }
619 <        catch (NullPointerException success){}  
619 >        catch (NullPointerException success) {}
620      }
621  
622 <    
623 <    /**
624 <     * Constructor throws if corePoolSize argument is less than zero
622 >
623 >    /**
624 >     * Constructor throws if corePoolSize argument is less than zero
625       */
626      public void testConstructor16() {
627          try {
628 <            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
628 >            new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
629              shouldThrow();
630          }
631 <        catch (IllegalArgumentException success){}
631 >        catch (IllegalArgumentException success) {}
632      }
633  
634 <    /**
635 <     * Constructor throws if maximumPoolSize is less than zero
634 >    /**
635 >     * Constructor throws if maximumPoolSize is less than zero
636       */
637      public void testConstructor17() {
638          try {
639 <            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
639 >            new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
640              shouldThrow();
641          }
642 <        catch (IllegalArgumentException success){}
642 >        catch (IllegalArgumentException success) {}
643      }
644  
645 <    /**
646 <     * Constructor throws if maximumPoolSize is equal to zero
645 >    /**
646 >     * Constructor throws if maximumPoolSize is equal to zero
647       */
648      public void testConstructor18() {
649          try {
650 <            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
650 >            new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
651              shouldThrow();
652          }
653 <        catch (IllegalArgumentException success){}
653 >        catch (IllegalArgumentException success) {}
654      }
655  
656 <    /**
657 <     * Constructor throws if keepAliveTime is less than zero
656 >    /**
657 >     * Constructor throws if keepAliveTime is less than zero
658       */
659      public void testConstructor19() {
660          try {
661 <            new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
661 >            new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
662              shouldThrow();
663          }
664 <        catch (IllegalArgumentException success){}
664 >        catch (IllegalArgumentException success) {}
665      }
666  
667 <    /**
668 <     * Constructor throws if corePoolSize is greater than the maximumPoolSize
667 >    /**
668 >     * Constructor throws if corePoolSize is greater than the maximumPoolSize
669       */
670      public void testConstructor20() {
671          try {
672 <            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
672 >            new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler());
673              shouldThrow();
674          }
675 <        catch (IllegalArgumentException success){}
675 >        catch (IllegalArgumentException success) {}
676      }
677  
678 <    /**
679 <     * Constructor throws if workQueue is set to null
678 >    /**
679 >     * Constructor throws if workQueue is set to null
680       */
681      public void testConstructorNullPointerException6() {
682          try {
683 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
683 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler());
684              shouldThrow();
685          }
686 <        catch (NullPointerException success){}  
686 >        catch (NullPointerException success) {}
687      }
688  
689 <    /**
690 <     * Constructor throws if handler is set to null
689 >    /**
690 >     * Constructor throws if handler is set to null
691       */
692      public void testConstructorNullPointerException7() {
693          try {
694              RejectedExecutionHandler r = null;
695 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
695 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r);
696              shouldThrow();
697          }
698 <        catch (NullPointerException success){}  
698 >        catch (NullPointerException success) {}
699      }
700  
701 <    /**
702 <     * Constructor throws if ThreadFactory is set top null
701 >    /**
702 >     * Constructor throws if ThreadFactory is set top null
703       */
704      public void testConstructorNullPointerException8() {
705          try {
706              ThreadFactory f = null;
707 <            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
707 >            new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler());
708              shouldThrow();
709          }
710 <        catch (NullPointerException successdn8){}  
710 >        catch (NullPointerException success) {}
711      }
712 <    
712 >
713  
714      /**
715       *  execute throws RejectedExecutionException
716       *  if saturated.
717       */
718      public void testSaturatedExecute() {
719 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1));
719 >        ThreadPoolExecutor p =
720 >            new ThreadPoolExecutor(1, 1,
721 >                                   LONG_DELAY_MS, MILLISECONDS,
722 >                                   new ArrayBlockingQueue<Runnable>(1));
723          try {
724 <            
748 <            for(int i = 0; i < 5; ++i){
724 >            for (int i = 0; i < 2; ++i)
725                  p.execute(new MediumRunnable());
726 +            for (int i = 0; i < 2; ++i) {
727 +                try {
728 +                    p.execute(new MediumRunnable());
729 +                    shouldThrow();
730 +                } catch (RejectedExecutionException success) {}
731              }
732 <            shouldThrow();
733 <        } catch(RejectedExecutionException success){}
734 <        joinPool(p);
732 >        } finally {
733 >            joinPool(p);
734 >        }
735      }
736  
737      /**
# Line 758 | Line 739 | public class ThreadPoolExecutorTest exte
739       */
740      public void testSaturatedExecute2() {
741          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
742 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
742 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
743          try {
744 <            
744 >
745              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
746 <            for(int i = 0; i < 5; ++i){
746 >            for (int i = 0; i < 5; ++i) {
747                  tasks[i] = new TrackedNoOpRunnable();
748              }
749              TrackedLongRunnable mr = new TrackedLongRunnable();
750              p.execute(mr);
751 <            for(int i = 0; i < 5; ++i){
751 >            for (int i = 0; i < 5; ++i) {
752                  p.execute(tasks[i]);
753              }
754 <            for(int i = 1; i < 5; ++i) {
754 >            for (int i = 1; i < 5; ++i) {
755                  assertTrue(tasks[i].done);
756              }
757 <            p.shutdownNow();
777 <        } catch(RejectedExecutionException ex){
778 <            unexpectedException();
757 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
758          } finally {
759              joinPool(p);
760          }
# Line 786 | Line 765 | public class ThreadPoolExecutorTest exte
765       */
766      public void testSaturatedExecute3() {
767          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
768 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
768 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
769          try {
770 <            
770 >
771              TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5];
772 <            for(int i = 0; i < 5; ++i){
772 >            for (int i = 0; i < 5; ++i) {
773                  tasks[i] = new TrackedNoOpRunnable();
774              }
775              p.execute(new TrackedLongRunnable());
776 <            for(int i = 0; i < 5; ++i){
776 >            for (int i = 0; i < 5; ++i) {
777                  p.execute(tasks[i]);
778              }
779 <            for(int i = 0; i < 5; ++i){
779 >            for (int i = 0; i < 5; ++i) {
780                  assertFalse(tasks[i].done);
781              }
782 <            p.shutdownNow();
804 <        } catch(RejectedExecutionException ex){
805 <            unexpectedException();
782 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
783          } finally {
784              joinPool(p);
785          }
# Line 813 | Line 790 | public class ThreadPoolExecutorTest exte
790       */
791      public void testSaturatedExecute4() {
792          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
793 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
793 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
794          try {
795              p.execute(new TrackedLongRunnable());
796              TrackedLongRunnable r2 = new TrackedLongRunnable();
# Line 823 | Line 800 | public class ThreadPoolExecutorTest exte
800              p.execute(r3);
801              assertFalse(p.getQueue().contains(r2));
802              assertTrue(p.getQueue().contains(r3));
803 <            p.shutdownNow();
827 <        } catch(RejectedExecutionException ex){
828 <            unexpectedException();
803 >            try { p.shutdownNow(); } catch (SecurityException ok) { return; }
804          } finally {
805              joinPool(p);
806          }
# Line 835 | Line 810 | public class ThreadPoolExecutorTest exte
810       *  execute throws RejectedExecutionException if shutdown
811       */
812      public void testRejectedExecutionExceptionOnShutdown() {
813 <        ThreadPoolExecutor tpe =
814 <            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
815 <        tpe.shutdown();
813 >        ThreadPoolExecutor tpe =
814 >            new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1));
815 >        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
816          try {
817              tpe.execute(new NoOpRunnable());
818              shouldThrow();
819 <        } catch(RejectedExecutionException success){}
820 <        
819 >        } catch (RejectedExecutionException success) {}
820 >
821          joinPool(tpe);
822      }
823  
# Line 851 | Line 826 | public class ThreadPoolExecutorTest exte
826       */
827      public void testCallerRunsOnShutdown() {
828          RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy();
829 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
829 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
830  
831 <        p.shutdown();
831 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
832          try {
833              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
834              p.execute(r);
835              assertFalse(r.done);
861        } catch(RejectedExecutionException success){
862            unexpectedException();
836          } finally {
837              joinPool(p);
838          }
# Line 870 | Line 843 | public class ThreadPoolExecutorTest exte
843       */
844      public void testDiscardOnShutdown() {
845          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy();
846 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
846 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
847  
848 <        p.shutdown();
848 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
849          try {
850              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
851              p.execute(r);
852              assertFalse(r.done);
880        } catch(RejectedExecutionException success){
881            unexpectedException();
853          } finally {
854              joinPool(p);
855          }
# Line 890 | Line 861 | public class ThreadPoolExecutorTest exte
861       */
862      public void testDiscardOldestOnShutdown() {
863          RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy();
864 <        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
864 >        ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h);
865  
866 <        p.shutdown();
866 >        try { p.shutdown(); } catch (SecurityException ok) { return; }
867          try {
868              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
869              p.execute(r);
870              assertFalse(r.done);
900        } catch(RejectedExecutionException success){
901            unexpectedException();
871          } finally {
872              joinPool(p);
873          }
# Line 911 | Line 880 | public class ThreadPoolExecutorTest exte
880      public void testExecuteNull() {
881          ThreadPoolExecutor tpe = null;
882          try {
883 <            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
883 >            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
884              tpe.execute(null);
885              shouldThrow();
886 <        } catch(NullPointerException success){}
887 <        
886 >        } catch (NullPointerException success) {}
887 >
888          joinPool(tpe);
889      }
890 <    
890 >
891      /**
892       *  setCorePoolSize of negative value throws IllegalArgumentException
893       */
894      public void testCorePoolSizeIllegalArgumentException() {
895 <        ThreadPoolExecutor tpe = null;
896 <        try {
897 <            tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
898 <        } catch(Exception e){}
895 >        ThreadPoolExecutor tpe =
896 >            new ThreadPoolExecutor(1, 2,
897 >                                   LONG_DELAY_MS, MILLISECONDS,
898 >                                   new ArrayBlockingQueue<Runnable>(10));
899          try {
900              tpe.setCorePoolSize(-1);
901              shouldThrow();
902 <        } catch(IllegalArgumentException success){
902 >        } catch (IllegalArgumentException success) {
903          } finally {
904 <            tpe.shutdown();
904 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
905          }
906          joinPool(tpe);
907 <    }  
907 >    }
908  
909      /**
910       *  setMaximumPoolSize(int) throws IllegalArgumentException if
911       *  given a value less the core pool size
912 <     */  
912 >     */
913      public void testMaximumPoolSizeIllegalArgumentException() {
914 <        ThreadPoolExecutor tpe = null;
915 <        try {
916 <            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
917 <        } catch(Exception e){}
914 >        ThreadPoolExecutor tpe =
915 >            new ThreadPoolExecutor(2, 3,
916 >                                   LONG_DELAY_MS, MILLISECONDS,
917 >                                   new ArrayBlockingQueue<Runnable>(10));
918          try {
919              tpe.setMaximumPoolSize(1);
920              shouldThrow();
921 <        } catch(IllegalArgumentException success){
921 >        } catch (IllegalArgumentException success) {
922          } finally {
923 <            tpe.shutdown();
923 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
924          }
925          joinPool(tpe);
926      }
927 <    
927 >
928      /**
929       *  setMaximumPoolSize throws IllegalArgumentException
930       *  if given a negative value
931       */
932      public void testMaximumPoolSizeIllegalArgumentException2() {
933 <        ThreadPoolExecutor tpe = null;
934 <        try {
935 <            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
936 <        } catch(Exception e){}
933 >        ThreadPoolExecutor tpe =
934 >            new ThreadPoolExecutor(2, 3,
935 >                                   LONG_DELAY_MS, MILLISECONDS,
936 >                                   new ArrayBlockingQueue<Runnable>(10));
937          try {
938              tpe.setMaximumPoolSize(-1);
939              shouldThrow();
940 <        } catch(IllegalArgumentException success){
940 >        } catch (IllegalArgumentException success) {
941          } finally {
942 <            tpe.shutdown();
942 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
943          }
944          joinPool(tpe);
945      }
946 <    
946 >
947  
948      /**
949       *  setKeepAliveTime  throws IllegalArgumentException
950       *  when given a negative value
951       */
952      public void testKeepAliveTimeIllegalArgumentException() {
953 <        ThreadPoolExecutor tpe = null;
954 <        try {
955 <            tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10));
956 <        } catch(Exception e){}
988 <        
953 >        ThreadPoolExecutor tpe =
954 >            new ThreadPoolExecutor(2, 3,
955 >                                   LONG_DELAY_MS, MILLISECONDS,
956 >                                   new ArrayBlockingQueue<Runnable>(10));
957          try {
958 <            tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS);
958 >            tpe.setKeepAliveTime(-1,MILLISECONDS);
959              shouldThrow();
960 <        } catch(IllegalArgumentException success){
960 >        } catch (IllegalArgumentException success) {
961          } finally {
962 <            tpe.shutdown();
962 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
963          }
964          joinPool(tpe);
965      }
# Line 1001 | Line 969 | public class ThreadPoolExecutorTest exte
969       */
970      public void testTerminated() {
971          ExtendedTPE tpe = new ExtendedTPE();
972 <        tpe.shutdown();
972 >        try { tpe.shutdown(); } catch (SecurityException ok) { return; }
973          assertTrue(tpe.terminatedCalled);
974          joinPool(tpe);
975      }
# Line 1009 | Line 977 | public class ThreadPoolExecutorTest exte
977      /**
978       * beforeExecute and afterExecute are called when executing task
979       */
980 <    public void testBeforeAfter() {
980 >    public void testBeforeAfter() throws InterruptedException {
981          ExtendedTPE tpe = new ExtendedTPE();
982          try {
983              TrackedNoOpRunnable r = new TrackedNoOpRunnable();
# Line 1018 | Line 986 | public class ThreadPoolExecutorTest exte
986              assertTrue(r.done);
987              assertTrue(tpe.beforeCalled);
988              assertTrue(tpe.afterCalled);
989 <            tpe.shutdown();
1022 <        }
1023 <        catch(Exception ex) {
1024 <            unexpectedException();
989 >            try { tpe.shutdown(); } catch (SecurityException ok) { return; }
990          } finally {
991              joinPool(tpe);
992          }
# Line 1030 | Line 995 | public class ThreadPoolExecutorTest exte
995      /**
996       * completed submit of callable returns result
997       */
998 <    public void testSubmitCallable() {
999 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
998 >    public void testSubmitCallable() throws Exception {
999 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1000          try {
1001              Future<String> future = e.submit(new StringTask());
1002              String result = future.get();
1003              assertSame(TEST_STRING, result);
1039        }
1040        catch (ExecutionException ex) {
1041            unexpectedException();
1042        }
1043        catch (InterruptedException ex) {
1044            unexpectedException();
1004          } finally {
1005              joinPool(e);
1006          }
# Line 1050 | Line 1009 | public class ThreadPoolExecutorTest exte
1009      /**
1010       * completed submit of runnable returns successfully
1011       */
1012 <    public void testSubmitRunnable() {
1013 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1012 >    public void testSubmitRunnable() throws Exception {
1013 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1014          try {
1015              Future<?> future = e.submit(new NoOpRunnable());
1016              future.get();
1017              assertTrue(future.isDone());
1059        }
1060        catch (ExecutionException ex) {
1061            unexpectedException();
1062        }
1063        catch (InterruptedException ex) {
1064            unexpectedException();
1018          } finally {
1019              joinPool(e);
1020          }
# Line 1070 | Line 1023 | public class ThreadPoolExecutorTest exte
1023      /**
1024       * completed submit of (runnable, result) returns result
1025       */
1026 <    public void testSubmitRunnable2() {
1027 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1026 >    public void testSubmitRunnable2() throws Exception {
1027 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1028          try {
1029              Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING);
1030              String result = future.get();
1031              assertSame(TEST_STRING, result);
1079        }
1080        catch (ExecutionException ex) {
1081            unexpectedException();
1082        }
1083        catch (InterruptedException ex) {
1084            unexpectedException();
1032          } finally {
1033              joinPool(e);
1034          }
1035      }
1036  
1037  
1091
1092
1093
1038      /**
1039       * invokeAny(null) throws NPE
1040       */
1041 <    public void testInvokeAny1() {
1042 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1041 >    public void testInvokeAny1() throws Exception {
1042 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1043          try {
1044              e.invokeAny(null);
1045 +            shouldThrow();
1046          } catch (NullPointerException success) {
1102        } catch(Exception ex) {
1103            unexpectedException();
1047          } finally {
1048              joinPool(e);
1049          }
# Line 1109 | Line 1052 | public class ThreadPoolExecutorTest exte
1052      /**
1053       * invokeAny(empty collection) throws IAE
1054       */
1055 <    public void testInvokeAny2() {
1056 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1055 >    public void testInvokeAny2() throws Exception {
1056 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1057          try {
1058              e.invokeAny(new ArrayList<Callable<String>>());
1059 +            shouldThrow();
1060          } catch (IllegalArgumentException success) {
1117        } catch(Exception ex) {
1118            unexpectedException();
1061          } finally {
1062              joinPool(e);
1063          }
# Line 1124 | Line 1066 | public class ThreadPoolExecutorTest exte
1066      /**
1067       * invokeAny(c) throws NPE if c has null elements
1068       */
1069 <    public void testInvokeAny3() {
1070 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1069 >    public void testInvokeAny3() throws Exception {
1070 >        final CountDownLatch latch = new CountDownLatch(1);
1071 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1072          try {
1073              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1074 <            l.add(new StringTask());
1074 >            l.add(new CheckedCallable<String>() {
1075 >                      public String realCall() throws InterruptedException {
1076 >                          latch.await();
1077 >                          return TEST_STRING;
1078 >                      }});
1079              l.add(null);
1080              e.invokeAny(l);
1081 +            shouldThrow();
1082          } catch (NullPointerException success) {
1135        } catch(Exception ex) {
1136            unexpectedException();
1083          } finally {
1084 +            latch.countDown();
1085              joinPool(e);
1086          }
1087      }
# Line 1142 | Line 1089 | public class ThreadPoolExecutorTest exte
1089      /**
1090       * invokeAny(c) throws ExecutionException if no task completes
1091       */
1092 <    public void testInvokeAny4() {
1093 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1092 >    public void testInvokeAny4() throws Exception {
1093 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1094          try {
1095              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1096              l.add(new NPETask());
1097              e.invokeAny(l);
1098 +            shouldThrow();
1099          } catch (ExecutionException success) {
1100 <        } catch(Exception ex) {
1153 <            unexpectedException();
1100 >            assertTrue(success.getCause() instanceof NullPointerException);
1101          } finally {
1102              joinPool(e);
1103          }
# Line 1159 | Line 1106 | public class ThreadPoolExecutorTest exte
1106      /**
1107       * invokeAny(c) returns result of some task
1108       */
1109 <    public void testInvokeAny5() {
1110 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1109 >    public void testInvokeAny5() throws Exception {
1110 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1111          try {
1112              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1113              l.add(new StringTask());
1114              l.add(new StringTask());
1115              String result = e.invokeAny(l);
1116              assertSame(TEST_STRING, result);
1170        } catch (ExecutionException success) {
1171        } catch(Exception ex) {
1172            unexpectedException();
1117          } finally {
1118              joinPool(e);
1119          }
# Line 1178 | Line 1122 | public class ThreadPoolExecutorTest exte
1122      /**
1123       * invokeAll(null) throws NPE
1124       */
1125 <    public void testInvokeAll1() {
1126 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1125 >    public void testInvokeAll1() throws Exception {
1126 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1127          try {
1128              e.invokeAll(null);
1129 +            shouldThrow();
1130          } catch (NullPointerException success) {
1186        } catch(Exception ex) {
1187            unexpectedException();
1131          } finally {
1132              joinPool(e);
1133          }
# Line 1193 | Line 1136 | public class ThreadPoolExecutorTest exte
1136      /**
1137       * invokeAll(empty collection) returns empty collection
1138       */
1139 <    public void testInvokeAll2() {
1140 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1139 >    public void testInvokeAll2() throws InterruptedException {
1140 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1141          try {
1142              List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>());
1143              assertTrue(r.isEmpty());
1201        } catch(Exception ex) {
1202            unexpectedException();
1144          } finally {
1145              joinPool(e);
1146          }
# Line 1208 | Line 1149 | public class ThreadPoolExecutorTest exte
1149      /**
1150       * invokeAll(c) throws NPE if c has null elements
1151       */
1152 <    public void testInvokeAll3() {
1153 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1152 >    public void testInvokeAll3() throws Exception {
1153 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1154          try {
1155              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1156              l.add(new StringTask());
1157              l.add(null);
1158              e.invokeAll(l);
1159 +            shouldThrow();
1160          } catch (NullPointerException success) {
1219        } catch(Exception ex) {
1220            unexpectedException();
1161          } finally {
1162              joinPool(e);
1163          }
# Line 1226 | Line 1166 | public class ThreadPoolExecutorTest exte
1166      /**
1167       * get of element of invokeAll(c) throws exception on failed task
1168       */
1169 <    public void testInvokeAll4() {
1170 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1169 >    public void testInvokeAll4() throws Exception {
1170 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1171          try {
1172              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1173              l.add(new NPETask());
1174              List<Future<String>> result = e.invokeAll(l);
1175              assertEquals(1, result.size());
1176 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1177 <                it.next().get();
1178 <        } catch(ExecutionException success) {
1179 <        } catch(Exception ex) {
1180 <            unexpectedException();
1176 >            for (Future<String> future : result) {
1177 >                try {
1178 >                    future.get();
1179 >                    shouldThrow();
1180 >                } catch (ExecutionException success) {
1181 >                    Throwable cause = success.getCause();
1182 >                    assertTrue(cause instanceof NullPointerException);
1183 >                }
1184 >            }
1185          } finally {
1186              joinPool(e);
1187          }
# Line 1246 | Line 1190 | public class ThreadPoolExecutorTest exte
1190      /**
1191       * invokeAll(c) returns results of all completed tasks
1192       */
1193 <    public void testInvokeAll5() {
1194 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1193 >    public void testInvokeAll5() throws Exception {
1194 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1195          try {
1196              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1197              l.add(new StringTask());
1198              l.add(new StringTask());
1199              List<Future<String>> result = e.invokeAll(l);
1200              assertEquals(2, result.size());
1201 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1202 <                assertSame(TEST_STRING, it.next().get());
1259 <        } catch (ExecutionException success) {
1260 <        } catch(Exception ex) {
1261 <            unexpectedException();
1201 >            for (Future<String> future : result)
1202 >                assertSame(TEST_STRING, future.get());
1203          } finally {
1204              joinPool(e);
1205          }
# Line 1269 | Line 1210 | public class ThreadPoolExecutorTest exte
1210      /**
1211       * timed invokeAny(null) throws NPE
1212       */
1213 <    public void testTimedInvokeAny1() {
1214 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1213 >    public void testTimedInvokeAny1() throws Exception {
1214 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1215          try {
1216 <            e.invokeAny(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1216 >            e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS);
1217 >            shouldThrow();
1218          } catch (NullPointerException success) {
1277        } catch(Exception ex) {
1278            unexpectedException();
1219          } finally {
1220              joinPool(e);
1221          }
# Line 1284 | Line 1224 | public class ThreadPoolExecutorTest exte
1224      /**
1225       * timed invokeAny(,,null) throws NPE
1226       */
1227 <    public void testTimedInvokeAnyNullTimeUnit() {
1228 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1227 >    public void testTimedInvokeAnyNullTimeUnit() throws Exception {
1228 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1229          try {
1230              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1231              l.add(new StringTask());
1232              e.invokeAny(l, MEDIUM_DELAY_MS, null);
1233 +            shouldThrow();
1234          } catch (NullPointerException success) {
1294        } catch(Exception ex) {
1295            unexpectedException();
1235          } finally {
1236              joinPool(e);
1237          }
# Line 1301 | Line 1240 | public class ThreadPoolExecutorTest exte
1240      /**
1241       * timed invokeAny(empty collection) throws IAE
1242       */
1243 <    public void testTimedInvokeAny2() {
1244 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1243 >    public void testTimedInvokeAny2() throws Exception {
1244 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1245          try {
1246 <            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1246 >            e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1247 >            shouldThrow();
1248          } catch (IllegalArgumentException success) {
1309        } catch(Exception ex) {
1310            unexpectedException();
1249          } finally {
1250              joinPool(e);
1251          }
# Line 1316 | Line 1254 | public class ThreadPoolExecutorTest exte
1254      /**
1255       * timed invokeAny(c) throws NPE if c has null elements
1256       */
1257 <    public void testTimedInvokeAny3() {
1258 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1257 >    public void testTimedInvokeAny3() throws Exception {
1258 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1259          try {
1260              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1261              l.add(new StringTask());
1262              l.add(null);
1263 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1263 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1264 >            shouldThrow();
1265          } catch (NullPointerException success) {
1327        } catch(Exception ex) {
1328            ex.printStackTrace();
1329            unexpectedException();
1266          } finally {
1267              joinPool(e);
1268          }
# Line 1335 | Line 1271 | public class ThreadPoolExecutorTest exte
1271      /**
1272       * timed invokeAny(c) throws ExecutionException if no task completes
1273       */
1274 <    public void testTimedInvokeAny4() {
1275 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1274 >    public void testTimedInvokeAny4() throws Exception {
1275 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1276          try {
1277              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1278              l.add(new NPETask());
1279 <            e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1280 <        } catch(ExecutionException success) {
1281 <        } catch(Exception ex) {
1282 <            unexpectedException();
1279 >            e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1280 >            shouldThrow();
1281 >        } catch (ExecutionException success) {
1282 >            assertTrue(success.getCause() instanceof NullPointerException);
1283          } finally {
1284              joinPool(e);
1285          }
# Line 1352 | Line 1288 | public class ThreadPoolExecutorTest exte
1288      /**
1289       * timed invokeAny(c) returns result of some task
1290       */
1291 <    public void testTimedInvokeAny5() {
1292 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1291 >    public void testTimedInvokeAny5() throws Exception {
1292 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1293          try {
1294              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1295              l.add(new StringTask());
1296              l.add(new StringTask());
1297 <            String result = e.invokeAny(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1297 >            String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS);
1298              assertSame(TEST_STRING, result);
1363        } catch (ExecutionException success) {
1364        } catch(Exception ex) {
1365            unexpectedException();
1299          } finally {
1300              joinPool(e);
1301          }
# Line 1371 | Line 1304 | public class ThreadPoolExecutorTest exte
1304      /**
1305       * timed invokeAll(null) throws NPE
1306       */
1307 <    public void testTimedInvokeAll1() {
1308 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1307 >    public void testTimedInvokeAll1() throws Exception {
1308 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1309          try {
1310 <            e.invokeAll(null, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1310 >            e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS);
1311 >            shouldThrow();
1312          } catch (NullPointerException success) {
1379        } catch(Exception ex) {
1380            unexpectedException();
1313          } finally {
1314              joinPool(e);
1315          }
# Line 1386 | Line 1318 | public class ThreadPoolExecutorTest exte
1318      /**
1319       * timed invokeAll(,,null) throws NPE
1320       */
1321 <    public void testTimedInvokeAllNullTimeUnit() {
1322 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1321 >    public void testTimedInvokeAllNullTimeUnit() throws Exception {
1322 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1323          try {
1324              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1325              l.add(new StringTask());
1326              e.invokeAll(l, MEDIUM_DELAY_MS, null);
1327 +            shouldThrow();
1328          } catch (NullPointerException success) {
1396        } catch(Exception ex) {
1397            unexpectedException();
1329          } finally {
1330              joinPool(e);
1331          }
# Line 1403 | Line 1334 | public class ThreadPoolExecutorTest exte
1334      /**
1335       * timed invokeAll(empty collection) returns empty collection
1336       */
1337 <    public void testTimedInvokeAll2() {
1338 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1337 >    public void testTimedInvokeAll2() throws InterruptedException {
1338 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1339          try {
1340 <            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1340 >            List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS);
1341              assertTrue(r.isEmpty());
1411        } catch(Exception ex) {
1412            unexpectedException();
1342          } finally {
1343              joinPool(e);
1344          }
# Line 1418 | Line 1347 | public class ThreadPoolExecutorTest exte
1347      /**
1348       * timed invokeAll(c) throws NPE if c has null elements
1349       */
1350 <    public void testTimedInvokeAll3() {
1351 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1350 >    public void testTimedInvokeAll3() throws Exception {
1351 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1352          try {
1353              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1354              l.add(new StringTask());
1355              l.add(null);
1356 <            e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1356 >            e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1357 >            shouldThrow();
1358          } catch (NullPointerException success) {
1429        } catch(Exception ex) {
1430            unexpectedException();
1359          } finally {
1360              joinPool(e);
1361          }
# Line 1436 | Line 1364 | public class ThreadPoolExecutorTest exte
1364      /**
1365       * get of element of invokeAll(c) throws exception on failed task
1366       */
1367 <    public void testTimedInvokeAll4() {
1368 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1367 >    public void testTimedInvokeAll4() throws Exception {
1368 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1369          try {
1370              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1371              l.add(new NPETask());
1372 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1372 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1373              assertEquals(1, result.size());
1374 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1375 <                it.next().get();
1376 <        } catch(ExecutionException success) {
1377 <        } catch(Exception ex) {
1378 <            unexpectedException();
1374 >            for (Future<String> future : result)
1375 >                future.get();
1376 >            shouldThrow();
1377 >        } catch (ExecutionException success) {
1378 >            assertTrue(success.getCause() instanceof NullPointerException);
1379          } finally {
1380              joinPool(e);
1381          }
# Line 1456 | Line 1384 | public class ThreadPoolExecutorTest exte
1384      /**
1385       * timed invokeAll(c) returns results of all completed tasks
1386       */
1387 <    public void testTimedInvokeAll5() {
1388 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1387 >    public void testTimedInvokeAll5() throws Exception {
1388 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1389          try {
1390              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1391              l.add(new StringTask());
1392              l.add(new StringTask());
1393 <            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, TimeUnit.MILLISECONDS);
1393 >            List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS);
1394              assertEquals(2, result.size());
1395 <            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1395 >            for (Iterator<Future<String>> it = result.iterator(); it.hasNext();)
1396                  assertSame(TEST_STRING, it.next().get());
1469        } catch (ExecutionException success) {
1470        } catch(Exception ex) {
1471            unexpectedException();
1397          } finally {
1398              joinPool(e);
1399          }
# Line 1477 | Line 1402 | public class ThreadPoolExecutorTest exte
1402      /**
1403       * timed invokeAll(c) cancels tasks not completed by timeout
1404       */
1405 <    public void testTimedInvokeAll6() {
1406 <        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1405 >    public void testTimedInvokeAll6() throws Exception {
1406 >        ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1407          try {
1408              ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1409              l.add(new StringTask());
1410              l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING));
1411 <            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, TimeUnit.MILLISECONDS);
1412 <            assertEquals(2, result.size());
1413 <            Iterator<Future<String>> it = result.iterator();
1411 >            l.add(new StringTask());
1412 >            List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS);
1413 >            assertEquals(3, result.size());
1414 >            Iterator<Future<String>> it = result.iterator();
1415              Future<String> f1 = it.next();
1416              Future<String> f2 = it.next();
1417 +            Future<String> f3 = it.next();
1418              assertTrue(f1.isDone());
1492            assertFalse(f1.isCancelled());
1419              assertTrue(f2.isDone());
1420 +            assertTrue(f3.isDone());
1421 +            assertFalse(f1.isCancelled());
1422              assertTrue(f2.isCancelled());
1495        } catch(Exception ex) {
1496            unexpectedException();
1423          } finally {
1424              joinPool(e);
1425          }
1426      }
1427  
1428 +    /**
1429 +     * Execution continues if there is at least one thread even if
1430 +     * thread factory fails to create more
1431 +     */
1432 +    public void testFailingThreadFactory() throws InterruptedException {
1433 +        ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory());
1434 +        try {
1435 +            ArrayList<Callable<String>> l = new ArrayList<Callable<String>>();
1436 +            for (int k = 0; k < 100; ++k) {
1437 +                e.execute(new NoOpRunnable());
1438 +            }
1439 +            Thread.sleep(LONG_DELAY_MS);
1440 +        } finally {
1441 +            joinPool(e);
1442 +        }
1443 +    }
1444 +
1445 +    /**
1446 +     * allowsCoreThreadTimeOut is by default false.
1447 +     */
1448 +    public void testAllowsCoreThreadTimeOut() {
1449 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1450 +        assertFalse(tpe.allowsCoreThreadTimeOut());
1451 +        joinPool(tpe);
1452 +    }
1453 +
1454 +    /**
1455 +     * allowCoreThreadTimeOut(true) causes idle threads to time out
1456 +     */
1457 +    public void testAllowCoreThreadTimeOut_true() throws InterruptedException {
1458 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1459 +        tpe.allowCoreThreadTimeOut(true);
1460 +        tpe.execute(new NoOpRunnable());
1461 +        try {
1462 +            Thread.sleep(MEDIUM_DELAY_MS);
1463 +            assertEquals(0, tpe.getPoolSize());
1464 +        } finally {
1465 +            joinPool(tpe);
1466 +        }
1467 +    }
1468 +
1469 +    /**
1470 +     * allowCoreThreadTimeOut(false) causes idle threads not to time out
1471 +     */
1472 +    public void testAllowCoreThreadTimeOut_false() throws InterruptedException {
1473 +        ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10));
1474 +        tpe.allowCoreThreadTimeOut(false);
1475 +        tpe.execute(new NoOpRunnable());
1476 +        try {
1477 +            Thread.sleep(MEDIUM_DELAY_MS);
1478 +            assertTrue(tpe.getPoolSize() >= 1);
1479 +        } finally {
1480 +            joinPool(tpe);
1481 +        }
1482 +    }
1483 +
1484 +    /**
1485 +     * execute allows the same task to be submitted multiple times, even
1486 +     * if rejected
1487 +     */
1488 +    public void testRejectedRecycledTask() throws InterruptedException {
1489 +        final int nTasks = 1000;
1490 +        final AtomicInteger nRun = new AtomicInteger(0);
1491 +        final Runnable recycledTask = new Runnable() {
1492 +                public void run() {
1493 +                    nRun.getAndIncrement();
1494 +                } };
1495 +        final ThreadPoolExecutor p =
1496 +            new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS,
1497 +                                   new ArrayBlockingQueue(30));
1498 +        try {
1499 +            for (int i = 0; i < nTasks; ++i) {
1500 +                for (;;) {
1501 +                    try {
1502 +                        p.execute(recycledTask);
1503 +                        break;
1504 +                    }
1505 +                    catch (RejectedExecutionException ignore) {
1506 +                    }
1507 +                }
1508 +            }
1509 +            Thread.sleep(5000); // enough time to run all tasks
1510 +            assertEquals(nRun.get(), nTasks);
1511 +        } finally {
1512 +            p.shutdown();
1513 +        }
1514 +    }
1515  
1516   }

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines