1 |
/* |
2 |
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
* Expert Group and released to the public domain, as explained at |
4 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
* Pat Fisher, Mike Judd. |
7 |
*/ |
8 |
|
9 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
13 |
import java.util.ArrayList; |
14 |
import java.util.List; |
15 |
import java.util.concurrent.ArrayBlockingQueue; |
16 |
import java.util.concurrent.BlockingQueue; |
17 |
import java.util.concurrent.Callable; |
18 |
import java.util.concurrent.CancellationException; |
19 |
import java.util.concurrent.CountDownLatch; |
20 |
import java.util.concurrent.ExecutionException; |
21 |
import java.util.concurrent.Executors; |
22 |
import java.util.concurrent.ExecutorService; |
23 |
import java.util.concurrent.Future; |
24 |
import java.util.concurrent.FutureTask; |
25 |
import java.util.concurrent.LinkedBlockingQueue; |
26 |
import java.util.concurrent.RejectedExecutionException; |
27 |
import java.util.concurrent.RejectedExecutionHandler; |
28 |
import java.util.concurrent.SynchronousQueue; |
29 |
import java.util.concurrent.ThreadFactory; |
30 |
import java.util.concurrent.ThreadPoolExecutor; |
31 |
import java.util.concurrent.TimeUnit; |
32 |
|
33 |
import junit.framework.Test; |
34 |
import junit.framework.TestSuite; |
35 |
|
36 |
public class ThreadPoolExecutorTest extends JSR166TestCase { |
37 |
public static void main(String[] args) { |
38 |
main(suite(), args); |
39 |
} |
40 |
public static Test suite() { |
41 |
return new TestSuite(ThreadPoolExecutorTest.class); |
42 |
} |
43 |
|
44 |
static class ExtendedTPE extends ThreadPoolExecutor { |
45 |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
46 |
final CountDownLatch afterCalled = new CountDownLatch(1); |
47 |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
48 |
|
49 |
public ExtendedTPE() { |
50 |
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
51 |
} |
52 |
protected void beforeExecute(Thread t, Runnable r) { |
53 |
beforeCalled.countDown(); |
54 |
} |
55 |
protected void afterExecute(Runnable r, Throwable t) { |
56 |
afterCalled.countDown(); |
57 |
} |
58 |
protected void terminated() { |
59 |
terminatedCalled.countDown(); |
60 |
} |
61 |
|
62 |
public boolean beforeCalled() { |
63 |
return beforeCalled.getCount() == 0; |
64 |
} |
65 |
public boolean afterCalled() { |
66 |
return afterCalled.getCount() == 0; |
67 |
} |
68 |
public boolean terminatedCalled() { |
69 |
return terminatedCalled.getCount() == 0; |
70 |
} |
71 |
} |
72 |
|
73 |
static class FailingThreadFactory implements ThreadFactory { |
74 |
int calls = 0; |
75 |
public Thread newThread(Runnable r) { |
76 |
if (++calls > 1) return null; |
77 |
return new Thread(r); |
78 |
} |
79 |
} |
80 |
|
81 |
/** |
82 |
* execute successfully executes a runnable |
83 |
*/ |
84 |
public void testExecute() throws InterruptedException { |
85 |
final ThreadPoolExecutor p = |
86 |
new ThreadPoolExecutor(1, 1, |
87 |
LONG_DELAY_MS, MILLISECONDS, |
88 |
new ArrayBlockingQueue<Runnable>(10)); |
89 |
final CountDownLatch done = new CountDownLatch(1); |
90 |
final Runnable task = new CheckedRunnable() { |
91 |
public void realRun() { |
92 |
done.countDown(); |
93 |
}}; |
94 |
try { |
95 |
p.execute(task); |
96 |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
97 |
} finally { |
98 |
joinPool(p); |
99 |
} |
100 |
} |
101 |
|
102 |
/** |
103 |
* getActiveCount increases but doesn't overestimate, when a |
104 |
* thread becomes active |
105 |
*/ |
106 |
public void testGetActiveCount() throws InterruptedException { |
107 |
final ThreadPoolExecutor p = |
108 |
new ThreadPoolExecutor(2, 2, |
109 |
LONG_DELAY_MS, MILLISECONDS, |
110 |
new ArrayBlockingQueue<Runnable>(10)); |
111 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
112 |
final CountDownLatch done = new CountDownLatch(1); |
113 |
try { |
114 |
assertEquals(0, p.getActiveCount()); |
115 |
p.execute(new CheckedRunnable() { |
116 |
public void realRun() throws InterruptedException { |
117 |
threadStarted.countDown(); |
118 |
assertEquals(1, p.getActiveCount()); |
119 |
done.await(); |
120 |
}}); |
121 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
122 |
assertEquals(1, p.getActiveCount()); |
123 |
} finally { |
124 |
done.countDown(); |
125 |
joinPool(p); |
126 |
} |
127 |
} |
128 |
|
129 |
/** |
130 |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
131 |
*/ |
132 |
public void testPrestartCoreThread() { |
133 |
final ThreadPoolExecutor p = |
134 |
new ThreadPoolExecutor(2, 2, |
135 |
LONG_DELAY_MS, MILLISECONDS, |
136 |
new ArrayBlockingQueue<Runnable>(10)); |
137 |
assertEquals(0, p.getPoolSize()); |
138 |
assertTrue(p.prestartCoreThread()); |
139 |
assertEquals(1, p.getPoolSize()); |
140 |
assertTrue(p.prestartCoreThread()); |
141 |
assertEquals(2, p.getPoolSize()); |
142 |
assertFalse(p.prestartCoreThread()); |
143 |
assertEquals(2, p.getPoolSize()); |
144 |
joinPool(p); |
145 |
} |
146 |
|
147 |
/** |
148 |
* prestartAllCoreThreads starts all corePoolSize threads |
149 |
*/ |
150 |
public void testPrestartAllCoreThreads() { |
151 |
final ThreadPoolExecutor p = |
152 |
new ThreadPoolExecutor(2, 2, |
153 |
LONG_DELAY_MS, MILLISECONDS, |
154 |
new ArrayBlockingQueue<Runnable>(10)); |
155 |
assertEquals(0, p.getPoolSize()); |
156 |
p.prestartAllCoreThreads(); |
157 |
assertEquals(2, p.getPoolSize()); |
158 |
p.prestartAllCoreThreads(); |
159 |
assertEquals(2, p.getPoolSize()); |
160 |
joinPool(p); |
161 |
} |
162 |
|
163 |
/** |
164 |
* getCompletedTaskCount increases, but doesn't overestimate, |
165 |
* when tasks complete |
166 |
*/ |
167 |
public void testGetCompletedTaskCount() throws InterruptedException { |
168 |
final ThreadPoolExecutor p = |
169 |
new ThreadPoolExecutor(2, 2, |
170 |
LONG_DELAY_MS, MILLISECONDS, |
171 |
new ArrayBlockingQueue<Runnable>(10)); |
172 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
173 |
final CountDownLatch threadProceed = new CountDownLatch(1); |
174 |
final CountDownLatch threadDone = new CountDownLatch(1); |
175 |
try { |
176 |
assertEquals(0, p.getCompletedTaskCount()); |
177 |
p.execute(new CheckedRunnable() { |
178 |
public void realRun() throws InterruptedException { |
179 |
threadStarted.countDown(); |
180 |
assertEquals(0, p.getCompletedTaskCount()); |
181 |
threadProceed.await(); |
182 |
threadDone.countDown(); |
183 |
}}); |
184 |
await(threadStarted); |
185 |
assertEquals(0, p.getCompletedTaskCount()); |
186 |
threadProceed.countDown(); |
187 |
threadDone.await(); |
188 |
long startTime = System.nanoTime(); |
189 |
while (p.getCompletedTaskCount() != 1) { |
190 |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
191 |
fail("timed out"); |
192 |
Thread.yield(); |
193 |
} |
194 |
} finally { |
195 |
joinPool(p); |
196 |
} |
197 |
} |
198 |
|
199 |
/** |
200 |
* getCorePoolSize returns size given in constructor if not otherwise set |
201 |
*/ |
202 |
public void testGetCorePoolSize() { |
203 |
final ThreadPoolExecutor p = |
204 |
new ThreadPoolExecutor(1, 1, |
205 |
LONG_DELAY_MS, MILLISECONDS, |
206 |
new ArrayBlockingQueue<Runnable>(10)); |
207 |
assertEquals(1, p.getCorePoolSize()); |
208 |
joinPool(p); |
209 |
} |
210 |
|
211 |
/** |
212 |
* getKeepAliveTime returns value given in constructor if not otherwise set |
213 |
*/ |
214 |
public void testGetKeepAliveTime() { |
215 |
final ThreadPoolExecutor p = |
216 |
new ThreadPoolExecutor(2, 2, |
217 |
1000, MILLISECONDS, |
218 |
new ArrayBlockingQueue<Runnable>(10)); |
219 |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
220 |
joinPool(p); |
221 |
} |
222 |
|
223 |
/** |
224 |
* getThreadFactory returns factory in constructor if not set |
225 |
*/ |
226 |
public void testGetThreadFactory() { |
227 |
ThreadFactory tf = new SimpleThreadFactory(); |
228 |
final ThreadPoolExecutor p = |
229 |
new ThreadPoolExecutor(1, 2, |
230 |
LONG_DELAY_MS, MILLISECONDS, |
231 |
new ArrayBlockingQueue<Runnable>(10), |
232 |
tf, |
233 |
new NoOpREHandler()); |
234 |
assertSame(tf, p.getThreadFactory()); |
235 |
joinPool(p); |
236 |
} |
237 |
|
238 |
/** |
239 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
240 |
*/ |
241 |
public void testSetThreadFactory() { |
242 |
final ThreadPoolExecutor p = |
243 |
new ThreadPoolExecutor(1, 2, |
244 |
LONG_DELAY_MS, MILLISECONDS, |
245 |
new ArrayBlockingQueue<Runnable>(10)); |
246 |
ThreadFactory tf = new SimpleThreadFactory(); |
247 |
p.setThreadFactory(tf); |
248 |
assertSame(tf, p.getThreadFactory()); |
249 |
joinPool(p); |
250 |
} |
251 |
|
252 |
/** |
253 |
* setThreadFactory(null) throws NPE |
254 |
*/ |
255 |
public void testSetThreadFactoryNull() { |
256 |
final ThreadPoolExecutor p = |
257 |
new ThreadPoolExecutor(1, 2, |
258 |
LONG_DELAY_MS, MILLISECONDS, |
259 |
new ArrayBlockingQueue<Runnable>(10)); |
260 |
try { |
261 |
p.setThreadFactory(null); |
262 |
shouldThrow(); |
263 |
} catch (NullPointerException success) { |
264 |
} finally { |
265 |
joinPool(p); |
266 |
} |
267 |
} |
268 |
|
269 |
/** |
270 |
* getRejectedExecutionHandler returns handler in constructor if not set |
271 |
*/ |
272 |
public void testGetRejectedExecutionHandler() { |
273 |
final RejectedExecutionHandler h = new NoOpREHandler(); |
274 |
final ThreadPoolExecutor p = |
275 |
new ThreadPoolExecutor(1, 2, |
276 |
LONG_DELAY_MS, MILLISECONDS, |
277 |
new ArrayBlockingQueue<Runnable>(10), |
278 |
h); |
279 |
assertSame(h, p.getRejectedExecutionHandler()); |
280 |
joinPool(p); |
281 |
} |
282 |
|
283 |
/** |
284 |
* setRejectedExecutionHandler sets the handler returned by |
285 |
* getRejectedExecutionHandler |
286 |
*/ |
287 |
public void testSetRejectedExecutionHandler() { |
288 |
final ThreadPoolExecutor p = |
289 |
new ThreadPoolExecutor(1, 2, |
290 |
LONG_DELAY_MS, MILLISECONDS, |
291 |
new ArrayBlockingQueue<Runnable>(10)); |
292 |
RejectedExecutionHandler h = new NoOpREHandler(); |
293 |
p.setRejectedExecutionHandler(h); |
294 |
assertSame(h, p.getRejectedExecutionHandler()); |
295 |
joinPool(p); |
296 |
} |
297 |
|
298 |
/** |
299 |
* setRejectedExecutionHandler(null) throws NPE |
300 |
*/ |
301 |
public void testSetRejectedExecutionHandlerNull() { |
302 |
final ThreadPoolExecutor p = |
303 |
new ThreadPoolExecutor(1, 2, |
304 |
LONG_DELAY_MS, MILLISECONDS, |
305 |
new ArrayBlockingQueue<Runnable>(10)); |
306 |
try { |
307 |
p.setRejectedExecutionHandler(null); |
308 |
shouldThrow(); |
309 |
} catch (NullPointerException success) { |
310 |
} finally { |
311 |
joinPool(p); |
312 |
} |
313 |
} |
314 |
|
315 |
/** |
316 |
* getLargestPoolSize increases, but doesn't overestimate, when |
317 |
* multiple threads active |
318 |
*/ |
319 |
public void testGetLargestPoolSize() throws InterruptedException { |
320 |
final int THREADS = 3; |
321 |
final ThreadPoolExecutor p = |
322 |
new ThreadPoolExecutor(THREADS, THREADS, |
323 |
LONG_DELAY_MS, MILLISECONDS, |
324 |
new ArrayBlockingQueue<Runnable>(10)); |
325 |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
326 |
final CountDownLatch done = new CountDownLatch(1); |
327 |
try { |
328 |
assertEquals(0, p.getLargestPoolSize()); |
329 |
for (int i = 0; i < THREADS; i++) |
330 |
p.execute(new CheckedRunnable() { |
331 |
public void realRun() throws InterruptedException { |
332 |
threadsStarted.countDown(); |
333 |
done.await(); |
334 |
assertEquals(THREADS, p.getLargestPoolSize()); |
335 |
}}); |
336 |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
337 |
assertEquals(THREADS, p.getLargestPoolSize()); |
338 |
} finally { |
339 |
done.countDown(); |
340 |
joinPool(p); |
341 |
assertEquals(THREADS, p.getLargestPoolSize()); |
342 |
} |
343 |
} |
344 |
|
345 |
/** |
346 |
* getMaximumPoolSize returns value given in constructor if not |
347 |
* otherwise set |
348 |
*/ |
349 |
public void testGetMaximumPoolSize() { |
350 |
final ThreadPoolExecutor p = |
351 |
new ThreadPoolExecutor(2, 3, |
352 |
LONG_DELAY_MS, MILLISECONDS, |
353 |
new ArrayBlockingQueue<Runnable>(10)); |
354 |
assertEquals(3, p.getMaximumPoolSize()); |
355 |
joinPool(p); |
356 |
} |
357 |
|
358 |
/** |
359 |
* getPoolSize increases, but doesn't overestimate, when threads |
360 |
* become active |
361 |
*/ |
362 |
public void testGetPoolSize() throws InterruptedException { |
363 |
final ThreadPoolExecutor p = |
364 |
new ThreadPoolExecutor(1, 1, |
365 |
LONG_DELAY_MS, MILLISECONDS, |
366 |
new ArrayBlockingQueue<Runnable>(10)); |
367 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
368 |
final CountDownLatch done = new CountDownLatch(1); |
369 |
try { |
370 |
assertEquals(0, p.getPoolSize()); |
371 |
p.execute(new CheckedRunnable() { |
372 |
public void realRun() throws InterruptedException { |
373 |
threadStarted.countDown(); |
374 |
assertEquals(1, p.getPoolSize()); |
375 |
done.await(); |
376 |
}}); |
377 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
378 |
assertEquals(1, p.getPoolSize()); |
379 |
} finally { |
380 |
done.countDown(); |
381 |
joinPool(p); |
382 |
} |
383 |
} |
384 |
|
385 |
/** |
386 |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
387 |
*/ |
388 |
public void testGetTaskCount() throws InterruptedException { |
389 |
final ThreadPoolExecutor p = |
390 |
new ThreadPoolExecutor(1, 1, |
391 |
LONG_DELAY_MS, MILLISECONDS, |
392 |
new ArrayBlockingQueue<Runnable>(10)); |
393 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
394 |
final CountDownLatch done = new CountDownLatch(1); |
395 |
try { |
396 |
assertEquals(0, p.getTaskCount()); |
397 |
p.execute(new CheckedRunnable() { |
398 |
public void realRun() throws InterruptedException { |
399 |
threadStarted.countDown(); |
400 |
assertEquals(1, p.getTaskCount()); |
401 |
done.await(); |
402 |
}}); |
403 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
404 |
assertEquals(1, p.getTaskCount()); |
405 |
} finally { |
406 |
done.countDown(); |
407 |
joinPool(p); |
408 |
} |
409 |
} |
410 |
|
411 |
/** |
412 |
* isShutdown is false before shutdown, true after |
413 |
*/ |
414 |
public void testIsShutdown() { |
415 |
final ThreadPoolExecutor p = |
416 |
new ThreadPoolExecutor(1, 1, |
417 |
LONG_DELAY_MS, MILLISECONDS, |
418 |
new ArrayBlockingQueue<Runnable>(10)); |
419 |
assertFalse(p.isShutdown()); |
420 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
421 |
assertTrue(p.isShutdown()); |
422 |
joinPool(p); |
423 |
} |
424 |
|
425 |
/** |
426 |
* awaitTermination on a non-shutdown pool times out |
427 |
*/ |
428 |
public void testAwaitTermination_timesOut() throws InterruptedException { |
429 |
final ThreadPoolExecutor p = |
430 |
new ThreadPoolExecutor(1, 1, |
431 |
LONG_DELAY_MS, MILLISECONDS, |
432 |
new ArrayBlockingQueue<Runnable>(10)); |
433 |
assertFalse(p.isTerminated()); |
434 |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
435 |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
436 |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
437 |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
438 |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
439 |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
440 |
long timeoutNanos = 999999L; |
441 |
long startTime = System.nanoTime(); |
442 |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
443 |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
444 |
assertFalse(p.isTerminated()); |
445 |
startTime = System.nanoTime(); |
446 |
long timeoutMillis = timeoutMillis(); |
447 |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
448 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
449 |
assertFalse(p.isTerminated()); |
450 |
p.shutdown(); |
451 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
452 |
assertTrue(p.isTerminated()); |
453 |
} |
454 |
|
455 |
/** |
456 |
* isTerminated is false before termination, true after |
457 |
*/ |
458 |
public void testIsTerminated() throws InterruptedException { |
459 |
final ThreadPoolExecutor p = |
460 |
new ThreadPoolExecutor(1, 1, |
461 |
LONG_DELAY_MS, MILLISECONDS, |
462 |
new ArrayBlockingQueue<Runnable>(10)); |
463 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
464 |
final CountDownLatch done = new CountDownLatch(1); |
465 |
assertFalse(p.isTerminated()); |
466 |
try { |
467 |
p.execute(new CheckedRunnable() { |
468 |
public void realRun() throws InterruptedException { |
469 |
assertFalse(p.isTerminated()); |
470 |
threadStarted.countDown(); |
471 |
done.await(); |
472 |
}}); |
473 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
474 |
assertFalse(p.isTerminating()); |
475 |
done.countDown(); |
476 |
} finally { |
477 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
478 |
} |
479 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
480 |
assertTrue(p.isTerminated()); |
481 |
} |
482 |
|
483 |
/** |
484 |
* isTerminating is not true when running or when terminated |
485 |
*/ |
486 |
public void testIsTerminating() throws InterruptedException { |
487 |
final ThreadPoolExecutor p = |
488 |
new ThreadPoolExecutor(1, 1, |
489 |
LONG_DELAY_MS, MILLISECONDS, |
490 |
new ArrayBlockingQueue<Runnable>(10)); |
491 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
492 |
final CountDownLatch done = new CountDownLatch(1); |
493 |
try { |
494 |
assertFalse(p.isTerminating()); |
495 |
p.execute(new CheckedRunnable() { |
496 |
public void realRun() throws InterruptedException { |
497 |
assertFalse(p.isTerminating()); |
498 |
threadStarted.countDown(); |
499 |
done.await(); |
500 |
}}); |
501 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
502 |
assertFalse(p.isTerminating()); |
503 |
done.countDown(); |
504 |
} finally { |
505 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
506 |
} |
507 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
508 |
assertTrue(p.isTerminated()); |
509 |
assertFalse(p.isTerminating()); |
510 |
} |
511 |
|
512 |
/** |
513 |
* getQueue returns the work queue, which contains queued tasks |
514 |
*/ |
515 |
public void testGetQueue() throws InterruptedException { |
516 |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
517 |
final ThreadPoolExecutor p = |
518 |
new ThreadPoolExecutor(1, 1, |
519 |
LONG_DELAY_MS, MILLISECONDS, |
520 |
q); |
521 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
522 |
final CountDownLatch done = new CountDownLatch(1); |
523 |
try { |
524 |
FutureTask[] tasks = new FutureTask[5]; |
525 |
for (int i = 0; i < tasks.length; i++) { |
526 |
Callable task = new CheckedCallable<Boolean>() { |
527 |
public Boolean realCall() throws InterruptedException { |
528 |
threadStarted.countDown(); |
529 |
assertSame(q, p.getQueue()); |
530 |
done.await(); |
531 |
return Boolean.TRUE; |
532 |
}}; |
533 |
tasks[i] = new FutureTask(task); |
534 |
p.execute(tasks[i]); |
535 |
} |
536 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
537 |
assertSame(q, p.getQueue()); |
538 |
assertFalse(q.contains(tasks[0])); |
539 |
assertTrue(q.contains(tasks[tasks.length - 1])); |
540 |
assertEquals(tasks.length - 1, q.size()); |
541 |
} finally { |
542 |
done.countDown(); |
543 |
joinPool(p); |
544 |
} |
545 |
} |
546 |
|
547 |
/** |
548 |
* remove(task) removes queued task, and fails to remove active task |
549 |
*/ |
550 |
public void testRemove() throws InterruptedException { |
551 |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
552 |
final ThreadPoolExecutor p = |
553 |
new ThreadPoolExecutor(1, 1, |
554 |
LONG_DELAY_MS, MILLISECONDS, |
555 |
q); |
556 |
Runnable[] tasks = new Runnable[5]; |
557 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
558 |
final CountDownLatch done = new CountDownLatch(1); |
559 |
try { |
560 |
for (int i = 0; i < tasks.length; i++) { |
561 |
tasks[i] = new CheckedRunnable() { |
562 |
public void realRun() throws InterruptedException { |
563 |
threadStarted.countDown(); |
564 |
done.await(); |
565 |
}}; |
566 |
p.execute(tasks[i]); |
567 |
} |
568 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
569 |
assertFalse(p.remove(tasks[0])); |
570 |
assertTrue(q.contains(tasks[4])); |
571 |
assertTrue(q.contains(tasks[3])); |
572 |
assertTrue(p.remove(tasks[4])); |
573 |
assertFalse(p.remove(tasks[4])); |
574 |
assertFalse(q.contains(tasks[4])); |
575 |
assertTrue(q.contains(tasks[3])); |
576 |
assertTrue(p.remove(tasks[3])); |
577 |
assertFalse(q.contains(tasks[3])); |
578 |
} finally { |
579 |
done.countDown(); |
580 |
joinPool(p); |
581 |
} |
582 |
} |
583 |
|
584 |
/** |
585 |
* purge removes cancelled tasks from the queue |
586 |
*/ |
587 |
public void testPurge() throws InterruptedException { |
588 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
589 |
final CountDownLatch done = new CountDownLatch(1); |
590 |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
591 |
final ThreadPoolExecutor p = |
592 |
new ThreadPoolExecutor(1, 1, |
593 |
LONG_DELAY_MS, MILLISECONDS, |
594 |
q); |
595 |
FutureTask[] tasks = new FutureTask[5]; |
596 |
try { |
597 |
for (int i = 0; i < tasks.length; i++) { |
598 |
Callable task = new CheckedCallable<Boolean>() { |
599 |
public Boolean realCall() throws InterruptedException { |
600 |
threadStarted.countDown(); |
601 |
done.await(); |
602 |
return Boolean.TRUE; |
603 |
}}; |
604 |
tasks[i] = new FutureTask(task); |
605 |
p.execute(tasks[i]); |
606 |
} |
607 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
608 |
assertEquals(tasks.length, p.getTaskCount()); |
609 |
assertEquals(tasks.length - 1, q.size()); |
610 |
assertEquals(1L, p.getActiveCount()); |
611 |
assertEquals(0L, p.getCompletedTaskCount()); |
612 |
tasks[4].cancel(true); |
613 |
tasks[3].cancel(false); |
614 |
p.purge(); |
615 |
assertEquals(tasks.length - 3, q.size()); |
616 |
assertEquals(tasks.length - 2, p.getTaskCount()); |
617 |
p.purge(); // Nothing to do |
618 |
assertEquals(tasks.length - 3, q.size()); |
619 |
assertEquals(tasks.length - 2, p.getTaskCount()); |
620 |
} finally { |
621 |
done.countDown(); |
622 |
joinPool(p); |
623 |
} |
624 |
} |
625 |
|
626 |
/** |
627 |
* shutdownNow returns a list containing tasks that were not run, |
628 |
* and those tasks are drained from the queue |
629 |
*/ |
630 |
public void testShutdownNow() { |
631 |
final ThreadPoolExecutor p = |
632 |
new ThreadPoolExecutor(1, 1, |
633 |
LONG_DELAY_MS, MILLISECONDS, |
634 |
new ArrayBlockingQueue<Runnable>(10)); |
635 |
List l; |
636 |
try { |
637 |
for (int i = 0; i < 5; i++) |
638 |
p.execute(new MediumPossiblyInterruptedRunnable()); |
639 |
} |
640 |
finally { |
641 |
try { |
642 |
l = p.shutdownNow(); |
643 |
} catch (SecurityException ok) { return; } |
644 |
} |
645 |
assertTrue(p.isShutdown()); |
646 |
assertTrue(p.getQueue().isEmpty()); |
647 |
assertTrue(l.size() <= 4); |
648 |
} |
649 |
|
650 |
// Exception Tests |
651 |
|
652 |
/** |
653 |
* Constructor throws if corePoolSize argument is less than zero |
654 |
*/ |
655 |
public void testConstructor1() { |
656 |
try { |
657 |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
658 |
new ArrayBlockingQueue<Runnable>(10)); |
659 |
shouldThrow(); |
660 |
} catch (IllegalArgumentException success) {} |
661 |
} |
662 |
|
663 |
/** |
664 |
* Constructor throws if maximumPoolSize is less than zero |
665 |
*/ |
666 |
public void testConstructor2() { |
667 |
try { |
668 |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
669 |
new ArrayBlockingQueue<Runnable>(10)); |
670 |
shouldThrow(); |
671 |
} catch (IllegalArgumentException success) {} |
672 |
} |
673 |
|
674 |
/** |
675 |
* Constructor throws if maximumPoolSize is equal to zero |
676 |
*/ |
677 |
public void testConstructor3() { |
678 |
try { |
679 |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
680 |
new ArrayBlockingQueue<Runnable>(10)); |
681 |
shouldThrow(); |
682 |
} catch (IllegalArgumentException success) {} |
683 |
} |
684 |
|
685 |
/** |
686 |
* Constructor throws if keepAliveTime is less than zero |
687 |
*/ |
688 |
public void testConstructor4() { |
689 |
try { |
690 |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
691 |
new ArrayBlockingQueue<Runnable>(10)); |
692 |
shouldThrow(); |
693 |
} catch (IllegalArgumentException success) {} |
694 |
} |
695 |
|
696 |
/** |
697 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
698 |
*/ |
699 |
public void testConstructor5() { |
700 |
try { |
701 |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
702 |
new ArrayBlockingQueue<Runnable>(10)); |
703 |
shouldThrow(); |
704 |
} catch (IllegalArgumentException success) {} |
705 |
} |
706 |
|
707 |
/** |
708 |
* Constructor throws if workQueue is set to null |
709 |
*/ |
710 |
public void testConstructorNullPointerException() { |
711 |
try { |
712 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
713 |
(BlockingQueue) null); |
714 |
shouldThrow(); |
715 |
} catch (NullPointerException success) {} |
716 |
} |
717 |
|
718 |
/** |
719 |
* Constructor throws if corePoolSize argument is less than zero |
720 |
*/ |
721 |
public void testConstructor6() { |
722 |
try { |
723 |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
724 |
new ArrayBlockingQueue<Runnable>(10), |
725 |
new SimpleThreadFactory()); |
726 |
shouldThrow(); |
727 |
} catch (IllegalArgumentException success) {} |
728 |
} |
729 |
|
730 |
/** |
731 |
* Constructor throws if maximumPoolSize is less than zero |
732 |
*/ |
733 |
public void testConstructor7() { |
734 |
try { |
735 |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
736 |
new ArrayBlockingQueue<Runnable>(10), |
737 |
new SimpleThreadFactory()); |
738 |
shouldThrow(); |
739 |
} catch (IllegalArgumentException success) {} |
740 |
} |
741 |
|
742 |
/** |
743 |
* Constructor throws if maximumPoolSize is equal to zero |
744 |
*/ |
745 |
public void testConstructor8() { |
746 |
try { |
747 |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
748 |
new ArrayBlockingQueue<Runnable>(10), |
749 |
new SimpleThreadFactory()); |
750 |
shouldThrow(); |
751 |
} catch (IllegalArgumentException success) {} |
752 |
} |
753 |
|
754 |
/** |
755 |
* Constructor throws if keepAliveTime is less than zero |
756 |
*/ |
757 |
public void testConstructor9() { |
758 |
try { |
759 |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
760 |
new ArrayBlockingQueue<Runnable>(10), |
761 |
new SimpleThreadFactory()); |
762 |
shouldThrow(); |
763 |
} catch (IllegalArgumentException success) {} |
764 |
} |
765 |
|
766 |
/** |
767 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
768 |
*/ |
769 |
public void testConstructor10() { |
770 |
try { |
771 |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
772 |
new ArrayBlockingQueue<Runnable>(10), |
773 |
new SimpleThreadFactory()); |
774 |
shouldThrow(); |
775 |
} catch (IllegalArgumentException success) {} |
776 |
} |
777 |
|
778 |
/** |
779 |
* Constructor throws if workQueue is set to null |
780 |
*/ |
781 |
public void testConstructorNullPointerException2() { |
782 |
try { |
783 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
784 |
(BlockingQueue) null, |
785 |
new SimpleThreadFactory()); |
786 |
shouldThrow(); |
787 |
} catch (NullPointerException success) {} |
788 |
} |
789 |
|
790 |
/** |
791 |
* Constructor throws if threadFactory is set to null |
792 |
*/ |
793 |
public void testConstructorNullPointerException3() { |
794 |
try { |
795 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
796 |
new ArrayBlockingQueue<Runnable>(10), |
797 |
(ThreadFactory) null); |
798 |
shouldThrow(); |
799 |
} catch (NullPointerException success) {} |
800 |
} |
801 |
|
802 |
/** |
803 |
* Constructor throws if corePoolSize argument is less than zero |
804 |
*/ |
805 |
public void testConstructor11() { |
806 |
try { |
807 |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
808 |
new ArrayBlockingQueue<Runnable>(10), |
809 |
new NoOpREHandler()); |
810 |
shouldThrow(); |
811 |
} catch (IllegalArgumentException success) {} |
812 |
} |
813 |
|
814 |
/** |
815 |
* Constructor throws if maximumPoolSize is less than zero |
816 |
*/ |
817 |
public void testConstructor12() { |
818 |
try { |
819 |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
820 |
new ArrayBlockingQueue<Runnable>(10), |
821 |
new NoOpREHandler()); |
822 |
shouldThrow(); |
823 |
} catch (IllegalArgumentException success) {} |
824 |
} |
825 |
|
826 |
/** |
827 |
* Constructor throws if maximumPoolSize is equal to zero |
828 |
*/ |
829 |
public void testConstructor13() { |
830 |
try { |
831 |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
832 |
new ArrayBlockingQueue<Runnable>(10), |
833 |
new NoOpREHandler()); |
834 |
shouldThrow(); |
835 |
} catch (IllegalArgumentException success) {} |
836 |
} |
837 |
|
838 |
/** |
839 |
* Constructor throws if keepAliveTime is less than zero |
840 |
*/ |
841 |
public void testConstructor14() { |
842 |
try { |
843 |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
844 |
new ArrayBlockingQueue<Runnable>(10), |
845 |
new NoOpREHandler()); |
846 |
shouldThrow(); |
847 |
} catch (IllegalArgumentException success) {} |
848 |
} |
849 |
|
850 |
/** |
851 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
852 |
*/ |
853 |
public void testConstructor15() { |
854 |
try { |
855 |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
856 |
new ArrayBlockingQueue<Runnable>(10), |
857 |
new NoOpREHandler()); |
858 |
shouldThrow(); |
859 |
} catch (IllegalArgumentException success) {} |
860 |
} |
861 |
|
862 |
/** |
863 |
* Constructor throws if workQueue is set to null |
864 |
*/ |
865 |
public void testConstructorNullPointerException4() { |
866 |
try { |
867 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
868 |
(BlockingQueue) null, |
869 |
new NoOpREHandler()); |
870 |
shouldThrow(); |
871 |
} catch (NullPointerException success) {} |
872 |
} |
873 |
|
874 |
/** |
875 |
* Constructor throws if handler is set to null |
876 |
*/ |
877 |
public void testConstructorNullPointerException5() { |
878 |
try { |
879 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
880 |
new ArrayBlockingQueue<Runnable>(10), |
881 |
(RejectedExecutionHandler) null); |
882 |
shouldThrow(); |
883 |
} catch (NullPointerException success) {} |
884 |
} |
885 |
|
886 |
/** |
887 |
* Constructor throws if corePoolSize argument is less than zero |
888 |
*/ |
889 |
public void testConstructor16() { |
890 |
try { |
891 |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
892 |
new ArrayBlockingQueue<Runnable>(10), |
893 |
new SimpleThreadFactory(), |
894 |
new NoOpREHandler()); |
895 |
shouldThrow(); |
896 |
} catch (IllegalArgumentException success) {} |
897 |
} |
898 |
|
899 |
/** |
900 |
* Constructor throws if maximumPoolSize is less than zero |
901 |
*/ |
902 |
public void testConstructor17() { |
903 |
try { |
904 |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
905 |
new ArrayBlockingQueue<Runnable>(10), |
906 |
new SimpleThreadFactory(), |
907 |
new NoOpREHandler()); |
908 |
shouldThrow(); |
909 |
} catch (IllegalArgumentException success) {} |
910 |
} |
911 |
|
912 |
/** |
913 |
* Constructor throws if maximumPoolSize is equal to zero |
914 |
*/ |
915 |
public void testConstructor18() { |
916 |
try { |
917 |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
918 |
new ArrayBlockingQueue<Runnable>(10), |
919 |
new SimpleThreadFactory(), |
920 |
new NoOpREHandler()); |
921 |
shouldThrow(); |
922 |
} catch (IllegalArgumentException success) {} |
923 |
} |
924 |
|
925 |
/** |
926 |
* Constructor throws if keepAliveTime is less than zero |
927 |
*/ |
928 |
public void testConstructor19() { |
929 |
try { |
930 |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
931 |
new ArrayBlockingQueue<Runnable>(10), |
932 |
new SimpleThreadFactory(), |
933 |
new NoOpREHandler()); |
934 |
shouldThrow(); |
935 |
} catch (IllegalArgumentException success) {} |
936 |
} |
937 |
|
938 |
/** |
939 |
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
940 |
*/ |
941 |
public void testConstructor20() { |
942 |
try { |
943 |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
944 |
new ArrayBlockingQueue<Runnable>(10), |
945 |
new SimpleThreadFactory(), |
946 |
new NoOpREHandler()); |
947 |
shouldThrow(); |
948 |
} catch (IllegalArgumentException success) {} |
949 |
} |
950 |
|
951 |
/** |
952 |
* Constructor throws if workQueue is null |
953 |
*/ |
954 |
public void testConstructorNullPointerException6() { |
955 |
try { |
956 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
957 |
(BlockingQueue) null, |
958 |
new SimpleThreadFactory(), |
959 |
new NoOpREHandler()); |
960 |
shouldThrow(); |
961 |
} catch (NullPointerException success) {} |
962 |
} |
963 |
|
964 |
/** |
965 |
* Constructor throws if handler is null |
966 |
*/ |
967 |
public void testConstructorNullPointerException7() { |
968 |
try { |
969 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
970 |
new ArrayBlockingQueue<Runnable>(10), |
971 |
new SimpleThreadFactory(), |
972 |
(RejectedExecutionHandler) null); |
973 |
shouldThrow(); |
974 |
} catch (NullPointerException success) {} |
975 |
} |
976 |
|
977 |
/** |
978 |
* Constructor throws if ThreadFactory is null |
979 |
*/ |
980 |
public void testConstructorNullPointerException8() { |
981 |
try { |
982 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
983 |
new ArrayBlockingQueue<Runnable>(10), |
984 |
(ThreadFactory) null, |
985 |
new NoOpREHandler()); |
986 |
shouldThrow(); |
987 |
} catch (NullPointerException success) {} |
988 |
} |
989 |
|
990 |
/** |
991 |
* get of submitted callable throws InterruptedException if interrupted |
992 |
*/ |
993 |
public void testInterruptedSubmit() throws InterruptedException { |
994 |
final ThreadPoolExecutor p = |
995 |
new ThreadPoolExecutor(1, 1, |
996 |
60, SECONDS, |
997 |
new ArrayBlockingQueue<Runnable>(10)); |
998 |
|
999 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1000 |
final CountDownLatch done = new CountDownLatch(1); |
1001 |
try { |
1002 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1003 |
public void realRun() throws Exception { |
1004 |
Callable task = new CheckedCallable<Boolean>() { |
1005 |
public Boolean realCall() throws InterruptedException { |
1006 |
threadStarted.countDown(); |
1007 |
done.await(); |
1008 |
return Boolean.TRUE; |
1009 |
}}; |
1010 |
p.submit(task).get(); |
1011 |
}}); |
1012 |
|
1013 |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1014 |
t.interrupt(); |
1015 |
awaitTermination(t, MEDIUM_DELAY_MS); |
1016 |
} finally { |
1017 |
done.countDown(); |
1018 |
joinPool(p); |
1019 |
} |
1020 |
} |
1021 |
|
1022 |
/** |
1023 |
* execute throws RejectedExecutionException if saturated. |
1024 |
*/ |
1025 |
public void testSaturatedExecute() { |
1026 |
ThreadPoolExecutor p = |
1027 |
new ThreadPoolExecutor(1, 1, |
1028 |
LONG_DELAY_MS, MILLISECONDS, |
1029 |
new ArrayBlockingQueue<Runnable>(1)); |
1030 |
final CountDownLatch done = new CountDownLatch(1); |
1031 |
try { |
1032 |
Runnable task = new CheckedRunnable() { |
1033 |
public void realRun() throws InterruptedException { |
1034 |
done.await(); |
1035 |
}}; |
1036 |
for (int i = 0; i < 2; ++i) |
1037 |
p.execute(task); |
1038 |
for (int i = 0; i < 2; ++i) { |
1039 |
try { |
1040 |
p.execute(task); |
1041 |
shouldThrow(); |
1042 |
} catch (RejectedExecutionException success) {} |
1043 |
assertTrue(p.getTaskCount() <= 2); |
1044 |
} |
1045 |
} finally { |
1046 |
done.countDown(); |
1047 |
joinPool(p); |
1048 |
} |
1049 |
} |
1050 |
|
1051 |
/** |
1052 |
* submit(runnable) throws RejectedExecutionException if saturated. |
1053 |
*/ |
1054 |
public void testSaturatedSubmitRunnable() { |
1055 |
ThreadPoolExecutor p = |
1056 |
new ThreadPoolExecutor(1, 1, |
1057 |
LONG_DELAY_MS, MILLISECONDS, |
1058 |
new ArrayBlockingQueue<Runnable>(1)); |
1059 |
final CountDownLatch done = new CountDownLatch(1); |
1060 |
try { |
1061 |
Runnable task = new CheckedRunnable() { |
1062 |
public void realRun() throws InterruptedException { |
1063 |
done.await(); |
1064 |
}}; |
1065 |
for (int i = 0; i < 2; ++i) |
1066 |
p.submit(task); |
1067 |
for (int i = 0; i < 2; ++i) { |
1068 |
try { |
1069 |
p.execute(task); |
1070 |
shouldThrow(); |
1071 |
} catch (RejectedExecutionException success) {} |
1072 |
assertTrue(p.getTaskCount() <= 2); |
1073 |
} |
1074 |
} finally { |
1075 |
done.countDown(); |
1076 |
joinPool(p); |
1077 |
} |
1078 |
} |
1079 |
|
1080 |
/** |
1081 |
* submit(callable) throws RejectedExecutionException if saturated. |
1082 |
*/ |
1083 |
public void testSaturatedSubmitCallable() { |
1084 |
ThreadPoolExecutor p = |
1085 |
new ThreadPoolExecutor(1, 1, |
1086 |
LONG_DELAY_MS, MILLISECONDS, |
1087 |
new ArrayBlockingQueue<Runnable>(1)); |
1088 |
final CountDownLatch done = new CountDownLatch(1); |
1089 |
try { |
1090 |
Runnable task = new CheckedRunnable() { |
1091 |
public void realRun() throws InterruptedException { |
1092 |
done.await(); |
1093 |
}}; |
1094 |
for (int i = 0; i < 2; ++i) |
1095 |
p.submit(Executors.callable(task)); |
1096 |
for (int i = 0; i < 2; ++i) { |
1097 |
try { |
1098 |
p.execute(task); |
1099 |
shouldThrow(); |
1100 |
} catch (RejectedExecutionException success) {} |
1101 |
assertTrue(p.getTaskCount() <= 2); |
1102 |
} |
1103 |
} finally { |
1104 |
done.countDown(); |
1105 |
joinPool(p); |
1106 |
} |
1107 |
} |
1108 |
|
1109 |
/** |
1110 |
* executor using CallerRunsPolicy runs task if saturated. |
1111 |
*/ |
1112 |
public void testSaturatedExecute2() { |
1113 |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1114 |
final ThreadPoolExecutor p = |
1115 |
new ThreadPoolExecutor(1, 1, |
1116 |
LONG_DELAY_MS, |
1117 |
MILLISECONDS, |
1118 |
new ArrayBlockingQueue<Runnable>(1), |
1119 |
h); |
1120 |
try { |
1121 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1122 |
for (int i = 0; i < tasks.length; ++i) |
1123 |
tasks[i] = new TrackedNoOpRunnable(); |
1124 |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1125 |
p.execute(mr); |
1126 |
for (int i = 0; i < tasks.length; ++i) |
1127 |
p.execute(tasks[i]); |
1128 |
for (int i = 1; i < tasks.length; ++i) |
1129 |
assertTrue(tasks[i].done); |
1130 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1131 |
} finally { |
1132 |
joinPool(p); |
1133 |
} |
1134 |
} |
1135 |
|
1136 |
/** |
1137 |
* executor using DiscardPolicy drops task if saturated. |
1138 |
*/ |
1139 |
public void testSaturatedExecute3() { |
1140 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1141 |
final ThreadPoolExecutor p = |
1142 |
new ThreadPoolExecutor(1, 1, |
1143 |
LONG_DELAY_MS, MILLISECONDS, |
1144 |
new ArrayBlockingQueue<Runnable>(1), |
1145 |
h); |
1146 |
try { |
1147 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1148 |
for (int i = 0; i < tasks.length; ++i) |
1149 |
tasks[i] = new TrackedNoOpRunnable(); |
1150 |
p.execute(new TrackedLongRunnable()); |
1151 |
for (TrackedNoOpRunnable task : tasks) |
1152 |
p.execute(task); |
1153 |
for (TrackedNoOpRunnable task : tasks) |
1154 |
assertFalse(task.done); |
1155 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1156 |
} finally { |
1157 |
joinPool(p); |
1158 |
} |
1159 |
} |
1160 |
|
1161 |
/** |
1162 |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1163 |
*/ |
1164 |
public void testSaturatedExecute4() { |
1165 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1166 |
final ThreadPoolExecutor p = |
1167 |
new ThreadPoolExecutor(1, 1, |
1168 |
LONG_DELAY_MS, MILLISECONDS, |
1169 |
new ArrayBlockingQueue<Runnable>(1), |
1170 |
h); |
1171 |
try { |
1172 |
p.execute(new TrackedLongRunnable()); |
1173 |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1174 |
p.execute(r2); |
1175 |
assertTrue(p.getQueue().contains(r2)); |
1176 |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
1177 |
p.execute(r3); |
1178 |
assertFalse(p.getQueue().contains(r2)); |
1179 |
assertTrue(p.getQueue().contains(r3)); |
1180 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1181 |
} finally { |
1182 |
joinPool(p); |
1183 |
} |
1184 |
} |
1185 |
|
1186 |
/** |
1187 |
* execute throws RejectedExecutionException if shutdown |
1188 |
*/ |
1189 |
public void testRejectedExecutionExceptionOnShutdown() { |
1190 |
ThreadPoolExecutor p = |
1191 |
new ThreadPoolExecutor(1, 1, |
1192 |
LONG_DELAY_MS, MILLISECONDS, |
1193 |
new ArrayBlockingQueue<Runnable>(1)); |
1194 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1195 |
try { |
1196 |
p.execute(new NoOpRunnable()); |
1197 |
shouldThrow(); |
1198 |
} catch (RejectedExecutionException success) {} |
1199 |
|
1200 |
joinPool(p); |
1201 |
} |
1202 |
|
1203 |
/** |
1204 |
* execute using CallerRunsPolicy drops task on shutdown |
1205 |
*/ |
1206 |
public void testCallerRunsOnShutdown() { |
1207 |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1208 |
final ThreadPoolExecutor p = |
1209 |
new ThreadPoolExecutor(1, 1, |
1210 |
LONG_DELAY_MS, MILLISECONDS, |
1211 |
new ArrayBlockingQueue<Runnable>(1), h); |
1212 |
|
1213 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1214 |
try { |
1215 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1216 |
p.execute(r); |
1217 |
assertFalse(r.done); |
1218 |
} finally { |
1219 |
joinPool(p); |
1220 |
} |
1221 |
} |
1222 |
|
1223 |
/** |
1224 |
* execute using DiscardPolicy drops task on shutdown |
1225 |
*/ |
1226 |
public void testDiscardOnShutdown() { |
1227 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1228 |
ThreadPoolExecutor p = |
1229 |
new ThreadPoolExecutor(1, 1, |
1230 |
LONG_DELAY_MS, MILLISECONDS, |
1231 |
new ArrayBlockingQueue<Runnable>(1), |
1232 |
h); |
1233 |
|
1234 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1235 |
try { |
1236 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1237 |
p.execute(r); |
1238 |
assertFalse(r.done); |
1239 |
} finally { |
1240 |
joinPool(p); |
1241 |
} |
1242 |
} |
1243 |
|
1244 |
/** |
1245 |
* execute using DiscardOldestPolicy drops task on shutdown |
1246 |
*/ |
1247 |
public void testDiscardOldestOnShutdown() { |
1248 |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1249 |
ThreadPoolExecutor p = |
1250 |
new ThreadPoolExecutor(1, 1, |
1251 |
LONG_DELAY_MS, MILLISECONDS, |
1252 |
new ArrayBlockingQueue<Runnable>(1), |
1253 |
h); |
1254 |
|
1255 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1256 |
try { |
1257 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1258 |
p.execute(r); |
1259 |
assertFalse(r.done); |
1260 |
} finally { |
1261 |
joinPool(p); |
1262 |
} |
1263 |
} |
1264 |
|
1265 |
/** |
1266 |
* execute(null) throws NPE |
1267 |
*/ |
1268 |
public void testExecuteNull() { |
1269 |
ThreadPoolExecutor p = |
1270 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1271 |
new ArrayBlockingQueue<Runnable>(10)); |
1272 |
try { |
1273 |
p.execute(null); |
1274 |
shouldThrow(); |
1275 |
} catch (NullPointerException success) {} |
1276 |
|
1277 |
joinPool(p); |
1278 |
} |
1279 |
|
1280 |
/** |
1281 |
* setCorePoolSize of negative value throws IllegalArgumentException |
1282 |
*/ |
1283 |
public void testCorePoolSizeIllegalArgumentException() { |
1284 |
ThreadPoolExecutor p = |
1285 |
new ThreadPoolExecutor(1, 2, |
1286 |
LONG_DELAY_MS, MILLISECONDS, |
1287 |
new ArrayBlockingQueue<Runnable>(10)); |
1288 |
try { |
1289 |
p.setCorePoolSize(-1); |
1290 |
shouldThrow(); |
1291 |
} catch (IllegalArgumentException success) { |
1292 |
} finally { |
1293 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1294 |
} |
1295 |
joinPool(p); |
1296 |
} |
1297 |
|
1298 |
/** |
1299 |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1300 |
* given a value less the core pool size |
1301 |
*/ |
1302 |
public void testMaximumPoolSizeIllegalArgumentException() { |
1303 |
ThreadPoolExecutor p = |
1304 |
new ThreadPoolExecutor(2, 3, |
1305 |
LONG_DELAY_MS, MILLISECONDS, |
1306 |
new ArrayBlockingQueue<Runnable>(10)); |
1307 |
try { |
1308 |
p.setMaximumPoolSize(1); |
1309 |
shouldThrow(); |
1310 |
} catch (IllegalArgumentException success) { |
1311 |
} finally { |
1312 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1313 |
} |
1314 |
joinPool(p); |
1315 |
} |
1316 |
|
1317 |
/** |
1318 |
* setMaximumPoolSize throws IllegalArgumentException |
1319 |
* if given a negative value |
1320 |
*/ |
1321 |
public void testMaximumPoolSizeIllegalArgumentException2() { |
1322 |
ThreadPoolExecutor p = |
1323 |
new ThreadPoolExecutor(2, 3, |
1324 |
LONG_DELAY_MS, MILLISECONDS, |
1325 |
new ArrayBlockingQueue<Runnable>(10)); |
1326 |
try { |
1327 |
p.setMaximumPoolSize(-1); |
1328 |
shouldThrow(); |
1329 |
} catch (IllegalArgumentException success) { |
1330 |
} finally { |
1331 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1332 |
} |
1333 |
joinPool(p); |
1334 |
} |
1335 |
|
1336 |
/** |
1337 |
* Configuration changes that allow core pool size greater than |
1338 |
* max pool size result in IllegalArgumentException. |
1339 |
*/ |
1340 |
public void testPoolSizeInvariants() { |
1341 |
ThreadPoolExecutor p = |
1342 |
new ThreadPoolExecutor(1, 1, |
1343 |
LONG_DELAY_MS, MILLISECONDS, |
1344 |
new ArrayBlockingQueue<Runnable>(10)); |
1345 |
for (int s = 1; s < 5; s++) { |
1346 |
p.setMaximumPoolSize(s); |
1347 |
p.setCorePoolSize(s); |
1348 |
try { |
1349 |
p.setMaximumPoolSize(s - 1); |
1350 |
shouldThrow(); |
1351 |
} catch (IllegalArgumentException success) {} |
1352 |
assertEquals(s, p.getCorePoolSize()); |
1353 |
assertEquals(s, p.getMaximumPoolSize()); |
1354 |
try { |
1355 |
p.setCorePoolSize(s + 1); |
1356 |
shouldThrow(); |
1357 |
} catch (IllegalArgumentException success) {} |
1358 |
assertEquals(s, p.getCorePoolSize()); |
1359 |
assertEquals(s, p.getMaximumPoolSize()); |
1360 |
} |
1361 |
joinPool(p); |
1362 |
} |
1363 |
|
1364 |
/** |
1365 |
* setKeepAliveTime throws IllegalArgumentException |
1366 |
* when given a negative value |
1367 |
*/ |
1368 |
public void testKeepAliveTimeIllegalArgumentException() { |
1369 |
ThreadPoolExecutor p = |
1370 |
new ThreadPoolExecutor(2, 3, |
1371 |
LONG_DELAY_MS, MILLISECONDS, |
1372 |
new ArrayBlockingQueue<Runnable>(10)); |
1373 |
try { |
1374 |
p.setKeepAliveTime(-1,MILLISECONDS); |
1375 |
shouldThrow(); |
1376 |
} catch (IllegalArgumentException success) { |
1377 |
} finally { |
1378 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1379 |
} |
1380 |
joinPool(p); |
1381 |
} |
1382 |
|
1383 |
/** |
1384 |
* terminated() is called on termination |
1385 |
*/ |
1386 |
public void testTerminated() { |
1387 |
ExtendedTPE p = new ExtendedTPE(); |
1388 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1389 |
assertTrue(p.terminatedCalled()); |
1390 |
joinPool(p); |
1391 |
} |
1392 |
|
1393 |
/** |
1394 |
* beforeExecute and afterExecute are called when executing task |
1395 |
*/ |
1396 |
public void testBeforeAfter() throws InterruptedException { |
1397 |
ExtendedTPE p = new ExtendedTPE(); |
1398 |
try { |
1399 |
final CountDownLatch done = new CountDownLatch(1); |
1400 |
p.execute(new CheckedRunnable() { |
1401 |
public void realRun() { |
1402 |
done.countDown(); |
1403 |
}}); |
1404 |
await(p.afterCalled); |
1405 |
assertEquals(0, done.getCount()); |
1406 |
assertTrue(p.afterCalled()); |
1407 |
assertTrue(p.beforeCalled()); |
1408 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1409 |
} finally { |
1410 |
joinPool(p); |
1411 |
} |
1412 |
} |
1413 |
|
1414 |
/** |
1415 |
* completed submit of callable returns result |
1416 |
*/ |
1417 |
public void testSubmitCallable() throws Exception { |
1418 |
ExecutorService e = |
1419 |
new ThreadPoolExecutor(2, 2, |
1420 |
LONG_DELAY_MS, MILLISECONDS, |
1421 |
new ArrayBlockingQueue<Runnable>(10)); |
1422 |
try { |
1423 |
Future<String> future = e.submit(new StringTask()); |
1424 |
String result = future.get(); |
1425 |
assertSame(TEST_STRING, result); |
1426 |
} finally { |
1427 |
joinPool(e); |
1428 |
} |
1429 |
} |
1430 |
|
1431 |
/** |
1432 |
* completed submit of runnable returns successfully |
1433 |
*/ |
1434 |
public void testSubmitRunnable() throws Exception { |
1435 |
ExecutorService e = |
1436 |
new ThreadPoolExecutor(2, 2, |
1437 |
LONG_DELAY_MS, MILLISECONDS, |
1438 |
new ArrayBlockingQueue<Runnable>(10)); |
1439 |
try { |
1440 |
Future<?> future = e.submit(new NoOpRunnable()); |
1441 |
future.get(); |
1442 |
assertTrue(future.isDone()); |
1443 |
} finally { |
1444 |
joinPool(e); |
1445 |
} |
1446 |
} |
1447 |
|
1448 |
/** |
1449 |
* completed submit of (runnable, result) returns result |
1450 |
*/ |
1451 |
public void testSubmitRunnable2() throws Exception { |
1452 |
ExecutorService e = |
1453 |
new ThreadPoolExecutor(2, 2, |
1454 |
LONG_DELAY_MS, MILLISECONDS, |
1455 |
new ArrayBlockingQueue<Runnable>(10)); |
1456 |
try { |
1457 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1458 |
String result = future.get(); |
1459 |
assertSame(TEST_STRING, result); |
1460 |
} finally { |
1461 |
joinPool(e); |
1462 |
} |
1463 |
} |
1464 |
|
1465 |
/** |
1466 |
* invokeAny(null) throws NPE |
1467 |
*/ |
1468 |
public void testInvokeAny1() throws Exception { |
1469 |
ExecutorService e = |
1470 |
new ThreadPoolExecutor(2, 2, |
1471 |
LONG_DELAY_MS, MILLISECONDS, |
1472 |
new ArrayBlockingQueue<Runnable>(10)); |
1473 |
try { |
1474 |
e.invokeAny(null); |
1475 |
shouldThrow(); |
1476 |
} catch (NullPointerException success) { |
1477 |
} finally { |
1478 |
joinPool(e); |
1479 |
} |
1480 |
} |
1481 |
|
1482 |
/** |
1483 |
* invokeAny(empty collection) throws IAE |
1484 |
*/ |
1485 |
public void testInvokeAny2() throws Exception { |
1486 |
ExecutorService e = |
1487 |
new ThreadPoolExecutor(2, 2, |
1488 |
LONG_DELAY_MS, MILLISECONDS, |
1489 |
new ArrayBlockingQueue<Runnable>(10)); |
1490 |
try { |
1491 |
e.invokeAny(new ArrayList<Callable<String>>()); |
1492 |
shouldThrow(); |
1493 |
} catch (IllegalArgumentException success) { |
1494 |
} finally { |
1495 |
joinPool(e); |
1496 |
} |
1497 |
} |
1498 |
|
1499 |
/** |
1500 |
* invokeAny(c) throws NPE if c has null elements |
1501 |
*/ |
1502 |
public void testInvokeAny3() throws Exception { |
1503 |
final CountDownLatch latch = new CountDownLatch(1); |
1504 |
final ExecutorService e = |
1505 |
new ThreadPoolExecutor(2, 2, |
1506 |
LONG_DELAY_MS, MILLISECONDS, |
1507 |
new ArrayBlockingQueue<Runnable>(10)); |
1508 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1509 |
l.add(latchAwaitingStringTask(latch)); |
1510 |
l.add(null); |
1511 |
try { |
1512 |
e.invokeAny(l); |
1513 |
shouldThrow(); |
1514 |
} catch (NullPointerException success) { |
1515 |
} finally { |
1516 |
latch.countDown(); |
1517 |
joinPool(e); |
1518 |
} |
1519 |
} |
1520 |
|
1521 |
/** |
1522 |
* invokeAny(c) throws ExecutionException if no task completes |
1523 |
*/ |
1524 |
public void testInvokeAny4() throws Exception { |
1525 |
ExecutorService e = |
1526 |
new ThreadPoolExecutor(2, 2, |
1527 |
LONG_DELAY_MS, MILLISECONDS, |
1528 |
new ArrayBlockingQueue<Runnable>(10)); |
1529 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1530 |
l.add(new NPETask()); |
1531 |
try { |
1532 |
e.invokeAny(l); |
1533 |
shouldThrow(); |
1534 |
} catch (ExecutionException success) { |
1535 |
assertTrue(success.getCause() instanceof NullPointerException); |
1536 |
} finally { |
1537 |
joinPool(e); |
1538 |
} |
1539 |
} |
1540 |
|
1541 |
/** |
1542 |
* invokeAny(c) returns result of some task |
1543 |
*/ |
1544 |
public void testInvokeAny5() throws Exception { |
1545 |
ExecutorService e = |
1546 |
new ThreadPoolExecutor(2, 2, |
1547 |
LONG_DELAY_MS, MILLISECONDS, |
1548 |
new ArrayBlockingQueue<Runnable>(10)); |
1549 |
try { |
1550 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1551 |
l.add(new StringTask()); |
1552 |
l.add(new StringTask()); |
1553 |
String result = e.invokeAny(l); |
1554 |
assertSame(TEST_STRING, result); |
1555 |
} finally { |
1556 |
joinPool(e); |
1557 |
} |
1558 |
} |
1559 |
|
1560 |
/** |
1561 |
* invokeAll(null) throws NPE |
1562 |
*/ |
1563 |
public void testInvokeAll1() throws Exception { |
1564 |
ExecutorService e = |
1565 |
new ThreadPoolExecutor(2, 2, |
1566 |
LONG_DELAY_MS, MILLISECONDS, |
1567 |
new ArrayBlockingQueue<Runnable>(10)); |
1568 |
try { |
1569 |
e.invokeAll(null); |
1570 |
shouldThrow(); |
1571 |
} catch (NullPointerException success) { |
1572 |
} finally { |
1573 |
joinPool(e); |
1574 |
} |
1575 |
} |
1576 |
|
1577 |
/** |
1578 |
* invokeAll(empty collection) returns empty collection |
1579 |
*/ |
1580 |
public void testInvokeAll2() throws InterruptedException { |
1581 |
ExecutorService e = |
1582 |
new ThreadPoolExecutor(2, 2, |
1583 |
LONG_DELAY_MS, MILLISECONDS, |
1584 |
new ArrayBlockingQueue<Runnable>(10)); |
1585 |
try { |
1586 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1587 |
assertTrue(r.isEmpty()); |
1588 |
} finally { |
1589 |
joinPool(e); |
1590 |
} |
1591 |
} |
1592 |
|
1593 |
/** |
1594 |
* invokeAll(c) throws NPE if c has null elements |
1595 |
*/ |
1596 |
public void testInvokeAll3() throws Exception { |
1597 |
ExecutorService e = |
1598 |
new ThreadPoolExecutor(2, 2, |
1599 |
LONG_DELAY_MS, MILLISECONDS, |
1600 |
new ArrayBlockingQueue<Runnable>(10)); |
1601 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1602 |
l.add(new StringTask()); |
1603 |
l.add(null); |
1604 |
try { |
1605 |
e.invokeAll(l); |
1606 |
shouldThrow(); |
1607 |
} catch (NullPointerException success) { |
1608 |
} finally { |
1609 |
joinPool(e); |
1610 |
} |
1611 |
} |
1612 |
|
1613 |
/** |
1614 |
* get of element of invokeAll(c) throws exception on failed task |
1615 |
*/ |
1616 |
public void testInvokeAll4() throws Exception { |
1617 |
ExecutorService e = |
1618 |
new ThreadPoolExecutor(2, 2, |
1619 |
LONG_DELAY_MS, MILLISECONDS, |
1620 |
new ArrayBlockingQueue<Runnable>(10)); |
1621 |
try { |
1622 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1623 |
l.add(new NPETask()); |
1624 |
List<Future<String>> futures = e.invokeAll(l); |
1625 |
assertEquals(1, futures.size()); |
1626 |
try { |
1627 |
futures.get(0).get(); |
1628 |
shouldThrow(); |
1629 |
} catch (ExecutionException success) { |
1630 |
assertTrue(success.getCause() instanceof NullPointerException); |
1631 |
} |
1632 |
} finally { |
1633 |
joinPool(e); |
1634 |
} |
1635 |
} |
1636 |
|
1637 |
/** |
1638 |
* invokeAll(c) returns results of all completed tasks |
1639 |
*/ |
1640 |
public void testInvokeAll5() throws Exception { |
1641 |
ExecutorService e = |
1642 |
new ThreadPoolExecutor(2, 2, |
1643 |
LONG_DELAY_MS, MILLISECONDS, |
1644 |
new ArrayBlockingQueue<Runnable>(10)); |
1645 |
try { |
1646 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1647 |
l.add(new StringTask()); |
1648 |
l.add(new StringTask()); |
1649 |
List<Future<String>> futures = e.invokeAll(l); |
1650 |
assertEquals(2, futures.size()); |
1651 |
for (Future<String> future : futures) |
1652 |
assertSame(TEST_STRING, future.get()); |
1653 |
} finally { |
1654 |
joinPool(e); |
1655 |
} |
1656 |
} |
1657 |
|
1658 |
/** |
1659 |
* timed invokeAny(null) throws NPE |
1660 |
*/ |
1661 |
public void testTimedInvokeAny1() throws Exception { |
1662 |
ExecutorService e = |
1663 |
new ThreadPoolExecutor(2, 2, |
1664 |
LONG_DELAY_MS, MILLISECONDS, |
1665 |
new ArrayBlockingQueue<Runnable>(10)); |
1666 |
try { |
1667 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1668 |
shouldThrow(); |
1669 |
} catch (NullPointerException success) { |
1670 |
} finally { |
1671 |
joinPool(e); |
1672 |
} |
1673 |
} |
1674 |
|
1675 |
/** |
1676 |
* timed invokeAny(,,null) throws NPE |
1677 |
*/ |
1678 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1679 |
ExecutorService e = |
1680 |
new ThreadPoolExecutor(2, 2, |
1681 |
LONG_DELAY_MS, MILLISECONDS, |
1682 |
new ArrayBlockingQueue<Runnable>(10)); |
1683 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1684 |
l.add(new StringTask()); |
1685 |
try { |
1686 |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1687 |
shouldThrow(); |
1688 |
} catch (NullPointerException success) { |
1689 |
} finally { |
1690 |
joinPool(e); |
1691 |
} |
1692 |
} |
1693 |
|
1694 |
/** |
1695 |
* timed invokeAny(empty collection) throws IAE |
1696 |
*/ |
1697 |
public void testTimedInvokeAny2() throws Exception { |
1698 |
ExecutorService e = |
1699 |
new ThreadPoolExecutor(2, 2, |
1700 |
LONG_DELAY_MS, MILLISECONDS, |
1701 |
new ArrayBlockingQueue<Runnable>(10)); |
1702 |
try { |
1703 |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1704 |
shouldThrow(); |
1705 |
} catch (IllegalArgumentException success) { |
1706 |
} finally { |
1707 |
joinPool(e); |
1708 |
} |
1709 |
} |
1710 |
|
1711 |
/** |
1712 |
* timed invokeAny(c) throws NPE if c has null elements |
1713 |
*/ |
1714 |
public void testTimedInvokeAny3() throws Exception { |
1715 |
final CountDownLatch latch = new CountDownLatch(1); |
1716 |
final ExecutorService e = |
1717 |
new ThreadPoolExecutor(2, 2, |
1718 |
LONG_DELAY_MS, MILLISECONDS, |
1719 |
new ArrayBlockingQueue<Runnable>(10)); |
1720 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1721 |
l.add(latchAwaitingStringTask(latch)); |
1722 |
l.add(null); |
1723 |
try { |
1724 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1725 |
shouldThrow(); |
1726 |
} catch (NullPointerException success) { |
1727 |
} finally { |
1728 |
latch.countDown(); |
1729 |
joinPool(e); |
1730 |
} |
1731 |
} |
1732 |
|
1733 |
/** |
1734 |
* timed invokeAny(c) throws ExecutionException if no task completes |
1735 |
*/ |
1736 |
public void testTimedInvokeAny4() throws Exception { |
1737 |
ExecutorService e = |
1738 |
new ThreadPoolExecutor(2, 2, |
1739 |
LONG_DELAY_MS, MILLISECONDS, |
1740 |
new ArrayBlockingQueue<Runnable>(10)); |
1741 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1742 |
l.add(new NPETask()); |
1743 |
try { |
1744 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1745 |
shouldThrow(); |
1746 |
} catch (ExecutionException success) { |
1747 |
assertTrue(success.getCause() instanceof NullPointerException); |
1748 |
} finally { |
1749 |
joinPool(e); |
1750 |
} |
1751 |
} |
1752 |
|
1753 |
/** |
1754 |
* timed invokeAny(c) returns result of some task |
1755 |
*/ |
1756 |
public void testTimedInvokeAny5() throws Exception { |
1757 |
ExecutorService e = |
1758 |
new ThreadPoolExecutor(2, 2, |
1759 |
LONG_DELAY_MS, MILLISECONDS, |
1760 |
new ArrayBlockingQueue<Runnable>(10)); |
1761 |
try { |
1762 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1763 |
l.add(new StringTask()); |
1764 |
l.add(new StringTask()); |
1765 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1766 |
assertSame(TEST_STRING, result); |
1767 |
} finally { |
1768 |
joinPool(e); |
1769 |
} |
1770 |
} |
1771 |
|
1772 |
/** |
1773 |
* timed invokeAll(null) throws NPE |
1774 |
*/ |
1775 |
public void testTimedInvokeAll1() throws Exception { |
1776 |
ExecutorService e = |
1777 |
new ThreadPoolExecutor(2, 2, |
1778 |
LONG_DELAY_MS, MILLISECONDS, |
1779 |
new ArrayBlockingQueue<Runnable>(10)); |
1780 |
try { |
1781 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1782 |
shouldThrow(); |
1783 |
} catch (NullPointerException success) { |
1784 |
} finally { |
1785 |
joinPool(e); |
1786 |
} |
1787 |
} |
1788 |
|
1789 |
/** |
1790 |
* timed invokeAll(,,null) throws NPE |
1791 |
*/ |
1792 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1793 |
ExecutorService e = |
1794 |
new ThreadPoolExecutor(2, 2, |
1795 |
LONG_DELAY_MS, MILLISECONDS, |
1796 |
new ArrayBlockingQueue<Runnable>(10)); |
1797 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1798 |
l.add(new StringTask()); |
1799 |
try { |
1800 |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1801 |
shouldThrow(); |
1802 |
} catch (NullPointerException success) { |
1803 |
} finally { |
1804 |
joinPool(e); |
1805 |
} |
1806 |
} |
1807 |
|
1808 |
/** |
1809 |
* timed invokeAll(empty collection) returns empty collection |
1810 |
*/ |
1811 |
public void testTimedInvokeAll2() throws InterruptedException { |
1812 |
ExecutorService e = |
1813 |
new ThreadPoolExecutor(2, 2, |
1814 |
LONG_DELAY_MS, MILLISECONDS, |
1815 |
new ArrayBlockingQueue<Runnable>(10)); |
1816 |
try { |
1817 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1818 |
assertTrue(r.isEmpty()); |
1819 |
} finally { |
1820 |
joinPool(e); |
1821 |
} |
1822 |
} |
1823 |
|
1824 |
/** |
1825 |
* timed invokeAll(c) throws NPE if c has null elements |
1826 |
*/ |
1827 |
public void testTimedInvokeAll3() throws Exception { |
1828 |
ExecutorService e = |
1829 |
new ThreadPoolExecutor(2, 2, |
1830 |
LONG_DELAY_MS, MILLISECONDS, |
1831 |
new ArrayBlockingQueue<Runnable>(10)); |
1832 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1833 |
l.add(new StringTask()); |
1834 |
l.add(null); |
1835 |
try { |
1836 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1837 |
shouldThrow(); |
1838 |
} catch (NullPointerException success) { |
1839 |
} finally { |
1840 |
joinPool(e); |
1841 |
} |
1842 |
} |
1843 |
|
1844 |
/** |
1845 |
* get of element of invokeAll(c) throws exception on failed task |
1846 |
*/ |
1847 |
public void testTimedInvokeAll4() throws Exception { |
1848 |
ExecutorService e = |
1849 |
new ThreadPoolExecutor(2, 2, |
1850 |
LONG_DELAY_MS, MILLISECONDS, |
1851 |
new ArrayBlockingQueue<Runnable>(10)); |
1852 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1853 |
l.add(new NPETask()); |
1854 |
List<Future<String>> futures = |
1855 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1856 |
assertEquals(1, futures.size()); |
1857 |
try { |
1858 |
futures.get(0).get(); |
1859 |
shouldThrow(); |
1860 |
} catch (ExecutionException success) { |
1861 |
assertTrue(success.getCause() instanceof NullPointerException); |
1862 |
} finally { |
1863 |
joinPool(e); |
1864 |
} |
1865 |
} |
1866 |
|
1867 |
/** |
1868 |
* timed invokeAll(c) returns results of all completed tasks |
1869 |
*/ |
1870 |
public void testTimedInvokeAll5() throws Exception { |
1871 |
ExecutorService e = |
1872 |
new ThreadPoolExecutor(2, 2, |
1873 |
LONG_DELAY_MS, MILLISECONDS, |
1874 |
new ArrayBlockingQueue<Runnable>(10)); |
1875 |
try { |
1876 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1877 |
l.add(new StringTask()); |
1878 |
l.add(new StringTask()); |
1879 |
List<Future<String>> futures = |
1880 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1881 |
assertEquals(2, futures.size()); |
1882 |
for (Future<String> future : futures) |
1883 |
assertSame(TEST_STRING, future.get()); |
1884 |
} finally { |
1885 |
joinPool(e); |
1886 |
} |
1887 |
} |
1888 |
|
1889 |
/** |
1890 |
* timed invokeAll(c) cancels tasks not completed by timeout |
1891 |
*/ |
1892 |
public void testTimedInvokeAll6() throws Exception { |
1893 |
ExecutorService e = |
1894 |
new ThreadPoolExecutor(2, 2, |
1895 |
LONG_DELAY_MS, MILLISECONDS, |
1896 |
new ArrayBlockingQueue<Runnable>(10)); |
1897 |
try { |
1898 |
for (long timeout = timeoutMillis();;) { |
1899 |
List<Callable<String>> tasks = new ArrayList<>(); |
1900 |
tasks.add(new StringTask("0")); |
1901 |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1902 |
tasks.add(new StringTask("2")); |
1903 |
long startTime = System.nanoTime(); |
1904 |
List<Future<String>> futures = |
1905 |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1906 |
assertEquals(tasks.size(), futures.size()); |
1907 |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1908 |
for (Future future : futures) |
1909 |
assertTrue(future.isDone()); |
1910 |
assertTrue(futures.get(1).isCancelled()); |
1911 |
try { |
1912 |
assertEquals("0", futures.get(0).get()); |
1913 |
assertEquals("2", futures.get(2).get()); |
1914 |
break; |
1915 |
} catch (CancellationException retryWithLongerTimeout) { |
1916 |
timeout *= 2; |
1917 |
if (timeout >= LONG_DELAY_MS / 2) |
1918 |
fail("expected exactly one task to be cancelled"); |
1919 |
} |
1920 |
} |
1921 |
} finally { |
1922 |
joinPool(e); |
1923 |
} |
1924 |
} |
1925 |
|
1926 |
/** |
1927 |
* Execution continues if there is at least one thread even if |
1928 |
* thread factory fails to create more |
1929 |
*/ |
1930 |
public void testFailingThreadFactory() throws InterruptedException { |
1931 |
final ExecutorService e = |
1932 |
new ThreadPoolExecutor(100, 100, |
1933 |
LONG_DELAY_MS, MILLISECONDS, |
1934 |
new LinkedBlockingQueue<Runnable>(), |
1935 |
new FailingThreadFactory()); |
1936 |
try { |
1937 |
final int TASKS = 100; |
1938 |
final CountDownLatch done = new CountDownLatch(TASKS); |
1939 |
for (int k = 0; k < TASKS; ++k) |
1940 |
e.execute(new CheckedRunnable() { |
1941 |
public void realRun() { |
1942 |
done.countDown(); |
1943 |
}}); |
1944 |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1945 |
} finally { |
1946 |
joinPool(e); |
1947 |
} |
1948 |
} |
1949 |
|
1950 |
/** |
1951 |
* allowsCoreThreadTimeOut is by default false. |
1952 |
*/ |
1953 |
public void testAllowsCoreThreadTimeOut() { |
1954 |
final ThreadPoolExecutor p = |
1955 |
new ThreadPoolExecutor(2, 2, |
1956 |
1000, MILLISECONDS, |
1957 |
new ArrayBlockingQueue<Runnable>(10)); |
1958 |
assertFalse(p.allowsCoreThreadTimeOut()); |
1959 |
joinPool(p); |
1960 |
} |
1961 |
|
1962 |
/** |
1963 |
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1964 |
*/ |
1965 |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1966 |
long keepAliveTime = timeoutMillis(); |
1967 |
final ThreadPoolExecutor p = |
1968 |
new ThreadPoolExecutor(2, 10, |
1969 |
keepAliveTime, MILLISECONDS, |
1970 |
new ArrayBlockingQueue<Runnable>(10)); |
1971 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1972 |
try { |
1973 |
p.allowCoreThreadTimeOut(true); |
1974 |
p.execute(new CheckedRunnable() { |
1975 |
public void realRun() { |
1976 |
threadStarted.countDown(); |
1977 |
assertEquals(1, p.getPoolSize()); |
1978 |
}}); |
1979 |
await(threadStarted); |
1980 |
delay(keepAliveTime); |
1981 |
long startTime = System.nanoTime(); |
1982 |
while (p.getPoolSize() > 0 |
1983 |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1984 |
Thread.yield(); |
1985 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1986 |
assertEquals(0, p.getPoolSize()); |
1987 |
} finally { |
1988 |
joinPool(p); |
1989 |
} |
1990 |
} |
1991 |
|
1992 |
/** |
1993 |
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1994 |
*/ |
1995 |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1996 |
long keepAliveTime = timeoutMillis(); |
1997 |
final ThreadPoolExecutor p = |
1998 |
new ThreadPoolExecutor(2, 10, |
1999 |
keepAliveTime, MILLISECONDS, |
2000 |
new ArrayBlockingQueue<Runnable>(10)); |
2001 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
2002 |
try { |
2003 |
p.allowCoreThreadTimeOut(false); |
2004 |
p.execute(new CheckedRunnable() { |
2005 |
public void realRun() throws InterruptedException { |
2006 |
threadStarted.countDown(); |
2007 |
assertTrue(p.getPoolSize() >= 1); |
2008 |
}}); |
2009 |
delay(2 * keepAliveTime); |
2010 |
assertTrue(p.getPoolSize() >= 1); |
2011 |
} finally { |
2012 |
joinPool(p); |
2013 |
} |
2014 |
} |
2015 |
|
2016 |
/** |
2017 |
* execute allows the same task to be submitted multiple times, even |
2018 |
* if rejected |
2019 |
*/ |
2020 |
public void testRejectedRecycledTask() throws InterruptedException { |
2021 |
final int nTasks = 1000; |
2022 |
final CountDownLatch done = new CountDownLatch(nTasks); |
2023 |
final Runnable recycledTask = new Runnable() { |
2024 |
public void run() { |
2025 |
done.countDown(); |
2026 |
}}; |
2027 |
final ThreadPoolExecutor p = |
2028 |
new ThreadPoolExecutor(1, 30, |
2029 |
60, SECONDS, |
2030 |
new ArrayBlockingQueue(30)); |
2031 |
try { |
2032 |
for (int i = 0; i < nTasks; ++i) { |
2033 |
for (;;) { |
2034 |
try { |
2035 |
p.execute(recycledTask); |
2036 |
break; |
2037 |
} |
2038 |
catch (RejectedExecutionException ignore) {} |
2039 |
} |
2040 |
} |
2041 |
// enough time to run all tasks |
2042 |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2043 |
} finally { |
2044 |
joinPool(p); |
2045 |
} |
2046 |
} |
2047 |
|
2048 |
} |