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