217 |
|
new ThreadPoolExecutor(1, 1, |
218 |
|
LONG_DELAY_MS, MILLISECONDS, |
219 |
|
new ArrayBlockingQueue<Runnable>(10)); |
220 |
< |
assertEquals(1, p.getCorePoolSize()); |
221 |
< |
joinPool(p); |
220 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
221 |
> |
assertEquals(1, p.getCorePoolSize()); |
222 |
> |
} |
223 |
|
} |
224 |
|
|
225 |
|
/** |
230 |
|
new ThreadPoolExecutor(2, 2, |
231 |
|
1000, MILLISECONDS, |
232 |
|
new ArrayBlockingQueue<Runnable>(10)); |
233 |
< |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
234 |
< |
joinPool(p); |
233 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
234 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
235 |
> |
} |
236 |
|
} |
237 |
|
|
238 |
|
/** |
239 |
|
* getThreadFactory returns factory in constructor if not set |
240 |
|
*/ |
241 |
|
public void testGetThreadFactory() { |
242 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
242 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
243 |
|
final ThreadPoolExecutor p = |
244 |
|
new ThreadPoolExecutor(1, 2, |
245 |
|
LONG_DELAY_MS, MILLISECONDS, |
246 |
|
new ArrayBlockingQueue<Runnable>(10), |
247 |
< |
tf, |
247 |
> |
threadFactory, |
248 |
|
new NoOpREHandler()); |
249 |
< |
assertSame(tf, p.getThreadFactory()); |
250 |
< |
joinPool(p); |
249 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
250 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
251 |
> |
} |
252 |
|
} |
253 |
|
|
254 |
|
/** |
259 |
|
new ThreadPoolExecutor(1, 2, |
260 |
|
LONG_DELAY_MS, MILLISECONDS, |
261 |
|
new ArrayBlockingQueue<Runnable>(10)); |
262 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
263 |
< |
p.setThreadFactory(tf); |
264 |
< |
assertSame(tf, p.getThreadFactory()); |
265 |
< |
joinPool(p); |
262 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
263 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
264 |
> |
p.setThreadFactory(threadFactory); |
265 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
266 |
> |
} |
267 |
|
} |
268 |
|
|
269 |
|
/** |
274 |
|
new ThreadPoolExecutor(1, 2, |
275 |
|
LONG_DELAY_MS, MILLISECONDS, |
276 |
|
new ArrayBlockingQueue<Runnable>(10)); |
277 |
< |
try { |
278 |
< |
p.setThreadFactory(null); |
279 |
< |
shouldThrow(); |
280 |
< |
} catch (NullPointerException success) { |
281 |
< |
} finally { |
278 |
< |
joinPool(p); |
277 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
278 |
> |
try { |
279 |
> |
p.setThreadFactory(null); |
280 |
> |
shouldThrow(); |
281 |
> |
} catch (NullPointerException success) {} |
282 |
|
} |
283 |
|
} |
284 |
|
|
286 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
287 |
|
*/ |
288 |
|
public void testGetRejectedExecutionHandler() { |
289 |
< |
final RejectedExecutionHandler h = new NoOpREHandler(); |
289 |
> |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
290 |
|
final ThreadPoolExecutor p = |
291 |
|
new ThreadPoolExecutor(1, 2, |
292 |
|
LONG_DELAY_MS, MILLISECONDS, |
293 |
|
new ArrayBlockingQueue<Runnable>(10), |
294 |
< |
h); |
295 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
296 |
< |
joinPool(p); |
294 |
> |
handler); |
295 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
296 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
297 |
> |
} |
298 |
|
} |
299 |
|
|
300 |
|
/** |
306 |
|
new ThreadPoolExecutor(1, 2, |
307 |
|
LONG_DELAY_MS, MILLISECONDS, |
308 |
|
new ArrayBlockingQueue<Runnable>(10)); |
309 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
310 |
< |
p.setRejectedExecutionHandler(h); |
311 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
312 |
< |
joinPool(p); |
309 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
310 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
311 |
> |
p.setRejectedExecutionHandler(handler); |
312 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
313 |
> |
} |
314 |
|
} |
315 |
|
|
316 |
|
/** |
321 |
|
new ThreadPoolExecutor(1, 2, |
322 |
|
LONG_DELAY_MS, MILLISECONDS, |
323 |
|
new ArrayBlockingQueue<Runnable>(10)); |
324 |
< |
try { |
325 |
< |
p.setRejectedExecutionHandler(null); |
326 |
< |
shouldThrow(); |
327 |
< |
} catch (NullPointerException success) { |
328 |
< |
} finally { |
324 |
< |
joinPool(p); |
324 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
325 |
> |
try { |
326 |
> |
p.setRejectedExecutionHandler(null); |
327 |
> |
shouldThrow(); |
328 |
> |
} catch (NullPointerException success) {} |
329 |
|
} |
330 |
|
} |
331 |
|
|
339 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
340 |
|
LONG_DELAY_MS, MILLISECONDS, |
341 |
|
new ArrayBlockingQueue<Runnable>(10)); |
342 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
343 |
< |
final CountDownLatch done = new CountDownLatch(1); |
344 |
< |
try { |
342 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
343 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
344 |
> |
final CountDownLatch done = new CountDownLatch(1); |
345 |
|
assertEquals(0, p.getLargestPoolSize()); |
346 |
|
for (int i = 0; i < THREADS; i++) |
347 |
|
p.execute(new CheckedRunnable() { |
350 |
|
done.await(); |
351 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
352 |
|
}}); |
353 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
350 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
351 |
< |
} finally { |
352 |
< |
done.countDown(); |
353 |
< |
joinPool(p); |
353 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
354 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
355 |
+ |
done.countDown(); // release pool |
356 |
|
} |
357 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
358 |
|
} |
359 |
|
|
360 |
|
/** |
366 |
|
new ThreadPoolExecutor(2, 3, |
367 |
|
LONG_DELAY_MS, MILLISECONDS, |
368 |
|
new ArrayBlockingQueue<Runnable>(10)); |
369 |
< |
assertEquals(3, p.getMaximumPoolSize()); |
370 |
< |
joinPool(p); |
369 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
370 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
371 |
> |
p.setMaximumPoolSize(5); |
372 |
> |
assertEquals(5, p.getMaximumPoolSize()); |
373 |
> |
p.setMaximumPoolSize(4); |
374 |
> |
assertEquals(4, p.getMaximumPoolSize()); |
375 |
> |
} |
376 |
|
} |
377 |
|
|
378 |
|
/** |
384 |
|
new ThreadPoolExecutor(1, 1, |
385 |
|
LONG_DELAY_MS, MILLISECONDS, |
386 |
|
new ArrayBlockingQueue<Runnable>(10)); |
387 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
388 |
< |
final CountDownLatch done = new CountDownLatch(1); |
389 |
< |
try { |
387 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
388 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
389 |
> |
final CountDownLatch done = new CountDownLatch(1); |
390 |
|
assertEquals(0, p.getPoolSize()); |
391 |
|
p.execute(new CheckedRunnable() { |
392 |
|
public void realRun() throws InterruptedException { |
394 |
|
assertEquals(1, p.getPoolSize()); |
395 |
|
done.await(); |
396 |
|
}}); |
397 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
397 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
398 |
|
assertEquals(1, p.getPoolSize()); |
399 |
< |
} finally { |
393 |
< |
done.countDown(); |
394 |
< |
joinPool(p); |
399 |
> |
done.countDown(); // release pool |
400 |
|
} |
401 |
|
} |
402 |
|
|