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/licenses/publicdomain |
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 java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
– |
import java.util.concurrent.locks.*; |
10 |
|
|
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
11 |
> |
import java.util.ArrayList; |
12 |
> |
import java.util.List; |
13 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
14 |
> |
import java.util.concurrent.BlockingQueue; |
15 |
> |
import java.util.concurrent.Callable; |
16 |
> |
import java.util.concurrent.CountDownLatch; |
17 |
> |
import java.util.concurrent.ExecutionException; |
18 |
> |
import java.util.concurrent.Executors; |
19 |
> |
import java.util.concurrent.ExecutorService; |
20 |
> |
import java.util.concurrent.Future; |
21 |
> |
import java.util.concurrent.FutureTask; |
22 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
23 |
> |
import java.util.concurrent.RejectedExecutionException; |
24 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
25 |
> |
import java.util.concurrent.RunnableFuture; |
26 |
> |
import java.util.concurrent.SynchronousQueue; |
27 |
> |
import java.util.concurrent.ThreadFactory; |
28 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
29 |
> |
import java.util.concurrent.TimeoutException; |
30 |
> |
import java.util.concurrent.TimeUnit; |
31 |
> |
import java.util.concurrent.locks.Condition; |
32 |
> |
import java.util.concurrent.locks.ReentrantLock; |
33 |
> |
|
34 |
> |
import junit.framework.Test; |
35 |
> |
import junit.framework.TestSuite; |
36 |
|
|
37 |
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
38 |
|
public static void main(String[] args) { |
81 |
|
finally { lock.unlock() ; } |
82 |
|
} |
83 |
|
public void run() { |
63 |
– |
boolean runme; |
84 |
|
lock.lock(); |
85 |
|
try { |
86 |
< |
runme = !done; |
87 |
< |
if (!runme) |
88 |
< |
thread = Thread.currentThread(); |
86 |
> |
if (done) |
87 |
> |
return; |
88 |
> |
thread = Thread.currentThread(); |
89 |
|
} |
90 |
|
finally { lock.unlock() ; } |
71 |
– |
if (!runme) return; |
91 |
|
V v = null; |
92 |
|
Exception e = null; |
93 |
|
try { |
136 |
|
} |
137 |
|
} |
138 |
|
|
120 |
– |
|
139 |
|
static class CustomTPE extends ThreadPoolExecutor { |
140 |
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
141 |
|
return new CustomTask<V>(c); |
182 |
|
workQueue, threadFactory, handler); |
183 |
|
} |
184 |
|
|
185 |
< |
volatile boolean beforeCalled = false; |
186 |
< |
volatile boolean afterCalled = false; |
187 |
< |
volatile boolean terminatedCalled = false; |
185 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
186 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
187 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
188 |
> |
|
189 |
|
public CustomTPE() { |
190 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
191 |
|
} |
192 |
|
protected void beforeExecute(Thread t, Runnable r) { |
193 |
< |
beforeCalled = true; |
193 |
> |
beforeCalled.countDown(); |
194 |
|
} |
195 |
|
protected void afterExecute(Runnable r, Throwable t) { |
196 |
< |
afterCalled = true; |
196 |
> |
afterCalled.countDown(); |
197 |
|
} |
198 |
|
protected void terminated() { |
199 |
< |
terminatedCalled = true; |
199 |
> |
terminatedCalled.countDown(); |
200 |
|
} |
201 |
|
|
202 |
+ |
public boolean beforeCalled() { |
203 |
+ |
return beforeCalled.getCount() == 0; |
204 |
+ |
} |
205 |
+ |
public boolean afterCalled() { |
206 |
+ |
return afterCalled.getCount() == 0; |
207 |
+ |
} |
208 |
+ |
public boolean terminatedCalled() { |
209 |
+ |
return terminatedCalled.getCount() == 0; |
210 |
+ |
} |
211 |
|
} |
212 |
|
|
213 |
|
static class FailingThreadFactory implements ThreadFactory { |
218 |
|
} |
219 |
|
} |
220 |
|
|
193 |
– |
|
221 |
|
/** |
222 |
< |
* execute successfully executes a runnable |
222 |
> |
* execute successfully executes a runnable |
223 |
|
*/ |
224 |
|
public void testExecute() throws InterruptedException { |
225 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
225 |
> |
final ThreadPoolExecutor p = |
226 |
> |
new CustomTPE(1, 1, |
227 |
> |
LONG_DELAY_MS, MILLISECONDS, |
228 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
229 |
> |
final CountDownLatch done = new CountDownLatch(1); |
230 |
> |
final Runnable task = new CheckedRunnable() { |
231 |
> |
public void realRun() { |
232 |
> |
done.countDown(); |
233 |
> |
}}; |
234 |
|
try { |
235 |
< |
p1.execute(new ShortRunnable()); |
236 |
< |
Thread.sleep(SMALL_DELAY_MS); |
235 |
> |
p.execute(task); |
236 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
237 |
|
} finally { |
238 |
< |
joinPool(p1); |
238 |
> |
joinPool(p); |
239 |
|
} |
240 |
|
} |
241 |
|
|
242 |
|
/** |
243 |
< |
* getActiveCount increases but doesn't overestimate, when a |
244 |
< |
* thread becomes active |
243 |
> |
* getActiveCount increases but doesn't overestimate, when a |
244 |
> |
* thread becomes active |
245 |
|
*/ |
246 |
|
public void testGetActiveCount() throws InterruptedException { |
247 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
248 |
< |
assertEquals(0, p2.getActiveCount()); |
249 |
< |
p2.execute(new MediumRunnable()); |
250 |
< |
Thread.sleep(SHORT_DELAY_MS); |
251 |
< |
assertEquals(1, p2.getActiveCount()); |
252 |
< |
joinPool(p2); |
247 |
> |
final ThreadPoolExecutor p = |
248 |
> |
new CustomTPE(2, 2, |
249 |
> |
LONG_DELAY_MS, MILLISECONDS, |
250 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
251 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
252 |
> |
final CountDownLatch done = new CountDownLatch(1); |
253 |
> |
try { |
254 |
> |
assertEquals(0, p.getActiveCount()); |
255 |
> |
p.execute(new CheckedRunnable() { |
256 |
> |
public void realRun() throws InterruptedException { |
257 |
> |
threadStarted.countDown(); |
258 |
> |
assertEquals(1, p.getActiveCount()); |
259 |
> |
done.await(); |
260 |
> |
}}); |
261 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
262 |
> |
assertEquals(1, p.getActiveCount()); |
263 |
> |
} finally { |
264 |
> |
done.countDown(); |
265 |
> |
joinPool(p); |
266 |
> |
} |
267 |
|
} |
268 |
|
|
269 |
|
/** |
270 |
< |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
270 |
> |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
271 |
|
*/ |
272 |
|
public void testPrestartCoreThread() { |
273 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
274 |
< |
assertEquals(0, p2.getPoolSize()); |
275 |
< |
assertTrue(p2.prestartCoreThread()); |
276 |
< |
assertEquals(1, p2.getPoolSize()); |
277 |
< |
assertTrue(p2.prestartCoreThread()); |
278 |
< |
assertEquals(2, p2.getPoolSize()); |
279 |
< |
assertFalse(p2.prestartCoreThread()); |
280 |
< |
assertEquals(2, p2.getPoolSize()); |
281 |
< |
joinPool(p2); |
273 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
274 |
> |
assertEquals(0, p.getPoolSize()); |
275 |
> |
assertTrue(p.prestartCoreThread()); |
276 |
> |
assertEquals(1, p.getPoolSize()); |
277 |
> |
assertTrue(p.prestartCoreThread()); |
278 |
> |
assertEquals(2, p.getPoolSize()); |
279 |
> |
assertFalse(p.prestartCoreThread()); |
280 |
> |
assertEquals(2, p.getPoolSize()); |
281 |
> |
joinPool(p); |
282 |
|
} |
283 |
|
|
284 |
|
/** |
285 |
< |
* prestartAllCoreThreads starts all corePoolSize threads |
285 |
> |
* prestartAllCoreThreads starts all corePoolSize threads |
286 |
|
*/ |
287 |
|
public void testPrestartAllCoreThreads() { |
288 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
289 |
< |
assertEquals(0, p2.getPoolSize()); |
290 |
< |
p2.prestartAllCoreThreads(); |
291 |
< |
assertEquals(2, p2.getPoolSize()); |
292 |
< |
p2.prestartAllCoreThreads(); |
293 |
< |
assertEquals(2, p2.getPoolSize()); |
294 |
< |
joinPool(p2); |
288 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
289 |
> |
assertEquals(0, p.getPoolSize()); |
290 |
> |
p.prestartAllCoreThreads(); |
291 |
> |
assertEquals(2, p.getPoolSize()); |
292 |
> |
p.prestartAllCoreThreads(); |
293 |
> |
assertEquals(2, p.getPoolSize()); |
294 |
> |
joinPool(p); |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
299 |
< |
* when tasks complete |
298 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
299 |
> |
* when tasks complete |
300 |
|
*/ |
301 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
302 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
303 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
304 |
< |
p2.execute(new ShortRunnable()); |
305 |
< |
Thread.sleep(SMALL_DELAY_MS); |
306 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
307 |
< |
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
308 |
< |
joinPool(p2); |
302 |
> |
final ThreadPoolExecutor p = |
303 |
> |
new CustomTPE(2, 2, |
304 |
> |
LONG_DELAY_MS, MILLISECONDS, |
305 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
306 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
307 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
308 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
309 |
> |
try { |
310 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
311 |
> |
p.execute(new CheckedRunnable() { |
312 |
> |
public void realRun() throws InterruptedException { |
313 |
> |
threadStarted.countDown(); |
314 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
315 |
> |
threadProceed.await(); |
316 |
> |
threadDone.countDown(); |
317 |
> |
}}); |
318 |
> |
await(threadStarted); |
319 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
320 |
> |
threadProceed.countDown(); |
321 |
> |
threadDone.await(); |
322 |
> |
long startTime = System.nanoTime(); |
323 |
> |
while (p.getCompletedTaskCount() != 1) { |
324 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
325 |
> |
fail("timed out"); |
326 |
> |
Thread.yield(); |
327 |
> |
} |
328 |
> |
} finally { |
329 |
> |
joinPool(p); |
330 |
> |
} |
331 |
|
} |
332 |
|
|
333 |
|
/** |
334 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
334 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
335 |
|
*/ |
336 |
|
public void testGetCorePoolSize() { |
337 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
338 |
< |
assertEquals(1, p1.getCorePoolSize()); |
339 |
< |
joinPool(p1); |
337 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
338 |
> |
assertEquals(1, p.getCorePoolSize()); |
339 |
> |
joinPool(p); |
340 |
|
} |
341 |
|
|
342 |
|
/** |
343 |
< |
* getKeepAliveTime returns value given in constructor if not otherwise set |
343 |
> |
* getKeepAliveTime returns value given in constructor if not otherwise set |
344 |
|
*/ |
345 |
|
public void testGetKeepAliveTime() { |
346 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
347 |
< |
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
348 |
< |
joinPool(p2); |
346 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
347 |
> |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
348 |
> |
joinPool(p); |
349 |
|
} |
350 |
|
|
280 |
– |
|
351 |
|
/** |
352 |
|
* getThreadFactory returns factory in constructor if not set |
353 |
|
*/ |
369 |
|
joinPool(p); |
370 |
|
} |
371 |
|
|
302 |
– |
|
372 |
|
/** |
373 |
|
* setThreadFactory(null) throws NPE |
374 |
|
*/ |
405 |
|
joinPool(p); |
406 |
|
} |
407 |
|
|
339 |
– |
|
408 |
|
/** |
409 |
|
* setRejectedExecutionHandler(null) throws NPE |
410 |
|
*/ |
419 |
|
} |
420 |
|
} |
421 |
|
|
354 |
– |
|
422 |
|
/** |
423 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
424 |
< |
* multiple threads active |
423 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
424 |
> |
* multiple threads active |
425 |
|
*/ |
426 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
427 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
428 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
429 |
< |
p2.execute(new MediumRunnable()); |
430 |
< |
p2.execute(new MediumRunnable()); |
431 |
< |
Thread.sleep(SHORT_DELAY_MS); |
432 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
433 |
< |
joinPool(p2); |
427 |
> |
final int THREADS = 3; |
428 |
> |
final ThreadPoolExecutor p = |
429 |
> |
new CustomTPE(THREADS, THREADS, |
430 |
> |
LONG_DELAY_MS, MILLISECONDS, |
431 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
432 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
433 |
> |
final CountDownLatch done = new CountDownLatch(1); |
434 |
> |
try { |
435 |
> |
assertEquals(0, p.getLargestPoolSize()); |
436 |
> |
for (int i = 0; i < THREADS; i++) |
437 |
> |
p.execute(new CheckedRunnable() { |
438 |
> |
public void realRun() throws InterruptedException { |
439 |
> |
threadsStarted.countDown(); |
440 |
> |
done.await(); |
441 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
442 |
> |
}}); |
443 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
444 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
445 |
> |
} finally { |
446 |
> |
done.countDown(); |
447 |
> |
joinPool(p); |
448 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
449 |
> |
} |
450 |
|
} |
451 |
|
|
452 |
|
/** |
453 |
< |
* getMaximumPoolSize returns value given in constructor if not |
454 |
< |
* otherwise set |
453 |
> |
* getMaximumPoolSize returns value given in constructor if not |
454 |
> |
* otherwise set |
455 |
|
*/ |
456 |
|
public void testGetMaximumPoolSize() { |
457 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
458 |
< |
assertEquals(2, p2.getMaximumPoolSize()); |
459 |
< |
joinPool(p2); |
457 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
458 |
> |
assertEquals(2, p.getMaximumPoolSize()); |
459 |
> |
joinPool(p); |
460 |
|
} |
461 |
|
|
462 |
|
/** |
463 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
464 |
< |
* become active |
463 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
464 |
> |
* become active |
465 |
|
*/ |
466 |
< |
public void testGetPoolSize() { |
467 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
468 |
< |
assertEquals(0, p1.getPoolSize()); |
469 |
< |
p1.execute(new MediumRunnable()); |
470 |
< |
assertEquals(1, p1.getPoolSize()); |
471 |
< |
joinPool(p1); |
466 |
> |
public void testGetPoolSize() throws InterruptedException { |
467 |
> |
final ThreadPoolExecutor p = |
468 |
> |
new CustomTPE(1, 1, |
469 |
> |
LONG_DELAY_MS, MILLISECONDS, |
470 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
471 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
472 |
> |
final CountDownLatch done = new CountDownLatch(1); |
473 |
> |
try { |
474 |
> |
assertEquals(0, p.getPoolSize()); |
475 |
> |
p.execute(new CheckedRunnable() { |
476 |
> |
public void realRun() throws InterruptedException { |
477 |
> |
threadStarted.countDown(); |
478 |
> |
assertEquals(1, p.getPoolSize()); |
479 |
> |
done.await(); |
480 |
> |
}}); |
481 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
482 |
> |
assertEquals(1, p.getPoolSize()); |
483 |
> |
} finally { |
484 |
> |
done.countDown(); |
485 |
> |
joinPool(p); |
486 |
> |
} |
487 |
|
} |
488 |
|
|
489 |
|
/** |
490 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
490 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
491 |
|
*/ |
492 |
|
public void testGetTaskCount() throws InterruptedException { |
493 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
494 |
< |
assertEquals(0, p1.getTaskCount()); |
495 |
< |
p1.execute(new MediumRunnable()); |
496 |
< |
Thread.sleep(SHORT_DELAY_MS); |
497 |
< |
assertEquals(1, p1.getTaskCount()); |
498 |
< |
joinPool(p1); |
493 |
> |
final ThreadPoolExecutor p = |
494 |
> |
new CustomTPE(1, 1, |
495 |
> |
LONG_DELAY_MS, MILLISECONDS, |
496 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
497 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
498 |
> |
final CountDownLatch done = new CountDownLatch(1); |
499 |
> |
try { |
500 |
> |
assertEquals(0, p.getTaskCount()); |
501 |
> |
p.execute(new CheckedRunnable() { |
502 |
> |
public void realRun() throws InterruptedException { |
503 |
> |
threadStarted.countDown(); |
504 |
> |
assertEquals(1, p.getTaskCount()); |
505 |
> |
done.await(); |
506 |
> |
}}); |
507 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
508 |
> |
assertEquals(1, p.getTaskCount()); |
509 |
> |
} finally { |
510 |
> |
done.countDown(); |
511 |
> |
joinPool(p); |
512 |
> |
} |
513 |
|
} |
514 |
|
|
515 |
|
/** |
516 |
< |
* isShutDown is false before shutdown, true after |
516 |
> |
* isShutdown is false before shutdown, true after |
517 |
|
*/ |
518 |
|
public void testIsShutdown() { |
519 |
|
|
520 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
521 |
< |
assertFalse(p1.isShutdown()); |
522 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
523 |
< |
assertTrue(p1.isShutdown()); |
524 |
< |
joinPool(p1); |
520 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
521 |
> |
assertFalse(p.isShutdown()); |
522 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
523 |
> |
assertTrue(p.isShutdown()); |
524 |
> |
joinPool(p); |
525 |
|
} |
526 |
|
|
415 |
– |
|
527 |
|
/** |
528 |
< |
* isTerminated is false before termination, true after |
528 |
> |
* isTerminated is false before termination, true after |
529 |
|
*/ |
530 |
|
public void testIsTerminated() throws InterruptedException { |
531 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
532 |
< |
assertFalse(p1.isTerminated()); |
533 |
< |
try { |
534 |
< |
p1.execute(new MediumRunnable()); |
535 |
< |
} finally { |
536 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
537 |
< |
} |
538 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
539 |
< |
assertTrue(p1.isTerminated()); |
531 |
> |
final ThreadPoolExecutor p = |
532 |
> |
new CustomTPE(1, 1, |
533 |
> |
LONG_DELAY_MS, MILLISECONDS, |
534 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
535 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
536 |
> |
final CountDownLatch done = new CountDownLatch(1); |
537 |
> |
try { |
538 |
> |
assertFalse(p.isTerminating()); |
539 |
> |
p.execute(new CheckedRunnable() { |
540 |
> |
public void realRun() throws InterruptedException { |
541 |
> |
assertFalse(p.isTerminating()); |
542 |
> |
threadStarted.countDown(); |
543 |
> |
done.await(); |
544 |
> |
}}); |
545 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
546 |
> |
assertFalse(p.isTerminating()); |
547 |
> |
done.countDown(); |
548 |
> |
} finally { |
549 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
550 |
> |
} |
551 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
552 |
> |
assertTrue(p.isTerminated()); |
553 |
> |
assertFalse(p.isTerminating()); |
554 |
|
} |
555 |
|
|
556 |
|
/** |
557 |
< |
* isTerminating is not true when running or when terminated |
557 |
> |
* isTerminating is not true when running or when terminated |
558 |
|
*/ |
559 |
|
public void testIsTerminating() throws InterruptedException { |
560 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
561 |
< |
assertFalse(p1.isTerminating()); |
562 |
< |
try { |
563 |
< |
p1.execute(new SmallRunnable()); |
564 |
< |
assertFalse(p1.isTerminating()); |
565 |
< |
} finally { |
566 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
567 |
< |
} |
568 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
569 |
< |
assertTrue(p1.isTerminated()); |
570 |
< |
assertFalse(p1.isTerminating()); |
560 |
> |
final ThreadPoolExecutor p = |
561 |
> |
new CustomTPE(1, 1, |
562 |
> |
LONG_DELAY_MS, MILLISECONDS, |
563 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
564 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
565 |
> |
final CountDownLatch done = new CountDownLatch(1); |
566 |
> |
try { |
567 |
> |
assertFalse(p.isTerminating()); |
568 |
> |
p.execute(new CheckedRunnable() { |
569 |
> |
public void realRun() throws InterruptedException { |
570 |
> |
assertFalse(p.isTerminating()); |
571 |
> |
threadStarted.countDown(); |
572 |
> |
done.await(); |
573 |
> |
}}); |
574 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
575 |
> |
assertFalse(p.isTerminating()); |
576 |
> |
done.countDown(); |
577 |
> |
} finally { |
578 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
579 |
> |
} |
580 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
581 |
> |
assertTrue(p.isTerminated()); |
582 |
> |
assertFalse(p.isTerminating()); |
583 |
|
} |
584 |
|
|
585 |
|
/** |
586 |
|
* getQueue returns the work queue, which contains queued tasks |
587 |
|
*/ |
588 |
|
public void testGetQueue() throws InterruptedException { |
589 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
590 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
591 |
< |
FutureTask[] tasks = new FutureTask[5]; |
592 |
< |
for (int i = 0; i < 5; i++) { |
593 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
594 |
< |
p1.execute(tasks[i]); |
595 |
< |
} |
596 |
< |
try { |
597 |
< |
Thread.sleep(SHORT_DELAY_MS); |
598 |
< |
BlockingQueue<Runnable> wq = p1.getQueue(); |
599 |
< |
assertSame(q, wq); |
600 |
< |
assertFalse(wq.contains(tasks[0])); |
601 |
< |
assertTrue(wq.contains(tasks[4])); |
602 |
< |
for (int i = 1; i < 5; ++i) |
603 |
< |
tasks[i].cancel(true); |
604 |
< |
p1.shutdownNow(); |
589 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
590 |
> |
final ThreadPoolExecutor p = |
591 |
> |
new CustomTPE(1, 1, |
592 |
> |
LONG_DELAY_MS, MILLISECONDS, |
593 |
> |
q); |
594 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
595 |
> |
final CountDownLatch done = new CountDownLatch(1); |
596 |
> |
try { |
597 |
> |
FutureTask[] tasks = new FutureTask[5]; |
598 |
> |
for (int i = 0; i < tasks.length; i++) { |
599 |
> |
Callable task = new CheckedCallable<Boolean>() { |
600 |
> |
public Boolean realCall() throws InterruptedException { |
601 |
> |
threadStarted.countDown(); |
602 |
> |
assertSame(q, p.getQueue()); |
603 |
> |
done.await(); |
604 |
> |
return Boolean.TRUE; |
605 |
> |
}}; |
606 |
> |
tasks[i] = new FutureTask(task); |
607 |
> |
p.execute(tasks[i]); |
608 |
> |
} |
609 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
610 |
> |
assertSame(q, p.getQueue()); |
611 |
> |
assertFalse(q.contains(tasks[0])); |
612 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
613 |
> |
assertEquals(tasks.length - 1, q.size()); |
614 |
|
} finally { |
615 |
< |
joinPool(p1); |
615 |
> |
done.countDown(); |
616 |
> |
joinPool(p); |
617 |
|
} |
618 |
|
} |
619 |
|
|
622 |
|
*/ |
623 |
|
public void testRemove() throws InterruptedException { |
624 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
625 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
626 |
< |
FutureTask[] tasks = new FutureTask[5]; |
627 |
< |
for (int i = 0; i < 5; i++) { |
628 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
629 |
< |
p1.execute(tasks[i]); |
630 |
< |
} |
631 |
< |
try { |
632 |
< |
Thread.sleep(SHORT_DELAY_MS); |
633 |
< |
assertFalse(p1.remove(tasks[0])); |
625 |
> |
final ThreadPoolExecutor p = |
626 |
> |
new CustomTPE(1, 1, |
627 |
> |
LONG_DELAY_MS, MILLISECONDS, |
628 |
> |
q); |
629 |
> |
Runnable[] tasks = new Runnable[6]; |
630 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
631 |
> |
final CountDownLatch done = new CountDownLatch(1); |
632 |
> |
try { |
633 |
> |
for (int i = 0; i < tasks.length; i++) { |
634 |
> |
tasks[i] = new CheckedRunnable() { |
635 |
> |
public void realRun() throws InterruptedException { |
636 |
> |
threadStarted.countDown(); |
637 |
> |
done.await(); |
638 |
> |
}}; |
639 |
> |
p.execute(tasks[i]); |
640 |
> |
} |
641 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
642 |
> |
assertFalse(p.remove(tasks[0])); |
643 |
|
assertTrue(q.contains(tasks[4])); |
644 |
|
assertTrue(q.contains(tasks[3])); |
645 |
< |
assertTrue(p1.remove(tasks[4])); |
646 |
< |
assertFalse(p1.remove(tasks[4])); |
645 |
> |
assertTrue(p.remove(tasks[4])); |
646 |
> |
assertFalse(p.remove(tasks[4])); |
647 |
|
assertFalse(q.contains(tasks[4])); |
648 |
|
assertTrue(q.contains(tasks[3])); |
649 |
< |
assertTrue(p1.remove(tasks[3])); |
649 |
> |
assertTrue(p.remove(tasks[3])); |
650 |
|
assertFalse(q.contains(tasks[3])); |
651 |
|
} finally { |
652 |
< |
joinPool(p1); |
652 |
> |
done.countDown(); |
653 |
> |
joinPool(p); |
654 |
|
} |
655 |
|
} |
656 |
|
|
657 |
|
/** |
658 |
< |
* purge removes cancelled tasks from the queue |
658 |
> |
* purge removes cancelled tasks from the queue |
659 |
|
*/ |
660 |
< |
public void testPurge() { |
661 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
660 |
> |
public void testPurge() throws InterruptedException { |
661 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
662 |
> |
final CountDownLatch done = new CountDownLatch(1); |
663 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
664 |
> |
final ThreadPoolExecutor p = |
665 |
> |
new CustomTPE(1, 1, |
666 |
> |
LONG_DELAY_MS, MILLISECONDS, |
667 |
> |
q); |
668 |
|
FutureTask[] tasks = new FutureTask[5]; |
669 |
< |
for (int i = 0; i < 5; i++) { |
670 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
671 |
< |
p1.execute(tasks[i]); |
669 |
> |
try { |
670 |
> |
for (int i = 0; i < tasks.length; i++) { |
671 |
> |
Callable task = new CheckedCallable<Boolean>() { |
672 |
> |
public Boolean realCall() throws InterruptedException { |
673 |
> |
threadStarted.countDown(); |
674 |
> |
done.await(); |
675 |
> |
return Boolean.TRUE; |
676 |
> |
}}; |
677 |
> |
tasks[i] = new FutureTask(task); |
678 |
> |
p.execute(tasks[i]); |
679 |
> |
} |
680 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
681 |
> |
assertEquals(tasks.length, p.getTaskCount()); |
682 |
> |
assertEquals(tasks.length - 1, q.size()); |
683 |
> |
assertEquals(1L, p.getActiveCount()); |
684 |
> |
assertEquals(0L, p.getCompletedTaskCount()); |
685 |
> |
tasks[4].cancel(true); |
686 |
> |
tasks[3].cancel(false); |
687 |
> |
p.purge(); |
688 |
> |
assertEquals(tasks.length - 3, q.size()); |
689 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
690 |
> |
p.purge(); // Nothing to do |
691 |
> |
assertEquals(tasks.length - 3, q.size()); |
692 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
693 |
> |
} finally { |
694 |
> |
done.countDown(); |
695 |
> |
joinPool(p); |
696 |
|
} |
510 |
– |
tasks[4].cancel(true); |
511 |
– |
tasks[3].cancel(true); |
512 |
– |
p1.purge(); |
513 |
– |
long count = p1.getTaskCount(); |
514 |
– |
assertTrue(count >= 2 && count < 5); |
515 |
– |
joinPool(p1); |
697 |
|
} |
698 |
|
|
699 |
|
/** |
700 |
< |
* shutDownNow returns a list containing tasks that were not run |
700 |
> |
* shutdownNow returns a list containing tasks that were not run |
701 |
|
*/ |
702 |
< |
public void testShutDownNow() { |
703 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
702 |
> |
public void testShutdownNow() { |
703 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
704 |
|
List l; |
705 |
|
try { |
706 |
|
for (int i = 0; i < 5; i++) |
707 |
< |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
707 |
> |
p.execute(new MediumPossiblyInterruptedRunnable()); |
708 |
|
} |
709 |
|
finally { |
710 |
|
try { |
711 |
< |
l = p1.shutdownNow(); |
711 |
> |
l = p.shutdownNow(); |
712 |
|
} catch (SecurityException ok) { return; } |
532 |
– |
|
713 |
|
} |
714 |
< |
assertTrue(p1.isShutdown()); |
714 |
> |
assertTrue(p.isShutdown()); |
715 |
|
assertTrue(l.size() <= 4); |
716 |
|
} |
717 |
|
|
718 |
|
// Exception Tests |
719 |
|
|
540 |
– |
|
720 |
|
/** |
721 |
|
* Constructor throws if corePoolSize argument is less than zero |
722 |
|
*/ |
777 |
|
} catch (NullPointerException success) {} |
778 |
|
} |
779 |
|
|
601 |
– |
|
602 |
– |
|
780 |
|
/** |
781 |
|
* Constructor throws if corePoolSize argument is less than zero |
782 |
|
*/ |
848 |
|
} catch (NullPointerException success) {} |
849 |
|
} |
850 |
|
|
674 |
– |
|
851 |
|
/** |
852 |
|
* Constructor throws if corePoolSize argument is less than zero |
853 |
|
*/ |
919 |
|
} catch (NullPointerException success) {} |
920 |
|
} |
921 |
|
|
746 |
– |
|
922 |
|
/** |
923 |
|
* Constructor throws if corePoolSize argument is less than zero |
924 |
|
*/ |
970 |
|
} |
971 |
|
|
972 |
|
/** |
973 |
< |
* Constructor throws if workQueue is set to null |
973 |
> |
* Constructor throws if workQueue is null |
974 |
|
*/ |
975 |
|
public void testConstructorNullPointerException6() { |
976 |
|
try { |
980 |
|
} |
981 |
|
|
982 |
|
/** |
983 |
< |
* Constructor throws if handler is set to null |
983 |
> |
* Constructor throws if handler is null |
984 |
|
*/ |
985 |
|
public void testConstructorNullPointerException7() { |
986 |
|
try { |
991 |
|
} |
992 |
|
|
993 |
|
/** |
994 |
< |
* Constructor throws if ThreadFactory is set top null |
994 |
> |
* Constructor throws if ThreadFactory is null |
995 |
|
*/ |
996 |
|
public void testConstructorNullPointerException8() { |
997 |
|
try { |
998 |
< |
ThreadFactory f = null; |
999 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
998 |
> |
new CustomTPE(1, 2, |
999 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1000 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1001 |
> |
(ThreadFactory) null, |
1002 |
> |
new NoOpREHandler()); |
1003 |
|
shouldThrow(); |
1004 |
|
} catch (NullPointerException success) {} |
1005 |
|
} |
1006 |
|
|
829 |
– |
|
1007 |
|
/** |
1008 |
< |
* execute throws RejectedExecutionException |
832 |
< |
* if saturated. |
1008 |
> |
* execute throws RejectedExecutionException if saturated. |
1009 |
|
*/ |
1010 |
|
public void testSaturatedExecute() { |
1011 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
1012 |
< |
try { |
1013 |
< |
|
1014 |
< |
for (int i = 0; i < 5; ++i) { |
1015 |
< |
p.execute(new MediumRunnable()); |
1011 |
> |
ThreadPoolExecutor p = |
1012 |
> |
new CustomTPE(1, 1, |
1013 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1014 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1015 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1016 |
> |
try { |
1017 |
> |
Runnable task = new CheckedRunnable() { |
1018 |
> |
public void realRun() throws InterruptedException { |
1019 |
> |
done.await(); |
1020 |
> |
}}; |
1021 |
> |
for (int i = 0; i < 2; ++i) |
1022 |
> |
p.execute(task); |
1023 |
> |
for (int i = 0; i < 2; ++i) { |
1024 |
> |
try { |
1025 |
> |
p.execute(task); |
1026 |
> |
shouldThrow(); |
1027 |
> |
} catch (RejectedExecutionException success) {} |
1028 |
> |
assertTrue(p.getTaskCount() <= 2); |
1029 |
|
} |
1030 |
< |
shouldThrow(); |
1031 |
< |
} catch (RejectedExecutionException success) {} |
1032 |
< |
joinPool(p); |
1030 |
> |
} finally { |
1031 |
> |
done.countDown(); |
1032 |
> |
joinPool(p); |
1033 |
> |
} |
1034 |
|
} |
1035 |
|
|
1036 |
|
/** |
1037 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1037 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
1038 |
|
*/ |
1039 |
|
public void testSaturatedExecute2() { |
1040 |
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
1041 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1041 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, |
1042 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1043 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1044 |
> |
h); |
1045 |
|
try { |
853 |
– |
|
1046 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1047 |
< |
for (int i = 0; i < 5; ++i) { |
1047 |
> |
for (int i = 0; i < tasks.length; ++i) |
1048 |
|
tasks[i] = new TrackedNoOpRunnable(); |
857 |
– |
} |
1049 |
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1050 |
|
p.execute(mr); |
1051 |
< |
for (int i = 0; i < 5; ++i) { |
1051 |
> |
for (int i = 0; i < tasks.length; ++i) |
1052 |
|
p.execute(tasks[i]); |
1053 |
< |
} |
863 |
< |
for (int i = 1; i < 5; ++i) { |
1053 |
> |
for (int i = 1; i < tasks.length; ++i) |
1054 |
|
assertTrue(tasks[i].done); |
865 |
– |
} |
1055 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1056 |
|
} finally { |
1057 |
|
joinPool(p); |
1059 |
|
} |
1060 |
|
|
1061 |
|
/** |
1062 |
< |
* executor using DiscardPolicy drops task if saturated. |
1062 |
> |
* executor using DiscardPolicy drops task if saturated. |
1063 |
|
*/ |
1064 |
|
public void testSaturatedExecute3() { |
1065 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
1066 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1066 |
> |
ThreadPoolExecutor p = |
1067 |
> |
new CustomTPE(1, 1, |
1068 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1069 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1070 |
> |
h); |
1071 |
|
try { |
879 |
– |
|
1072 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1073 |
< |
for (int i = 0; i < 5; ++i) { |
1073 |
> |
for (int i = 0; i < tasks.length; ++i) |
1074 |
|
tasks[i] = new TrackedNoOpRunnable(); |
883 |
– |
} |
1075 |
|
p.execute(new TrackedLongRunnable()); |
1076 |
< |
for (int i = 0; i < 5; ++i) { |
1077 |
< |
p.execute(tasks[i]); |
1078 |
< |
} |
1079 |
< |
for (int i = 0; i < 5; ++i) { |
889 |
< |
assertFalse(tasks[i].done); |
890 |
< |
} |
1076 |
> |
for (TrackedNoOpRunnable task : tasks) |
1077 |
> |
p.execute(task); |
1078 |
> |
for (TrackedNoOpRunnable task : tasks) |
1079 |
> |
assertFalse(task.done); |
1080 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1081 |
|
} finally { |
1082 |
|
joinPool(p); |
1084 |
|
} |
1085 |
|
|
1086 |
|
/** |
1087 |
< |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1087 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1088 |
|
*/ |
1089 |
|
public void testSaturatedExecute4() { |
1090 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
1105 |
|
} |
1106 |
|
|
1107 |
|
/** |
1108 |
< |
* execute throws RejectedExecutionException if shutdown |
1108 |
> |
* execute throws RejectedExecutionException if shutdown |
1109 |
|
*/ |
1110 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1111 |
< |
ThreadPoolExecutor tpe = |
1111 |
> |
ThreadPoolExecutor p = |
1112 |
|
new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1113 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1113 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1114 |
|
try { |
1115 |
< |
tpe.execute(new NoOpRunnable()); |
1115 |
> |
p.execute(new NoOpRunnable()); |
1116 |
|
shouldThrow(); |
1117 |
|
} catch (RejectedExecutionException success) {} |
1118 |
|
|
1119 |
< |
joinPool(tpe); |
1119 |
> |
joinPool(p); |
1120 |
|
} |
1121 |
|
|
1122 |
|
/** |
1123 |
< |
* execute using CallerRunsPolicy drops task on shutdown |
1123 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
1124 |
|
*/ |
1125 |
|
public void testCallerRunsOnShutdown() { |
1126 |
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
1137 |
|
} |
1138 |
|
|
1139 |
|
/** |
1140 |
< |
* execute using DiscardPolicy drops task on shutdown |
1140 |
> |
* execute using DiscardPolicy drops task on shutdown |
1141 |
|
*/ |
1142 |
|
public void testDiscardOnShutdown() { |
1143 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
1153 |
|
} |
1154 |
|
} |
1155 |
|
|
967 |
– |
|
1156 |
|
/** |
1157 |
< |
* execute using DiscardOldestPolicy drops task on shutdown |
1157 |
> |
* execute using DiscardOldestPolicy drops task on shutdown |
1158 |
|
*/ |
1159 |
|
public void testDiscardOldestOnShutdown() { |
1160 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
1170 |
|
} |
1171 |
|
} |
1172 |
|
|
985 |
– |
|
1173 |
|
/** |
1174 |
< |
* execute (null) throws NPE |
1174 |
> |
* execute(null) throws NPE |
1175 |
|
*/ |
1176 |
|
public void testExecuteNull() { |
1177 |
< |
ThreadPoolExecutor tpe = null; |
1177 |
> |
ThreadPoolExecutor p = null; |
1178 |
|
try { |
1179 |
< |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1180 |
< |
tpe.execute(null); |
1179 |
> |
p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1180 |
> |
p.execute(null); |
1181 |
|
shouldThrow(); |
1182 |
|
} catch (NullPointerException success) {} |
1183 |
|
|
1184 |
< |
joinPool(tpe); |
1184 |
> |
joinPool(p); |
1185 |
|
} |
1186 |
|
|
1187 |
|
/** |
1188 |
< |
* setCorePoolSize of negative value throws IllegalArgumentException |
1188 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
1189 |
|
*/ |
1190 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1191 |
< |
ThreadPoolExecutor tpe = |
1191 |
> |
ThreadPoolExecutor p = |
1192 |
|
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1193 |
|
try { |
1194 |
< |
tpe.setCorePoolSize(-1); |
1194 |
> |
p.setCorePoolSize(-1); |
1195 |
|
shouldThrow(); |
1196 |
|
} catch (IllegalArgumentException success) { |
1197 |
|
} finally { |
1198 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1198 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1199 |
|
} |
1200 |
< |
joinPool(tpe); |
1200 |
> |
joinPool(p); |
1201 |
|
} |
1202 |
|
|
1203 |
|
/** |
1204 |
< |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1205 |
< |
* given a value less the core pool size |
1204 |
> |
* setMaximumPoolSize(int) throws IllegalArgumentException |
1205 |
> |
* if given a value less the core pool size |
1206 |
|
*/ |
1207 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1208 |
< |
ThreadPoolExecutor tpe = |
1208 |
> |
ThreadPoolExecutor p = |
1209 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1210 |
|
try { |
1211 |
< |
tpe.setMaximumPoolSize(1); |
1211 |
> |
p.setMaximumPoolSize(1); |
1212 |
|
shouldThrow(); |
1213 |
|
} catch (IllegalArgumentException success) { |
1214 |
|
} finally { |
1215 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1215 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1216 |
|
} |
1217 |
< |
joinPool(tpe); |
1217 |
> |
joinPool(p); |
1218 |
|
} |
1219 |
|
|
1220 |
|
/** |
1221 |
< |
* setMaximumPoolSize throws IllegalArgumentException |
1222 |
< |
* if given a negative value |
1221 |
> |
* setMaximumPoolSize throws IllegalArgumentException |
1222 |
> |
* if given a negative value |
1223 |
|
*/ |
1224 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1225 |
< |
ThreadPoolExecutor tpe = |
1225 |
> |
ThreadPoolExecutor p = |
1226 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1227 |
|
try { |
1228 |
< |
tpe.setMaximumPoolSize(-1); |
1228 |
> |
p.setMaximumPoolSize(-1); |
1229 |
|
shouldThrow(); |
1230 |
|
} catch (IllegalArgumentException success) { |
1231 |
|
} finally { |
1232 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1232 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1233 |
|
} |
1234 |
< |
joinPool(tpe); |
1234 |
> |
joinPool(p); |
1235 |
|
} |
1236 |
|
|
1050 |
– |
|
1237 |
|
/** |
1238 |
< |
* setKeepAliveTime throws IllegalArgumentException |
1239 |
< |
* when given a negative value |
1238 |
> |
* setKeepAliveTime throws IllegalArgumentException |
1239 |
> |
* when given a negative value |
1240 |
|
*/ |
1241 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1242 |
< |
ThreadPoolExecutor tpe = |
1242 |
> |
ThreadPoolExecutor p = |
1243 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1244 |
|
|
1245 |
|
try { |
1246 |
< |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
1246 |
> |
p.setKeepAliveTime(-1,MILLISECONDS); |
1247 |
|
shouldThrow(); |
1248 |
|
} catch (IllegalArgumentException success) { |
1249 |
|
} finally { |
1250 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1250 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1251 |
|
} |
1252 |
< |
joinPool(tpe); |
1252 |
> |
joinPool(p); |
1253 |
|
} |
1254 |
|
|
1255 |
|
/** |
1256 |
|
* terminated() is called on termination |
1257 |
|
*/ |
1258 |
|
public void testTerminated() { |
1259 |
< |
CustomTPE tpe = new CustomTPE(); |
1260 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1261 |
< |
assertTrue(tpe.terminatedCalled); |
1262 |
< |
joinPool(tpe); |
1259 |
> |
CustomTPE p = new CustomTPE(); |
1260 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1261 |
> |
assertTrue(p.terminatedCalled()); |
1262 |
> |
joinPool(p); |
1263 |
|
} |
1264 |
|
|
1265 |
|
/** |
1266 |
|
* beforeExecute and afterExecute are called when executing task |
1267 |
|
*/ |
1268 |
|
public void testBeforeAfter() throws InterruptedException { |
1269 |
< |
CustomTPE tpe = new CustomTPE(); |
1269 |
> |
CustomTPE p = new CustomTPE(); |
1270 |
|
try { |
1271 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1272 |
< |
tpe.execute(r); |
1273 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1274 |
< |
assertTrue(r.done); |
1275 |
< |
assertTrue(tpe.beforeCalled); |
1276 |
< |
assertTrue(tpe.afterCalled); |
1277 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1271 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1272 |
> |
p.execute(new CheckedRunnable() { |
1273 |
> |
public void realRun() { |
1274 |
> |
done.countDown(); |
1275 |
> |
}}); |
1276 |
> |
await(p.afterCalled); |
1277 |
> |
assertEquals(0, done.getCount()); |
1278 |
> |
assertTrue(p.afterCalled()); |
1279 |
> |
assertTrue(p.beforeCalled()); |
1280 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1281 |
|
} finally { |
1282 |
< |
joinPool(tpe); |
1282 |
> |
joinPool(p); |
1283 |
|
} |
1284 |
|
} |
1285 |
|
|
1325 |
|
} |
1326 |
|
} |
1327 |
|
|
1139 |
– |
|
1328 |
|
/** |
1329 |
|
* invokeAny(null) throws NPE |
1330 |
|
*/ |
1357 |
|
* invokeAny(c) throws NPE if c has null elements |
1358 |
|
*/ |
1359 |
|
public void testInvokeAny3() throws Exception { |
1360 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
1360 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1361 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1362 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1363 |
+ |
l.add(latchAwaitingStringTask(latch)); |
1364 |
+ |
l.add(null); |
1365 |
|
try { |
1175 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1176 |
– |
l.add(new Callable<String>() { |
1177 |
– |
public String call() { |
1178 |
– |
try { |
1179 |
– |
latch.await(); |
1180 |
– |
} catch (InterruptedException ok) {} |
1181 |
– |
return TEST_STRING; |
1182 |
– |
}}); |
1183 |
– |
l.add(null); |
1366 |
|
e.invokeAny(l); |
1367 |
|
shouldThrow(); |
1368 |
|
} catch (NullPointerException success) { |
1377 |
|
*/ |
1378 |
|
public void testInvokeAny4() throws Exception { |
1379 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1380 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1381 |
+ |
l.add(new NPETask()); |
1382 |
|
try { |
1199 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1200 |
– |
l.add(new NPETask()); |
1383 |
|
e.invokeAny(l); |
1384 |
|
shouldThrow(); |
1385 |
|
} catch (ExecutionException success) { |
1395 |
|
public void testInvokeAny5() throws Exception { |
1396 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1397 |
|
try { |
1398 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1398 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1399 |
|
l.add(new StringTask()); |
1400 |
|
l.add(new StringTask()); |
1401 |
|
String result = e.invokeAny(l); |
1437 |
|
*/ |
1438 |
|
public void testInvokeAll3() throws Exception { |
1439 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1440 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1441 |
+ |
l.add(new StringTask()); |
1442 |
+ |
l.add(null); |
1443 |
|
try { |
1259 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1260 |
– |
l.add(new StringTask()); |
1261 |
– |
l.add(null); |
1444 |
|
e.invokeAll(l); |
1445 |
|
shouldThrow(); |
1446 |
|
} catch (NullPointerException success) { |
1454 |
|
*/ |
1455 |
|
public void testInvokeAll4() throws Exception { |
1456 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1457 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1458 |
+ |
l.add(new NPETask()); |
1459 |
+ |
List<Future<String>> futures = e.invokeAll(l); |
1460 |
+ |
assertEquals(1, futures.size()); |
1461 |
|
try { |
1462 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1277 |
< |
l.add(new NPETask()); |
1278 |
< |
List<Future<String>> result = e.invokeAll(l); |
1279 |
< |
assertEquals(1, result.size()); |
1280 |
< |
for (Future<String> future : result) |
1281 |
< |
future.get(); |
1462 |
> |
futures.get(0).get(); |
1463 |
|
shouldThrow(); |
1464 |
|
} catch (ExecutionException success) { |
1465 |
+ |
assertTrue(success.getCause() instanceof NullPointerException); |
1466 |
|
} finally { |
1467 |
|
joinPool(e); |
1468 |
|
} |
1474 |
|
public void testInvokeAll5() throws Exception { |
1475 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1476 |
|
try { |
1477 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1477 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1478 |
|
l.add(new StringTask()); |
1479 |
|
l.add(new StringTask()); |
1480 |
< |
List<Future<String>> result = e.invokeAll(l); |
1481 |
< |
assertEquals(2, result.size()); |
1482 |
< |
for (Future<String> future : result) |
1480 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1481 |
> |
assertEquals(2, futures.size()); |
1482 |
> |
for (Future<String> future : futures) |
1483 |
|
assertSame(TEST_STRING, future.get()); |
1484 |
|
} finally { |
1485 |
|
joinPool(e); |
1486 |
|
} |
1487 |
|
} |
1488 |
|
|
1307 |
– |
|
1308 |
– |
|
1489 |
|
/** |
1490 |
|
* timed invokeAny(null) throws NPE |
1491 |
|
*/ |
1505 |
|
*/ |
1506 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1507 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1508 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1509 |
+ |
l.add(new StringTask()); |
1510 |
|
try { |
1329 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1330 |
– |
l.add(new StringTask()); |
1511 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1512 |
|
shouldThrow(); |
1513 |
|
} catch (NullPointerException success) { |
1534 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1535 |
|
*/ |
1536 |
|
public void testTimedInvokeAny3() throws Exception { |
1537 |
< |
final CountDownLatch latch = new CountDownLatch(1); |
1537 |
> |
CountDownLatch latch = new CountDownLatch(1); |
1538 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1539 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1540 |
+ |
l.add(latchAwaitingStringTask(latch)); |
1541 |
+ |
l.add(null); |
1542 |
|
try { |
1360 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1361 |
– |
l.add(new Callable<String>() { |
1362 |
– |
public String call() { |
1363 |
– |
try { |
1364 |
– |
latch.await(); |
1365 |
– |
} catch (InterruptedException ok) {} |
1366 |
– |
return TEST_STRING; |
1367 |
– |
}}); |
1368 |
– |
l.add(null); |
1543 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1544 |
|
shouldThrow(); |
1545 |
|
} catch (NullPointerException success) { |
1554 |
|
*/ |
1555 |
|
public void testTimedInvokeAny4() throws Exception { |
1556 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1557 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1558 |
+ |
l.add(new NPETask()); |
1559 |
|
try { |
1384 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1385 |
– |
l.add(new NPETask()); |
1560 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1561 |
|
shouldThrow(); |
1562 |
|
} catch (ExecutionException success) { |
1572 |
|
public void testTimedInvokeAny5() throws Exception { |
1573 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1574 |
|
try { |
1575 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
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, MEDIUM_DELAY_MS, MILLISECONDS); |
1601 |
|
*/ |
1602 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1603 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1604 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1605 |
+ |
l.add(new StringTask()); |
1606 |
|
try { |
1431 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1432 |
– |
l.add(new StringTask()); |
1607 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1608 |
|
shouldThrow(); |
1609 |
|
} catch (NullPointerException success) { |
1630 |
|
*/ |
1631 |
|
public void testTimedInvokeAll3() throws Exception { |
1632 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1633 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1634 |
+ |
l.add(new StringTask()); |
1635 |
+ |
l.add(null); |
1636 |
|
try { |
1460 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1461 |
– |
l.add(new StringTask()); |
1462 |
– |
l.add(null); |
1637 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1638 |
|
shouldThrow(); |
1639 |
|
} catch (NullPointerException success) { |
1647 |
|
*/ |
1648 |
|
public void testTimedInvokeAll4() throws Exception { |
1649 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1650 |
+ |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1651 |
+ |
l.add(new NPETask()); |
1652 |
+ |
List<Future<String>> futures = |
1653 |
+ |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1654 |
+ |
assertEquals(1, futures.size()); |
1655 |
|
try { |
1656 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1478 |
< |
l.add(new NPETask()); |
1479 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1480 |
< |
assertEquals(1, result.size()); |
1481 |
< |
for (Future<String> future : result) |
1482 |
< |
future.get(); |
1656 |
> |
futures.get(0).get(); |
1657 |
|
shouldThrow(); |
1658 |
|
} catch (ExecutionException success) { |
1659 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1668 |
|
public void testTimedInvokeAll5() throws Exception { |
1669 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1670 |
|
try { |
1671 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1671 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1672 |
|
l.add(new StringTask()); |
1673 |
|
l.add(new StringTask()); |
1674 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1675 |
< |
assertEquals(2, result.size()); |
1676 |
< |
for (Future<String> future : result) |
1674 |
> |
List<Future<String>> futures = |
1675 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1676 |
> |
assertEquals(2, futures.size()); |
1677 |
> |
for (Future<String> future : futures) |
1678 |
|
assertSame(TEST_STRING, future.get()); |
1679 |
|
} finally { |
1680 |
|
joinPool(e); |
1687 |
|
public void testTimedInvokeAll6() throws Exception { |
1688 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1689 |
|
try { |
1690 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1690 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1691 |
|
l.add(new StringTask()); |
1692 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1693 |
|
l.add(new StringTask()); |
1694 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1695 |
< |
assertEquals(3, result.size()); |
1696 |
< |
Iterator<Future<String>> it = result.iterator(); |
1697 |
< |
Future<String> f1 = it.next(); |
1698 |
< |
Future<String> f2 = it.next(); |
1699 |
< |
Future<String> f3 = it.next(); |
1700 |
< |
assertTrue(f1.isDone()); |
1526 |
< |
assertTrue(f2.isDone()); |
1527 |
< |
assertTrue(f3.isDone()); |
1528 |
< |
assertFalse(f1.isCancelled()); |
1529 |
< |
assertTrue(f2.isCancelled()); |
1694 |
> |
List<Future<String>> futures = |
1695 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1696 |
> |
assertEquals(l.size(), futures.size()); |
1697 |
> |
for (Future future : futures) |
1698 |
> |
assertTrue(future.isDone()); |
1699 |
> |
assertFalse(futures.get(0).isCancelled()); |
1700 |
> |
assertTrue(futures.get(1).isCancelled()); |
1701 |
|
} finally { |
1702 |
|
joinPool(e); |
1703 |
|
} |
1708 |
|
* thread factory fails to create more |
1709 |
|
*/ |
1710 |
|
public void testFailingThreadFactory() throws InterruptedException { |
1711 |
< |
ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1712 |
< |
try { |
1713 |
< |
for (int k = 0; k < 100; ++k) { |
1714 |
< |
e.execute(new NoOpRunnable()); |
1715 |
< |
} |
1716 |
< |
Thread.sleep(LONG_DELAY_MS); |
1711 |
> |
final ExecutorService e = |
1712 |
> |
new CustomTPE(100, 100, |
1713 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1714 |
> |
new LinkedBlockingQueue<Runnable>(), |
1715 |
> |
new FailingThreadFactory()); |
1716 |
> |
try { |
1717 |
> |
final int TASKS = 100; |
1718 |
> |
final CountDownLatch done = new CountDownLatch(TASKS); |
1719 |
> |
for (int k = 0; k < TASKS; ++k) |
1720 |
> |
e.execute(new CheckedRunnable() { |
1721 |
> |
public void realRun() { |
1722 |
> |
done.countDown(); |
1723 |
> |
}}); |
1724 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1725 |
|
} finally { |
1726 |
|
joinPool(e); |
1727 |
|
} |
1731 |
|
* allowsCoreThreadTimeOut is by default false. |
1732 |
|
*/ |
1733 |
|
public void testAllowsCoreThreadTimeOut() { |
1734 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1735 |
< |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1736 |
< |
joinPool(tpe); |
1734 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1735 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1736 |
> |
joinPool(p); |
1737 |
|
} |
1738 |
|
|
1739 |
|
/** |
1740 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1741 |
|
*/ |
1742 |
< |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1743 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1744 |
< |
tpe.allowCoreThreadTimeOut(true); |
1745 |
< |
tpe.execute(new NoOpRunnable()); |
1746 |
< |
try { |
1747 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1748 |
< |
assertEquals(0, tpe.getPoolSize()); |
1742 |
> |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1743 |
> |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1744 |
> |
final ThreadPoolExecutor p = |
1745 |
> |
new CustomTPE(2, 10, |
1746 |
> |
coreThreadTimeOut, MILLISECONDS, |
1747 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1748 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1749 |
> |
try { |
1750 |
> |
p.allowCoreThreadTimeOut(true); |
1751 |
> |
p.execute(new CheckedRunnable() { |
1752 |
> |
public void realRun() throws InterruptedException { |
1753 |
> |
threadStarted.countDown(); |
1754 |
> |
assertEquals(1, p.getPoolSize()); |
1755 |
> |
}}); |
1756 |
> |
await(threadStarted); |
1757 |
> |
delay(coreThreadTimeOut); |
1758 |
> |
long startTime = System.nanoTime(); |
1759 |
> |
while (p.getPoolSize() > 0 |
1760 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1761 |
> |
Thread.yield(); |
1762 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1763 |
> |
assertEquals(0, p.getPoolSize()); |
1764 |
|
} finally { |
1765 |
< |
joinPool(tpe); |
1765 |
> |
joinPool(p); |
1766 |
|
} |
1767 |
|
} |
1768 |
|
|
1769 |
|
/** |
1770 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1771 |
|
*/ |
1772 |
< |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1773 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1774 |
< |
tpe.allowCoreThreadTimeOut(false); |
1775 |
< |
tpe.execute(new NoOpRunnable()); |
1776 |
< |
try { |
1777 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1778 |
< |
assertTrue(tpe.getPoolSize() >= 1); |
1772 |
> |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1773 |
> |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1774 |
> |
final ThreadPoolExecutor p = |
1775 |
> |
new CustomTPE(2, 10, |
1776 |
> |
coreThreadTimeOut, MILLISECONDS, |
1777 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1778 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1779 |
> |
try { |
1780 |
> |
p.allowCoreThreadTimeOut(false); |
1781 |
> |
p.execute(new CheckedRunnable() { |
1782 |
> |
public void realRun() throws InterruptedException { |
1783 |
> |
threadStarted.countDown(); |
1784 |
> |
assertTrue(p.getPoolSize() >= 1); |
1785 |
> |
}}); |
1786 |
> |
delay(2 * coreThreadTimeOut); |
1787 |
> |
assertTrue(p.getPoolSize() >= 1); |
1788 |
|
} finally { |
1789 |
< |
joinPool(tpe); |
1789 |
> |
joinPool(p); |
1790 |
|
} |
1791 |
|
} |
1792 |
|
|