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 |
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 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 |
|
|
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 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
256 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
257 |
> |
final CountDownLatch done = new CountDownLatch(1); |
258 |
> |
assertEquals(0, p.getActiveCount()); |
259 |
> |
p.execute(new CheckedRunnable() { |
260 |
> |
public void realRun() throws InterruptedException { |
261 |
> |
threadStarted.countDown(); |
262 |
> |
assertEquals(1, p.getActiveCount()); |
263 |
> |
done.await(); |
264 |
> |
}}); |
265 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
266 |
> |
assertEquals(1, p.getActiveCount()); |
267 |
> |
done.countDown(); |
268 |
> |
} |
269 |
|
} |
270 |
|
|
271 |
|
/** |
272 |
|
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
273 |
|
*/ |
274 |
|
public void testPrestartCoreThread() { |
275 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
276 |
< |
assertEquals(0, p2.getPoolSize()); |
277 |
< |
assertTrue(p2.prestartCoreThread()); |
278 |
< |
assertEquals(1, p2.getPoolSize()); |
279 |
< |
assertTrue(p2.prestartCoreThread()); |
280 |
< |
assertEquals(2, p2.getPoolSize()); |
281 |
< |
assertFalse(p2.prestartCoreThread()); |
282 |
< |
assertEquals(2, p2.getPoolSize()); |
283 |
< |
joinPool(p2); |
275 |
> |
ThreadPoolExecutor p = |
276 |
> |
new CustomTPE(2, 6, |
277 |
> |
LONG_DELAY_MS, MILLISECONDS, |
278 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
279 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
280 |
> |
assertEquals(0, p.getPoolSize()); |
281 |
> |
assertTrue(p.prestartCoreThread()); |
282 |
> |
assertEquals(1, p.getPoolSize()); |
283 |
> |
assertTrue(p.prestartCoreThread()); |
284 |
> |
assertEquals(2, p.getPoolSize()); |
285 |
> |
assertFalse(p.prestartCoreThread()); |
286 |
> |
assertEquals(2, p.getPoolSize()); |
287 |
> |
p.setCorePoolSize(4); |
288 |
> |
assertTrue(p.prestartCoreThread()); |
289 |
> |
assertEquals(3, p.getPoolSize()); |
290 |
> |
assertTrue(p.prestartCoreThread()); |
291 |
> |
assertEquals(4, p.getPoolSize()); |
292 |
> |
assertFalse(p.prestartCoreThread()); |
293 |
> |
assertEquals(4, p.getPoolSize()); |
294 |
> |
} |
295 |
|
} |
296 |
|
|
297 |
|
/** |
298 |
|
* prestartAllCoreThreads starts all corePoolSize threads |
299 |
|
*/ |
300 |
|
public void testPrestartAllCoreThreads() { |
301 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
302 |
< |
assertEquals(0, p2.getPoolSize()); |
303 |
< |
p2.prestartAllCoreThreads(); |
304 |
< |
assertEquals(2, p2.getPoolSize()); |
305 |
< |
p2.prestartAllCoreThreads(); |
306 |
< |
assertEquals(2, p2.getPoolSize()); |
307 |
< |
joinPool(p2); |
301 |
> |
ThreadPoolExecutor p = |
302 |
> |
new CustomTPE(2, 6, |
303 |
> |
LONG_DELAY_MS, MILLISECONDS, |
304 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
305 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
306 |
> |
assertEquals(0, p.getPoolSize()); |
307 |
> |
p.prestartAllCoreThreads(); |
308 |
> |
assertEquals(2, p.getPoolSize()); |
309 |
> |
p.prestartAllCoreThreads(); |
310 |
> |
assertEquals(2, p.getPoolSize()); |
311 |
> |
p.setCorePoolSize(4); |
312 |
> |
p.prestartAllCoreThreads(); |
313 |
> |
assertEquals(4, p.getPoolSize()); |
314 |
> |
p.prestartAllCoreThreads(); |
315 |
> |
assertEquals(4, p.getPoolSize()); |
316 |
> |
} |
317 |
|
} |
318 |
|
|
319 |
|
/** |
321 |
|
* when tasks complete |
322 |
|
*/ |
323 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
324 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
325 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
326 |
< |
p2.execute(new ShortRunnable()); |
327 |
< |
Thread.sleep(SMALL_DELAY_MS); |
328 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
329 |
< |
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
330 |
< |
joinPool(p2); |
324 |
> |
final ThreadPoolExecutor p = |
325 |
> |
new CustomTPE(2, 2, |
326 |
> |
LONG_DELAY_MS, MILLISECONDS, |
327 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
328 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
329 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
330 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
331 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
332 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
333 |
> |
p.execute(new CheckedRunnable() { |
334 |
> |
public void realRun() throws InterruptedException { |
335 |
> |
threadStarted.countDown(); |
336 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
337 |
> |
threadProceed.await(); |
338 |
> |
threadDone.countDown(); |
339 |
> |
}}); |
340 |
> |
await(threadStarted); |
341 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
342 |
> |
threadProceed.countDown(); |
343 |
> |
threadDone.await(); |
344 |
> |
long startTime = System.nanoTime(); |
345 |
> |
while (p.getCompletedTaskCount() != 1) { |
346 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
347 |
> |
fail("timed out"); |
348 |
> |
Thread.yield(); |
349 |
> |
} |
350 |
> |
} |
351 |
|
} |
352 |
|
|
353 |
|
/** |
354 |
|
* getCorePoolSize returns size given in constructor if not otherwise set |
355 |
|
*/ |
356 |
|
public void testGetCorePoolSize() { |
357 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
358 |
< |
assertEquals(1, p1.getCorePoolSize()); |
359 |
< |
joinPool(p1); |
357 |
> |
final ThreadPoolExecutor p = |
358 |
> |
new CustomTPE(1, 1, |
359 |
> |
LONG_DELAY_MS, MILLISECONDS, |
360 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
361 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
362 |
> |
assertEquals(1, p.getCorePoolSize()); |
363 |
> |
} |
364 |
|
} |
365 |
|
|
366 |
|
/** |
367 |
|
* getKeepAliveTime returns value given in constructor if not otherwise set |
368 |
|
*/ |
369 |
|
public void testGetKeepAliveTime() { |
370 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
371 |
< |
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
372 |
< |
joinPool(p2); |
370 |
> |
final ThreadPoolExecutor p = |
371 |
> |
new CustomTPE(2, 2, |
372 |
> |
1000, MILLISECONDS, |
373 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
374 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
375 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
376 |
> |
} |
377 |
|
} |
378 |
|
|
280 |
– |
|
379 |
|
/** |
380 |
|
* getThreadFactory returns factory in constructor if not set |
381 |
|
*/ |
382 |
|
public void testGetThreadFactory() { |
383 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
384 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
385 |
< |
assertSame(tf, p.getThreadFactory()); |
386 |
< |
joinPool(p); |
383 |
> |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
384 |
> |
final ThreadPoolExecutor p = |
385 |
> |
new CustomTPE(1, 2, |
386 |
> |
LONG_DELAY_MS, MILLISECONDS, |
387 |
> |
new ArrayBlockingQueue<Runnable>(10), |
388 |
> |
threadFactory, |
389 |
> |
new NoOpREHandler()); |
390 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
391 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
392 |
> |
} |
393 |
|
} |
394 |
|
|
395 |
|
/** |
396 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
397 |
|
*/ |
398 |
|
public void testSetThreadFactory() { |
399 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
400 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
401 |
< |
p.setThreadFactory(tf); |
402 |
< |
assertSame(tf, p.getThreadFactory()); |
403 |
< |
joinPool(p); |
399 |
> |
final ThreadPoolExecutor p = |
400 |
> |
new CustomTPE(1, 2, |
401 |
> |
LONG_DELAY_MS, MILLISECONDS, |
402 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
403 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
404 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
405 |
> |
p.setThreadFactory(threadFactory); |
406 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
407 |
> |
} |
408 |
|
} |
409 |
|
|
302 |
– |
|
410 |
|
/** |
411 |
|
* setThreadFactory(null) throws NPE |
412 |
|
*/ |
413 |
|
public void testSetThreadFactoryNull() { |
414 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
415 |
< |
try { |
416 |
< |
p.setThreadFactory(null); |
417 |
< |
shouldThrow(); |
418 |
< |
} catch (NullPointerException success) { |
419 |
< |
} finally { |
420 |
< |
joinPool(p); |
414 |
> |
final ThreadPoolExecutor p = |
415 |
> |
new CustomTPE(1, 2, |
416 |
> |
LONG_DELAY_MS, MILLISECONDS, |
417 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
418 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
419 |
> |
try { |
420 |
> |
p.setThreadFactory(null); |
421 |
> |
shouldThrow(); |
422 |
> |
} catch (NullPointerException success) {} |
423 |
|
} |
424 |
|
} |
425 |
|
|
427 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
428 |
|
*/ |
429 |
|
public void testGetRejectedExecutionHandler() { |
430 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
431 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
432 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
433 |
< |
joinPool(p); |
430 |
> |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
431 |
> |
final ThreadPoolExecutor p = |
432 |
> |
new CustomTPE(1, 2, |
433 |
> |
LONG_DELAY_MS, MILLISECONDS, |
434 |
> |
new ArrayBlockingQueue<Runnable>(10), |
435 |
> |
handler); |
436 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
437 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
438 |
> |
} |
439 |
|
} |
440 |
|
|
441 |
|
/** |
443 |
|
* getRejectedExecutionHandler |
444 |
|
*/ |
445 |
|
public void testSetRejectedExecutionHandler() { |
446 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
447 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
448 |
< |
p.setRejectedExecutionHandler(h); |
449 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
450 |
< |
joinPool(p); |
446 |
> |
final ThreadPoolExecutor p = |
447 |
> |
new CustomTPE(1, 2, |
448 |
> |
LONG_DELAY_MS, MILLISECONDS, |
449 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
450 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
451 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
452 |
> |
p.setRejectedExecutionHandler(handler); |
453 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
454 |
> |
} |
455 |
|
} |
456 |
|
|
339 |
– |
|
457 |
|
/** |
458 |
|
* setRejectedExecutionHandler(null) throws NPE |
459 |
|
*/ |
460 |
|
public void testSetRejectedExecutionHandlerNull() { |
461 |
< |
ThreadPoolExecutor p = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
462 |
< |
try { |
463 |
< |
p.setRejectedExecutionHandler(null); |
464 |
< |
shouldThrow(); |
465 |
< |
} catch (NullPointerException success) { |
466 |
< |
} finally { |
467 |
< |
joinPool(p); |
461 |
> |
final ThreadPoolExecutor p = |
462 |
> |
new CustomTPE(1, 2, |
463 |
> |
LONG_DELAY_MS, MILLISECONDS, |
464 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
465 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
466 |
> |
try { |
467 |
> |
p.setRejectedExecutionHandler(null); |
468 |
> |
shouldThrow(); |
469 |
> |
} catch (NullPointerException success) {} |
470 |
|
} |
471 |
|
} |
472 |
|
|
354 |
– |
|
473 |
|
/** |
474 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
475 |
|
* multiple threads active |
476 |
|
*/ |
477 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
478 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
479 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
480 |
< |
p2.execute(new MediumRunnable()); |
481 |
< |
p2.execute(new MediumRunnable()); |
482 |
< |
Thread.sleep(SHORT_DELAY_MS); |
483 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
484 |
< |
joinPool(p2); |
478 |
> |
final int THREADS = 3; |
479 |
> |
final ThreadPoolExecutor p = |
480 |
> |
new CustomTPE(THREADS, THREADS, |
481 |
> |
LONG_DELAY_MS, MILLISECONDS, |
482 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
483 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
484 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
485 |
> |
final CountDownLatch done = new CountDownLatch(1); |
486 |
> |
assertEquals(0, p.getLargestPoolSize()); |
487 |
> |
for (int i = 0; i < THREADS; i++) |
488 |
> |
p.execute(new CheckedRunnable() { |
489 |
> |
public void realRun() throws InterruptedException { |
490 |
> |
threadsStarted.countDown(); |
491 |
> |
done.await(); |
492 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
493 |
> |
}}); |
494 |
> |
assertTrue(threadsStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
495 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
496 |
> |
done.countDown(); // release pool |
497 |
> |
} |
498 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
499 |
|
} |
500 |
|
|
501 |
|
/** |
503 |
|
* otherwise set |
504 |
|
*/ |
505 |
|
public void testGetMaximumPoolSize() { |
506 |
< |
ThreadPoolExecutor p2 = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
507 |
< |
assertEquals(2, p2.getMaximumPoolSize()); |
508 |
< |
joinPool(p2); |
506 |
> |
final ThreadPoolExecutor p = |
507 |
> |
new CustomTPE(2, 3, |
508 |
> |
LONG_DELAY_MS, MILLISECONDS, |
509 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
510 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
511 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
512 |
> |
p.setMaximumPoolSize(5); |
513 |
> |
assertEquals(5, p.getMaximumPoolSize()); |
514 |
> |
p.setMaximumPoolSize(4); |
515 |
> |
assertEquals(4, p.getMaximumPoolSize()); |
516 |
> |
} |
517 |
|
} |
518 |
|
|
519 |
|
/** |
520 |
|
* getPoolSize increases, but doesn't overestimate, when threads |
521 |
|
* become active |
522 |
|
*/ |
523 |
< |
public void testGetPoolSize() { |
524 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
525 |
< |
assertEquals(0, p1.getPoolSize()); |
526 |
< |
p1.execute(new MediumRunnable()); |
527 |
< |
assertEquals(1, p1.getPoolSize()); |
528 |
< |
joinPool(p1); |
523 |
> |
public void testGetPoolSize() throws InterruptedException { |
524 |
> |
final ThreadPoolExecutor p = |
525 |
> |
new CustomTPE(1, 1, |
526 |
> |
LONG_DELAY_MS, MILLISECONDS, |
527 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
528 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
529 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
530 |
> |
final CountDownLatch done = new CountDownLatch(1); |
531 |
> |
assertEquals(0, p.getPoolSize()); |
532 |
> |
p.execute(new CheckedRunnable() { |
533 |
> |
public void realRun() throws InterruptedException { |
534 |
> |
threadStarted.countDown(); |
535 |
> |
assertEquals(1, p.getPoolSize()); |
536 |
> |
done.await(); |
537 |
> |
}}); |
538 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
539 |
> |
assertEquals(1, p.getPoolSize()); |
540 |
> |
done.countDown(); // release pool |
541 |
> |
} |
542 |
|
} |
543 |
|
|
544 |
|
/** |
545 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
546 |
|
*/ |
547 |
|
public void testGetTaskCount() throws InterruptedException { |
548 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
549 |
< |
assertEquals(0, p1.getTaskCount()); |
550 |
< |
p1.execute(new MediumRunnable()); |
551 |
< |
Thread.sleep(SHORT_DELAY_MS); |
552 |
< |
assertEquals(1, p1.getTaskCount()); |
553 |
< |
joinPool(p1); |
548 |
> |
final ThreadPoolExecutor p = |
549 |
> |
new CustomTPE(1, 1, |
550 |
> |
LONG_DELAY_MS, MILLISECONDS, |
551 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
552 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
553 |
> |
final CountDownLatch done = new CountDownLatch(1); |
554 |
> |
try { |
555 |
> |
assertEquals(0, p.getTaskCount()); |
556 |
> |
p.execute(new CheckedRunnable() { |
557 |
> |
public void realRun() throws InterruptedException { |
558 |
> |
threadStarted.countDown(); |
559 |
> |
assertEquals(1, p.getTaskCount()); |
560 |
> |
done.await(); |
561 |
> |
}}); |
562 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
563 |
> |
assertEquals(1, p.getTaskCount()); |
564 |
> |
} finally { |
565 |
> |
done.countDown(); |
566 |
> |
joinPool(p); |
567 |
> |
} |
568 |
|
} |
569 |
|
|
570 |
|
/** |
571 |
< |
* isShutDown is false before shutdown, true after |
571 |
> |
* isShutdown is false before shutdown, true after |
572 |
|
*/ |
573 |
|
public void testIsShutdown() { |
574 |
|
|
575 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
576 |
< |
assertFalse(p1.isShutdown()); |
577 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
578 |
< |
assertTrue(p1.isShutdown()); |
579 |
< |
joinPool(p1); |
575 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
576 |
> |
assertFalse(p.isShutdown()); |
577 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
578 |
> |
assertTrue(p.isShutdown()); |
579 |
> |
joinPool(p); |
580 |
|
} |
581 |
|
|
415 |
– |
|
582 |
|
/** |
583 |
|
* isTerminated is false before termination, true after |
584 |
|
*/ |
585 |
|
public void testIsTerminated() throws InterruptedException { |
586 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
587 |
< |
assertFalse(p1.isTerminated()); |
588 |
< |
try { |
589 |
< |
p1.execute(new MediumRunnable()); |
590 |
< |
} finally { |
591 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
592 |
< |
} |
593 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
594 |
< |
assertTrue(p1.isTerminated()); |
586 |
> |
final ThreadPoolExecutor p = |
587 |
> |
new CustomTPE(1, 1, |
588 |
> |
LONG_DELAY_MS, MILLISECONDS, |
589 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
590 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
591 |
> |
final CountDownLatch done = new CountDownLatch(1); |
592 |
> |
try { |
593 |
> |
assertFalse(p.isTerminating()); |
594 |
> |
p.execute(new CheckedRunnable() { |
595 |
> |
public void realRun() throws InterruptedException { |
596 |
> |
assertFalse(p.isTerminating()); |
597 |
> |
threadStarted.countDown(); |
598 |
> |
done.await(); |
599 |
> |
}}); |
600 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
601 |
> |
assertFalse(p.isTerminating()); |
602 |
> |
done.countDown(); |
603 |
> |
} finally { |
604 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
605 |
> |
} |
606 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
607 |
> |
assertTrue(p.isTerminated()); |
608 |
> |
assertFalse(p.isTerminating()); |
609 |
|
} |
610 |
|
|
611 |
|
/** |
612 |
|
* isTerminating is not true when running or when terminated |
613 |
|
*/ |
614 |
|
public void testIsTerminating() throws InterruptedException { |
615 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
616 |
< |
assertFalse(p1.isTerminating()); |
617 |
< |
try { |
618 |
< |
p1.execute(new SmallRunnable()); |
619 |
< |
assertFalse(p1.isTerminating()); |
620 |
< |
} finally { |
621 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
622 |
< |
} |
623 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
624 |
< |
assertTrue(p1.isTerminated()); |
625 |
< |
assertFalse(p1.isTerminating()); |
615 |
> |
final ThreadPoolExecutor p = |
616 |
> |
new CustomTPE(1, 1, |
617 |
> |
LONG_DELAY_MS, MILLISECONDS, |
618 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
619 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
620 |
> |
final CountDownLatch done = new CountDownLatch(1); |
621 |
> |
try { |
622 |
> |
assertFalse(p.isTerminating()); |
623 |
> |
p.execute(new CheckedRunnable() { |
624 |
> |
public void realRun() throws InterruptedException { |
625 |
> |
assertFalse(p.isTerminating()); |
626 |
> |
threadStarted.countDown(); |
627 |
> |
done.await(); |
628 |
> |
}}); |
629 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
630 |
> |
assertFalse(p.isTerminating()); |
631 |
> |
done.countDown(); |
632 |
> |
} finally { |
633 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
634 |
> |
} |
635 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
636 |
> |
assertTrue(p.isTerminated()); |
637 |
> |
assertFalse(p.isTerminating()); |
638 |
|
} |
639 |
|
|
640 |
|
/** |
641 |
|
* getQueue returns the work queue, which contains queued tasks |
642 |
|
*/ |
643 |
|
public void testGetQueue() throws InterruptedException { |
644 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
645 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
646 |
< |
FutureTask[] tasks = new FutureTask[5]; |
647 |
< |
for (int i = 0; i < 5; i++) { |
648 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
649 |
< |
p1.execute(tasks[i]); |
650 |
< |
} |
651 |
< |
try { |
652 |
< |
Thread.sleep(SHORT_DELAY_MS); |
653 |
< |
BlockingQueue<Runnable> wq = p1.getQueue(); |
654 |
< |
assertSame(q, wq); |
655 |
< |
assertFalse(wq.contains(tasks[0])); |
656 |
< |
assertTrue(wq.contains(tasks[4])); |
657 |
< |
for (int i = 1; i < 5; ++i) |
658 |
< |
tasks[i].cancel(true); |
659 |
< |
p1.shutdownNow(); |
644 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
645 |
> |
final ThreadPoolExecutor p = |
646 |
> |
new CustomTPE(1, 1, |
647 |
> |
LONG_DELAY_MS, MILLISECONDS, |
648 |
> |
q); |
649 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
650 |
> |
final CountDownLatch done = new CountDownLatch(1); |
651 |
> |
try { |
652 |
> |
FutureTask[] tasks = new FutureTask[5]; |
653 |
> |
for (int i = 0; i < tasks.length; i++) { |
654 |
> |
Callable task = new CheckedCallable<Boolean>() { |
655 |
> |
public Boolean realCall() throws InterruptedException { |
656 |
> |
threadStarted.countDown(); |
657 |
> |
assertSame(q, p.getQueue()); |
658 |
> |
done.await(); |
659 |
> |
return Boolean.TRUE; |
660 |
> |
}}; |
661 |
> |
tasks[i] = new FutureTask(task); |
662 |
> |
p.execute(tasks[i]); |
663 |
> |
} |
664 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
665 |
> |
assertSame(q, p.getQueue()); |
666 |
> |
assertFalse(q.contains(tasks[0])); |
667 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
668 |
> |
assertEquals(tasks.length - 1, q.size()); |
669 |
|
} finally { |
670 |
< |
joinPool(p1); |
670 |
> |
done.countDown(); |
671 |
> |
joinPool(p); |
672 |
|
} |
673 |
|
} |
674 |
|
|
677 |
|
*/ |
678 |
|
public void testRemove() throws InterruptedException { |
679 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
680 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
681 |
< |
FutureTask[] tasks = new FutureTask[5]; |
682 |
< |
for (int i = 0; i < 5; i++) { |
683 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
684 |
< |
p1.execute(tasks[i]); |
685 |
< |
} |
686 |
< |
try { |
687 |
< |
Thread.sleep(SHORT_DELAY_MS); |
688 |
< |
assertFalse(p1.remove(tasks[0])); |
680 |
> |
final ThreadPoolExecutor p = |
681 |
> |
new CustomTPE(1, 1, |
682 |
> |
LONG_DELAY_MS, MILLISECONDS, |
683 |
> |
q); |
684 |
> |
Runnable[] tasks = new Runnable[6]; |
685 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
686 |
> |
final CountDownLatch done = new CountDownLatch(1); |
687 |
> |
try { |
688 |
> |
for (int i = 0; i < tasks.length; i++) { |
689 |
> |
tasks[i] = new CheckedRunnable() { |
690 |
> |
public void realRun() throws InterruptedException { |
691 |
> |
threadStarted.countDown(); |
692 |
> |
done.await(); |
693 |
> |
}}; |
694 |
> |
p.execute(tasks[i]); |
695 |
> |
} |
696 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
697 |
> |
assertFalse(p.remove(tasks[0])); |
698 |
|
assertTrue(q.contains(tasks[4])); |
699 |
|
assertTrue(q.contains(tasks[3])); |
700 |
< |
assertTrue(p1.remove(tasks[4])); |
701 |
< |
assertFalse(p1.remove(tasks[4])); |
700 |
> |
assertTrue(p.remove(tasks[4])); |
701 |
> |
assertFalse(p.remove(tasks[4])); |
702 |
|
assertFalse(q.contains(tasks[4])); |
703 |
|
assertTrue(q.contains(tasks[3])); |
704 |
< |
assertTrue(p1.remove(tasks[3])); |
704 |
> |
assertTrue(p.remove(tasks[3])); |
705 |
|
assertFalse(q.contains(tasks[3])); |
706 |
|
} finally { |
707 |
< |
joinPool(p1); |
707 |
> |
done.countDown(); |
708 |
> |
joinPool(p); |
709 |
|
} |
710 |
|
} |
711 |
|
|
712 |
|
/** |
713 |
|
* purge removes cancelled tasks from the queue |
714 |
|
*/ |
715 |
< |
public void testPurge() { |
716 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
715 |
> |
public void testPurge() throws InterruptedException { |
716 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
717 |
> |
final CountDownLatch done = new CountDownLatch(1); |
718 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
719 |
> |
final ThreadPoolExecutor p = |
720 |
> |
new CustomTPE(1, 1, |
721 |
> |
LONG_DELAY_MS, MILLISECONDS, |
722 |
> |
q); |
723 |
|
FutureTask[] tasks = new FutureTask[5]; |
724 |
< |
for (int i = 0; i < 5; i++) { |
725 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
726 |
< |
p1.execute(tasks[i]); |
724 |
> |
try { |
725 |
> |
for (int i = 0; i < tasks.length; i++) { |
726 |
> |
Callable task = new CheckedCallable<Boolean>() { |
727 |
> |
public Boolean realCall() throws InterruptedException { |
728 |
> |
threadStarted.countDown(); |
729 |
> |
done.await(); |
730 |
> |
return Boolean.TRUE; |
731 |
> |
}}; |
732 |
> |
tasks[i] = new FutureTask(task); |
733 |
> |
p.execute(tasks[i]); |
734 |
> |
} |
735 |
> |
assertTrue(threadStarted.await(MEDIUM_DELAY_MS, MILLISECONDS)); |
736 |
> |
assertEquals(tasks.length, p.getTaskCount()); |
737 |
> |
assertEquals(tasks.length - 1, q.size()); |
738 |
> |
assertEquals(1L, p.getActiveCount()); |
739 |
> |
assertEquals(0L, p.getCompletedTaskCount()); |
740 |
> |
tasks[4].cancel(true); |
741 |
> |
tasks[3].cancel(false); |
742 |
> |
p.purge(); |
743 |
> |
assertEquals(tasks.length - 3, q.size()); |
744 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
745 |
> |
p.purge(); // Nothing to do |
746 |
> |
assertEquals(tasks.length - 3, q.size()); |
747 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
748 |
> |
} finally { |
749 |
> |
done.countDown(); |
750 |
> |
joinPool(p); |
751 |
|
} |
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); |
752 |
|
} |
753 |
|
|
754 |
|
/** |
755 |
< |
* shutDownNow returns a list containing tasks that were not run |
755 |
> |
* shutdownNow returns a list containing tasks that were not run, |
756 |
> |
* and those tasks are drained from the queue |
757 |
|
*/ |
758 |
< |
public void testShutDownNow() { |
759 |
< |
ThreadPoolExecutor p1 = new CustomTPE(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
760 |
< |
List l; |
761 |
< |
try { |
762 |
< |
for (int i = 0; i < 5; i++) |
763 |
< |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
764 |
< |
} |
765 |
< |
finally { |
758 |
> |
public void testShutdownNow() throws InterruptedException { |
759 |
> |
final int poolSize = 2; |
760 |
> |
final int count = 5; |
761 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
762 |
> |
ThreadPoolExecutor p = |
763 |
> |
new CustomTPE(poolSize, poolSize, LONG_DELAY_MS, MILLISECONDS, |
764 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
765 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
766 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
767 |
> |
threadsStarted.countDown(); |
768 |
|
try { |
769 |
< |
l = p1.shutdownNow(); |
770 |
< |
} catch (SecurityException ok) { return; } |
771 |
< |
} |
772 |
< |
assertTrue(p1.isShutdown()); |
773 |
< |
assertTrue(l.size() <= 4); |
769 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
770 |
> |
} catch (InterruptedException success) {} |
771 |
> |
ran.getAndIncrement(); |
772 |
> |
}}; |
773 |
> |
for (int i = 0; i < count; i++) |
774 |
> |
p.execute(waiter); |
775 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
776 |
> |
assertEquals(poolSize, p.getActiveCount()); |
777 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
778 |
> |
final List<Runnable> queuedTasks; |
779 |
> |
try { |
780 |
> |
queuedTasks = p.shutdownNow(); |
781 |
> |
} catch (SecurityException ok) { |
782 |
> |
return; // Allowed in case test doesn't have privs |
783 |
> |
} |
784 |
> |
assertTrue(p.isShutdown()); |
785 |
> |
assertTrue(p.getQueue().isEmpty()); |
786 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
787 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
788 |
> |
assertTrue(p.isTerminated()); |
789 |
> |
assertEquals(poolSize, ran.get()); |
790 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
791 |
|
} |
792 |
|
|
793 |
|
// Exception Tests |
794 |
|
|
539 |
– |
|
795 |
|
/** |
796 |
|
* Constructor throws if corePoolSize argument is less than zero |
797 |
|
*/ |
798 |
|
public void testConstructor1() { |
799 |
|
try { |
800 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
800 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
801 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
802 |
|
shouldThrow(); |
803 |
|
} catch (IllegalArgumentException success) {} |
804 |
|
} |
808 |
|
*/ |
809 |
|
public void testConstructor2() { |
810 |
|
try { |
811 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
811 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
812 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
813 |
|
shouldThrow(); |
814 |
|
} catch (IllegalArgumentException success) {} |
815 |
|
} |
819 |
|
*/ |
820 |
|
public void testConstructor3() { |
821 |
|
try { |
822 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
822 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
823 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
824 |
|
shouldThrow(); |
825 |
|
} catch (IllegalArgumentException success) {} |
826 |
|
} |
830 |
|
*/ |
831 |
|
public void testConstructor4() { |
832 |
|
try { |
833 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
833 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
834 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
835 |
|
shouldThrow(); |
836 |
|
} catch (IllegalArgumentException success) {} |
837 |
|
} |
841 |
|
*/ |
842 |
|
public void testConstructor5() { |
843 |
|
try { |
844 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
844 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
845 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
846 |
|
shouldThrow(); |
847 |
|
} catch (IllegalArgumentException success) {} |
848 |
|
} |
852 |
|
*/ |
853 |
|
public void testConstructorNullPointerException() { |
854 |
|
try { |
855 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
855 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null); |
856 |
|
shouldThrow(); |
857 |
|
} catch (NullPointerException success) {} |
858 |
|
} |
859 |
|
|
600 |
– |
|
601 |
– |
|
860 |
|
/** |
861 |
|
* Constructor throws if corePoolSize argument is less than zero |
862 |
|
*/ |
863 |
|
public void testConstructor6() { |
864 |
|
try { |
865 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
865 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
866 |
> |
new ArrayBlockingQueue<Runnable>(10), |
867 |
> |
new SimpleThreadFactory()); |
868 |
|
shouldThrow(); |
869 |
|
} catch (IllegalArgumentException success) {} |
870 |
|
} |
874 |
|
*/ |
875 |
|
public void testConstructor7() { |
876 |
|
try { |
877 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
877 |
> |
new CustomTPE(1,-1, 1L, SECONDS, |
878 |
> |
new ArrayBlockingQueue<Runnable>(10), |
879 |
> |
new SimpleThreadFactory()); |
880 |
|
shouldThrow(); |
881 |
|
} catch (IllegalArgumentException success) {} |
882 |
|
} |
886 |
|
*/ |
887 |
|
public void testConstructor8() { |
888 |
|
try { |
889 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
889 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
890 |
> |
new ArrayBlockingQueue<Runnable>(10), |
891 |
> |
new SimpleThreadFactory()); |
892 |
|
shouldThrow(); |
893 |
|
} catch (IllegalArgumentException success) {} |
894 |
|
} |
898 |
|
*/ |
899 |
|
public void testConstructor9() { |
900 |
|
try { |
901 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
901 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
902 |
> |
new ArrayBlockingQueue<Runnable>(10), |
903 |
> |
new SimpleThreadFactory()); |
904 |
|
shouldThrow(); |
905 |
|
} catch (IllegalArgumentException success) {} |
906 |
|
} |
910 |
|
*/ |
911 |
|
public void testConstructor10() { |
912 |
|
try { |
913 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
913 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
914 |
> |
new ArrayBlockingQueue<Runnable>(10), |
915 |
> |
new SimpleThreadFactory()); |
916 |
|
shouldThrow(); |
917 |
|
} catch (IllegalArgumentException success) {} |
918 |
|
} |
922 |
|
*/ |
923 |
|
public void testConstructorNullPointerException2() { |
924 |
|
try { |
925 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
925 |
> |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
926 |
|
shouldThrow(); |
927 |
|
} catch (NullPointerException success) {} |
928 |
|
} |
932 |
|
*/ |
933 |
|
public void testConstructorNullPointerException3() { |
934 |
|
try { |
935 |
< |
ThreadFactory f = null; |
936 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
935 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
936 |
> |
new ArrayBlockingQueue<Runnable>(10), |
937 |
> |
(ThreadFactory) null); |
938 |
|
shouldThrow(); |
939 |
|
} catch (NullPointerException success) {} |
940 |
|
} |
941 |
|
|
673 |
– |
|
942 |
|
/** |
943 |
|
* Constructor throws if corePoolSize argument is less than zero |
944 |
|
*/ |
945 |
|
public void testConstructor11() { |
946 |
|
try { |
947 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
947 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
948 |
> |
new ArrayBlockingQueue<Runnable>(10), |
949 |
> |
new NoOpREHandler()); |
950 |
|
shouldThrow(); |
951 |
|
} catch (IllegalArgumentException success) {} |
952 |
|
} |
956 |
|
*/ |
957 |
|
public void testConstructor12() { |
958 |
|
try { |
959 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
959 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
960 |
> |
new ArrayBlockingQueue<Runnable>(10), |
961 |
> |
new NoOpREHandler()); |
962 |
|
shouldThrow(); |
963 |
|
} catch (IllegalArgumentException success) {} |
964 |
|
} |
968 |
|
*/ |
969 |
|
public void testConstructor13() { |
970 |
|
try { |
971 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
971 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
972 |
> |
new ArrayBlockingQueue<Runnable>(10), |
973 |
> |
new NoOpREHandler()); |
974 |
|
shouldThrow(); |
975 |
|
} catch (IllegalArgumentException success) {} |
976 |
|
} |
980 |
|
*/ |
981 |
|
public void testConstructor14() { |
982 |
|
try { |
983 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
983 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
984 |
> |
new ArrayBlockingQueue<Runnable>(10), |
985 |
> |
new NoOpREHandler()); |
986 |
|
shouldThrow(); |
987 |
|
} catch (IllegalArgumentException success) {} |
988 |
|
} |
992 |
|
*/ |
993 |
|
public void testConstructor15() { |
994 |
|
try { |
995 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
995 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
996 |
> |
new ArrayBlockingQueue<Runnable>(10), |
997 |
> |
new NoOpREHandler()); |
998 |
|
shouldThrow(); |
999 |
|
} catch (IllegalArgumentException success) {} |
1000 |
|
} |
1004 |
|
*/ |
1005 |
|
public void testConstructorNullPointerException4() { |
1006 |
|
try { |
1007 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
1007 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1008 |
> |
null, |
1009 |
> |
new NoOpREHandler()); |
1010 |
|
shouldThrow(); |
1011 |
|
} catch (NullPointerException success) {} |
1012 |
|
} |
1016 |
|
*/ |
1017 |
|
public void testConstructorNullPointerException5() { |
1018 |
|
try { |
1019 |
< |
RejectedExecutionHandler r = null; |
1020 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
1019 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1020 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1021 |
> |
(RejectedExecutionHandler) null); |
1022 |
|
shouldThrow(); |
1023 |
|
} catch (NullPointerException success) {} |
1024 |
|
} |
1025 |
|
|
745 |
– |
|
1026 |
|
/** |
1027 |
|
* Constructor throws if corePoolSize argument is less than zero |
1028 |
|
*/ |
1029 |
|
public void testConstructor16() { |
1030 |
|
try { |
1031 |
< |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1031 |
> |
new CustomTPE(-1, 1, 1L, SECONDS, |
1032 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1033 |
> |
new SimpleThreadFactory(), |
1034 |
> |
new NoOpREHandler()); |
1035 |
|
shouldThrow(); |
1036 |
|
} catch (IllegalArgumentException success) {} |
1037 |
|
} |
1041 |
|
*/ |
1042 |
|
public void testConstructor17() { |
1043 |
|
try { |
1044 |
< |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1044 |
> |
new CustomTPE(1, -1, 1L, SECONDS, |
1045 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1046 |
> |
new SimpleThreadFactory(), |
1047 |
> |
new NoOpREHandler()); |
1048 |
|
shouldThrow(); |
1049 |
|
} catch (IllegalArgumentException success) {} |
1050 |
|
} |
1054 |
|
*/ |
1055 |
|
public void testConstructor18() { |
1056 |
|
try { |
1057 |
< |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1057 |
> |
new CustomTPE(1, 0, 1L, SECONDS, |
1058 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1059 |
> |
new SimpleThreadFactory(), |
1060 |
> |
new NoOpREHandler()); |
1061 |
|
shouldThrow(); |
1062 |
|
} catch (IllegalArgumentException success) {} |
1063 |
|
} |
1067 |
|
*/ |
1068 |
|
public void testConstructor19() { |
1069 |
|
try { |
1070 |
< |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1070 |
> |
new CustomTPE(1, 2, -1L, SECONDS, |
1071 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1072 |
> |
new SimpleThreadFactory(), |
1073 |
> |
new NoOpREHandler()); |
1074 |
|
shouldThrow(); |
1075 |
|
} catch (IllegalArgumentException success) {} |
1076 |
|
} |
1080 |
|
*/ |
1081 |
|
public void testConstructor20() { |
1082 |
|
try { |
1083 |
< |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
1083 |
> |
new CustomTPE(2, 1, 1L, SECONDS, |
1084 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1085 |
> |
new SimpleThreadFactory(), |
1086 |
> |
new NoOpREHandler()); |
1087 |
|
shouldThrow(); |
1088 |
|
} catch (IllegalArgumentException success) {} |
1089 |
|
} |
1093 |
|
*/ |
1094 |
|
public void testConstructorNullPointerException6() { |
1095 |
|
try { |
1096 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
1096 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1097 |
> |
null, |
1098 |
> |
new SimpleThreadFactory(), |
1099 |
> |
new NoOpREHandler()); |
1100 |
|
shouldThrow(); |
1101 |
|
} catch (NullPointerException success) {} |
1102 |
|
} |
1106 |
|
*/ |
1107 |
|
public void testConstructorNullPointerException7() { |
1108 |
|
try { |
1109 |
< |
RejectedExecutionHandler r = null; |
1110 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
1109 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1110 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1111 |
> |
new SimpleThreadFactory(), |
1112 |
> |
(RejectedExecutionHandler) null); |
1113 |
|
shouldThrow(); |
1114 |
|
} catch (NullPointerException success) {} |
1115 |
|
} |
1119 |
|
*/ |
1120 |
|
public void testConstructorNullPointerException8() { |
1121 |
|
try { |
1122 |
< |
ThreadFactory f = null; |
1123 |
< |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
1122 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1123 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1124 |
> |
(ThreadFactory) null, |
1125 |
> |
new NoOpREHandler()); |
1126 |
|
shouldThrow(); |
1127 |
|
} catch (NullPointerException success) {} |
1128 |
|
} |
1129 |
|
|
828 |
– |
|
1130 |
|
/** |
1131 |
|
* execute throws RejectedExecutionException if saturated. |
1132 |
|
*/ |
1133 |
|
public void testSaturatedExecute() { |
1134 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
1135 |
< |
try { |
1136 |
< |
|
1137 |
< |
for (int i = 0; i < 5; ++i) { |
1138 |
< |
p.execute(new MediumRunnable()); |
1134 |
> |
ThreadPoolExecutor p = |
1135 |
> |
new CustomTPE(1, 1, |
1136 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1137 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1138 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1139 |
> |
try { |
1140 |
> |
Runnable task = new CheckedRunnable() { |
1141 |
> |
public void realRun() throws InterruptedException { |
1142 |
> |
done.await(); |
1143 |
> |
}}; |
1144 |
> |
for (int i = 0; i < 2; ++i) |
1145 |
> |
p.execute(task); |
1146 |
> |
for (int i = 0; i < 2; ++i) { |
1147 |
> |
try { |
1148 |
> |
p.execute(task); |
1149 |
> |
shouldThrow(); |
1150 |
> |
} catch (RejectedExecutionException success) {} |
1151 |
> |
assertTrue(p.getTaskCount() <= 2); |
1152 |
|
} |
1153 |
< |
shouldThrow(); |
1154 |
< |
} catch (RejectedExecutionException success) {} |
1155 |
< |
joinPool(p); |
1153 |
> |
} finally { |
1154 |
> |
done.countDown(); |
1155 |
> |
joinPool(p); |
1156 |
> |
} |
1157 |
|
} |
1158 |
|
|
1159 |
|
/** |
1161 |
|
*/ |
1162 |
|
public void testSaturatedExecute2() { |
1163 |
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
1164 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1164 |
> |
ThreadPoolExecutor p = new CustomTPE(1, 1, |
1165 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1166 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1167 |
> |
h); |
1168 |
|
try { |
851 |
– |
|
1169 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1170 |
< |
for (int i = 0; i < 5; ++i) { |
1170 |
> |
for (int i = 0; i < tasks.length; ++i) |
1171 |
|
tasks[i] = new TrackedNoOpRunnable(); |
855 |
– |
} |
1172 |
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1173 |
|
p.execute(mr); |
1174 |
< |
for (int i = 0; i < 5; ++i) { |
1174 |
> |
for (int i = 0; i < tasks.length; ++i) |
1175 |
|
p.execute(tasks[i]); |
1176 |
< |
} |
861 |
< |
for (int i = 1; i < 5; ++i) { |
1176 |
> |
for (int i = 1; i < tasks.length; ++i) |
1177 |
|
assertTrue(tasks[i].done); |
863 |
– |
} |
1178 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1179 |
|
} finally { |
1180 |
|
joinPool(p); |
1186 |
|
*/ |
1187 |
|
public void testSaturatedExecute3() { |
1188 |
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
1189 |
< |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1189 |
> |
ThreadPoolExecutor p = |
1190 |
> |
new CustomTPE(1, 1, |
1191 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1192 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1193 |
> |
h); |
1194 |
|
try { |
877 |
– |
|
1195 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1196 |
< |
for (int i = 0; i < 5; ++i) { |
1196 |
> |
for (int i = 0; i < tasks.length; ++i) |
1197 |
|
tasks[i] = new TrackedNoOpRunnable(); |
881 |
– |
} |
1198 |
|
p.execute(new TrackedLongRunnable()); |
1199 |
< |
for (int i = 0; i < 5; ++i) { |
1200 |
< |
p.execute(tasks[i]); |
1201 |
< |
} |
1202 |
< |
for (int i = 0; i < 5; ++i) { |
887 |
< |
assertFalse(tasks[i].done); |
888 |
< |
} |
1199 |
> |
for (TrackedNoOpRunnable task : tasks) |
1200 |
> |
p.execute(task); |
1201 |
> |
for (TrackedNoOpRunnable task : tasks) |
1202 |
> |
assertFalse(task.done); |
1203 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1204 |
|
} finally { |
1205 |
|
joinPool(p); |
1231 |
|
* execute throws RejectedExecutionException if shutdown |
1232 |
|
*/ |
1233 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1234 |
< |
ThreadPoolExecutor tpe = |
1234 |
> |
ThreadPoolExecutor p = |
1235 |
|
new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1236 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1236 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1237 |
|
try { |
1238 |
< |
tpe.execute(new NoOpRunnable()); |
1238 |
> |
p.execute(new NoOpRunnable()); |
1239 |
|
shouldThrow(); |
1240 |
|
} catch (RejectedExecutionException success) {} |
1241 |
|
|
1242 |
< |
joinPool(tpe); |
1242 |
> |
joinPool(p); |
1243 |
|
} |
1244 |
|
|
1245 |
|
/** |
1276 |
|
} |
1277 |
|
} |
1278 |
|
|
965 |
– |
|
1279 |
|
/** |
1280 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1281 |
|
*/ |
1293 |
|
} |
1294 |
|
} |
1295 |
|
|
983 |
– |
|
1296 |
|
/** |
1297 |
|
* execute(null) throws NPE |
1298 |
|
*/ |
1299 |
|
public void testExecuteNull() { |
1300 |
< |
ThreadPoolExecutor tpe = null; |
1300 |
> |
ThreadPoolExecutor p = |
1301 |
> |
new CustomTPE(1, 2, 1L, SECONDS, |
1302 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1303 |
|
try { |
1304 |
< |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
991 |
< |
tpe.execute(null); |
1304 |
> |
p.execute(null); |
1305 |
|
shouldThrow(); |
1306 |
|
} catch (NullPointerException success) {} |
1307 |
|
|
1308 |
< |
joinPool(tpe); |
1308 |
> |
joinPool(p); |
1309 |
|
} |
1310 |
|
|
1311 |
|
/** |
1312 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1313 |
|
*/ |
1314 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1315 |
< |
ThreadPoolExecutor tpe = |
1315 |
> |
ThreadPoolExecutor p = |
1316 |
|
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1317 |
|
try { |
1318 |
< |
tpe.setCorePoolSize(-1); |
1318 |
> |
p.setCorePoolSize(-1); |
1319 |
|
shouldThrow(); |
1320 |
|
} catch (IllegalArgumentException success) { |
1321 |
|
} finally { |
1322 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1322 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1323 |
|
} |
1324 |
< |
joinPool(tpe); |
1324 |
> |
joinPool(p); |
1325 |
|
} |
1326 |
|
|
1327 |
|
/** |
1329 |
|
* if given a value less the core pool size |
1330 |
|
*/ |
1331 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1332 |
< |
ThreadPoolExecutor tpe = |
1332 |
> |
ThreadPoolExecutor p = |
1333 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1334 |
|
try { |
1335 |
< |
tpe.setMaximumPoolSize(1); |
1335 |
> |
p.setMaximumPoolSize(1); |
1336 |
|
shouldThrow(); |
1337 |
|
} catch (IllegalArgumentException success) { |
1338 |
|
} finally { |
1339 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1339 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1340 |
|
} |
1341 |
< |
joinPool(tpe); |
1341 |
> |
joinPool(p); |
1342 |
|
} |
1343 |
|
|
1344 |
|
/** |
1346 |
|
* if given a negative value |
1347 |
|
*/ |
1348 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1349 |
< |
ThreadPoolExecutor tpe = |
1349 |
> |
ThreadPoolExecutor p = |
1350 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1351 |
|
try { |
1352 |
< |
tpe.setMaximumPoolSize(-1); |
1352 |
> |
p.setMaximumPoolSize(-1); |
1353 |
|
shouldThrow(); |
1354 |
|
} catch (IllegalArgumentException success) { |
1355 |
|
} finally { |
1356 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1356 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1357 |
|
} |
1358 |
< |
joinPool(tpe); |
1358 |
> |
joinPool(p); |
1359 |
|
} |
1360 |
|
|
1048 |
– |
|
1361 |
|
/** |
1362 |
|
* setKeepAliveTime throws IllegalArgumentException |
1363 |
|
* when given a negative value |
1364 |
|
*/ |
1365 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1366 |
< |
ThreadPoolExecutor tpe = |
1366 |
> |
ThreadPoolExecutor p = |
1367 |
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1368 |
|
|
1369 |
|
try { |
1370 |
< |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
1370 |
> |
p.setKeepAliveTime(-1,MILLISECONDS); |
1371 |
|
shouldThrow(); |
1372 |
|
} catch (IllegalArgumentException success) { |
1373 |
|
} finally { |
1374 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1374 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1375 |
|
} |
1376 |
< |
joinPool(tpe); |
1376 |
> |
joinPool(p); |
1377 |
|
} |
1378 |
|
|
1379 |
|
/** |
1380 |
|
* terminated() is called on termination |
1381 |
|
*/ |
1382 |
|
public void testTerminated() { |
1383 |
< |
CustomTPE tpe = new CustomTPE(); |
1384 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1385 |
< |
assertTrue(tpe.terminatedCalled); |
1386 |
< |
joinPool(tpe); |
1383 |
> |
CustomTPE p = new CustomTPE(); |
1384 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1385 |
> |
assertTrue(p.terminatedCalled()); |
1386 |
> |
joinPool(p); |
1387 |
|
} |
1388 |
|
|
1389 |
|
/** |
1390 |
|
* beforeExecute and afterExecute are called when executing task |
1391 |
|
*/ |
1392 |
|
public void testBeforeAfter() throws InterruptedException { |
1393 |
< |
CustomTPE tpe = new CustomTPE(); |
1393 |
> |
CustomTPE p = new CustomTPE(); |
1394 |
|
try { |
1395 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1396 |
< |
tpe.execute(r); |
1397 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1398 |
< |
assertTrue(r.done); |
1399 |
< |
assertTrue(tpe.beforeCalled); |
1400 |
< |
assertTrue(tpe.afterCalled); |
1401 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1395 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1396 |
> |
p.execute(new CheckedRunnable() { |
1397 |
> |
public void realRun() { |
1398 |
> |
done.countDown(); |
1399 |
> |
}}); |
1400 |
> |
await(p.afterCalled); |
1401 |
> |
assertEquals(0, done.getCount()); |
1402 |
> |
assertTrue(p.afterCalled()); |
1403 |
> |
assertTrue(p.beforeCalled()); |
1404 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1405 |
|
} finally { |
1406 |
< |
joinPool(tpe); |
1406 |
> |
joinPool(p); |
1407 |
|
} |
1408 |
|
} |
1409 |
|
|
1449 |
|
} |
1450 |
|
} |
1451 |
|
|
1137 |
– |
|
1452 |
|
/** |
1453 |
|
* invokeAny(null) throws NPE |
1454 |
|
*/ |
1610 |
|
} |
1611 |
|
} |
1612 |
|
|
1299 |
– |
|
1300 |
– |
|
1613 |
|
/** |
1614 |
|
* timed invokeAny(null) throws NPE |
1615 |
|
*/ |
1796 |
|
l.add(new StringTask()); |
1797 |
|
l.add(new StringTask()); |
1798 |
|
List<Future<String>> futures = |
1799 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1799 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1800 |
|
assertEquals(2, futures.size()); |
1801 |
|
for (Future<String> future : futures) |
1802 |
|
assertSame(TEST_STRING, future.get()); |
1811 |
|
public void testTimedInvokeAll6() throws Exception { |
1812 |
|
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1813 |
|
try { |
1814 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1815 |
< |
l.add(new StringTask()); |
1816 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1817 |
< |
l.add(new StringTask()); |
1818 |
< |
List<Future<String>> futures = |
1819 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1820 |
< |
assertEquals(3, futures.size()); |
1821 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1822 |
< |
Future<String> f1 = it.next(); |
1823 |
< |
Future<String> f2 = it.next(); |
1824 |
< |
Future<String> f3 = it.next(); |
1825 |
< |
assertTrue(f1.isDone()); |
1826 |
< |
assertTrue(f2.isDone()); |
1827 |
< |
assertTrue(f3.isDone()); |
1828 |
< |
assertFalse(f1.isCancelled()); |
1829 |
< |
assertTrue(f2.isCancelled()); |
1814 |
> |
for (long timeout = timeoutMillis();;) { |
1815 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1816 |
> |
tasks.add(new StringTask("0")); |
1817 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1818 |
> |
tasks.add(new StringTask("2")); |
1819 |
> |
long startTime = System.nanoTime(); |
1820 |
> |
List<Future<String>> futures = |
1821 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1822 |
> |
assertEquals(tasks.size(), futures.size()); |
1823 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1824 |
> |
for (Future future : futures) |
1825 |
> |
assertTrue(future.isDone()); |
1826 |
> |
assertTrue(futures.get(1).isCancelled()); |
1827 |
> |
try { |
1828 |
> |
assertEquals("0", futures.get(0).get()); |
1829 |
> |
assertEquals("2", futures.get(2).get()); |
1830 |
> |
break; |
1831 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1832 |
> |
timeout *= 2; |
1833 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1834 |
> |
fail("expected exactly one task to be cancelled"); |
1835 |
> |
} |
1836 |
> |
} |
1837 |
|
} finally { |
1838 |
|
joinPool(e); |
1839 |
|
} |
1844 |
|
* thread factory fails to create more |
1845 |
|
*/ |
1846 |
|
public void testFailingThreadFactory() throws InterruptedException { |
1847 |
< |
ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1848 |
< |
try { |
1849 |
< |
for (int k = 0; k < 100; ++k) { |
1850 |
< |
e.execute(new NoOpRunnable()); |
1851 |
< |
} |
1852 |
< |
Thread.sleep(LONG_DELAY_MS); |
1847 |
> |
final ExecutorService e = |
1848 |
> |
new CustomTPE(100, 100, |
1849 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1850 |
> |
new LinkedBlockingQueue<Runnable>(), |
1851 |
> |
new FailingThreadFactory()); |
1852 |
> |
try { |
1853 |
> |
final int TASKS = 100; |
1854 |
> |
final CountDownLatch done = new CountDownLatch(TASKS); |
1855 |
> |
for (int k = 0; k < TASKS; ++k) |
1856 |
> |
e.execute(new CheckedRunnable() { |
1857 |
> |
public void realRun() { |
1858 |
> |
done.countDown(); |
1859 |
> |
}}); |
1860 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1861 |
|
} finally { |
1862 |
|
joinPool(e); |
1863 |
|
} |
1867 |
|
* allowsCoreThreadTimeOut is by default false. |
1868 |
|
*/ |
1869 |
|
public void testAllowsCoreThreadTimeOut() { |
1870 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1871 |
< |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1872 |
< |
joinPool(tpe); |
1870 |
> |
ThreadPoolExecutor p = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1871 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1872 |
> |
joinPool(p); |
1873 |
|
} |
1874 |
|
|
1875 |
|
/** |
1876 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1877 |
|
*/ |
1878 |
< |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1879 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1880 |
< |
tpe.allowCoreThreadTimeOut(true); |
1881 |
< |
tpe.execute(new NoOpRunnable()); |
1882 |
< |
try { |
1883 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1884 |
< |
assertEquals(0, tpe.getPoolSize()); |
1878 |
> |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1879 |
> |
long keepAliveTime = timeoutMillis(); |
1880 |
> |
final ThreadPoolExecutor p = |
1881 |
> |
new CustomTPE(2, 10, |
1882 |
> |
keepAliveTime, MILLISECONDS, |
1883 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1884 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1885 |
> |
try { |
1886 |
> |
p.allowCoreThreadTimeOut(true); |
1887 |
> |
p.execute(new CheckedRunnable() { |
1888 |
> |
public void realRun() { |
1889 |
> |
threadStarted.countDown(); |
1890 |
> |
assertEquals(1, p.getPoolSize()); |
1891 |
> |
}}); |
1892 |
> |
await(threadStarted); |
1893 |
> |
delay(keepAliveTime); |
1894 |
> |
long startTime = System.nanoTime(); |
1895 |
> |
while (p.getPoolSize() > 0 |
1896 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1897 |
> |
Thread.yield(); |
1898 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1899 |
> |
assertEquals(0, p.getPoolSize()); |
1900 |
|
} finally { |
1901 |
< |
joinPool(tpe); |
1901 |
> |
joinPool(p); |
1902 |
|
} |
1903 |
|
} |
1904 |
|
|
1905 |
|
/** |
1906 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1907 |
|
*/ |
1908 |
< |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1909 |
< |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1910 |
< |
tpe.allowCoreThreadTimeOut(false); |
1911 |
< |
tpe.execute(new NoOpRunnable()); |
1912 |
< |
try { |
1913 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1914 |
< |
assertTrue(tpe.getPoolSize() >= 1); |
1908 |
> |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1909 |
> |
long keepAliveTime = timeoutMillis(); |
1910 |
> |
final ThreadPoolExecutor p = |
1911 |
> |
new CustomTPE(2, 10, |
1912 |
> |
keepAliveTime, MILLISECONDS, |
1913 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1914 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1915 |
> |
try { |
1916 |
> |
p.allowCoreThreadTimeOut(false); |
1917 |
> |
p.execute(new CheckedRunnable() { |
1918 |
> |
public void realRun() throws InterruptedException { |
1919 |
> |
threadStarted.countDown(); |
1920 |
> |
assertTrue(p.getPoolSize() >= 1); |
1921 |
> |
}}); |
1922 |
> |
delay(2 * keepAliveTime); |
1923 |
> |
assertTrue(p.getPoolSize() >= 1); |
1924 |
|
} finally { |
1925 |
< |
joinPool(tpe); |
1925 |
> |
joinPool(p); |
1926 |
> |
} |
1927 |
> |
} |
1928 |
> |
|
1929 |
> |
/** |
1930 |
> |
* get(cancelled task) throws CancellationException |
1931 |
> |
* (in part, a test of CustomTPE itself) |
1932 |
> |
*/ |
1933 |
> |
public void testGet_cancelled() throws Exception { |
1934 |
> |
final ExecutorService e = |
1935 |
> |
new CustomTPE(1, 1, |
1936 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1937 |
> |
new LinkedBlockingQueue<Runnable>()); |
1938 |
> |
try { |
1939 |
> |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
1940 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1941 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
1942 |
> |
for (int i = 0; i < 2; i++) { |
1943 |
> |
Runnable r = new CheckedRunnable() { public void realRun() |
1944 |
> |
throws Throwable { |
1945 |
> |
blockerStarted.countDown(); |
1946 |
> |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
1947 |
> |
}}; |
1948 |
> |
futures.add(e.submit(r)); |
1949 |
> |
} |
1950 |
> |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
1951 |
> |
for (Future<?> future : futures) future.cancel(false); |
1952 |
> |
for (Future<?> future : futures) { |
1953 |
> |
try { |
1954 |
> |
future.get(); |
1955 |
> |
shouldThrow(); |
1956 |
> |
} catch (CancellationException success) {} |
1957 |
> |
try { |
1958 |
> |
future.get(LONG_DELAY_MS, MILLISECONDS); |
1959 |
> |
shouldThrow(); |
1960 |
> |
} catch (CancellationException success) {} |
1961 |
> |
assertTrue(future.isCancelled()); |
1962 |
> |
assertTrue(future.isDone()); |
1963 |
> |
} |
1964 |
> |
done.countDown(); |
1965 |
> |
} finally { |
1966 |
> |
joinPool(e); |
1967 |
|
} |
1968 |
|
} |
1969 |
|
|