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 { |
90 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
91 |
> |
final CountDownLatch done = new CountDownLatch(1); |
92 |
> |
final Runnable task = new CheckedRunnable() { |
93 |
> |
public void realRun() { done.countDown(); }}; |
94 |
|
p.execute(task); |
95 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
98 |
< |
} finally { |
99 |
< |
joinPool(p); |
95 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
96 |
|
} |
97 |
|
} |
98 |
|
|
105 |
|
new ThreadPoolExecutor(2, 2, |
106 |
|
LONG_DELAY_MS, MILLISECONDS, |
107 |
|
new ArrayBlockingQueue<Runnable>(10)); |
108 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
109 |
< |
final CountDownLatch done = new CountDownLatch(1); |
110 |
< |
try { |
108 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
109 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
110 |
> |
final CountDownLatch done = new CountDownLatch(1); |
111 |
|
assertEquals(0, p.getActiveCount()); |
112 |
|
p.execute(new CheckedRunnable() { |
113 |
|
public void realRun() throws InterruptedException { |
115 |
|
assertEquals(1, p.getActiveCount()); |
116 |
|
done.await(); |
117 |
|
}}); |
118 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
118 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
119 |
|
assertEquals(1, p.getActiveCount()); |
124 |
– |
} finally { |
120 |
|
done.countDown(); |
126 |
– |
joinPool(p); |
121 |
|
} |
122 |
|
} |
123 |
|
|
300 |
|
new ThreadPoolExecutor(1, 2, |
301 |
|
LONG_DELAY_MS, MILLISECONDS, |
302 |
|
new ArrayBlockingQueue<Runnable>(10)); |
303 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
304 |
< |
p.setRejectedExecutionHandler(h); |
305 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
306 |
< |
joinPool(p); |
303 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
304 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
305 |
> |
p.setRejectedExecutionHandler(handler); |
306 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
307 |
> |
} |
308 |
|
} |
309 |
|
|
310 |
|
/** |
315 |
|
new ThreadPoolExecutor(1, 2, |
316 |
|
LONG_DELAY_MS, MILLISECONDS, |
317 |
|
new ArrayBlockingQueue<Runnable>(10)); |
318 |
< |
try { |
319 |
< |
p.setRejectedExecutionHandler(null); |
320 |
< |
shouldThrow(); |
321 |
< |
} catch (NullPointerException success) { |
322 |
< |
} finally { |
328 |
< |
joinPool(p); |
318 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
319 |
> |
try { |
320 |
> |
p.setRejectedExecutionHandler(null); |
321 |
> |
shouldThrow(); |
322 |
> |
} catch (NullPointerException success) {} |
323 |
|
} |
324 |
|
} |
325 |
|
|
333 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
334 |
|
LONG_DELAY_MS, MILLISECONDS, |
335 |
|
new ArrayBlockingQueue<Runnable>(10)); |
336 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
337 |
< |
final CountDownLatch done = new CountDownLatch(1); |
338 |
< |
try { |
336 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
338 |
> |
final CountDownLatch done = new CountDownLatch(1); |
339 |
|
assertEquals(0, p.getLargestPoolSize()); |
340 |
|
for (int i = 0; i < THREADS; i++) |
341 |
|
p.execute(new CheckedRunnable() { |
344 |
|
done.await(); |
345 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
346 |
|
}}); |
347 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
354 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
355 |
< |
} finally { |
356 |
< |
done.countDown(); |
357 |
< |
joinPool(p); |
347 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
348 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
349 |
+ |
done.countDown(); // release pool |
350 |
|
} |
351 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
352 |
|
} |
353 |
|
|
354 |
|
/** |
360 |
|
new ThreadPoolExecutor(2, 3, |
361 |
|
LONG_DELAY_MS, MILLISECONDS, |
362 |
|
new ArrayBlockingQueue<Runnable>(10)); |
363 |
< |
assertEquals(3, p.getMaximumPoolSize()); |
364 |
< |
joinPool(p); |
363 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
364 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
365 |
> |
p.setMaximumPoolSize(5); |
366 |
> |
assertEquals(5, p.getMaximumPoolSize()); |
367 |
> |
p.setMaximumPoolSize(4); |
368 |
> |
assertEquals(4, p.getMaximumPoolSize()); |
369 |
> |
} |
370 |
|
} |
371 |
|
|
372 |
|
/** |
378 |
|
new ThreadPoolExecutor(1, 1, |
379 |
|
LONG_DELAY_MS, MILLISECONDS, |
380 |
|
new ArrayBlockingQueue<Runnable>(10)); |
381 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
382 |
< |
final CountDownLatch done = new CountDownLatch(1); |
383 |
< |
try { |
381 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
382 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
383 |
> |
final CountDownLatch done = new CountDownLatch(1); |
384 |
|
assertEquals(0, p.getPoolSize()); |
385 |
|
p.execute(new CheckedRunnable() { |
386 |
|
public void realRun() throws InterruptedException { |
388 |
|
assertEquals(1, p.getPoolSize()); |
389 |
|
done.await(); |
390 |
|
}}); |
391 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
391 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
392 |
|
assertEquals(1, p.getPoolSize()); |
393 |
< |
} finally { |
397 |
< |
done.countDown(); |
398 |
< |
joinPool(p); |
393 |
> |
done.countDown(); // release pool |
394 |
|
} |
395 |
|
} |
396 |
|
|
402 |
|
new ThreadPoolExecutor(1, 1, |
403 |
|
LONG_DELAY_MS, MILLISECONDS, |
404 |
|
new ArrayBlockingQueue<Runnable>(10)); |
405 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
406 |
< |
final CountDownLatch done = new CountDownLatch(1); |
407 |
< |
try { |
405 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
406 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
407 |
> |
final CountDownLatch done = new CountDownLatch(1); |
408 |
|
assertEquals(0, p.getTaskCount()); |
409 |
|
p.execute(new CheckedRunnable() { |
410 |
|
public void realRun() throws InterruptedException { |
412 |
|
assertEquals(1, p.getTaskCount()); |
413 |
|
done.await(); |
414 |
|
}}); |
415 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
415 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
416 |
|
assertEquals(1, p.getTaskCount()); |
422 |
– |
} finally { |
417 |
|
done.countDown(); |
424 |
– |
joinPool(p); |
418 |
|
} |
419 |
|
} |
420 |
|
|
426 |
|
new ThreadPoolExecutor(1, 1, |
427 |
|
LONG_DELAY_MS, MILLISECONDS, |
428 |
|
new ArrayBlockingQueue<Runnable>(10)); |
429 |
< |
assertFalse(p.isShutdown()); |
430 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
431 |
< |
assertTrue(p.isShutdown()); |
432 |
< |
joinPool(p); |
429 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
430 |
> |
assertFalse(p.isShutdown()); |
431 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
432 |
> |
assertTrue(p.isShutdown()); |
433 |
> |
} |
434 |
|
} |
435 |
|
|
436 |
|
/** |
471 |
|
new ThreadPoolExecutor(1, 1, |
472 |
|
LONG_DELAY_MS, MILLISECONDS, |
473 |
|
new ArrayBlockingQueue<Runnable>(10)); |
474 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
475 |
< |
final CountDownLatch done = new CountDownLatch(1); |
476 |
< |
assertFalse(p.isTerminated()); |
477 |
< |
try { |
474 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
475 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
476 |
> |
final CountDownLatch done = new CountDownLatch(1); |
477 |
> |
assertFalse(p.isTerminating()); |
478 |
|
p.execute(new CheckedRunnable() { |
479 |
|
public void realRun() throws InterruptedException { |
480 |
< |
assertFalse(p.isTerminated()); |
480 |
> |
assertFalse(p.isTerminating()); |
481 |
|
threadStarted.countDown(); |
482 |
|
done.await(); |
483 |
|
}}); |
484 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
484 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
485 |
|
assertFalse(p.isTerminating()); |
486 |
|
done.countDown(); |
493 |
– |
} finally { |
487 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
488 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
489 |
+ |
assertTrue(p.isTerminated()); |
490 |
+ |
assertFalse(p.isTerminating()); |
491 |
|
} |
496 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
497 |
– |
assertTrue(p.isTerminated()); |
492 |
|
} |
493 |
|
|
494 |
|
/** |
509 |
|
threadStarted.countDown(); |
510 |
|
done.await(); |
511 |
|
}}); |
512 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
512 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
513 |
|
assertFalse(p.isTerminating()); |
514 |
|
done.countDown(); |
515 |
|
} finally { |
544 |
|
tasks[i] = new FutureTask(task); |
545 |
|
p.execute(tasks[i]); |
546 |
|
} |
547 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
547 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
548 |
|
assertSame(q, p.getQueue()); |
549 |
|
assertFalse(q.contains(tasks[0])); |
550 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
576 |
|
}}; |
577 |
|
p.execute(tasks[i]); |
578 |
|
} |
579 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
579 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
580 |
|
assertFalse(p.remove(tasks[0])); |
581 |
|
assertTrue(q.contains(tasks[4])); |
582 |
|
assertTrue(q.contains(tasks[3])); |
615 |
|
tasks[i] = new FutureTask(task); |
616 |
|
p.execute(tasks[i]); |
617 |
|
} |
618 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
618 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
619 |
|
assertEquals(tasks.length, p.getTaskCount()); |
620 |
|
assertEquals(tasks.length - 1, q.size()); |
621 |
|
assertEquals(1L, p.getActiveCount()); |
1037 |
|
p.submit(task).get(); |
1038 |
|
}}); |
1039 |
|
|
1040 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1040 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
1041 |
|
t.interrupt(); |
1042 |
|
awaitTermination(t, MEDIUM_DELAY_MS); |
1043 |
|
} finally { |