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; |
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() ; } |
84 |
|
finally { lock.unlock() ; } |
85 |
|
} |
86 |
|
public void run() { |
63 |
– |
boolean runme; |
87 |
|
lock.lock(); |
88 |
|
try { |
89 |
< |
runme = !done; |
90 |
< |
if (!runme) |
91 |
< |
thread = Thread.currentThread(); |
89 |
> |
if (done) |
90 |
> |
return; |
91 |
> |
thread = Thread.currentThread(); |
92 |
|
} |
93 |
|
finally { lock.unlock() ; } |
71 |
– |
if (!runme) return; |
94 |
|
V v = null; |
95 |
|
Exception e = null; |
96 |
|
try { |
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; |
108 |
< |
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 |
|
|
120 |
– |
|
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 |
|
|
193 |
– |
|
229 |
|
/** |
230 |
< |
* execute successfully executes a runnable |
230 |
> |
* execute successfully executes a runnable |
231 |
|
*/ |
232 |
|
public void testExecute() throws InterruptedException { |
233 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
234 |
< |
try { |
235 |
< |
p1.execute(new ShortRunnable()); |
236 |
< |
Thread.sleep(SMALL_DELAY_MS); |
237 |
< |
} finally { |
238 |
< |
joinPool(p1); |
233 |
> |
final ThreadPoolExecutor p = |
234 |
> |
new CustomTPE(1, 1, |
235 |
> |
2 * LONG_DELAY_MS, MILLISECONDS, |
236 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
237 |
> |
try (PoolCleaner<ThreadPoolExecutor> 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(LONG_DELAY_MS, MILLISECONDS)); |
243 |
|
} |
244 |
|
} |
245 |
|
|
246 |
|
/** |
247 |
< |
* getActiveCount increases but doesn't overestimate, when a |
248 |
< |
* thread becomes active |
247 |
> |
* getActiveCount increases but doesn't overestimate, when a |
248 |
> |
* thread becomes active |
249 |
|
*/ |
250 |
|
public void testGetActiveCount() throws InterruptedException { |
251 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
252 |
< |
assertEquals(0, p2.getActiveCount()); |
253 |
< |
p2.execute(new MediumRunnable()); |
254 |
< |
Thread.sleep(SHORT_DELAY_MS); |
255 |
< |
assertEquals(1, p2.getActiveCount()); |
256 |
< |
joinPool(p2); |
251 |
> |
final ThreadPoolExecutor p = |
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 { |
258 |
> |
assertEquals(0, p.getActiveCount()); |
259 |
> |
p.execute(new CheckedRunnable() { |
260 |
> |
public void realRun() throws InterruptedException { |
261 |
> |
threadStarted.countDown(); |
262 |
> |
assertEquals(1, p.getActiveCount()); |
263 |
> |
done.await(); |
264 |
> |
}}); |
265 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
266 |
> |
assertEquals(1, p.getActiveCount()); |
267 |
> |
} finally { |
268 |
> |
done.countDown(); |
269 |
> |
joinPool(p); |
270 |
> |
} |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
< |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
274 |
> |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
275 |
|
*/ |
276 |
|
public void testPrestartCoreThread() { |
277 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
278 |
< |
assertEquals(0, p2.getPoolSize()); |
279 |
< |
assertTrue(p2.prestartCoreThread()); |
280 |
< |
assertEquals(1, p2.getPoolSize()); |
281 |
< |
assertTrue(p2.prestartCoreThread()); |
282 |
< |
assertEquals(2, p2.getPoolSize()); |
283 |
< |
assertFalse(p2.prestartCoreThread()); |
284 |
< |
assertEquals(2, p2.getPoolSize()); |
285 |
< |
joinPool(p2); |
277 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
278 |
> |
assertEquals(0, p.getPoolSize()); |
279 |
> |
assertTrue(p.prestartCoreThread()); |
280 |
> |
assertEquals(1, p.getPoolSize()); |
281 |
> |
assertTrue(p.prestartCoreThread()); |
282 |
> |
assertEquals(2, p.getPoolSize()); |
283 |
> |
assertFalse(p.prestartCoreThread()); |
284 |
> |
assertEquals(2, p.getPoolSize()); |
285 |
> |
joinPool(p); |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* prestartAllCoreThreads starts all corePoolSize threads |
289 |
> |
* prestartAllCoreThreads starts all corePoolSize threads |
290 |
|
*/ |
291 |
|
public void testPrestartAllCoreThreads() { |
292 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
293 |
< |
assertEquals(0, p2.getPoolSize()); |
294 |
< |
p2.prestartAllCoreThreads(); |
295 |
< |
assertEquals(2, p2.getPoolSize()); |
296 |
< |
p2.prestartAllCoreThreads(); |
297 |
< |
assertEquals(2, p2.getPoolSize()); |
298 |
< |
joinPool(p2); |
292 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
293 |
> |
assertEquals(0, p.getPoolSize()); |
294 |
> |
p.prestartAllCoreThreads(); |
295 |
> |
assertEquals(2, p.getPoolSize()); |
296 |
> |
p.prestartAllCoreThreads(); |
297 |
> |
assertEquals(2, p.getPoolSize()); |
298 |
> |
joinPool(p); |
299 |
|
} |
300 |
|
|
301 |
|
/** |
302 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
303 |
< |
* when tasks complete |
302 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
303 |
> |
* when tasks complete |
304 |
|
*/ |
305 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
306 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
307 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
308 |
< |
p2.execute(new ShortRunnable()); |
309 |
< |
Thread.sleep(SMALL_DELAY_MS); |
310 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
311 |
< |
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
312 |
< |
joinPool(p2); |
306 |
> |
final ThreadPoolExecutor p = |
307 |
> |
new CustomTPE(2, 2, |
308 |
> |
LONG_DELAY_MS, MILLISECONDS, |
309 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
310 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
311 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
312 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
313 |
> |
try { |
314 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
315 |
> |
p.execute(new CheckedRunnable() { |
316 |
> |
public void realRun() throws InterruptedException { |
317 |
> |
threadStarted.countDown(); |
318 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
319 |
> |
threadProceed.await(); |
320 |
> |
threadDone.countDown(); |
321 |
> |
}}); |
322 |
> |
await(threadStarted); |
323 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
324 |
> |
threadProceed.countDown(); |
325 |
> |
threadDone.await(); |
326 |
> |
long startTime = System.nanoTime(); |
327 |
> |
while (p.getCompletedTaskCount() != 1) { |
328 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
329 |
> |
fail("timed out"); |
330 |
> |
Thread.yield(); |
331 |
> |
} |
332 |
> |
} finally { |
333 |
> |
joinPool(p); |
334 |
> |
} |
335 |
|
} |
336 |
|
|
337 |
|
/** |
338 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
338 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
339 |
|
*/ |
340 |
|
public void testGetCorePoolSize() { |
341 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
342 |
< |
assertEquals(1, p1.getCorePoolSize()); |
343 |
< |
joinPool(p1); |
341 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
342 |
> |
assertEquals(1, p.getCorePoolSize()); |
343 |
> |
joinPool(p); |
344 |
|
} |
345 |
|
|
346 |
|
/** |
347 |
< |
* getKeepAliveTime returns value given in constructor if not otherwise set |
347 |
> |
* getKeepAliveTime returns value given in constructor if not otherwise set |
348 |
|
*/ |
349 |
|
public void testGetKeepAliveTime() { |
350 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
351 |
< |
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
352 |
< |
joinPool(p2); |
350 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
351 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
352 |
> |
joinPool(p); |
353 |
|
} |
354 |
|
|
280 |
– |
|
355 |
|
/** |
356 |
|
* getThreadFactory returns factory in constructor if not set |
357 |
|
*/ |
373 |
|
joinPool(p); |
374 |
|
} |
375 |
|
|
302 |
– |
|
376 |
|
/** |
377 |
|
* setThreadFactory(null) throws NPE |
378 |
|
*/ |
409 |
|
joinPool(p); |
410 |
|
} |
411 |
|
|
339 |
– |
|
412 |
|
/** |
413 |
|
* setRejectedExecutionHandler(null) throws NPE |
414 |
|
*/ |
423 |
|
} |
424 |
|
} |
425 |
|
|
354 |
– |
|
426 |
|
/** |
427 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
428 |
< |
* multiple threads active |
427 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
428 |
> |
* multiple threads active |
429 |
|
*/ |
430 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
431 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
432 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
433 |
< |
p2.execute(new MediumRunnable()); |
434 |
< |
p2.execute(new MediumRunnable()); |
435 |
< |
Thread.sleep(SHORT_DELAY_MS); |
436 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
437 |
< |
joinPool(p2); |
431 |
> |
final int THREADS = 3; |
432 |
> |
final ThreadPoolExecutor p = |
433 |
> |
new CustomTPE(THREADS, THREADS, |
434 |
> |
LONG_DELAY_MS, MILLISECONDS, |
435 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
436 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
437 |
> |
final CountDownLatch done = new CountDownLatch(1); |
438 |
> |
try { |
439 |
> |
assertEquals(0, p.getLargestPoolSize()); |
440 |
> |
for (int i = 0; i < THREADS; i++) |
441 |
> |
p.execute(new CheckedRunnable() { |
442 |
> |
public void realRun() throws InterruptedException { |
443 |
> |
threadsStarted.countDown(); |
444 |
> |
done.await(); |
445 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
446 |
> |
}}); |
447 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
448 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
449 |
> |
} finally { |
450 |
> |
done.countDown(); |
451 |
> |
joinPool(p); |
452 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
453 |
> |
} |
454 |
|
} |
455 |
|
|
456 |
|
/** |
457 |
< |
* getMaximumPoolSize returns value given in constructor if not |
458 |
< |
* otherwise set |
457 |
> |
* getMaximumPoolSize returns value given in constructor if not |
458 |
> |
* otherwise set |
459 |
|
*/ |
460 |
|
public void testGetMaximumPoolSize() { |
461 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
462 |
< |
assertEquals(2, p2.getMaximumPoolSize()); |
463 |
< |
joinPool(p2); |
461 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
462 |
> |
assertEquals(2, p.getMaximumPoolSize()); |
463 |
> |
joinPool(p); |
464 |
|
} |
465 |
|
|
466 |
|
/** |
467 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
468 |
< |
* become active |
467 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
468 |
> |
* become active |
469 |
|
*/ |
470 |
< |
public void testGetPoolSize() { |
471 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
472 |
< |
assertEquals(0, p1.getPoolSize()); |
473 |
< |
p1.execute(new MediumRunnable()); |
474 |
< |
assertEquals(1, p1.getPoolSize()); |
475 |
< |
joinPool(p1); |
470 |
> |
public void testGetPoolSize() throws InterruptedException { |
471 |
> |
final ThreadPoolExecutor p = |
472 |
> |
new CustomTPE(1, 1, |
473 |
> |
LONG_DELAY_MS, MILLISECONDS, |
474 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
475 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
476 |
> |
final CountDownLatch done = new CountDownLatch(1); |
477 |
> |
try { |
478 |
> |
assertEquals(0, p.getPoolSize()); |
479 |
> |
p.execute(new CheckedRunnable() { |
480 |
> |
public void realRun() throws InterruptedException { |
481 |
> |
threadStarted.countDown(); |
482 |
> |
assertEquals(1, p.getPoolSize()); |
483 |
> |
done.await(); |
484 |
> |
}}); |
485 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
486 |
> |
assertEquals(1, p.getPoolSize()); |
487 |
> |
} finally { |
488 |
> |
done.countDown(); |
489 |
> |
joinPool(p); |
490 |
> |
} |
491 |
|
} |
492 |
|
|
493 |
|
/** |
494 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
494 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
495 |
|
*/ |
496 |
|
public void testGetTaskCount() throws InterruptedException { |
497 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
498 |
< |
assertEquals(0, p1.getTaskCount()); |
499 |
< |
p1.execute(new MediumRunnable()); |
500 |
< |
Thread.sleep(SHORT_DELAY_MS); |
501 |
< |
assertEquals(1, p1.getTaskCount()); |
502 |
< |
joinPool(p1); |
497 |
> |
final ThreadPoolExecutor p = |
498 |
> |
new CustomTPE(1, 1, |
499 |
> |
LONG_DELAY_MS, MILLISECONDS, |
500 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
501 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
502 |
> |
final CountDownLatch done = new CountDownLatch(1); |
503 |
> |
try { |
504 |
> |
assertEquals(0, p.getTaskCount()); |
505 |
> |
p.execute(new CheckedRunnable() { |
506 |
> |
public void realRun() throws InterruptedException { |
507 |
> |
threadStarted.countDown(); |
508 |
> |
assertEquals(1, p.getTaskCount()); |
509 |
> |
done.await(); |
510 |
> |
}}); |
511 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
512 |
> |
assertEquals(1, p.getTaskCount()); |
513 |
> |
} finally { |
514 |
> |
done.countDown(); |
515 |
> |
joinPool(p); |
516 |
> |
} |
517 |
|
} |
518 |
|
|
519 |
|
/** |
520 |
< |
* isShutDown is false before shutdown, true after |
520 |
> |
* isShutdown is false before shutdown, true after |
521 |
|
*/ |
522 |
|
public void testIsShutdown() { |
523 |
|
|
524 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
525 |
< |
assertFalse(p1.isShutdown()); |
526 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
527 |
< |
assertTrue(p1.isShutdown()); |
528 |
< |
joinPool(p1); |
524 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
525 |
> |
assertFalse(p.isShutdown()); |
526 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
527 |
> |
assertTrue(p.isShutdown()); |
528 |
> |
joinPool(p); |
529 |
|
} |
530 |
|
|
415 |
– |
|
531 |
|
/** |
532 |
< |
* isTerminated is false before termination, true after |
532 |
> |
* isTerminated is false before termination, true after |
533 |
|
*/ |
534 |
|
public void testIsTerminated() throws InterruptedException { |
535 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
536 |
< |
assertFalse(p1.isTerminated()); |
537 |
< |
try { |
538 |
< |
p1.execute(new MediumRunnable()); |
539 |
< |
} finally { |
540 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
541 |
< |
} |
542 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
543 |
< |
assertTrue(p1.isTerminated()); |
535 |
> |
final ThreadPoolExecutor p = |
536 |
> |
new CustomTPE(1, 1, |
537 |
> |
LONG_DELAY_MS, MILLISECONDS, |
538 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
539 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
540 |
> |
final CountDownLatch done = new CountDownLatch(1); |
541 |
> |
try { |
542 |
> |
assertFalse(p.isTerminating()); |
543 |
> |
p.execute(new CheckedRunnable() { |
544 |
> |
public void realRun() throws InterruptedException { |
545 |
> |
assertFalse(p.isTerminating()); |
546 |
> |
threadStarted.countDown(); |
547 |
> |
done.await(); |
548 |
> |
}}); |
549 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
550 |
> |
assertFalse(p.isTerminating()); |
551 |
> |
done.countDown(); |
552 |
> |
} finally { |
553 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
554 |
> |
} |
555 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
556 |
> |
assertTrue(p.isTerminated()); |
557 |
> |
assertFalse(p.isTerminating()); |
558 |
|
} |
559 |
|
|
560 |
|
/** |
561 |
< |
* isTerminating is not true when running or when terminated |
561 |
> |
* isTerminating is not true when running or when terminated |
562 |
|
*/ |
563 |
|
public void testIsTerminating() throws InterruptedException { |
564 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
565 |
< |
assertFalse(p1.isTerminating()); |
566 |
< |
try { |
567 |
< |
p1.execute(new SmallRunnable()); |
568 |
< |
assertFalse(p1.isTerminating()); |
569 |
< |
} finally { |
570 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
571 |
< |
} |
572 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
573 |
< |
assertTrue(p1.isTerminated()); |
574 |
< |
assertFalse(p1.isTerminating()); |
564 |
> |
final ThreadPoolExecutor p = |
565 |
> |
new CustomTPE(1, 1, |
566 |
> |
LONG_DELAY_MS, MILLISECONDS, |
567 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
568 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
569 |
> |
final CountDownLatch done = new CountDownLatch(1); |
570 |
> |
try { |
571 |
> |
assertFalse(p.isTerminating()); |
572 |
> |
p.execute(new CheckedRunnable() { |
573 |
> |
public void realRun() throws InterruptedException { |
574 |
> |
assertFalse(p.isTerminating()); |
575 |
> |
threadStarted.countDown(); |
576 |
> |
done.await(); |
577 |
> |
}}); |
578 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
579 |
> |
assertFalse(p.isTerminating()); |
580 |
> |
done.countDown(); |
581 |
> |
} finally { |
582 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
583 |
> |
} |
584 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
585 |
> |
assertTrue(p.isTerminated()); |
586 |
> |
assertFalse(p.isTerminating()); |
587 |
|
} |
588 |
|
|
589 |
|
/** |
590 |
|
* getQueue returns the work queue, which contains queued tasks |
591 |
|
*/ |
592 |
|
public void testGetQueue() throws InterruptedException { |
593 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
594 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
595 |
< |
FutureTask[] tasks = new FutureTask[5]; |
596 |
< |
for (int i = 0; i < 5; i++) { |
597 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
598 |
< |
p1.execute(tasks[i]); |
599 |
< |
} |
600 |
< |
try { |
601 |
< |
Thread.sleep(SHORT_DELAY_MS); |
602 |
< |
BlockingQueue<Runnable> wq = p1.getQueue(); |
603 |
< |
assertSame(q, wq); |
604 |
< |
assertFalse(wq.contains(tasks[0])); |
605 |
< |
assertTrue(wq.contains(tasks[4])); |
606 |
< |
for (int i = 1; i < 5; ++i) |
607 |
< |
tasks[i].cancel(true); |
608 |
< |
p1.shutdownNow(); |
593 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
594 |
> |
final ThreadPoolExecutor p = |
595 |
> |
new CustomTPE(1, 1, |
596 |
> |
LONG_DELAY_MS, MILLISECONDS, |
597 |
> |
q); |
598 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
599 |
> |
final CountDownLatch done = new CountDownLatch(1); |
600 |
> |
try { |
601 |
> |
FutureTask[] tasks = new FutureTask[5]; |
602 |
> |
for (int i = 0; i < tasks.length; i++) { |
603 |
> |
Callable task = new CheckedCallable<Boolean>() { |
604 |
> |
public Boolean realCall() throws InterruptedException { |
605 |
> |
threadStarted.countDown(); |
606 |
> |
assertSame(q, p.getQueue()); |
607 |
> |
done.await(); |
608 |
> |
return Boolean.TRUE; |
609 |
> |
}}; |
610 |
> |
tasks[i] = new FutureTask(task); |
611 |
> |
p.execute(tasks[i]); |
612 |
> |
} |
613 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
614 |
> |
assertSame(q, p.getQueue()); |
615 |
> |
assertFalse(q.contains(tasks[0])); |
616 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
617 |
> |
assertEquals(tasks.length - 1, q.size()); |
618 |
|
} finally { |
619 |
< |
joinPool(p1); |
619 |
> |
done.countDown(); |
620 |
> |
joinPool(p); |
621 |
|
} |
622 |
|
} |
623 |
|
|
626 |
|
*/ |
627 |
|
public void testRemove() throws InterruptedException { |
628 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
629 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
630 |
< |
FutureTask[] tasks = new FutureTask[5]; |
631 |
< |
for (int i = 0; i < 5; i++) { |
632 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
633 |
< |
p1.execute(tasks[i]); |
634 |
< |
} |
635 |
< |
try { |
636 |
< |
Thread.sleep(SHORT_DELAY_MS); |
637 |
< |
assertFalse(p1.remove(tasks[0])); |
629 |
> |
final ThreadPoolExecutor p = |
630 |
> |
new CustomTPE(1, 1, |
631 |
> |
LONG_DELAY_MS, MILLISECONDS, |
632 |
> |
q); |
633 |
> |
Runnable[] tasks = new Runnable[6]; |
634 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
635 |
> |
final CountDownLatch done = new CountDownLatch(1); |
636 |
> |
try { |
637 |
> |
for (int i = 0; i < tasks.length; i++) { |
638 |
> |
tasks[i] = new CheckedRunnable() { |
639 |
> |
public void realRun() throws InterruptedException { |
640 |
> |
threadStarted.countDown(); |
641 |
> |
done.await(); |
642 |
> |
}}; |
643 |
> |
p.execute(tasks[i]); |
644 |
> |
} |
645 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
646 |
> |
assertFalse(p.remove(tasks[0])); |
647 |
|
assertTrue(q.contains(tasks[4])); |
648 |
|
assertTrue(q.contains(tasks[3])); |
649 |
< |
assertTrue(p1.remove(tasks[4])); |
650 |
< |
assertFalse(p1.remove(tasks[4])); |
649 |
> |
assertTrue(p.remove(tasks[4])); |
650 |
> |
assertFalse(p.remove(tasks[4])); |
651 |
|
assertFalse(q.contains(tasks[4])); |
652 |
|
assertTrue(q.contains(tasks[3])); |
653 |
< |
assertTrue(p1.remove(tasks[3])); |
653 |
> |
assertTrue(p.remove(tasks[3])); |
654 |
|
assertFalse(q.contains(tasks[3])); |
655 |
|
} finally { |
656 |
< |
joinPool(p1); |
656 |
> |
done.countDown(); |
657 |
> |
joinPool(p); |
658 |
|
} |
659 |
|
} |
660 |
|
|
661 |
|
/** |
662 |
< |
* purge removes cancelled tasks from the queue |
662 |
> |
* purge removes cancelled tasks from the queue |
663 |
|
*/ |
664 |
< |
public void testPurge() { |
665 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
664 |
> |
public void testPurge() throws InterruptedException { |
665 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
666 |
> |
final CountDownLatch done = new CountDownLatch(1); |
667 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
668 |
> |
final ThreadPoolExecutor p = |
669 |
> |
new CustomTPE(1, 1, |
670 |
> |
LONG_DELAY_MS, MILLISECONDS, |
671 |
> |
q); |
672 |
|
FutureTask[] tasks = new FutureTask[5]; |
673 |
< |
for (int i = 0; i < 5; i++) { |
674 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
675 |
< |
p1.execute(tasks[i]); |
673 |
> |
try { |
674 |
> |
for (int i = 0; i < tasks.length; i++) { |
675 |
> |
Callable task = new CheckedCallable<Boolean>() { |
676 |
> |
public Boolean realCall() throws InterruptedException { |
677 |
> |
threadStarted.countDown(); |
678 |
> |
done.await(); |
679 |
> |
return Boolean.TRUE; |
680 |
> |
}}; |
681 |
> |
tasks[i] = new FutureTask(task); |
682 |
> |
p.execute(tasks[i]); |
683 |
> |
} |
684 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
685 |
> |
assertEquals(tasks.length, p.getTaskCount()); |
686 |
> |
assertEquals(tasks.length - 1, q.size()); |
687 |
> |
assertEquals(1L, p.getActiveCount()); |
688 |
> |
assertEquals(0L, p.getCompletedTaskCount()); |
689 |
> |
tasks[4].cancel(true); |
690 |
> |
tasks[3].cancel(false); |
691 |
> |
p.purge(); |
692 |
> |
assertEquals(tasks.length - 3, q.size()); |
693 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
694 |
> |
p.purge(); // Nothing to do |
695 |
> |
assertEquals(tasks.length - 3, q.size()); |
696 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
697 |
> |
} finally { |
698 |
> |
done.countDown(); |
699 |
> |
joinPool(p); |
700 |
|
} |
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); |
701 |
|
} |
702 |
|
|
703 |
|
/** |
704 |
< |
* shutDownNow returns a list containing tasks that were not run |
704 |
> |
* shutdownNow returns a list containing tasks that were not run, |
705 |
> |
* and those tasks are drained from the queue |
706 |
|
*/ |
707 |
< |
public void testShutDownNow() { |
708 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
709 |
< |
List l; |
710 |
< |
try { |
711 |
< |
for (int i = 0; i < 5; i++) |
712 |
< |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
713 |
< |
} |
714 |
< |
finally { |
707 |
> |
public void testShutdownNow() throws InterruptedException { |
708 |
> |
final int poolSize = 2; |
709 |
> |
final int count = 5; |
710 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
711 |
> |
ThreadPoolExecutor p = |
712 |
> |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
713 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
714 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
715 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
716 |
> |
threadsStarted.countDown(); |
717 |
|
try { |
718 |
< |
l = p1.shutdownNow(); |
719 |
< |
} catch (SecurityException ok) { return; } |
720 |
< |
} |
721 |
< |
assertTrue(p1.isShutdown()); |
722 |
< |
assertTrue(l.size() <= 4); |
718 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
719 |
> |
} catch (InterruptedException success) {} |
720 |
> |
ran.getAndIncrement(); |
721 |
> |
}}; |
722 |
> |
for (int i = 0; i < count; i++) |
723 |
> |
p.execute(waiter); |
724 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
725 |
> |
assertEquals(poolSize, p.getActiveCount()); |
726 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
727 |
> |
final List<Runnable> queuedTasks; |
728 |
> |
try { |
729 |
> |
queuedTasks = p.shutdownNow(); |
730 |
> |
} catch (SecurityException ok) { |
731 |
> |
return; // Allowed in case test doesn't have privs |
732 |
> |
} |
733 |
> |
assertTrue(p.isShutdown()); |
734 |
> |
assertTrue(p.getQueue().isEmpty()); |
735 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
736 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
737 |
> |
assertTrue(p.isTerminated()); |
738 |
> |
assertEquals(poolSize, ran.get()); |
739 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
740 |
|
} |
741 |
|
|
742 |
|
// Exception Tests |
743 |
|
|
539 |
– |
|
744 |
|
/** |
745 |
|
* Constructor throws if corePoolSize argument is less than zero |
746 |
|
*/ |
747 |
|
public void testConstructor1() { |
748 |
|
try { |
749 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
749 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
750 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
751 |
|
shouldThrow(); |
752 |
|
} catch (IllegalArgumentException success) {} |
753 |
|
} |
757 |
|
*/ |
758 |
|
public void testConstructor2() { |
759 |
|
try { |
760 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
760 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
761 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
762 |
|
shouldThrow(); |
763 |
|
} catch (IllegalArgumentException success) {} |
764 |
|
} |
768 |
|
*/ |
769 |
|
public void testConstructor3() { |
770 |
|
try { |
771 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
771 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
772 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
773 |
|
shouldThrow(); |
774 |
|
} catch (IllegalArgumentException success) {} |
775 |
|
} |
779 |
|
*/ |
780 |
|
public void testConstructor4() { |
781 |
|
try { |
782 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
782 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
783 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
784 |
|
shouldThrow(); |
785 |
|
} catch (IllegalArgumentException success) {} |
786 |
|
} |
790 |
|
*/ |
791 |
|
public void testConstructor5() { |
792 |
|
try { |
793 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
793 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
794 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
795 |
|
shouldThrow(); |
796 |
|
} catch (IllegalArgumentException success) {} |
797 |
|
} |
801 |
|
*/ |
802 |
|
public void testConstructorNullPointerException() { |
803 |
|
try { |
804 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
804 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
805 |
|
shouldThrow(); |
806 |
|
} catch (NullPointerException success) {} |
807 |
|
} |
808 |
|
|
600 |
– |
|
601 |
– |
|
809 |
|
/** |
810 |
|
* Constructor throws if corePoolSize argument is less than zero |
811 |
|
*/ |
812 |
|
public void testConstructor6() { |
813 |
|
try { |
814 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
814 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
815 |
> |
new ArrayBlockingQueue<Runnable>(10), |
816 |
> |
new SimpleThreadFactory()); |
817 |
|
shouldThrow(); |
818 |
|
} catch (IllegalArgumentException success) {} |
819 |
|
} |
823 |
|
*/ |
824 |
|
public void testConstructor7() { |
825 |
|
try { |
826 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
826 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
827 |
> |
new ArrayBlockingQueue<Runnable>(10), |
828 |
> |
new SimpleThreadFactory()); |
829 |
|
shouldThrow(); |
830 |
|
} catch (IllegalArgumentException success) {} |
831 |
|
} |
835 |
|
*/ |
836 |
|
public void testConstructor8() { |
837 |
|
try { |
838 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
838 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
839 |
> |
new ArrayBlockingQueue<Runnable>(10), |
840 |
> |
new SimpleThreadFactory()); |
841 |
|
shouldThrow(); |
842 |
|
} catch (IllegalArgumentException success) {} |
843 |
|
} |
847 |
|
*/ |
848 |
|
public void testConstructor9() { |
849 |
|
try { |
850 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
850 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
851 |
> |
new ArrayBlockingQueue<Runnable>(10), |
852 |
> |
new SimpleThreadFactory()); |
853 |
|
shouldThrow(); |
854 |
|
} catch (IllegalArgumentException success) {} |
855 |
|
} |
859 |
|
*/ |
860 |
|
public void testConstructor10() { |
861 |
|
try { |
862 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
862 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
863 |
> |
new ArrayBlockingQueue<Runnable>(10), |
864 |
> |
new SimpleThreadFactory()); |
865 |
|
shouldThrow(); |
866 |
|
} catch (IllegalArgumentException success) {} |
867 |
|
} |
871 |
|
*/ |
872 |
|
public void testConstructorNullPointerException2() { |
873 |
|
try { |
874 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
874 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
875 |
|
shouldThrow(); |
876 |
|
} catch (NullPointerException success) {} |
877 |
|
} |
881 |
|
*/ |
882 |
|
public void testConstructorNullPointerException3() { |
883 |
|
try { |
884 |
< |
ThreadFactory f = null; |
885 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
884 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
885 |
> |
new ArrayBlockingQueue<Runnable>(10), |
886 |
> |
(ThreadFactory) null); |
887 |
|
shouldThrow(); |
888 |
|
} catch (NullPointerException success) {} |
889 |
|
} |
890 |
|
|
673 |
– |
|
891 |
|
/** |
892 |
|
* Constructor throws if corePoolSize argument is less than zero |
893 |
|
*/ |
894 |
|
public void testConstructor11() { |
895 |
|
try { |
896 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
896 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
897 |
> |
new ArrayBlockingQueue<Runnable>(10), |
898 |
> |
new NoOpREHandler()); |
899 |
|
shouldThrow(); |
900 |
|
} catch (IllegalArgumentException success) {} |
901 |
|
} |
905 |
|
*/ |
906 |
|
public void testConstructor12() { |
907 |
|
try { |
908 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
908 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
909 |
> |
new ArrayBlockingQueue<Runnable>(10), |
910 |
> |
new NoOpREHandler()); |
911 |
|
shouldThrow(); |
912 |
|
} catch (IllegalArgumentException success) {} |
913 |
|
} |
917 |
|
*/ |
918 |
|
public void testConstructor13() { |
919 |
|
try { |
920 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
920 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
921 |
> |
new ArrayBlockingQueue<Runnable>(10), |
922 |
> |
new NoOpREHandler()); |
923 |
|
shouldThrow(); |
924 |
|
} catch (IllegalArgumentException success) {} |
925 |
|
} |
929 |
|
*/ |
930 |
|
public void testConstructor14() { |
931 |
|
try { |
932 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
932 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
933 |
> |
new ArrayBlockingQueue<Runnable>(10), |
934 |
> |
new NoOpREHandler()); |
935 |
|
shouldThrow(); |
936 |
|
} catch (IllegalArgumentException success) {} |
937 |
|
} |
941 |
|
*/ |
942 |
|
public void testConstructor15() { |
943 |
|
try { |
944 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
944 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
945 |
> |
new ArrayBlockingQueue<Runnable>(10), |
946 |
> |
new NoOpREHandler()); |
947 |
|
shouldThrow(); |
948 |
|
} catch (IllegalArgumentException success) {} |
949 |
|
} |
953 |
|
*/ |
954 |
|
public void testConstructorNullPointerException4() { |
955 |
|
try { |
956 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
956 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
957 |
> |
null, |
958 |
> |
new NoOpREHandler()); |
959 |
|
shouldThrow(); |
960 |
|
} catch (NullPointerException success) {} |
961 |
|
} |
965 |
|
*/ |
966 |
|
public void testConstructorNullPointerException5() { |
967 |
|
try { |
968 |
< |
RejectedExecutionHandler r = null; |
969 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
968 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
969 |
> |
new ArrayBlockingQueue<Runnable>(10), |
970 |
> |
(RejectedExecutionHandler) null); |
971 |
|
shouldThrow(); |
972 |
|
} catch (NullPointerException success) {} |
973 |
|
} |
974 |
|
|
745 |
– |
|
975 |
|
/** |
976 |
|
* Constructor throws if corePoolSize argument is less than zero |
977 |
|
*/ |
978 |
|
public void testConstructor16() { |
979 |
|
try { |
980 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
980 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
981 |
> |
new ArrayBlockingQueue<Runnable>(10), |
982 |
> |
new SimpleThreadFactory(), |
983 |
> |
new NoOpREHandler()); |
984 |
|
shouldThrow(); |
985 |
|
} catch (IllegalArgumentException success) {} |
986 |
|
} |
990 |
|
*/ |
991 |
|
public void testConstructor17() { |
992 |
|
try { |
993 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
993 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
994 |
> |
new ArrayBlockingQueue<Runnable>(10), |
995 |
> |
new SimpleThreadFactory(), |
996 |
> |
new NoOpREHandler()); |
997 |
|
shouldThrow(); |
998 |
|
} catch (IllegalArgumentException success) {} |
999 |
|
} |
1003 |
|
*/ |
1004 |
|
public void testConstructor18() { |
1005 |
|
try { |
1006 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1006 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
1007 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1008 |
> |
new SimpleThreadFactory(), |
1009 |
> |
new NoOpREHandler()); |
1010 |
|
shouldThrow(); |
1011 |
|
} catch (IllegalArgumentException success) {} |
1012 |
|
} |
1016 |
|
*/ |
1017 |
|
public void testConstructor19() { |
1018 |
|
try { |
1019 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1019 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
1020 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1021 |
> |
new SimpleThreadFactory(), |
1022 |
> |
new NoOpREHandler()); |
1023 |
|
shouldThrow(); |
1024 |
|
} catch (IllegalArgumentException success) {} |
1025 |
|
} |
1029 |
|
*/ |
1030 |
|
public void testConstructor20() { |
1031 |
|
try { |
1032 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1032 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1033 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1034 |
> |
new SimpleThreadFactory(), |
1035 |
> |
new NoOpREHandler()); |
1036 |
|
shouldThrow(); |
1037 |
|
} catch (IllegalArgumentException success) {} |
1038 |
|
} |
1039 |
|
|
1040 |
|
/** |
1041 |
< |
* Constructor throws if workQueue is set to null |
1041 |
> |
* Constructor throws if workQueue is null |
1042 |
|
*/ |
1043 |
|
public void testConstructorNullPointerException6() { |
1044 |
|
try { |
1045 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1045 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1046 |
> |
null, |
1047 |
> |
new SimpleThreadFactory(), |
1048 |
> |
new NoOpREHandler()); |
1049 |
|
shouldThrow(); |
1050 |
|
} catch (NullPointerException success) {} |
1051 |
|
} |
1052 |
|
|
1053 |
|
/** |
1054 |
< |
* Constructor throws if handler is set to null |
1054 |
> |
* Constructor throws if handler is null |
1055 |
|
*/ |
1056 |
|
public void testConstructorNullPointerException7() { |
1057 |
|
try { |
1058 |
< |
RejectedExecutionHandler r = null; |
1059 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1058 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1059 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1060 |
> |
new SimpleThreadFactory(), |
1061 |
> |
(RejectedExecutionHandler) null); |
1062 |
|
shouldThrow(); |
1063 |
|
} catch (NullPointerException success) {} |
1064 |
|
} |
1065 |
|
|
1066 |
|
/** |
1067 |
< |
* Constructor throws if ThreadFactory is set top null |
1067 |
> |
* Constructor throws if ThreadFactory is null |
1068 |
|
*/ |
1069 |
|
public void testConstructorNullPointerException8() { |
1070 |
|
try { |
1071 |
< |
ThreadFactory f = null; |
1072 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
1071 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1072 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1073 |
> |
(ThreadFactory) null, |
1074 |
> |
new NoOpREHandler()); |
1075 |
|
shouldThrow(); |
1076 |
|
} catch (NullPointerException success) {} |
1077 |
|
} |
1078 |
|
|
828 |
– |
|
1079 |
|
/** |
1080 |
< |
* execute throws RejectedExecutionException |
831 |
< |
* if saturated. |
1080 |
> |
* execute throws RejectedExecutionException if saturated. |
1081 |
|
*/ |
1082 |
|
public void testSaturatedExecute() { |
1083 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
1084 |
< |
try { |
1085 |
< |
|
1086 |
< |
for (int i = 0; i < 5; ++i) { |
1087 |
< |
p.execute(new MediumRunnable()); |
1083 |
> |
ThreadPoolExecutor p = |
1084 |
> |
new CustomTPE(1, 1, |
1085 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1086 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1087 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1088 |
> |
try { |
1089 |
> |
Runnable task = new CheckedRunnable() { |
1090 |
> |
public void realRun() throws InterruptedException { |
1091 |
> |
done.await(); |
1092 |
> |
}}; |
1093 |
> |
for (int i = 0; i < 2; ++i) |
1094 |
> |
p.execute(task); |
1095 |
> |
for (int i = 0; i < 2; ++i) { |
1096 |
> |
try { |
1097 |
> |
p.execute(task); |
1098 |
> |
shouldThrow(); |
1099 |
> |
} catch (RejectedExecutionException success) {} |
1100 |
> |
assertTrue(p.getTaskCount() <= 2); |
1101 |
|
} |
1102 |
< |
shouldThrow(); |
1103 |
< |
} catch (RejectedExecutionException success) {} |
1104 |
< |
joinPool(p); |
1102 |
> |
} finally { |
1103 |
> |
done.countDown(); |
1104 |
> |
joinPool(p); |
1105 |
> |
} |
1106 |
|
} |
1107 |
|
|
1108 |
|
/** |
1109 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1109 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
1110 |
|
*/ |
1111 |
|
public void testSaturatedExecute2() { |
1112 |
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
1113 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1113 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, |
1114 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1115 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1116 |
> |
h); |
1117 |
|
try { |
852 |
– |
|
1118 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1119 |
< |
for (int i = 0; i < 5; ++i) { |
1119 |
> |
for (int i = 0; i < tasks.length; ++i) |
1120 |
|
tasks[i] = new TrackedNoOpRunnable(); |
856 |
– |
} |
1121 |
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1122 |
|
p.execute(mr); |
1123 |
< |
for (int i = 0; i < 5; ++i) { |
1123 |
> |
for (int i = 0; i < tasks.length; ++i) |
1124 |
|
p.execute(tasks[i]); |
1125 |
< |
} |
862 |
< |
for (int i = 1; i < 5; ++i) { |
1125 |
> |
for (int i = 1; i < tasks.length; ++i) |
1126 |
|
assertTrue(tasks[i].done); |
864 |
– |
} |
1127 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1128 |
|
} finally { |
1129 |
|
joinPool(p); |
1131 |
|
} |
1132 |
|
|
1133 |
|
/** |
1134 |
< |
* executor using DiscardPolicy drops task if saturated. |
1134 |
> |
* executor using DiscardPolicy drops task if saturated. |
1135 |
|
*/ |
1136 |
|
public void testSaturatedExecute3() { |
1137 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
1138 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1138 |
> |
ThreadPoolExecutor p = |
1139 |
> |
new CustomTPE(1, 1, |
1140 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1141 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1142 |
> |
h); |
1143 |
|
try { |
878 |
– |
|
1144 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1145 |
< |
for (int i = 0; i < 5; ++i) { |
1145 |
> |
for (int i = 0; i < tasks.length; ++i) |
1146 |
|
tasks[i] = new TrackedNoOpRunnable(); |
882 |
– |
} |
1147 |
|
p.execute(new TrackedLongRunnable()); |
1148 |
< |
for (int i = 0; i < 5; ++i) { |
1149 |
< |
p.execute(tasks[i]); |
1150 |
< |
} |
1151 |
< |
for (int i = 0; i < 5; ++i) { |
888 |
< |
assertFalse(tasks[i].done); |
889 |
< |
} |
1148 |
> |
for (TrackedNoOpRunnable task : tasks) |
1149 |
> |
p.execute(task); |
1150 |
> |
for (TrackedNoOpRunnable task : tasks) |
1151 |
> |
assertFalse(task.done); |
1152 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1153 |
|
} finally { |
1154 |
|
joinPool(p); |
1156 |
|
} |
1157 |
|
|
1158 |
|
/** |
1159 |
< |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1159 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1160 |
|
*/ |
1161 |
|
public void testSaturatedExecute4() { |
1162 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
1177 |
|
} |
1178 |
|
|
1179 |
|
/** |
1180 |
< |
* execute throws RejectedExecutionException if shutdown |
1180 |
> |
* execute throws RejectedExecutionException if shutdown |
1181 |
|
*/ |
1182 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1183 |
< |
ThreadPoolExecutor tpe = |
1183 |
> |
ThreadPoolExecutor p = |
1184 |
|
new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1185 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1185 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1186 |
|
try { |
1187 |
< |
tpe.execute(new NoOpRunnable()); |
1187 |
> |
p.execute(new NoOpRunnable()); |
1188 |
|
shouldThrow(); |
1189 |
|
} catch (RejectedExecutionException success) {} |
1190 |
|
|
1191 |
< |
joinPool(tpe); |
1191 |
> |
joinPool(p); |
1192 |
|
} |
1193 |
|
|
1194 |
|
/** |
1195 |
< |
* execute using CallerRunsPolicy drops task on shutdown |
1195 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
1196 |
|
*/ |
1197 |
|
public void testCallerRunsOnShutdown() { |
1198 |
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
1209 |
|
} |
1210 |
|
|
1211 |
|
/** |
1212 |
< |
* execute using DiscardPolicy drops task on shutdown |
1212 |
> |
* execute using DiscardPolicy drops task on shutdown |
1213 |
|
*/ |
1214 |
|
public void testDiscardOnShutdown() { |
1215 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
1225 |
|
} |
1226 |
|
} |
1227 |
|
|
966 |
– |
|
1228 |
|
/** |
1229 |
< |
* execute using DiscardOldestPolicy drops task on shutdown |
1229 |
> |
* execute using DiscardOldestPolicy drops task on shutdown |
1230 |
|
*/ |
1231 |
|
public void testDiscardOldestOnShutdown() { |
1232 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
1242 |
|
} |
1243 |
|
} |
1244 |
|
|
984 |
– |
|
1245 |
|
/** |
1246 |
|
* execute(null) throws NPE |
1247 |
|
*/ |
1248 |
|
public void testExecuteNull() { |
1249 |
< |
ThreadPoolExecutor tpe = null; |
1249 |
> |
ThreadPoolExecutor p = |
1250 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1251 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1252 |
|
try { |
1253 |
< |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
992 |
< |
tpe.execute(null); |
1253 |
> |
p.execute(null); |
1254 |
|
shouldThrow(); |
1255 |
|
} catch (NullPointerException success) {} |
1256 |
|
|
1257 |
< |
joinPool(tpe); |
1257 |
> |
joinPool(p); |
1258 |
|
} |
1259 |
|
|
1260 |
|
/** |
1261 |
< |
* setCorePoolSize of negative value throws IllegalArgumentException |
1261 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
1262 |
|
*/ |
1263 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1264 |
< |
ThreadPoolExecutor tpe = |
1264 |
> |
ThreadPoolExecutor p = |
1265 |
|
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1266 |
|
try { |
1267 |
< |
tpe.setCorePoolSize(-1); |
1267 |
> |
p.setCorePoolSize(-1); |
1268 |
|
shouldThrow(); |
1269 |
|
} catch (IllegalArgumentException success) { |
1270 |
|
} finally { |
1271 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1271 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1272 |
|
} |
1273 |
< |
joinPool(tpe); |
1273 |
> |
joinPool(p); |
1274 |
|
} |
1275 |
|
|
1276 |
|
/** |
1277 |
< |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1278 |
< |
* given a value less the core pool size |
1277 |
> |
* setMaximumPoolSize(int) throws IllegalArgumentException |
1278 |
> |
* if given a value less the core pool size |
1279 |
|
*/ |
1280 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1281 |
< |
ThreadPoolExecutor tpe = |
1281 |
> |
ThreadPoolExecutor p = |
1282 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1283 |
|
try { |
1284 |
< |
tpe.setMaximumPoolSize(1); |
1284 |
> |
p.setMaximumPoolSize(1); |
1285 |
|
shouldThrow(); |
1286 |
|
} catch (IllegalArgumentException success) { |
1287 |
|
} finally { |
1288 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1288 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1289 |
|
} |
1290 |
< |
joinPool(tpe); |
1290 |
> |
joinPool(p); |
1291 |
|
} |
1292 |
|
|
1293 |
|
/** |
1294 |
< |
* setMaximumPoolSize throws IllegalArgumentException |
1295 |
< |
* if given a negative value |
1294 |
> |
* setMaximumPoolSize throws IllegalArgumentException |
1295 |
> |
* if given a negative value |
1296 |
|
*/ |
1297 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1298 |
< |
ThreadPoolExecutor tpe = |
1298 |
> |
ThreadPoolExecutor p = |
1299 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1300 |
|
try { |
1301 |
< |
tpe.setMaximumPoolSize(-1); |
1301 |
> |
p.setMaximumPoolSize(-1); |
1302 |
|
shouldThrow(); |
1303 |
|
} catch (IllegalArgumentException success) { |
1304 |
|
} finally { |
1305 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1305 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1306 |
|
} |
1307 |
< |
joinPool(tpe); |
1307 |
> |
joinPool(p); |
1308 |
|
} |
1309 |
|
|
1049 |
– |
|
1310 |
|
/** |
1311 |
< |
* setKeepAliveTime throws IllegalArgumentException |
1312 |
< |
* when given a negative value |
1311 |
> |
* setKeepAliveTime throws IllegalArgumentException |
1312 |
> |
* when given a negative value |
1313 |
|
*/ |
1314 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1315 |
< |
ThreadPoolExecutor tpe = |
1315 |
> |
ThreadPoolExecutor p = |
1316 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1317 |
|
|
1318 |
|
try { |
1319 |
< |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
1319 |
> |
p.setKeepAliveTime(-1,MILLISECONDS); |
1320 |
|
shouldThrow(); |
1321 |
|
} catch (IllegalArgumentException success) { |
1322 |
|
} finally { |
1323 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1323 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1324 |
|
} |
1325 |
< |
joinPool(tpe); |
1325 |
> |
joinPool(p); |
1326 |
|
} |
1327 |
|
|
1328 |
|
/** |
1329 |
|
* terminated() is called on termination |
1330 |
|
*/ |
1331 |
|
public void testTerminated() { |
1332 |
< |
CustomTPE tpe = new CustomTPE(); |
1333 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1334 |
< |
assertTrue(tpe.terminatedCalled); |
1335 |
< |
joinPool(tpe); |
1332 |
> |
CustomTPE p = new CustomTPE(); |
1333 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1334 |
> |
assertTrue(p.terminatedCalled()); |
1335 |
> |
joinPool(p); |
1336 |
|
} |
1337 |
|
|
1338 |
|
/** |
1339 |
|
* beforeExecute and afterExecute are called when executing task |
1340 |
|
*/ |
1341 |
|
public void testBeforeAfter() throws InterruptedException { |
1342 |
< |
CustomTPE tpe = new CustomTPE(); |
1342 |
> |
CustomTPE p = new CustomTPE(); |
1343 |
|
try { |
1344 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1345 |
< |
tpe.execute(r); |
1346 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1347 |
< |
assertTrue(r.done); |
1348 |
< |
assertTrue(tpe.beforeCalled); |
1349 |
< |
assertTrue(tpe.afterCalled); |
1350 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1344 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1345 |
> |
p.execute(new CheckedRunnable() { |
1346 |
> |
public void realRun() { |
1347 |
> |
done.countDown(); |
1348 |
> |
}}); |
1349 |
> |
await(p.afterCalled); |
1350 |
> |
assertEquals(0, done.getCount()); |
1351 |
> |
assertTrue(p.afterCalled()); |
1352 |
> |
assertTrue(p.beforeCalled()); |
1353 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1354 |
|
} finally { |
1355 |
< |
joinPool(tpe); |
1355 |
> |
joinPool(p); |
1356 |
|
} |
1357 |
|
} |
1358 |
|
|
1398 |
|
} |
1399 |
|
} |
1400 |
|
|
1138 |
– |
|
1401 |
|
/** |
1402 |
|
* invokeAny(null) throws NPE |
1403 |
|
*/ |
1559 |
|
} |
1560 |
|
} |
1561 |
|
|
1300 |
– |
|
1301 |
– |
|
1562 |
|
/** |
1563 |
|
* timed invokeAny(null) throws NPE |
1564 |
|
*/ |
1745 |
|
l.add(new StringTask()); |
1746 |
|
l.add(new StringTask()); |
1747 |
|
List<Future<String>> futures = |
1748 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1748 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1749 |
|
assertEquals(2, futures.size()); |
1750 |
|
for (Future<String> future : futures) |
1751 |
|
assertSame(TEST_STRING, future.get()); |
1760 |
|
public void testTimedInvokeAll6() throws Exception { |
1761 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1762 |
|
try { |
1763 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1764 |
< |
l.add(new StringTask()); |
1765 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1766 |
< |
l.add(new StringTask()); |
1767 |
< |
List<Future<String>> futures = |
1768 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1769 |
< |
assertEquals(3, futures.size()); |
1770 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1771 |
< |
Future<String> f1 = it.next(); |
1772 |
< |
Future<String> f2 = it.next(); |
1773 |
< |
Future<String> f3 = it.next(); |
1774 |
< |
assertTrue(f1.isDone()); |
1775 |
< |
assertTrue(f2.isDone()); |
1776 |
< |
assertTrue(f3.isDone()); |
1777 |
< |
assertFalse(f1.isCancelled()); |
1778 |
< |
assertTrue(f2.isCancelled()); |
1763 |
> |
for (long timeout = timeoutMillis();;) { |
1764 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1765 |
> |
tasks.add(new StringTask("0")); |
1766 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1767 |
> |
tasks.add(new StringTask("2")); |
1768 |
> |
long startTime = System.nanoTime(); |
1769 |
> |
List<Future<String>> futures = |
1770 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1771 |
> |
assertEquals(tasks.size(), futures.size()); |
1772 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1773 |
> |
for (Future future : futures) |
1774 |
> |
assertTrue(future.isDone()); |
1775 |
> |
assertTrue(futures.get(1).isCancelled()); |
1776 |
> |
try { |
1777 |
> |
assertEquals("0", futures.get(0).get()); |
1778 |
> |
assertEquals("2", futures.get(2).get()); |
1779 |
> |
break; |
1780 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1781 |
> |
timeout *= 2; |
1782 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1783 |
> |
fail("expected exactly one task to be cancelled"); |
1784 |
> |
} |
1785 |
> |
} |
1786 |
|
} finally { |
1787 |
|
joinPool(e); |
1788 |
|
} |
1793 |
|
* thread factory fails to create more |
1794 |
|
*/ |
1795 |
|
public void testFailingThreadFactory() throws InterruptedException { |
1796 |
< |
ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1797 |
< |
try { |
1798 |
< |
for (int k = 0; k < 100; ++k) { |
1799 |
< |
e.execute(new NoOpRunnable()); |
1800 |
< |
} |
1801 |
< |
Thread.sleep(LONG_DELAY_MS); |
1796 |
> |
final ExecutorService e = |
1797 |
> |
new CustomTPE(100, 100, |
1798 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1799 |
> |
new LinkedBlockingQueue<Runnable>(), |
1800 |
> |
new FailingThreadFactory()); |
1801 |
> |
try { |
1802 |
> |
final int TASKS = 100; |
1803 |
> |
final CountDownLatch done = new CountDownLatch(TASKS); |
1804 |
> |
for (int k = 0; k < TASKS; ++k) |
1805 |
> |
e.execute(new CheckedRunnable() { |
1806 |
> |
public void realRun() { |
1807 |
> |
done.countDown(); |
1808 |
> |
}}); |
1809 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1810 |
|
} finally { |
1811 |
|
joinPool(e); |
1812 |
|
} |
1816 |
|
* allowsCoreThreadTimeOut is by default false. |
1817 |
|
*/ |
1818 |
|
public void testAllowsCoreThreadTimeOut() { |
1819 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1820 |
< |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1821 |
< |
joinPool(tpe); |
1819 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1820 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1821 |
> |
joinPool(p); |
1822 |
|
} |
1823 |
|
|
1824 |
|
/** |
1825 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1826 |
|
*/ |
1827 |
< |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1828 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1829 |
< |
tpe.allowCoreThreadTimeOut(true); |
1830 |
< |
tpe.execute(new NoOpRunnable()); |
1831 |
< |
try { |
1832 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1833 |
< |
assertEquals(0, tpe.getPoolSize()); |
1827 |
> |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1828 |
> |
long keepAliveTime = timeoutMillis(); |
1829 |
> |
final ThreadPoolExecutor p = |
1830 |
> |
new CustomTPE(2, 10, |
1831 |
> |
keepAliveTime, MILLISECONDS, |
1832 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1833 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1834 |
> |
try { |
1835 |
> |
p.allowCoreThreadTimeOut(true); |
1836 |
> |
p.execute(new CheckedRunnable() { |
1837 |
> |
public void realRun() { |
1838 |
> |
threadStarted.countDown(); |
1839 |
> |
assertEquals(1, p.getPoolSize()); |
1840 |
> |
}}); |
1841 |
> |
await(threadStarted); |
1842 |
> |
delay(keepAliveTime); |
1843 |
> |
long startTime = System.nanoTime(); |
1844 |
> |
while (p.getPoolSize() > 0 |
1845 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1846 |
> |
Thread.yield(); |
1847 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1848 |
> |
assertEquals(0, p.getPoolSize()); |
1849 |
|
} finally { |
1850 |
< |
joinPool(tpe); |
1850 |
> |
joinPool(p); |
1851 |
|
} |
1852 |
|
} |
1853 |
|
|
1854 |
|
/** |
1855 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1856 |
|
*/ |
1857 |
< |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1858 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1859 |
< |
tpe.allowCoreThreadTimeOut(false); |
1860 |
< |
tpe.execute(new NoOpRunnable()); |
1861 |
< |
try { |
1862 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1863 |
< |
assertTrue(tpe.getPoolSize() >= 1); |
1857 |
> |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1858 |
> |
long keepAliveTime = timeoutMillis(); |
1859 |
> |
final ThreadPoolExecutor p = |
1860 |
> |
new CustomTPE(2, 10, |
1861 |
> |
keepAliveTime, MILLISECONDS, |
1862 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1863 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1864 |
> |
try { |
1865 |
> |
p.allowCoreThreadTimeOut(false); |
1866 |
> |
p.execute(new CheckedRunnable() { |
1867 |
> |
public void realRun() throws InterruptedException { |
1868 |
> |
threadStarted.countDown(); |
1869 |
> |
assertTrue(p.getPoolSize() >= 1); |
1870 |
> |
}}); |
1871 |
> |
delay(2 * keepAliveTime); |
1872 |
> |
assertTrue(p.getPoolSize() >= 1); |
1873 |
> |
} finally { |
1874 |
> |
joinPool(p); |
1875 |
> |
} |
1876 |
> |
} |
1877 |
> |
|
1878 |
> |
/** |
1879 |
> |
* get(cancelled task) throws CancellationException |
1880 |
> |
* (in part, a test of CustomTPE itself) |
1881 |
> |
*/ |
1882 |
> |
public void testGet_cancelled() throws Exception { |
1883 |
> |
final ExecutorService e = |
1884 |
> |
new CustomTPE(1, 1, |
1885 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1886 |
> |
new LinkedBlockingQueue<Runnable>()); |
1887 |
> |
try { |
1888 |
> |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
1889 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1890 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
1891 |
> |
for (int i = 0; i < 2; i++) { |
1892 |
> |
Runnable r = new CheckedRunnable() { public void realRun() |
1893 |
> |
throws Throwable { |
1894 |
> |
blockerStarted.countDown(); |
1895 |
> |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
1896 |
> |
}}; |
1897 |
> |
futures.add(e.submit(r)); |
1898 |
> |
} |
1899 |
> |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1900 |
> |
for (Future<?> future : futures) future.cancel(false); |
1901 |
> |
for (Future<?> future : futures) { |
1902 |
> |
try { |
1903 |
> |
future.get(); |
1904 |
> |
shouldThrow(); |
1905 |
> |
} catch (CancellationException success) {} |
1906 |
> |
try { |
1907 |
> |
future.get(LONG_DELAY_MS, MILLISECONDS); |
1908 |
> |
shouldThrow(); |
1909 |
> |
} catch (CancellationException success) {} |
1910 |
> |
assertTrue(future.isCancelled()); |
1911 |
> |
assertTrue(future.isDone()); |
1912 |
> |
} |
1913 |
> |
done.countDown(); |
1914 |
|
} finally { |
1915 |
< |
joinPool(tpe); |
1915 |
> |
joinPool(e); |
1916 |
|
} |
1917 |
|
} |
1918 |
|
|