6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.concurrent.locks.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
|
|
12 |
< |
import junit.framework.*; |
13 |
< |
import java.util.*; |
12 |
> |
import java.util.ArrayList; |
13 |
> |
import java.util.List; |
14 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.Callable; |
17 |
> |
import java.util.concurrent.CancellationException; |
18 |
> |
import java.util.concurrent.CountDownLatch; |
19 |
> |
import java.util.concurrent.ExecutionException; |
20 |
> |
import java.util.concurrent.Executors; |
21 |
> |
import java.util.concurrent.ExecutorService; |
22 |
> |
import java.util.concurrent.Future; |
23 |
> |
import java.util.concurrent.FutureTask; |
24 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
25 |
> |
import java.util.concurrent.RejectedExecutionException; |
26 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
27 |
> |
import java.util.concurrent.RunnableFuture; |
28 |
> |
import java.util.concurrent.SynchronousQueue; |
29 |
> |
import java.util.concurrent.ThreadFactory; |
30 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
31 |
> |
import java.util.concurrent.TimeoutException; |
32 |
> |
import java.util.concurrent.TimeUnit; |
33 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
34 |
> |
import java.util.concurrent.locks.Condition; |
35 |
> |
import java.util.concurrent.locks.ReentrantLock; |
36 |
> |
|
37 |
> |
import junit.framework.Test; |
38 |
> |
import junit.framework.TestSuite; |
39 |
|
|
40 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
41 |
|
public static void main(String[] args) { |
42 |
< |
junit.textui.TestRunner.run(suite()); |
42 |
> |
main(suite(), args); |
43 |
|
} |
44 |
|
public static Test suite() { |
45 |
|
return new TestSuite(ThreadPoolExecutorSubclassTest.class); |
61 |
|
CustomTask(final Runnable r, final V res) { |
62 |
|
if (r == null) throw new NullPointerException(); |
63 |
|
callable = new Callable<V>() { |
64 |
< |
public V call() throws Exception { r.run(); return res; }}; |
64 |
> |
public V call() throws Exception { r.run(); return res; }}; |
65 |
|
} |
66 |
|
public boolean isDone() { |
67 |
|
lock.lock(); try { return done; } finally { lock.unlock() ; } |
101 |
|
} |
102 |
|
lock.lock(); |
103 |
|
try { |
104 |
< |
result = v; |
105 |
< |
exception = e; |
106 |
< |
done = true; |
107 |
< |
thread = null; |
108 |
< |
cond.signalAll(); |
104 |
> |
if (!done) { |
105 |
> |
result = v; |
106 |
> |
exception = e; |
107 |
> |
done = true; |
108 |
> |
thread = null; |
109 |
> |
cond.signalAll(); |
110 |
> |
} |
111 |
|
} |
112 |
|
finally { lock.unlock(); } |
113 |
|
} |
116 |
|
try { |
117 |
|
while (!done) |
118 |
|
cond.await(); |
119 |
+ |
if (cancelled) |
120 |
+ |
throw new CancellationException(); |
121 |
|
if (exception != null) |
122 |
|
throw new ExecutionException(exception); |
123 |
|
return result; |
129 |
|
long nanos = unit.toNanos(timeout); |
130 |
|
lock.lock(); |
131 |
|
try { |
132 |
< |
for (;;) { |
133 |
< |
if (done) break; |
106 |
< |
if (nanos < 0) |
132 |
> |
while (!done) { |
133 |
> |
if (nanos <= 0L) |
134 |
|
throw new TimeoutException(); |
135 |
|
nanos = cond.awaitNanos(nanos); |
136 |
|
} |
137 |
+ |
if (cancelled) |
138 |
+ |
throw new CancellationException(); |
139 |
|
if (exception != null) |
140 |
|
throw new ExecutionException(exception); |
141 |
|
return result; |
144 |
|
} |
145 |
|
} |
146 |
|
|
118 |
– |
|
147 |
|
static class CustomTPE extends ThreadPoolExecutor { |
148 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
149 |
|
return new CustomTask<V>(c); |
190 |
|
workQueue, threadFactory, handler); |
191 |
|
} |
192 |
|
|
193 |
< |
volatile boolean beforeCalled = false; |
194 |
< |
volatile boolean afterCalled = false; |
195 |
< |
volatile boolean terminatedCalled = false; |
193 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
194 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
195 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
196 |
> |
|
197 |
|
public CustomTPE() { |
198 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
199 |
|
} |
200 |
|
protected void beforeExecute(Thread t, Runnable r) { |
201 |
< |
beforeCalled = true; |
201 |
> |
beforeCalled.countDown(); |
202 |
|
} |
203 |
|
protected void afterExecute(Runnable r, Throwable t) { |
204 |
< |
afterCalled = true; |
204 |
> |
afterCalled.countDown(); |
205 |
|
} |
206 |
|
protected void terminated() { |
207 |
< |
terminatedCalled = true; |
207 |
> |
terminatedCalled.countDown(); |
208 |
|
} |
209 |
|
|
210 |
+ |
public boolean beforeCalled() { |
211 |
+ |
return beforeCalled.getCount() == 0; |
212 |
+ |
} |
213 |
+ |
public boolean afterCalled() { |
214 |
+ |
return afterCalled.getCount() == 0; |
215 |
+ |
} |
216 |
+ |
public boolean terminatedCalled() { |
217 |
+ |
return terminatedCalled.getCount() == 0; |
218 |
+ |
} |
219 |
|
} |
220 |
|
|
221 |
|
static class FailingThreadFactory implements ThreadFactory { |
226 |
|
} |
227 |
|
} |
228 |
|
|
191 |
– |
|
229 |
|
/** |
230 |
|
* execute successfully executes a runnable |
231 |
|
*/ |
232 |
|
public void testExecute() throws InterruptedException { |
233 |
|
final ThreadPoolExecutor p = |
234 |
|
new CustomTPE(1, 1, |
235 |
< |
LONG_DELAY_MS, MILLISECONDS, |
235 |
> |
2 * LONG_DELAY_MS, MILLISECONDS, |
236 |
|
new ArrayBlockingQueue<Runnable>(10)); |
237 |
< |
final CountDownLatch done = new CountDownLatch(1); |
238 |
< |
final Runnable task = new CheckedRunnable() { |
239 |
< |
public void realRun() { |
240 |
< |
done.countDown(); |
204 |
< |
}}; |
205 |
< |
try { |
237 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
238 |
> |
final CountDownLatch done = new CountDownLatch(1); |
239 |
> |
final Runnable task = new CheckedRunnable() { |
240 |
> |
public void realRun() { done.countDown(); }}; |
241 |
|
p.execute(task); |
242 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
208 |
< |
} finally { |
209 |
< |
joinPool(p); |
242 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
243 |
|
} |
244 |
|
} |
245 |
|
|
252 |
|
new CustomTPE(2, 2, |
253 |
|
LONG_DELAY_MS, MILLISECONDS, |
254 |
|
new ArrayBlockingQueue<Runnable>(10)); |
255 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
256 |
< |
final CountDownLatch done = new CountDownLatch(1); |
257 |
< |
try { |
255 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
256 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
257 |
> |
final CountDownLatch done = new CountDownLatch(1); |
258 |
|
assertEquals(0, p.getActiveCount()); |
259 |
|
p.execute(new CheckedRunnable() { |
260 |
|
public void realRun() throws InterruptedException { |
262 |
|
assertEquals(1, p.getActiveCount()); |
263 |
|
done.await(); |
264 |
|
}}); |
265 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
265 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
266 |
|
assertEquals(1, p.getActiveCount()); |
234 |
– |
} finally { |
267 |
|
done.countDown(); |
236 |
– |
joinPool(p); |
268 |
|
} |
269 |
|
} |
270 |
|
|
272 |
|
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
273 |
|
*/ |
274 |
|
public void testPrestartCoreThread() { |
275 |
< |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
276 |
< |
assertEquals(0, p.getPoolSize()); |
277 |
< |
assertTrue(p.prestartCoreThread()); |
278 |
< |
assertEquals(1, p.getPoolSize()); |
279 |
< |
assertTrue(p.prestartCoreThread()); |
280 |
< |
assertEquals(2, p.getPoolSize()); |
281 |
< |
assertFalse(p.prestartCoreThread()); |
282 |
< |
assertEquals(2, p.getPoolSize()); |
283 |
< |
joinPool(p); |
275 |
> |
ThreadPoolExecutor p = |
276 |
> |
new CustomTPE(2, 6, |
277 |
> |
LONG_DELAY_MS, MILLISECONDS, |
278 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
279 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
280 |
> |
assertEquals(0, p.getPoolSize()); |
281 |
> |
assertTrue(p.prestartCoreThread()); |
282 |
> |
assertEquals(1, p.getPoolSize()); |
283 |
> |
assertTrue(p.prestartCoreThread()); |
284 |
> |
assertEquals(2, p.getPoolSize()); |
285 |
> |
assertFalse(p.prestartCoreThread()); |
286 |
> |
assertEquals(2, p.getPoolSize()); |
287 |
> |
p.setCorePoolSize(4); |
288 |
> |
assertTrue(p.prestartCoreThread()); |
289 |
> |
assertEquals(3, p.getPoolSize()); |
290 |
> |
assertTrue(p.prestartCoreThread()); |
291 |
> |
assertEquals(4, p.getPoolSize()); |
292 |
> |
assertFalse(p.prestartCoreThread()); |
293 |
> |
assertEquals(4, p.getPoolSize()); |
294 |
> |
} |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
|
* prestartAllCoreThreads starts all corePoolSize threads |
299 |
|
*/ |
300 |
|
public void testPrestartAllCoreThreads() { |
301 |
< |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
302 |
< |
assertEquals(0, p.getPoolSize()); |
303 |
< |
p.prestartAllCoreThreads(); |
304 |
< |
assertEquals(2, p.getPoolSize()); |
305 |
< |
p.prestartAllCoreThreads(); |
306 |
< |
assertEquals(2, p.getPoolSize()); |
307 |
< |
joinPool(p); |
301 |
> |
ThreadPoolExecutor p = |
302 |
> |
new CustomTPE(2, 6, |
303 |
> |
LONG_DELAY_MS, MILLISECONDS, |
304 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
305 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
306 |
> |
assertEquals(0, p.getPoolSize()); |
307 |
> |
p.prestartAllCoreThreads(); |
308 |
> |
assertEquals(2, p.getPoolSize()); |
309 |
> |
p.prestartAllCoreThreads(); |
310 |
> |
assertEquals(2, p.getPoolSize()); |
311 |
> |
p.setCorePoolSize(4); |
312 |
> |
p.prestartAllCoreThreads(); |
313 |
> |
assertEquals(4, p.getPoolSize()); |
314 |
> |
p.prestartAllCoreThreads(); |
315 |
> |
assertEquals(4, p.getPoolSize()); |
316 |
> |
} |
317 |
|
} |
318 |
|
|
319 |
|
/** |
325 |
|
new CustomTPE(2, 2, |
326 |
|
LONG_DELAY_MS, MILLISECONDS, |
327 |
|
new ArrayBlockingQueue<Runnable>(10)); |
328 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
329 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
330 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
331 |
< |
try { |
328 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
329 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
330 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
331 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
332 |
|
assertEquals(0, p.getCompletedTaskCount()); |
333 |
|
p.execute(new CheckedRunnable() { |
334 |
|
public void realRun() throws InterruptedException { |
337 |
|
threadProceed.await(); |
338 |
|
threadDone.countDown(); |
339 |
|
}}); |
340 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
340 |
> |
await(threadStarted); |
341 |
|
assertEquals(0, p.getCompletedTaskCount()); |
342 |
|
threadProceed.countDown(); |
343 |
|
threadDone.await(); |
344 |
< |
Thread.sleep(SHORT_DELAY_MS); |
345 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
346 |
< |
} finally { |
347 |
< |
joinPool(p); |
344 |
> |
long startTime = System.nanoTime(); |
345 |
> |
while (p.getCompletedTaskCount() != 1) { |
346 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
347 |
> |
fail("timed out"); |
348 |
> |
Thread.yield(); |
349 |
> |
} |
350 |
|
} |
351 |
|
} |
352 |
|
|
354 |
|
* getCorePoolSize returns size given in constructor if not otherwise set |
355 |
|
*/ |
356 |
|
public void testGetCorePoolSize() { |
357 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
358 |
< |
assertEquals(1, p.getCorePoolSize()); |
359 |
< |
joinPool(p); |
357 |
> |
final ThreadPoolExecutor p = |
358 |
> |
new CustomTPE(1, 1, |
359 |
> |
LONG_DELAY_MS, MILLISECONDS, |
360 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
361 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
362 |
> |
assertEquals(1, p.getCorePoolSize()); |
363 |
> |
} |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
|
* getKeepAliveTime returns value given in constructor if not otherwise set |
368 |
|
*/ |
369 |
|
public void testGetKeepAliveTime() { |
370 |
< |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
371 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
372 |
< |
joinPool(p); |
370 |
> |
final ThreadPoolExecutor p = |
371 |
> |
new CustomTPE(2, 2, |
372 |
> |
1000, MILLISECONDS, |
373 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
374 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
375 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
376 |
> |
} |
377 |
|
} |
378 |
|
|
318 |
– |
|
379 |
|
/** |
380 |
|
* getThreadFactory returns factory in constructor if not set |
381 |
|
*/ |
382 |
|
public void testGetThreadFactory() { |
383 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
384 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
385 |
< |
assertSame(tf, p.getThreadFactory()); |
386 |
< |
joinPool(p); |
383 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
384 |
> |
final ThreadPoolExecutor p = |
385 |
> |
new CustomTPE(1, 2, |
386 |
> |
LONG_DELAY_MS, MILLISECONDS, |
387 |
> |
new ArrayBlockingQueue<Runnable>(10), |
388 |
> |
threadFactory, |
389 |
> |
new NoOpREHandler()); |
390 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
391 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
392 |
> |
} |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
397 |
|
*/ |
398 |
|
public void testSetThreadFactory() { |
399 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
400 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
401 |
< |
p.setThreadFactory(tf); |
402 |
< |
assertSame(tf, p.getThreadFactory()); |
403 |
< |
joinPool(p); |
399 |
> |
final ThreadPoolExecutor p = |
400 |
> |
new CustomTPE(1, 2, |
401 |
> |
LONG_DELAY_MS, MILLISECONDS, |
402 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
403 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
404 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
405 |
> |
p.setThreadFactory(threadFactory); |
406 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
407 |
> |
} |
408 |
|
} |
409 |
|
|
340 |
– |
|
410 |
|
/** |
411 |
|
* setThreadFactory(null) throws NPE |
412 |
|
*/ |
413 |
|
public void testSetThreadFactoryNull() { |
414 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
415 |
< |
try { |
416 |
< |
p.setThreadFactory(null); |
417 |
< |
shouldThrow(); |
418 |
< |
} catch (NullPointerException success) { |
419 |
< |
} finally { |
420 |
< |
joinPool(p); |
414 |
> |
final ThreadPoolExecutor p = |
415 |
> |
new CustomTPE(1, 2, |
416 |
> |
LONG_DELAY_MS, MILLISECONDS, |
417 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
418 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
419 |
> |
try { |
420 |
> |
p.setThreadFactory(null); |
421 |
> |
shouldThrow(); |
422 |
> |
} catch (NullPointerException success) {} |
423 |
|
} |
424 |
|
} |
425 |
|
|
427 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
428 |
|
*/ |
429 |
|
public void testGetRejectedExecutionHandler() { |
430 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
431 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
432 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
433 |
< |
joinPool(p); |
430 |
> |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
431 |
> |
final ThreadPoolExecutor p = |
432 |
> |
new CustomTPE(1, 2, |
433 |
> |
LONG_DELAY_MS, MILLISECONDS, |
434 |
> |
new ArrayBlockingQueue<Runnable>(10), |
435 |
> |
handler); |
436 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
437 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
438 |
> |
} |
439 |
|
} |
440 |
|
|
441 |
|
/** |
443 |
|
* getRejectedExecutionHandler |
444 |
|
*/ |
445 |
|
public void testSetRejectedExecutionHandler() { |
446 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
447 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
448 |
< |
p.setRejectedExecutionHandler(h); |
449 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
450 |
< |
joinPool(p); |
446 |
> |
final ThreadPoolExecutor p = |
447 |
> |
new CustomTPE(1, 2, |
448 |
> |
LONG_DELAY_MS, MILLISECONDS, |
449 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
450 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
451 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
452 |
> |
p.setRejectedExecutionHandler(handler); |
453 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
454 |
> |
} |
455 |
|
} |
456 |
|
|
377 |
– |
|
457 |
|
/** |
458 |
|
* setRejectedExecutionHandler(null) throws NPE |
459 |
|
*/ |
460 |
|
public void testSetRejectedExecutionHandlerNull() { |
461 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
462 |
< |
try { |
463 |
< |
p.setRejectedExecutionHandler(null); |
464 |
< |
shouldThrow(); |
465 |
< |
} catch (NullPointerException success) { |
466 |
< |
} finally { |
467 |
< |
joinPool(p); |
461 |
> |
final ThreadPoolExecutor p = |
462 |
> |
new CustomTPE(1, 2, |
463 |
> |
LONG_DELAY_MS, MILLISECONDS, |
464 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
465 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
466 |
> |
try { |
467 |
> |
p.setRejectedExecutionHandler(null); |
468 |
> |
shouldThrow(); |
469 |
> |
} catch (NullPointerException success) {} |
470 |
|
} |
471 |
|
} |
472 |
|
|
392 |
– |
|
473 |
|
/** |
474 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
475 |
|
* multiple threads active |
480 |
|
new CustomTPE(THREADS, THREADS, |
481 |
|
LONG_DELAY_MS, MILLISECONDS, |
482 |
|
new ArrayBlockingQueue<Runnable>(10)); |
483 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
484 |
< |
final CountDownLatch done = new CountDownLatch(1); |
485 |
< |
try { |
483 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
484 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
485 |
> |
final CountDownLatch done = new CountDownLatch(1); |
486 |
|
assertEquals(0, p.getLargestPoolSize()); |
487 |
|
for (int i = 0; i < THREADS; i++) |
488 |
|
p.execute(new CheckedRunnable() { |
491 |
|
done.await(); |
492 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
493 |
|
}}); |
494 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
415 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
416 |
< |
} finally { |
417 |
< |
done.countDown(); |
418 |
< |
joinPool(p); |
494 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
495 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
496 |
+ |
done.countDown(); // release pool |
497 |
|
} |
498 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
503 |
|
* otherwise set |
504 |
|
*/ |
505 |
|
public void testGetMaximumPoolSize() { |
506 |
< |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
507 |
< |
assertEquals(2, p.getMaximumPoolSize()); |
508 |
< |
joinPool(p); |
506 |
> |
final ThreadPoolExecutor p = |
507 |
> |
new CustomTPE(2, 3, |
508 |
> |
LONG_DELAY_MS, MILLISECONDS, |
509 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
510 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
511 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
512 |
> |
p.setMaximumPoolSize(5); |
513 |
> |
assertEquals(5, p.getMaximumPoolSize()); |
514 |
> |
p.setMaximumPoolSize(4); |
515 |
> |
assertEquals(4, p.getMaximumPoolSize()); |
516 |
> |
} |
517 |
|
} |
518 |
|
|
519 |
|
/** |
525 |
|
new CustomTPE(1, 1, |
526 |
|
LONG_DELAY_MS, MILLISECONDS, |
527 |
|
new ArrayBlockingQueue<Runnable>(10)); |
528 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
529 |
< |
final CountDownLatch done = new CountDownLatch(1); |
530 |
< |
try { |
528 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
529 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
530 |
> |
final CountDownLatch done = new CountDownLatch(1); |
531 |
|
assertEquals(0, p.getPoolSize()); |
532 |
|
p.execute(new CheckedRunnable() { |
533 |
|
public void realRun() throws InterruptedException { |
535 |
|
assertEquals(1, p.getPoolSize()); |
536 |
|
done.await(); |
537 |
|
}}); |
538 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
538 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
539 |
|
assertEquals(1, p.getPoolSize()); |
540 |
< |
} finally { |
455 |
< |
done.countDown(); |
456 |
< |
joinPool(p); |
540 |
> |
done.countDown(); // release pool |
541 |
|
} |
542 |
|
} |
543 |
|
|
549 |
|
new CustomTPE(1, 1, |
550 |
|
LONG_DELAY_MS, MILLISECONDS, |
551 |
|
new ArrayBlockingQueue<Runnable>(10)); |
552 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
553 |
< |
final CountDownLatch done = new CountDownLatch(1); |
554 |
< |
try { |
552 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
553 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
554 |
> |
final CountDownLatch done = new CountDownLatch(1); |
555 |
|
assertEquals(0, p.getTaskCount()); |
556 |
|
p.execute(new CheckedRunnable() { |
557 |
|
public void realRun() throws InterruptedException { |
559 |
|
assertEquals(1, p.getTaskCount()); |
560 |
|
done.await(); |
561 |
|
}}); |
562 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
562 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
563 |
|
assertEquals(1, p.getTaskCount()); |
480 |
– |
} finally { |
564 |
|
done.countDown(); |
482 |
– |
joinPool(p); |
565 |
|
} |
566 |
|
} |
567 |
|
|
568 |
|
/** |
569 |
< |
* isShutDown is false before shutdown, true after |
569 |
> |
* isShutdown is false before shutdown, true after |
570 |
|
*/ |
571 |
|
public void testIsShutdown() { |
572 |
< |
|
573 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
574 |
< |
assertFalse(p.isShutdown()); |
575 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
576 |
< |
assertTrue(p.isShutdown()); |
577 |
< |
joinPool(p); |
572 |
> |
final ThreadPoolExecutor p = |
573 |
> |
new CustomTPE(1, 1, |
574 |
> |
LONG_DELAY_MS, MILLISECONDS, |
575 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
576 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
577 |
> |
assertFalse(p.isShutdown()); |
578 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
579 |
> |
assertTrue(p.isShutdown()); |
580 |
> |
} |
581 |
|
} |
582 |
|
|
498 |
– |
|
583 |
|
/** |
584 |
|
* isTerminated is false before termination, true after |
585 |
|
*/ |
588 |
|
new CustomTPE(1, 1, |
589 |
|
LONG_DELAY_MS, MILLISECONDS, |
590 |
|
new ArrayBlockingQueue<Runnable>(10)); |
591 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
592 |
< |
final CountDownLatch done = new CountDownLatch(1); |
593 |
< |
try { |
591 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
592 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
593 |
> |
final CountDownLatch done = new CountDownLatch(1); |
594 |
|
assertFalse(p.isTerminating()); |
595 |
|
p.execute(new CheckedRunnable() { |
596 |
|
public void realRun() throws InterruptedException { |
598 |
|
threadStarted.countDown(); |
599 |
|
done.await(); |
600 |
|
}}); |
601 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
601 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
602 |
|
assertFalse(p.isTerminating()); |
603 |
|
done.countDown(); |
520 |
– |
} finally { |
604 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
605 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
606 |
+ |
assertTrue(p.isTerminated()); |
607 |
+ |
assertFalse(p.isTerminating()); |
608 |
|
} |
523 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
524 |
– |
assertTrue(p.isTerminated()); |
525 |
– |
assertFalse(p.isTerminating()); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
626 |
|
threadStarted.countDown(); |
627 |
|
done.await(); |
628 |
|
}}); |
629 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
629 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
630 |
|
assertFalse(p.isTerminating()); |
631 |
|
done.countDown(); |
632 |
|
} finally { |
661 |
|
tasks[i] = new FutureTask(task); |
662 |
|
p.execute(tasks[i]); |
663 |
|
} |
664 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
664 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
665 |
|
assertSame(q, p.getQueue()); |
666 |
|
assertFalse(q.contains(tasks[0])); |
667 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
693 |
|
}}; |
694 |
|
p.execute(tasks[i]); |
695 |
|
} |
696 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
696 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
697 |
|
assertFalse(p.remove(tasks[0])); |
698 |
|
assertTrue(q.contains(tasks[4])); |
699 |
|
assertTrue(q.contains(tasks[3])); |
732 |
|
tasks[i] = new FutureTask(task); |
733 |
|
p.execute(tasks[i]); |
734 |
|
} |
735 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
735 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
736 |
|
assertEquals(tasks.length, p.getTaskCount()); |
737 |
|
assertEquals(tasks.length - 1, q.size()); |
738 |
|
assertEquals(1L, p.getActiveCount()); |
752 |
|
} |
753 |
|
|
754 |
|
/** |
755 |
< |
* shutDownNow returns a list containing tasks that were not run |
755 |
> |
* shutdownNow returns a list containing tasks that were not run, |
756 |
> |
* and those tasks are drained from the queue |
757 |
|
*/ |
758 |
< |
public void testShutDownNow() { |
759 |
< |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
760 |
< |
List l; |
761 |
< |
try { |
762 |
< |
for (int i = 0; i < 5; i++) |
763 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
764 |
< |
} |
765 |
< |
finally { |
758 |
> |
public void testShutdownNow() throws InterruptedException { |
759 |
> |
final int poolSize = 2; |
760 |
> |
final int count = 5; |
761 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
762 |
> |
ThreadPoolExecutor p = |
763 |
> |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
764 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
765 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
766 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
767 |
> |
threadsStarted.countDown(); |
768 |
|
try { |
769 |
< |
l = p.shutdownNow(); |
770 |
< |
} catch (SecurityException ok) { return; } |
769 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
770 |
> |
} catch (InterruptedException success) {} |
771 |
> |
ran.getAndIncrement(); |
772 |
> |
}}; |
773 |
> |
for (int i = 0; i < count; i++) |
774 |
> |
p.execute(waiter); |
775 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
776 |
> |
assertEquals(poolSize, p.getActiveCount()); |
777 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
778 |
> |
final List<Runnable> queuedTasks; |
779 |
> |
try { |
780 |
> |
queuedTasks = p.shutdownNow(); |
781 |
> |
} catch (SecurityException ok) { |
782 |
> |
return; // Allowed in case test doesn't have privs |
783 |
|
} |
784 |
|
assertTrue(p.isShutdown()); |
785 |
< |
assertTrue(l.size() <= 4); |
785 |
> |
assertTrue(p.getQueue().isEmpty()); |
786 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
787 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
788 |
> |
assertTrue(p.isTerminated()); |
789 |
> |
assertEquals(poolSize, ran.get()); |
790 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
791 |
|
} |
792 |
|
|
793 |
|
// Exception Tests |
794 |
|
|
692 |
– |
|
795 |
|
/** |
796 |
|
* Constructor throws if corePoolSize argument is less than zero |
797 |
|
*/ |
798 |
|
public void testConstructor1() { |
799 |
|
try { |
800 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
800 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
801 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
802 |
|
shouldThrow(); |
803 |
|
} catch (IllegalArgumentException success) {} |
804 |
|
} |
808 |
|
*/ |
809 |
|
public void testConstructor2() { |
810 |
|
try { |
811 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
811 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
812 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
813 |
|
shouldThrow(); |
814 |
|
} catch (IllegalArgumentException success) {} |
815 |
|
} |
819 |
|
*/ |
820 |
|
public void testConstructor3() { |
821 |
|
try { |
822 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
822 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
823 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
824 |
|
shouldThrow(); |
825 |
|
} catch (IllegalArgumentException success) {} |
826 |
|
} |
830 |
|
*/ |
831 |
|
public void testConstructor4() { |
832 |
|
try { |
833 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
833 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
834 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
835 |
|
shouldThrow(); |
836 |
|
} catch (IllegalArgumentException success) {} |
837 |
|
} |
841 |
|
*/ |
842 |
|
public void testConstructor5() { |
843 |
|
try { |
844 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
844 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
845 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
846 |
|
shouldThrow(); |
847 |
|
} catch (IllegalArgumentException success) {} |
848 |
|
} |
852 |
|
*/ |
853 |
|
public void testConstructorNullPointerException() { |
854 |
|
try { |
855 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
855 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
856 |
|
shouldThrow(); |
857 |
|
} catch (NullPointerException success) {} |
858 |
|
} |
859 |
|
|
753 |
– |
|
754 |
– |
|
860 |
|
/** |
861 |
|
* Constructor throws if corePoolSize argument is less than zero |
862 |
|
*/ |
863 |
|
public void testConstructor6() { |
864 |
|
try { |
865 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
865 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
866 |
> |
new ArrayBlockingQueue<Runnable>(10), |
867 |
> |
new SimpleThreadFactory()); |
868 |
|
shouldThrow(); |
869 |
|
} catch (IllegalArgumentException success) {} |
870 |
|
} |
874 |
|
*/ |
875 |
|
public void testConstructor7() { |
876 |
|
try { |
877 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
877 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
878 |
> |
new ArrayBlockingQueue<Runnable>(10), |
879 |
> |
new SimpleThreadFactory()); |
880 |
|
shouldThrow(); |
881 |
|
} catch (IllegalArgumentException success) {} |
882 |
|
} |
886 |
|
*/ |
887 |
|
public void testConstructor8() { |
888 |
|
try { |
889 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
889 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
890 |
> |
new ArrayBlockingQueue<Runnable>(10), |
891 |
> |
new SimpleThreadFactory()); |
892 |
|
shouldThrow(); |
893 |
|
} catch (IllegalArgumentException success) {} |
894 |
|
} |
898 |
|
*/ |
899 |
|
public void testConstructor9() { |
900 |
|
try { |
901 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
901 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
902 |
> |
new ArrayBlockingQueue<Runnable>(10), |
903 |
> |
new SimpleThreadFactory()); |
904 |
|
shouldThrow(); |
905 |
|
} catch (IllegalArgumentException success) {} |
906 |
|
} |
910 |
|
*/ |
911 |
|
public void testConstructor10() { |
912 |
|
try { |
913 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
913 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
914 |
> |
new ArrayBlockingQueue<Runnable>(10), |
915 |
> |
new SimpleThreadFactory()); |
916 |
|
shouldThrow(); |
917 |
|
} catch (IllegalArgumentException success) {} |
918 |
|
} |
922 |
|
*/ |
923 |
|
public void testConstructorNullPointerException2() { |
924 |
|
try { |
925 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
925 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
926 |
|
shouldThrow(); |
927 |
|
} catch (NullPointerException success) {} |
928 |
|
} |
932 |
|
*/ |
933 |
|
public void testConstructorNullPointerException3() { |
934 |
|
try { |
935 |
< |
ThreadFactory f = null; |
936 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
935 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
936 |
> |
new ArrayBlockingQueue<Runnable>(10), |
937 |
> |
(ThreadFactory) null); |
938 |
|
shouldThrow(); |
939 |
|
} catch (NullPointerException success) {} |
940 |
|
} |
941 |
|
|
826 |
– |
|
942 |
|
/** |
943 |
|
* Constructor throws if corePoolSize argument is less than zero |
944 |
|
*/ |
945 |
|
public void testConstructor11() { |
946 |
|
try { |
947 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
947 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
948 |
> |
new ArrayBlockingQueue<Runnable>(10), |
949 |
> |
new NoOpREHandler()); |
950 |
|
shouldThrow(); |
951 |
|
} catch (IllegalArgumentException success) {} |
952 |
|
} |
956 |
|
*/ |
957 |
|
public void testConstructor12() { |
958 |
|
try { |
959 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
959 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
960 |
> |
new ArrayBlockingQueue<Runnable>(10), |
961 |
> |
new NoOpREHandler()); |
962 |
|
shouldThrow(); |
963 |
|
} catch (IllegalArgumentException success) {} |
964 |
|
} |
968 |
|
*/ |
969 |
|
public void testConstructor13() { |
970 |
|
try { |
971 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
971 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
972 |
> |
new ArrayBlockingQueue<Runnable>(10), |
973 |
> |
new NoOpREHandler()); |
974 |
|
shouldThrow(); |
975 |
|
} catch (IllegalArgumentException success) {} |
976 |
|
} |
980 |
|
*/ |
981 |
|
public void testConstructor14() { |
982 |
|
try { |
983 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
983 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
984 |
> |
new ArrayBlockingQueue<Runnable>(10), |
985 |
> |
new NoOpREHandler()); |
986 |
|
shouldThrow(); |
987 |
|
} catch (IllegalArgumentException success) {} |
988 |
|
} |
992 |
|
*/ |
993 |
|
public void testConstructor15() { |
994 |
|
try { |
995 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
995 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
996 |
> |
new ArrayBlockingQueue<Runnable>(10), |
997 |
> |
new NoOpREHandler()); |
998 |
|
shouldThrow(); |
999 |
|
} catch (IllegalArgumentException success) {} |
1000 |
|
} |
1004 |
|
*/ |
1005 |
|
public void testConstructorNullPointerException4() { |
1006 |
|
try { |
1007 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
1007 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1008 |
> |
null, |
1009 |
> |
new NoOpREHandler()); |
1010 |
|
shouldThrow(); |
1011 |
|
} catch (NullPointerException success) {} |
1012 |
|
} |
1016 |
|
*/ |
1017 |
|
public void testConstructorNullPointerException5() { |
1018 |
|
try { |
1019 |
< |
RejectedExecutionHandler r = null; |
1020 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
1019 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1020 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1021 |
> |
(RejectedExecutionHandler) null); |
1022 |
|
shouldThrow(); |
1023 |
|
} catch (NullPointerException success) {} |
1024 |
|
} |
1025 |
|
|
898 |
– |
|
1026 |
|
/** |
1027 |
|
* Constructor throws if corePoolSize argument is less than zero |
1028 |
|
*/ |
1029 |
|
public void testConstructor16() { |
1030 |
|
try { |
1031 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1031 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
1032 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1033 |
> |
new SimpleThreadFactory(), |
1034 |
> |
new NoOpREHandler()); |
1035 |
|
shouldThrow(); |
1036 |
|
} catch (IllegalArgumentException success) {} |
1037 |
|
} |
1041 |
|
*/ |
1042 |
|
public void testConstructor17() { |
1043 |
|
try { |
1044 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1044 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
1045 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1046 |
> |
new SimpleThreadFactory(), |
1047 |
> |
new NoOpREHandler()); |
1048 |
|
shouldThrow(); |
1049 |
|
} catch (IllegalArgumentException success) {} |
1050 |
|
} |
1054 |
|
*/ |
1055 |
|
public void testConstructor18() { |
1056 |
|
try { |
1057 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1057 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
1058 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1059 |
> |
new SimpleThreadFactory(), |
1060 |
> |
new NoOpREHandler()); |
1061 |
|
shouldThrow(); |
1062 |
|
} catch (IllegalArgumentException success) {} |
1063 |
|
} |
1067 |
|
*/ |
1068 |
|
public void testConstructor19() { |
1069 |
|
try { |
1070 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1070 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
1071 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1072 |
> |
new SimpleThreadFactory(), |
1073 |
> |
new NoOpREHandler()); |
1074 |
|
shouldThrow(); |
1075 |
|
} catch (IllegalArgumentException success) {} |
1076 |
|
} |
1080 |
|
*/ |
1081 |
|
public void testConstructor20() { |
1082 |
|
try { |
1083 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1083 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1084 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1085 |
> |
new SimpleThreadFactory(), |
1086 |
> |
new NoOpREHandler()); |
1087 |
|
shouldThrow(); |
1088 |
|
} catch (IllegalArgumentException success) {} |
1089 |
|
} |
1093 |
|
*/ |
1094 |
|
public void testConstructorNullPointerException6() { |
1095 |
|
try { |
1096 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1096 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1097 |
> |
null, |
1098 |
> |
new SimpleThreadFactory(), |
1099 |
> |
new NoOpREHandler()); |
1100 |
|
shouldThrow(); |
1101 |
|
} catch (NullPointerException success) {} |
1102 |
|
} |
1106 |
|
*/ |
1107 |
|
public void testConstructorNullPointerException7() { |
1108 |
|
try { |
1109 |
< |
RejectedExecutionHandler r = null; |
1110 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1109 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1110 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1111 |
> |
new SimpleThreadFactory(), |
1112 |
> |
(RejectedExecutionHandler) null); |
1113 |
|
shouldThrow(); |
1114 |
|
} catch (NullPointerException success) {} |
1115 |
|
} |
1119 |
|
*/ |
1120 |
|
public void testConstructorNullPointerException8() { |
1121 |
|
try { |
1122 |
< |
new CustomTPE(1, 2, |
976 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1122 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1123 |
|
new ArrayBlockingQueue<Runnable>(10), |
1124 |
|
(ThreadFactory) null, |
1125 |
|
new NoOpREHandler()); |
1127 |
|
} catch (NullPointerException success) {} |
1128 |
|
} |
1129 |
|
|
984 |
– |
|
1130 |
|
/** |
1131 |
|
* execute throws RejectedExecutionException if saturated. |
1132 |
|
*/ |
1276 |
|
} |
1277 |
|
} |
1278 |
|
|
1134 |
– |
|
1279 |
|
/** |
1280 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1281 |
|
*/ |
1293 |
|
} |
1294 |
|
} |
1295 |
|
|
1152 |
– |
|
1296 |
|
/** |
1297 |
|
* execute(null) throws NPE |
1298 |
|
*/ |
1299 |
|
public void testExecuteNull() { |
1300 |
< |
ThreadPoolExecutor p = null; |
1300 |
> |
ThreadPoolExecutor p = |
1301 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1302 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1303 |
|
try { |
1159 |
– |
p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1304 |
|
p.execute(null); |
1305 |
|
shouldThrow(); |
1306 |
|
} catch (NullPointerException success) {} |
1358 |
|
joinPool(p); |
1359 |
|
} |
1360 |
|
|
1217 |
– |
|
1361 |
|
/** |
1362 |
|
* setKeepAliveTime throws IllegalArgumentException |
1363 |
|
* when given a negative value |
1382 |
|
public void testTerminated() { |
1383 |
|
CustomTPE p = new CustomTPE(); |
1384 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1385 |
< |
assertTrue(p.terminatedCalled); |
1385 |
> |
assertTrue(p.terminatedCalled()); |
1386 |
|
joinPool(p); |
1387 |
|
} |
1388 |
|
|
1392 |
|
public void testBeforeAfter() throws InterruptedException { |
1393 |
|
CustomTPE p = new CustomTPE(); |
1394 |
|
try { |
1395 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1396 |
< |
p.execute(r); |
1397 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1398 |
< |
assertTrue(r.done); |
1399 |
< |
assertTrue(p.beforeCalled); |
1400 |
< |
assertTrue(p.afterCalled); |
1395 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1396 |
> |
p.execute(new CheckedRunnable() { |
1397 |
> |
public void realRun() { |
1398 |
> |
done.countDown(); |
1399 |
> |
}}); |
1400 |
> |
await(p.afterCalled); |
1401 |
> |
assertEquals(0, done.getCount()); |
1402 |
> |
assertTrue(p.afterCalled()); |
1403 |
> |
assertTrue(p.beforeCalled()); |
1404 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1405 |
|
} finally { |
1406 |
|
joinPool(p); |
1449 |
|
} |
1450 |
|
} |
1451 |
|
|
1306 |
– |
|
1452 |
|
/** |
1453 |
|
* invokeAny(null) throws NPE |
1454 |
|
*/ |
1610 |
|
} |
1611 |
|
} |
1612 |
|
|
1468 |
– |
|
1469 |
– |
|
1613 |
|
/** |
1614 |
|
* timed invokeAny(null) throws NPE |
1615 |
|
*/ |
1796 |
|
l.add(new StringTask()); |
1797 |
|
l.add(new StringTask()); |
1798 |
|
List<Future<String>> futures = |
1799 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1799 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1800 |
|
assertEquals(2, futures.size()); |
1801 |
|
for (Future<String> future : futures) |
1802 |
|
assertSame(TEST_STRING, future.get()); |
1811 |
|
public void testTimedInvokeAll6() throws Exception { |
1812 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1813 |
|
try { |
1814 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1815 |
< |
l.add(new StringTask()); |
1816 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1817 |
< |
l.add(new StringTask()); |
1818 |
< |
List<Future<String>> futures = |
1819 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1820 |
< |
assertEquals(3, futures.size()); |
1821 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1822 |
< |
Future<String> f1 = it.next(); |
1823 |
< |
Future<String> f2 = it.next(); |
1824 |
< |
Future<String> f3 = it.next(); |
1825 |
< |
assertTrue(f1.isDone()); |
1826 |
< |
assertTrue(f2.isDone()); |
1827 |
< |
assertTrue(f3.isDone()); |
1828 |
< |
assertFalse(f1.isCancelled()); |
1829 |
< |
assertTrue(f2.isCancelled()); |
1814 |
> |
for (long timeout = timeoutMillis();;) { |
1815 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1816 |
> |
tasks.add(new StringTask("0")); |
1817 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1818 |
> |
tasks.add(new StringTask("2")); |
1819 |
> |
long startTime = System.nanoTime(); |
1820 |
> |
List<Future<String>> futures = |
1821 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1822 |
> |
assertEquals(tasks.size(), futures.size()); |
1823 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1824 |
> |
for (Future future : futures) |
1825 |
> |
assertTrue(future.isDone()); |
1826 |
> |
assertTrue(futures.get(1).isCancelled()); |
1827 |
> |
try { |
1828 |
> |
assertEquals("0", futures.get(0).get()); |
1829 |
> |
assertEquals("2", futures.get(2).get()); |
1830 |
> |
break; |
1831 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1832 |
> |
timeout *= 2; |
1833 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1834 |
> |
fail("expected exactly one task to be cancelled"); |
1835 |
> |
} |
1836 |
> |
} |
1837 |
|
} finally { |
1838 |
|
joinPool(e); |
1839 |
|
} |
1876 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1877 |
|
*/ |
1878 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1879 |
+ |
long keepAliveTime = timeoutMillis(); |
1880 |
|
final ThreadPoolExecutor p = |
1881 |
|
new CustomTPE(2, 10, |
1882 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1882 |
> |
keepAliveTime, MILLISECONDS, |
1883 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1884 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1885 |
|
try { |
1886 |
|
p.allowCoreThreadTimeOut(true); |
1887 |
|
p.execute(new CheckedRunnable() { |
1888 |
< |
public void realRun() throws InterruptedException { |
1888 |
> |
public void realRun() { |
1889 |
|
threadStarted.countDown(); |
1890 |
|
assertEquals(1, p.getPoolSize()); |
1891 |
|
}}); |
1892 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1893 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1894 |
< |
if (p.getPoolSize() == 0) |
1895 |
< |
break; |
1896 |
< |
Thread.sleep(10); |
1897 |
< |
} |
1892 |
> |
await(threadStarted); |
1893 |
> |
delay(keepAliveTime); |
1894 |
> |
long startTime = System.nanoTime(); |
1895 |
> |
while (p.getPoolSize() > 0 |
1896 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1897 |
> |
Thread.yield(); |
1898 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1899 |
|
assertEquals(0, p.getPoolSize()); |
1900 |
|
} finally { |
1901 |
|
joinPool(p); |
1906 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1907 |
|
*/ |
1908 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1909 |
+ |
long keepAliveTime = timeoutMillis(); |
1910 |
|
final ThreadPoolExecutor p = |
1911 |
|
new CustomTPE(2, 10, |
1912 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1912 |
> |
keepAliveTime, MILLISECONDS, |
1913 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1914 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1915 |
|
try { |
1919 |
|
threadStarted.countDown(); |
1920 |
|
assertTrue(p.getPoolSize() >= 1); |
1921 |
|
}}); |
1922 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1922 |
> |
delay(2 * keepAliveTime); |
1923 |
|
assertTrue(p.getPoolSize() >= 1); |
1924 |
|
} finally { |
1925 |
|
joinPool(p); |
1926 |
|
} |
1927 |
|
} |
1928 |
|
|
1929 |
+ |
/** |
1930 |
+ |
* get(cancelled task) throws CancellationException |
1931 |
+ |
* (in part, a test of CustomTPE itself) |
1932 |
+ |
*/ |
1933 |
+ |
public void testGet_cancelled() throws Exception { |
1934 |
+ |
final ExecutorService e = |
1935 |
+ |
new CustomTPE(1, 1, |
1936 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1937 |
+ |
new LinkedBlockingQueue<Runnable>()); |
1938 |
+ |
try { |
1939 |
+ |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
1940 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1941 |
+ |
final List<Future<?>> futures = new ArrayList<>(); |
1942 |
+ |
for (int i = 0; i < 2; i++) { |
1943 |
+ |
Runnable r = new CheckedRunnable() { public void realRun() |
1944 |
+ |
throws Throwable { |
1945 |
+ |
blockerStarted.countDown(); |
1946 |
+ |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
1947 |
+ |
}}; |
1948 |
+ |
futures.add(e.submit(r)); |
1949 |
+ |
} |
1950 |
+ |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1951 |
+ |
for (Future<?> future : futures) future.cancel(false); |
1952 |
+ |
for (Future<?> future : futures) { |
1953 |
+ |
try { |
1954 |
+ |
future.get(); |
1955 |
+ |
shouldThrow(); |
1956 |
+ |
} catch (CancellationException success) {} |
1957 |
+ |
try { |
1958 |
+ |
future.get(LONG_DELAY_MS, MILLISECONDS); |
1959 |
+ |
shouldThrow(); |
1960 |
+ |
} catch (CancellationException success) {} |
1961 |
+ |
assertTrue(future.isCancelled()); |
1962 |
+ |
assertTrue(future.isDone()); |
1963 |
+ |
} |
1964 |
+ |
done.countDown(); |
1965 |
+ |
} finally { |
1966 |
+ |
joinPool(e); |
1967 |
+ |
} |
1968 |
+ |
} |
1969 |
+ |
|
1970 |
|
} |