ViewVC Help
View File | Revision Log | Show Annotations | Download File | Root Listing
root/jsr166/jsr166/src/test/tck/ThreadPoolExecutorTest.java
Revision: 1.26
Committed: Mon Nov 16 05:30:08 2009 UTC (14 years, 6 months ago) by jsr166
Branch: MAIN
Changes since 1.25: +66 -66 lines
Log Message:
whitespace

File Contents

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