14 |
|
import java.util.List; |
15 |
|
import java.util.concurrent.ArrayBlockingQueue; |
16 |
|
import java.util.concurrent.BlockingQueue; |
17 |
– |
import java.util.concurrent.CancellationException; |
17 |
|
import java.util.concurrent.Callable; |
18 |
+ |
import java.util.concurrent.CancellationException; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.ExecutionException; |
21 |
|
import java.util.concurrent.Executors; |
29 |
|
import java.util.concurrent.ThreadFactory; |
30 |
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
|
import java.util.concurrent.TimeUnit; |
32 |
+ |
import java.util.concurrent.atomic.AtomicInteger; |
33 |
|
|
34 |
|
import junit.framework.Test; |
35 |
|
import junit.framework.TestSuite; |
87 |
|
new ThreadPoolExecutor(1, 1, |
88 |
|
LONG_DELAY_MS, MILLISECONDS, |
89 |
|
new ArrayBlockingQueue<Runnable>(10)); |
90 |
< |
final CountDownLatch done = new CountDownLatch(1); |
91 |
< |
final Runnable task = new CheckedRunnable() { |
92 |
< |
public void realRun() { |
93 |
< |
done.countDown(); |
93 |
< |
}}; |
94 |
< |
try { |
90 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
91 |
> |
final CountDownLatch done = new CountDownLatch(1); |
92 |
> |
final Runnable task = new CheckedRunnable() { |
93 |
> |
public void realRun() { done.countDown(); }}; |
94 |
|
p.execute(task); |
95 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
97 |
< |
} finally { |
98 |
< |
joinPool(p); |
95 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
96 |
|
} |
97 |
|
} |
98 |
|
|
101 |
|
* thread becomes active |
102 |
|
*/ |
103 |
|
public void testGetActiveCount() throws InterruptedException { |
104 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
105 |
|
final ThreadPoolExecutor p = |
106 |
|
new ThreadPoolExecutor(2, 2, |
107 |
|
LONG_DELAY_MS, MILLISECONDS, |
108 |
|
new ArrayBlockingQueue<Runnable>(10)); |
109 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
110 |
< |
final CountDownLatch done = new CountDownLatch(1); |
113 |
< |
try { |
109 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
110 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
111 |
|
assertEquals(0, p.getActiveCount()); |
112 |
|
p.execute(new CheckedRunnable() { |
113 |
|
public void realRun() throws InterruptedException { |
114 |
|
threadStarted.countDown(); |
115 |
|
assertEquals(1, p.getActiveCount()); |
116 |
< |
done.await(); |
116 |
> |
await(done); |
117 |
|
}}); |
118 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
118 |
> |
await(threadStarted); |
119 |
|
assertEquals(1, p.getActiveCount()); |
123 |
– |
} finally { |
124 |
– |
done.countDown(); |
125 |
– |
joinPool(p); |
120 |
|
} |
121 |
|
} |
122 |
|
|
125 |
|
*/ |
126 |
|
public void testPrestartCoreThread() { |
127 |
|
final ThreadPoolExecutor p = |
128 |
< |
new ThreadPoolExecutor(2, 2, |
128 |
> |
new ThreadPoolExecutor(2, 6, |
129 |
|
LONG_DELAY_MS, MILLISECONDS, |
130 |
|
new ArrayBlockingQueue<Runnable>(10)); |
131 |
< |
assertEquals(0, p.getPoolSize()); |
132 |
< |
assertTrue(p.prestartCoreThread()); |
133 |
< |
assertEquals(1, p.getPoolSize()); |
134 |
< |
assertTrue(p.prestartCoreThread()); |
135 |
< |
assertEquals(2, p.getPoolSize()); |
136 |
< |
assertFalse(p.prestartCoreThread()); |
137 |
< |
assertEquals(2, p.getPoolSize()); |
138 |
< |
joinPool(p); |
131 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
132 |
> |
assertEquals(0, p.getPoolSize()); |
133 |
> |
assertTrue(p.prestartCoreThread()); |
134 |
> |
assertEquals(1, p.getPoolSize()); |
135 |
> |
assertTrue(p.prestartCoreThread()); |
136 |
> |
assertEquals(2, p.getPoolSize()); |
137 |
> |
assertFalse(p.prestartCoreThread()); |
138 |
> |
assertEquals(2, p.getPoolSize()); |
139 |
> |
p.setCorePoolSize(4); |
140 |
> |
assertTrue(p.prestartCoreThread()); |
141 |
> |
assertEquals(3, p.getPoolSize()); |
142 |
> |
assertTrue(p.prestartCoreThread()); |
143 |
> |
assertEquals(4, p.getPoolSize()); |
144 |
> |
assertFalse(p.prestartCoreThread()); |
145 |
> |
assertEquals(4, p.getPoolSize()); |
146 |
> |
} |
147 |
|
} |
148 |
|
|
149 |
|
/** |
151 |
|
*/ |
152 |
|
public void testPrestartAllCoreThreads() { |
153 |
|
final ThreadPoolExecutor p = |
154 |
< |
new ThreadPoolExecutor(2, 2, |
154 |
> |
new ThreadPoolExecutor(2, 6, |
155 |
|
LONG_DELAY_MS, MILLISECONDS, |
156 |
|
new ArrayBlockingQueue<Runnable>(10)); |
157 |
< |
assertEquals(0, p.getPoolSize()); |
158 |
< |
p.prestartAllCoreThreads(); |
159 |
< |
assertEquals(2, p.getPoolSize()); |
160 |
< |
p.prestartAllCoreThreads(); |
161 |
< |
assertEquals(2, p.getPoolSize()); |
162 |
< |
joinPool(p); |
157 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
158 |
> |
assertEquals(0, p.getPoolSize()); |
159 |
> |
p.prestartAllCoreThreads(); |
160 |
> |
assertEquals(2, p.getPoolSize()); |
161 |
> |
p.prestartAllCoreThreads(); |
162 |
> |
assertEquals(2, p.getPoolSize()); |
163 |
> |
p.setCorePoolSize(4); |
164 |
> |
p.prestartAllCoreThreads(); |
165 |
> |
assertEquals(4, p.getPoolSize()); |
166 |
> |
p.prestartAllCoreThreads(); |
167 |
> |
assertEquals(4, p.getPoolSize()); |
168 |
> |
} |
169 |
|
} |
170 |
|
|
171 |
|
/** |
177 |
|
new ThreadPoolExecutor(2, 2, |
178 |
|
LONG_DELAY_MS, MILLISECONDS, |
179 |
|
new ArrayBlockingQueue<Runnable>(10)); |
180 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
181 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
182 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
183 |
< |
try { |
180 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
181 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
182 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
183 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
184 |
|
assertEquals(0, p.getCompletedTaskCount()); |
185 |
|
p.execute(new CheckedRunnable() { |
186 |
|
public void realRun() throws InterruptedException { |
199 |
|
fail("timed out"); |
200 |
|
Thread.yield(); |
201 |
|
} |
194 |
– |
} finally { |
195 |
– |
joinPool(p); |
202 |
|
} |
203 |
|
} |
204 |
|
|
210 |
|
new ThreadPoolExecutor(1, 1, |
211 |
|
LONG_DELAY_MS, MILLISECONDS, |
212 |
|
new ArrayBlockingQueue<Runnable>(10)); |
213 |
< |
assertEquals(1, p.getCorePoolSize()); |
214 |
< |
joinPool(p); |
213 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
214 |
> |
assertEquals(1, p.getCorePoolSize()); |
215 |
> |
} |
216 |
|
} |
217 |
|
|
218 |
|
/** |
223 |
|
new ThreadPoolExecutor(2, 2, |
224 |
|
1000, MILLISECONDS, |
225 |
|
new ArrayBlockingQueue<Runnable>(10)); |
226 |
< |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
227 |
< |
joinPool(p); |
226 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
227 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
228 |
> |
} |
229 |
|
} |
230 |
|
|
231 |
|
/** |
232 |
|
* getThreadFactory returns factory in constructor if not set |
233 |
|
*/ |
234 |
|
public void testGetThreadFactory() { |
235 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
235 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
236 |
|
final ThreadPoolExecutor p = |
237 |
|
new ThreadPoolExecutor(1, 2, |
238 |
|
LONG_DELAY_MS, MILLISECONDS, |
239 |
|
new ArrayBlockingQueue<Runnable>(10), |
240 |
< |
tf, |
240 |
> |
threadFactory, |
241 |
|
new NoOpREHandler()); |
242 |
< |
assertSame(tf, p.getThreadFactory()); |
243 |
< |
joinPool(p); |
242 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
243 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
244 |
> |
} |
245 |
|
} |
246 |
|
|
247 |
|
/** |
252 |
|
new ThreadPoolExecutor(1, 2, |
253 |
|
LONG_DELAY_MS, MILLISECONDS, |
254 |
|
new ArrayBlockingQueue<Runnable>(10)); |
255 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
256 |
< |
p.setThreadFactory(tf); |
257 |
< |
assertSame(tf, p.getThreadFactory()); |
258 |
< |
joinPool(p); |
255 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
256 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
257 |
> |
p.setThreadFactory(threadFactory); |
258 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
259 |
> |
} |
260 |
|
} |
261 |
|
|
262 |
|
/** |
267 |
|
new ThreadPoolExecutor(1, 2, |
268 |
|
LONG_DELAY_MS, MILLISECONDS, |
269 |
|
new ArrayBlockingQueue<Runnable>(10)); |
270 |
< |
try { |
271 |
< |
p.setThreadFactory(null); |
272 |
< |
shouldThrow(); |
273 |
< |
} catch (NullPointerException success) { |
274 |
< |
} finally { |
265 |
< |
joinPool(p); |
270 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
271 |
> |
try { |
272 |
> |
p.setThreadFactory(null); |
273 |
> |
shouldThrow(); |
274 |
> |
} catch (NullPointerException success) {} |
275 |
|
} |
276 |
|
} |
277 |
|
|
279 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
280 |
|
*/ |
281 |
|
public void testGetRejectedExecutionHandler() { |
282 |
< |
final RejectedExecutionHandler h = new NoOpREHandler(); |
282 |
> |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
283 |
|
final ThreadPoolExecutor p = |
284 |
|
new ThreadPoolExecutor(1, 2, |
285 |
|
LONG_DELAY_MS, MILLISECONDS, |
286 |
|
new ArrayBlockingQueue<Runnable>(10), |
287 |
< |
h); |
288 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
289 |
< |
joinPool(p); |
287 |
> |
handler); |
288 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
289 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
290 |
> |
} |
291 |
|
} |
292 |
|
|
293 |
|
/** |
299 |
|
new ThreadPoolExecutor(1, 2, |
300 |
|
LONG_DELAY_MS, MILLISECONDS, |
301 |
|
new ArrayBlockingQueue<Runnable>(10)); |
302 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
303 |
< |
p.setRejectedExecutionHandler(h); |
304 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
305 |
< |
joinPool(p); |
302 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
303 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
304 |
> |
p.setRejectedExecutionHandler(handler); |
305 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
306 |
> |
} |
307 |
|
} |
308 |
|
|
309 |
|
/** |
314 |
|
new ThreadPoolExecutor(1, 2, |
315 |
|
LONG_DELAY_MS, MILLISECONDS, |
316 |
|
new ArrayBlockingQueue<Runnable>(10)); |
317 |
< |
try { |
318 |
< |
p.setRejectedExecutionHandler(null); |
319 |
< |
shouldThrow(); |
320 |
< |
} catch (NullPointerException success) { |
321 |
< |
} finally { |
311 |
< |
joinPool(p); |
317 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
318 |
> |
try { |
319 |
> |
p.setRejectedExecutionHandler(null); |
320 |
> |
shouldThrow(); |
321 |
> |
} catch (NullPointerException success) {} |
322 |
|
} |
323 |
|
} |
324 |
|
|
328 |
|
*/ |
329 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
330 |
|
final int THREADS = 3; |
331 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
332 |
|
final ThreadPoolExecutor p = |
333 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
334 |
|
LONG_DELAY_MS, MILLISECONDS, |
335 |
|
new ArrayBlockingQueue<Runnable>(10)); |
336 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
326 |
< |
final CountDownLatch done = new CountDownLatch(1); |
327 |
< |
try { |
336 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
337 |
|
assertEquals(0, p.getLargestPoolSize()); |
338 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
339 |
|
for (int i = 0; i < THREADS; i++) |
340 |
|
p.execute(new CheckedRunnable() { |
341 |
|
public void realRun() throws InterruptedException { |
342 |
|
threadsStarted.countDown(); |
343 |
< |
done.await(); |
343 |
> |
await(done); |
344 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
345 |
|
}}); |
346 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
337 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
338 |
< |
} finally { |
339 |
< |
done.countDown(); |
340 |
< |
joinPool(p); |
346 |
> |
await(threadsStarted); |
347 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
348 |
|
} |
349 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
350 |
|
} |
351 |
|
|
352 |
|
/** |
358 |
|
new ThreadPoolExecutor(2, 3, |
359 |
|
LONG_DELAY_MS, MILLISECONDS, |
360 |
|
new ArrayBlockingQueue<Runnable>(10)); |
361 |
< |
assertEquals(3, p.getMaximumPoolSize()); |
362 |
< |
joinPool(p); |
361 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
362 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
363 |
> |
p.setMaximumPoolSize(5); |
364 |
> |
assertEquals(5, p.getMaximumPoolSize()); |
365 |
> |
p.setMaximumPoolSize(4); |
366 |
> |
assertEquals(4, p.getMaximumPoolSize()); |
367 |
> |
} |
368 |
|
} |
369 |
|
|
370 |
|
/** |
372 |
|
* become active |
373 |
|
*/ |
374 |
|
public void testGetPoolSize() throws InterruptedException { |
375 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
376 |
|
final ThreadPoolExecutor p = |
377 |
|
new ThreadPoolExecutor(1, 1, |
378 |
|
LONG_DELAY_MS, MILLISECONDS, |
379 |
|
new ArrayBlockingQueue<Runnable>(10)); |
380 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
368 |
< |
final CountDownLatch done = new CountDownLatch(1); |
369 |
< |
try { |
380 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
381 |
|
assertEquals(0, p.getPoolSize()); |
382 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
383 |
|
p.execute(new CheckedRunnable() { |
384 |
|
public void realRun() throws InterruptedException { |
385 |
|
threadStarted.countDown(); |
386 |
|
assertEquals(1, p.getPoolSize()); |
387 |
< |
done.await(); |
387 |
> |
await(done); |
388 |
|
}}); |
389 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
389 |
> |
await(threadStarted); |
390 |
|
assertEquals(1, p.getPoolSize()); |
379 |
– |
} finally { |
380 |
– |
done.countDown(); |
381 |
– |
joinPool(p); |
391 |
|
} |
392 |
|
} |
393 |
|
|
395 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
396 |
|
*/ |
397 |
|
public void testGetTaskCount() throws InterruptedException { |
398 |
+ |
final int TASKS = 3; |
399 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
400 |
|
final ThreadPoolExecutor p = |
401 |
|
new ThreadPoolExecutor(1, 1, |
402 |
|
LONG_DELAY_MS, MILLISECONDS, |
403 |
|
new ArrayBlockingQueue<Runnable>(10)); |
404 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
405 |
< |
final CountDownLatch done = new CountDownLatch(1); |
395 |
< |
try { |
404 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
405 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
406 |
|
assertEquals(0, p.getTaskCount()); |
407 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
408 |
|
p.execute(new CheckedRunnable() { |
409 |
|
public void realRun() throws InterruptedException { |
410 |
|
threadStarted.countDown(); |
411 |
< |
assertEquals(1, p.getTaskCount()); |
401 |
< |
done.await(); |
411 |
> |
await(done); |
412 |
|
}}); |
413 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
413 |
> |
await(threadStarted); |
414 |
|
assertEquals(1, p.getTaskCount()); |
415 |
< |
} finally { |
416 |
< |
done.countDown(); |
417 |
< |
joinPool(p); |
415 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
416 |
> |
for (int i = 0; i < TASKS; i++) { |
417 |
> |
assertEquals(1 + i, p.getTaskCount()); |
418 |
> |
p.execute(new CheckedRunnable() { |
419 |
> |
public void realRun() throws InterruptedException { |
420 |
> |
threadStarted.countDown(); |
421 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
422 |
> |
await(done); |
423 |
> |
}}); |
424 |
> |
} |
425 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
426 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
427 |
|
} |
428 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
429 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
430 |
|
} |
431 |
|
|
432 |
|
/** |
437 |
|
new ThreadPoolExecutor(1, 1, |
438 |
|
LONG_DELAY_MS, MILLISECONDS, |
439 |
|
new ArrayBlockingQueue<Runnable>(10)); |
440 |
< |
assertFalse(p.isShutdown()); |
441 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
442 |
< |
assertTrue(p.isShutdown()); |
443 |
< |
joinPool(p); |
440 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
441 |
> |
assertFalse(p.isShutdown()); |
442 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
443 |
> |
assertTrue(p.isShutdown()); |
444 |
> |
} |
445 |
|
} |
446 |
|
|
447 |
|
/** |
452 |
|
new ThreadPoolExecutor(1, 1, |
453 |
|
LONG_DELAY_MS, MILLISECONDS, |
454 |
|
new ArrayBlockingQueue<Runnable>(10)); |
455 |
< |
assertFalse(p.isTerminated()); |
456 |
< |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
457 |
< |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
458 |
< |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
459 |
< |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
460 |
< |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
461 |
< |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
462 |
< |
long timeoutNanos = 999999L; |
463 |
< |
long startTime = System.nanoTime(); |
464 |
< |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
465 |
< |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
466 |
< |
assertFalse(p.isTerminated()); |
467 |
< |
startTime = System.nanoTime(); |
468 |
< |
long timeoutMillis = timeoutMillis(); |
469 |
< |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
470 |
< |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
471 |
< |
assertFalse(p.isTerminated()); |
472 |
< |
p.shutdown(); |
473 |
< |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
474 |
< |
assertTrue(p.isTerminated()); |
455 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
456 |
> |
assertFalse(p.isTerminated()); |
457 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
458 |
> |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
459 |
> |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
460 |
> |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
461 |
> |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
462 |
> |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
463 |
> |
long timeoutNanos = 999999L; |
464 |
> |
long startTime = System.nanoTime(); |
465 |
> |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
466 |
> |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
467 |
> |
assertFalse(p.isTerminated()); |
468 |
> |
startTime = System.nanoTime(); |
469 |
> |
long timeoutMillis = timeoutMillis(); |
470 |
> |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
471 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
472 |
> |
assertFalse(p.isTerminated()); |
473 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
474 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
475 |
> |
assertTrue(p.isTerminated()); |
476 |
> |
} |
477 |
|
} |
478 |
|
|
479 |
|
/** |
484 |
|
new ThreadPoolExecutor(1, 1, |
485 |
|
LONG_DELAY_MS, MILLISECONDS, |
486 |
|
new ArrayBlockingQueue<Runnable>(10)); |
487 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
488 |
< |
final CountDownLatch done = new CountDownLatch(1); |
489 |
< |
assertFalse(p.isTerminated()); |
490 |
< |
try { |
487 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
488 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
489 |
> |
final CountDownLatch done = new CountDownLatch(1); |
490 |
> |
assertFalse(p.isTerminating()); |
491 |
|
p.execute(new CheckedRunnable() { |
492 |
|
public void realRun() throws InterruptedException { |
493 |
< |
assertFalse(p.isTerminated()); |
493 |
> |
assertFalse(p.isTerminating()); |
494 |
|
threadStarted.countDown(); |
495 |
< |
done.await(); |
495 |
> |
await(done); |
496 |
|
}}); |
497 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
497 |
> |
await(threadStarted); |
498 |
|
assertFalse(p.isTerminating()); |
499 |
|
done.countDown(); |
476 |
– |
} finally { |
500 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
501 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
502 |
+ |
assertTrue(p.isTerminated()); |
503 |
+ |
assertFalse(p.isTerminating()); |
504 |
|
} |
479 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
480 |
– |
assertTrue(p.isTerminated()); |
505 |
|
} |
506 |
|
|
507 |
|
/** |
512 |
|
new ThreadPoolExecutor(1, 1, |
513 |
|
LONG_DELAY_MS, MILLISECONDS, |
514 |
|
new ArrayBlockingQueue<Runnable>(10)); |
515 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
516 |
< |
final CountDownLatch done = new CountDownLatch(1); |
517 |
< |
try { |
515 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
516 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
517 |
> |
final CountDownLatch done = new CountDownLatch(1); |
518 |
|
assertFalse(p.isTerminating()); |
519 |
|
p.execute(new CheckedRunnable() { |
520 |
|
public void realRun() throws InterruptedException { |
521 |
|
assertFalse(p.isTerminating()); |
522 |
|
threadStarted.countDown(); |
523 |
< |
done.await(); |
523 |
> |
await(done); |
524 |
|
}}); |
525 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
525 |
> |
await(threadStarted); |
526 |
|
assertFalse(p.isTerminating()); |
527 |
|
done.countDown(); |
504 |
– |
} finally { |
528 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
529 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
530 |
+ |
assertTrue(p.isTerminated()); |
531 |
+ |
assertFalse(p.isTerminating()); |
532 |
|
} |
507 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
508 |
– |
assertTrue(p.isTerminated()); |
509 |
– |
assertFalse(p.isTerminating()); |
533 |
|
} |
534 |
|
|
535 |
|
/** |
536 |
|
* getQueue returns the work queue, which contains queued tasks |
537 |
|
*/ |
538 |
|
public void testGetQueue() throws InterruptedException { |
539 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
540 |
|
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
541 |
|
final ThreadPoolExecutor p = |
542 |
|
new ThreadPoolExecutor(1, 1, |
543 |
|
LONG_DELAY_MS, MILLISECONDS, |
544 |
|
q); |
545 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
546 |
< |
final CountDownLatch done = new CountDownLatch(1); |
523 |
< |
try { |
545 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
546 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
547 |
|
FutureTask[] tasks = new FutureTask[5]; |
548 |
|
for (int i = 0; i < tasks.length; i++) { |
549 |
|
Callable task = new CheckedCallable<Boolean>() { |
550 |
|
public Boolean realCall() throws InterruptedException { |
551 |
|
threadStarted.countDown(); |
552 |
|
assertSame(q, p.getQueue()); |
553 |
< |
done.await(); |
553 |
> |
await(done); |
554 |
|
return Boolean.TRUE; |
555 |
|
}}; |
556 |
|
tasks[i] = new FutureTask(task); |
557 |
|
p.execute(tasks[i]); |
558 |
|
} |
559 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
559 |
> |
await(threadStarted); |
560 |
|
assertSame(q, p.getQueue()); |
561 |
|
assertFalse(q.contains(tasks[0])); |
562 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
563 |
|
assertEquals(tasks.length - 1, q.size()); |
541 |
– |
} finally { |
542 |
– |
done.countDown(); |
543 |
– |
joinPool(p); |
564 |
|
} |
565 |
|
} |
566 |
|
|
568 |
|
* remove(task) removes queued task, and fails to remove active task |
569 |
|
*/ |
570 |
|
public void testRemove() throws InterruptedException { |
571 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
572 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
573 |
|
final ThreadPoolExecutor p = |
574 |
|
new ThreadPoolExecutor(1, 1, |
575 |
|
LONG_DELAY_MS, MILLISECONDS, |
576 |
|
q); |
577 |
< |
Runnable[] tasks = new Runnable[5]; |
578 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
579 |
< |
final CountDownLatch done = new CountDownLatch(1); |
559 |
< |
try { |
577 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
578 |
> |
Runnable[] tasks = new Runnable[6]; |
579 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
580 |
|
for (int i = 0; i < tasks.length; i++) { |
581 |
|
tasks[i] = new CheckedRunnable() { |
582 |
|
public void realRun() throws InterruptedException { |
583 |
|
threadStarted.countDown(); |
584 |
< |
done.await(); |
584 |
> |
await(done); |
585 |
|
}}; |
586 |
|
p.execute(tasks[i]); |
587 |
|
} |
588 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
588 |
> |
await(threadStarted); |
589 |
|
assertFalse(p.remove(tasks[0])); |
590 |
|
assertTrue(q.contains(tasks[4])); |
591 |
|
assertTrue(q.contains(tasks[3])); |
595 |
|
assertTrue(q.contains(tasks[3])); |
596 |
|
assertTrue(p.remove(tasks[3])); |
597 |
|
assertFalse(q.contains(tasks[3])); |
578 |
– |
} finally { |
579 |
– |
done.countDown(); |
580 |
– |
joinPool(p); |
598 |
|
} |
599 |
|
} |
600 |
|
|
609 |
|
new ThreadPoolExecutor(1, 1, |
610 |
|
LONG_DELAY_MS, MILLISECONDS, |
611 |
|
q); |
612 |
< |
FutureTask[] tasks = new FutureTask[5]; |
613 |
< |
try { |
612 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
613 |
> |
FutureTask[] tasks = new FutureTask[5]; |
614 |
|
for (int i = 0; i < tasks.length; i++) { |
615 |
|
Callable task = new CheckedCallable<Boolean>() { |
616 |
|
public Boolean realCall() throws InterruptedException { |
617 |
|
threadStarted.countDown(); |
618 |
< |
done.await(); |
618 |
> |
await(done); |
619 |
|
return Boolean.TRUE; |
620 |
|
}}; |
621 |
|
tasks[i] = new FutureTask(task); |
622 |
|
p.execute(tasks[i]); |
623 |
|
} |
624 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
624 |
> |
await(threadStarted); |
625 |
|
assertEquals(tasks.length, p.getTaskCount()); |
626 |
|
assertEquals(tasks.length - 1, q.size()); |
627 |
|
assertEquals(1L, p.getActiveCount()); |
634 |
|
p.purge(); // Nothing to do |
635 |
|
assertEquals(tasks.length - 3, q.size()); |
636 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
620 |
– |
} finally { |
621 |
– |
done.countDown(); |
622 |
– |
joinPool(p); |
637 |
|
} |
638 |
|
} |
639 |
|
|
640 |
|
/** |
641 |
< |
* shutdownNow returns a list containing tasks that were not run |
641 |
> |
* shutdownNow returns a list containing tasks that were not run, |
642 |
> |
* and those tasks are drained from the queue |
643 |
|
*/ |
644 |
< |
public void testShutdownNow() { |
644 |
> |
public void testShutdownNow() throws InterruptedException { |
645 |
> |
final int poolSize = 2; |
646 |
> |
final int count = 5; |
647 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
648 |
|
final ThreadPoolExecutor p = |
649 |
< |
new ThreadPoolExecutor(1, 1, |
649 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
650 |
|
LONG_DELAY_MS, MILLISECONDS, |
651 |
|
new ArrayBlockingQueue<Runnable>(10)); |
652 |
< |
List l; |
653 |
< |
try { |
654 |
< |
for (int i = 0; i < 5; i++) |
637 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
638 |
< |
} |
639 |
< |
finally { |
652 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
653 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
654 |
> |
threadsStarted.countDown(); |
655 |
|
try { |
656 |
< |
l = p.shutdownNow(); |
657 |
< |
} catch (SecurityException ok) { return; } |
656 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
657 |
> |
} catch (InterruptedException success) {} |
658 |
> |
ran.getAndIncrement(); |
659 |
> |
}}; |
660 |
> |
for (int i = 0; i < count; i++) |
661 |
> |
p.execute(waiter); |
662 |
> |
await(threadsStarted); |
663 |
> |
assertEquals(poolSize, p.getActiveCount()); |
664 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
665 |
> |
final List<Runnable> queuedTasks; |
666 |
> |
try { |
667 |
> |
queuedTasks = p.shutdownNow(); |
668 |
> |
} catch (SecurityException ok) { |
669 |
> |
return; // Allowed in case test doesn't have privs |
670 |
|
} |
671 |
|
assertTrue(p.isShutdown()); |
672 |
< |
assertTrue(l.size() <= 4); |
672 |
> |
assertTrue(p.getQueue().isEmpty()); |
673 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
674 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
675 |
> |
assertTrue(p.isTerminated()); |
676 |
> |
assertEquals(poolSize, ran.get()); |
677 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
678 |
|
} |
679 |
|
|
680 |
|
// Exception Tests |
1021 |
|
* get of submitted callable throws InterruptedException if interrupted |
1022 |
|
*/ |
1023 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1024 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1025 |
|
final ThreadPoolExecutor p = |
1026 |
|
new ThreadPoolExecutor(1, 1, |
1027 |
|
60, SECONDS, |
1028 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1029 |
|
|
1030 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1031 |
< |
final CountDownLatch done = new CountDownLatch(1); |
999 |
< |
try { |
1030 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1031 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1032 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1033 |
|
public void realRun() throws Exception { |
1034 |
|
Callable task = new CheckedCallable<Boolean>() { |
1035 |
|
public Boolean realCall() throws InterruptedException { |
1036 |
|
threadStarted.countDown(); |
1037 |
< |
done.await(); |
1037 |
> |
await(done); |
1038 |
|
return Boolean.TRUE; |
1039 |
|
}}; |
1040 |
|
p.submit(task).get(); |
1041 |
|
}}); |
1042 |
|
|
1043 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1043 |
> |
await(threadStarted); |
1044 |
|
t.interrupt(); |
1045 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
1014 |
< |
} finally { |
1015 |
< |
done.countDown(); |
1016 |
< |
joinPool(p); |
1045 |
> |
awaitTermination(t); |
1046 |
|
} |
1047 |
|
} |
1048 |
|
|
1050 |
|
* execute throws RejectedExecutionException if saturated. |
1051 |
|
*/ |
1052 |
|
public void testSaturatedExecute() { |
1053 |
< |
ThreadPoolExecutor p = |
1053 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1054 |
> |
final ThreadPoolExecutor p = |
1055 |
|
new ThreadPoolExecutor(1, 1, |
1056 |
|
LONG_DELAY_MS, MILLISECONDS, |
1057 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1058 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1029 |
< |
try { |
1058 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1059 |
|
Runnable task = new CheckedRunnable() { |
1060 |
|
public void realRun() throws InterruptedException { |
1061 |
< |
done.await(); |
1061 |
> |
await(done); |
1062 |
|
}}; |
1063 |
|
for (int i = 0; i < 2; ++i) |
1064 |
|
p.execute(task); |
1069 |
|
} catch (RejectedExecutionException success) {} |
1070 |
|
assertTrue(p.getTaskCount() <= 2); |
1071 |
|
} |
1043 |
– |
} finally { |
1044 |
– |
done.countDown(); |
1045 |
– |
joinPool(p); |
1072 |
|
} |
1073 |
|
} |
1074 |
|
|
1076 |
|
* submit(runnable) throws RejectedExecutionException if saturated. |
1077 |
|
*/ |
1078 |
|
public void testSaturatedSubmitRunnable() { |
1079 |
< |
ThreadPoolExecutor p = |
1079 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1080 |
> |
final ThreadPoolExecutor p = |
1081 |
|
new ThreadPoolExecutor(1, 1, |
1082 |
|
LONG_DELAY_MS, MILLISECONDS, |
1083 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1084 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1058 |
< |
try { |
1084 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1085 |
|
Runnable task = new CheckedRunnable() { |
1086 |
|
public void realRun() throws InterruptedException { |
1087 |
< |
done.await(); |
1087 |
> |
await(done); |
1088 |
|
}}; |
1089 |
|
for (int i = 0; i < 2; ++i) |
1090 |
|
p.submit(task); |
1095 |
|
} catch (RejectedExecutionException success) {} |
1096 |
|
assertTrue(p.getTaskCount() <= 2); |
1097 |
|
} |
1072 |
– |
} finally { |
1073 |
– |
done.countDown(); |
1074 |
– |
joinPool(p); |
1098 |
|
} |
1099 |
|
} |
1100 |
|
|
1102 |
|
* submit(callable) throws RejectedExecutionException if saturated. |
1103 |
|
*/ |
1104 |
|
public void testSaturatedSubmitCallable() { |
1105 |
< |
ThreadPoolExecutor p = |
1105 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1106 |
> |
final ThreadPoolExecutor p = |
1107 |
|
new ThreadPoolExecutor(1, 1, |
1108 |
|
LONG_DELAY_MS, MILLISECONDS, |
1109 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1110 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1087 |
< |
try { |
1110 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1111 |
|
Runnable task = new CheckedRunnable() { |
1112 |
|
public void realRun() throws InterruptedException { |
1113 |
< |
done.await(); |
1113 |
> |
await(done); |
1114 |
|
}}; |
1115 |
|
for (int i = 0; i < 2; ++i) |
1116 |
|
p.submit(Executors.callable(task)); |
1121 |
|
} catch (RejectedExecutionException success) {} |
1122 |
|
assertTrue(p.getTaskCount() <= 2); |
1123 |
|
} |
1101 |
– |
} finally { |
1102 |
– |
done.countDown(); |
1103 |
– |
joinPool(p); |
1124 |
|
} |
1125 |
|
} |
1126 |
|
|
1128 |
|
* executor using CallerRunsPolicy runs task if saturated. |
1129 |
|
*/ |
1130 |
|
public void testSaturatedExecute2() { |
1111 |
– |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1131 |
|
final ThreadPoolExecutor p = |
1132 |
|
new ThreadPoolExecutor(1, 1, |
1133 |
|
LONG_DELAY_MS, |
1134 |
|
MILLISECONDS, |
1135 |
|
new ArrayBlockingQueue<Runnable>(1), |
1136 |
< |
h); |
1137 |
< |
try { |
1136 |
> |
new ThreadPoolExecutor.CallerRunsPolicy()); |
1137 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1138 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1139 |
> |
Runnable blocker = new CheckedRunnable() { |
1140 |
> |
public void realRun() throws InterruptedException { |
1141 |
> |
await(done); |
1142 |
> |
}}; |
1143 |
> |
p.execute(blocker); |
1144 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1145 |
< |
for (int i = 0; i < tasks.length; ++i) |
1145 |
> |
for (int i = 0; i < tasks.length; i++) |
1146 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1147 |
< |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1123 |
< |
p.execute(mr); |
1124 |
< |
for (int i = 0; i < tasks.length; ++i) |
1147 |
> |
for (int i = 0; i < tasks.length; i++) |
1148 |
|
p.execute(tasks[i]); |
1149 |
< |
for (int i = 1; i < tasks.length; ++i) |
1149 |
> |
for (int i = 1; i < tasks.length; i++) |
1150 |
|
assertTrue(tasks[i].done); |
1151 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1152 |
< |
} finally { |
1130 |
< |
joinPool(p); |
1151 |
> |
assertFalse(tasks[0].done); // waiting in queue |
1152 |
> |
done.countDown(); |
1153 |
|
} |
1154 |
|
} |
1155 |
|
|
1157 |
|
* executor using DiscardPolicy drops task if saturated. |
1158 |
|
*/ |
1159 |
|
public void testSaturatedExecute3() { |
1160 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1160 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1161 |
> |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1162 |
> |
for (int i = 0; i < tasks.length; ++i) |
1163 |
> |
tasks[i] = new TrackedNoOpRunnable(); |
1164 |
|
final ThreadPoolExecutor p = |
1165 |
|
new ThreadPoolExecutor(1, 1, |
1166 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1167 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1168 |
< |
h); |
1169 |
< |
try { |
1170 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1171 |
< |
for (int i = 0; i < tasks.length; ++i) |
1147 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1148 |
< |
p.execute(new TrackedLongRunnable()); |
1166 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1167 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1168 |
> |
new ThreadPoolExecutor.DiscardPolicy()); |
1169 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1170 |
> |
p.execute(awaiter(done)); |
1171 |
> |
|
1172 |
|
for (TrackedNoOpRunnable task : tasks) |
1173 |
|
p.execute(task); |
1174 |
< |
for (TrackedNoOpRunnable task : tasks) |
1175 |
< |
assertFalse(task.done); |
1153 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1154 |
< |
} finally { |
1155 |
< |
joinPool(p); |
1174 |
> |
for (int i = 1; i < tasks.length; i++) |
1175 |
> |
assertFalse(tasks[i].done); |
1176 |
|
} |
1177 |
+ |
for (int i = 1; i < tasks.length; i++) |
1178 |
+ |
assertFalse(tasks[i].done); |
1179 |
+ |
assertTrue(tasks[0].done); // was waiting in queue |
1180 |
|
} |
1181 |
|
|
1182 |
|
/** |
1183 |
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1184 |
|
*/ |
1185 |
|
public void testSaturatedExecute4() { |
1186 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1186 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1187 |
> |
LatchAwaiter r1 = awaiter(done); |
1188 |
> |
LatchAwaiter r2 = awaiter(done); |
1189 |
> |
LatchAwaiter r3 = awaiter(done); |
1190 |
|
final ThreadPoolExecutor p = |
1191 |
|
new ThreadPoolExecutor(1, 1, |
1192 |
|
LONG_DELAY_MS, MILLISECONDS, |
1193 |
|
new ArrayBlockingQueue<Runnable>(1), |
1194 |
< |
h); |
1195 |
< |
try { |
1196 |
< |
p.execute(new TrackedLongRunnable()); |
1197 |
< |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1194 |
> |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1195 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1196 |
> |
assertEquals(LatchAwaiter.NEW, r1.state); |
1197 |
> |
assertEquals(LatchAwaiter.NEW, r2.state); |
1198 |
> |
assertEquals(LatchAwaiter.NEW, r3.state); |
1199 |
> |
p.execute(r1); |
1200 |
|
p.execute(r2); |
1201 |
|
assertTrue(p.getQueue().contains(r2)); |
1174 |
– |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
1202 |
|
p.execute(r3); |
1203 |
|
assertFalse(p.getQueue().contains(r2)); |
1204 |
|
assertTrue(p.getQueue().contains(r3)); |
1178 |
– |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1179 |
– |
} finally { |
1180 |
– |
joinPool(p); |
1205 |
|
} |
1206 |
+ |
assertEquals(LatchAwaiter.DONE, r1.state); |
1207 |
+ |
assertEquals(LatchAwaiter.NEW, r2.state); |
1208 |
+ |
assertEquals(LatchAwaiter.DONE, r3.state); |
1209 |
|
} |
1210 |
|
|
1211 |
|
/** |
1212 |
|
* execute throws RejectedExecutionException if shutdown |
1213 |
|
*/ |
1214 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1215 |
< |
ThreadPoolExecutor p = |
1215 |
> |
final ThreadPoolExecutor p = |
1216 |
|
new ThreadPoolExecutor(1, 1, |
1217 |
|
LONG_DELAY_MS, MILLISECONDS, |
1218 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1219 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1220 |
< |
try { |
1221 |
< |
p.execute(new NoOpRunnable()); |
1222 |
< |
shouldThrow(); |
1223 |
< |
} catch (RejectedExecutionException success) {} |
1224 |
< |
|
1225 |
< |
joinPool(p); |
1220 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1221 |
> |
try { |
1222 |
> |
p.execute(new NoOpRunnable()); |
1223 |
> |
shouldThrow(); |
1224 |
> |
} catch (RejectedExecutionException success) {} |
1225 |
> |
} |
1226 |
|
} |
1227 |
|
|
1228 |
|
/** |
1236 |
|
new ArrayBlockingQueue<Runnable>(1), h); |
1237 |
|
|
1238 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1239 |
< |
try { |
1239 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1240 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1241 |
|
p.execute(r); |
1242 |
|
assertFalse(r.done); |
1216 |
– |
} finally { |
1217 |
– |
joinPool(p); |
1243 |
|
} |
1244 |
|
} |
1245 |
|
|
1247 |
|
* execute using DiscardPolicy drops task on shutdown |
1248 |
|
*/ |
1249 |
|
public void testDiscardOnShutdown() { |
1250 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1226 |
< |
ThreadPoolExecutor p = |
1250 |
> |
final ThreadPoolExecutor p = |
1251 |
|
new ThreadPoolExecutor(1, 1, |
1252 |
|
LONG_DELAY_MS, MILLISECONDS, |
1253 |
|
new ArrayBlockingQueue<Runnable>(1), |
1254 |
< |
h); |
1254 |
> |
new ThreadPoolExecutor.DiscardPolicy()); |
1255 |
|
|
1256 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1257 |
< |
try { |
1257 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1258 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1259 |
|
p.execute(r); |
1260 |
|
assertFalse(r.done); |
1237 |
– |
} finally { |
1238 |
– |
joinPool(p); |
1261 |
|
} |
1262 |
|
} |
1263 |
|
|
1265 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1266 |
|
*/ |
1267 |
|
public void testDiscardOldestOnShutdown() { |
1268 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1247 |
< |
ThreadPoolExecutor p = |
1268 |
> |
final ThreadPoolExecutor p = |
1269 |
|
new ThreadPoolExecutor(1, 1, |
1270 |
|
LONG_DELAY_MS, MILLISECONDS, |
1271 |
|
new ArrayBlockingQueue<Runnable>(1), |
1272 |
< |
h); |
1272 |
> |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1273 |
|
|
1274 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1275 |
< |
try { |
1275 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1276 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1277 |
|
p.execute(r); |
1278 |
|
assertFalse(r.done); |
1258 |
– |
} finally { |
1259 |
– |
joinPool(p); |
1279 |
|
} |
1280 |
|
} |
1281 |
|
|
1283 |
|
* execute(null) throws NPE |
1284 |
|
*/ |
1285 |
|
public void testExecuteNull() { |
1286 |
< |
ThreadPoolExecutor p = |
1287 |
< |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1286 |
> |
final ThreadPoolExecutor p = |
1287 |
> |
new ThreadPoolExecutor(1, 2, |
1288 |
> |
1L, SECONDS, |
1289 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1290 |
< |
try { |
1291 |
< |
p.execute(null); |
1292 |
< |
shouldThrow(); |
1293 |
< |
} catch (NullPointerException success) {} |
1294 |
< |
|
1295 |
< |
joinPool(p); |
1290 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1291 |
> |
try { |
1292 |
> |
p.execute(null); |
1293 |
> |
shouldThrow(); |
1294 |
> |
} catch (NullPointerException success) {} |
1295 |
> |
} |
1296 |
|
} |
1297 |
|
|
1298 |
|
/** |
1299 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1300 |
|
*/ |
1301 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1302 |
< |
ThreadPoolExecutor p = |
1302 |
> |
final ThreadPoolExecutor p = |
1303 |
|
new ThreadPoolExecutor(1, 2, |
1304 |
|
LONG_DELAY_MS, MILLISECONDS, |
1305 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1306 |
< |
try { |
1307 |
< |
p.setCorePoolSize(-1); |
1308 |
< |
shouldThrow(); |
1309 |
< |
} catch (IllegalArgumentException success) { |
1310 |
< |
} finally { |
1291 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1306 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1307 |
> |
try { |
1308 |
> |
p.setCorePoolSize(-1); |
1309 |
> |
shouldThrow(); |
1310 |
> |
} catch (IllegalArgumentException success) {} |
1311 |
|
} |
1293 |
– |
joinPool(p); |
1312 |
|
} |
1313 |
|
|
1314 |
|
/** |
1316 |
|
* given a value less the core pool size |
1317 |
|
*/ |
1318 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1319 |
< |
ThreadPoolExecutor p = |
1319 |
> |
final ThreadPoolExecutor p = |
1320 |
|
new ThreadPoolExecutor(2, 3, |
1321 |
|
LONG_DELAY_MS, MILLISECONDS, |
1322 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1323 |
< |
try { |
1324 |
< |
p.setMaximumPoolSize(1); |
1325 |
< |
shouldThrow(); |
1326 |
< |
} catch (IllegalArgumentException success) { |
1327 |
< |
} finally { |
1310 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1323 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1324 |
> |
try { |
1325 |
> |
p.setMaximumPoolSize(1); |
1326 |
> |
shouldThrow(); |
1327 |
> |
} catch (IllegalArgumentException success) {} |
1328 |
|
} |
1312 |
– |
joinPool(p); |
1329 |
|
} |
1330 |
|
|
1331 |
|
/** |
1333 |
|
* if given a negative value |
1334 |
|
*/ |
1335 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1336 |
< |
ThreadPoolExecutor p = |
1336 |
> |
final ThreadPoolExecutor p = |
1337 |
|
new ThreadPoolExecutor(2, 3, |
1338 |
|
LONG_DELAY_MS, MILLISECONDS, |
1339 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1340 |
< |
try { |
1341 |
< |
p.setMaximumPoolSize(-1); |
1342 |
< |
shouldThrow(); |
1343 |
< |
} catch (IllegalArgumentException success) { |
1344 |
< |
} finally { |
1329 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1340 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1341 |
> |
try { |
1342 |
> |
p.setMaximumPoolSize(-1); |
1343 |
> |
shouldThrow(); |
1344 |
> |
} catch (IllegalArgumentException success) {} |
1345 |
|
} |
1331 |
– |
joinPool(p); |
1346 |
|
} |
1347 |
|
|
1348 |
|
/** |
1350 |
|
* max pool size result in IllegalArgumentException. |
1351 |
|
*/ |
1352 |
|
public void testPoolSizeInvariants() { |
1353 |
< |
ThreadPoolExecutor p = |
1353 |
> |
final ThreadPoolExecutor p = |
1354 |
|
new ThreadPoolExecutor(1, 1, |
1355 |
|
LONG_DELAY_MS, MILLISECONDS, |
1356 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1357 |
< |
for (int s = 1; s < 5; s++) { |
1358 |
< |
p.setMaximumPoolSize(s); |
1359 |
< |
p.setCorePoolSize(s); |
1360 |
< |
try { |
1361 |
< |
p.setMaximumPoolSize(s - 1); |
1362 |
< |
shouldThrow(); |
1363 |
< |
} catch (IllegalArgumentException success) {} |
1364 |
< |
assertEquals(s, p.getCorePoolSize()); |
1365 |
< |
assertEquals(s, p.getMaximumPoolSize()); |
1366 |
< |
try { |
1367 |
< |
p.setCorePoolSize(s + 1); |
1368 |
< |
shouldThrow(); |
1369 |
< |
} catch (IllegalArgumentException success) {} |
1370 |
< |
assertEquals(s, p.getCorePoolSize()); |
1371 |
< |
assertEquals(s, p.getMaximumPoolSize()); |
1357 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1358 |
> |
for (int s = 1; s < 5; s++) { |
1359 |
> |
p.setMaximumPoolSize(s); |
1360 |
> |
p.setCorePoolSize(s); |
1361 |
> |
try { |
1362 |
> |
p.setMaximumPoolSize(s - 1); |
1363 |
> |
shouldThrow(); |
1364 |
> |
} catch (IllegalArgumentException success) {} |
1365 |
> |
assertEquals(s, p.getCorePoolSize()); |
1366 |
> |
assertEquals(s, p.getMaximumPoolSize()); |
1367 |
> |
try { |
1368 |
> |
p.setCorePoolSize(s + 1); |
1369 |
> |
shouldThrow(); |
1370 |
> |
} catch (IllegalArgumentException success) {} |
1371 |
> |
assertEquals(s, p.getCorePoolSize()); |
1372 |
> |
assertEquals(s, p.getMaximumPoolSize()); |
1373 |
> |
} |
1374 |
|
} |
1359 |
– |
joinPool(p); |
1375 |
|
} |
1376 |
|
|
1377 |
|
/** |
1379 |
|
* when given a negative value |
1380 |
|
*/ |
1381 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1382 |
< |
ThreadPoolExecutor p = |
1382 |
> |
final ThreadPoolExecutor p = |
1383 |
|
new ThreadPoolExecutor(2, 3, |
1384 |
|
LONG_DELAY_MS, MILLISECONDS, |
1385 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1386 |
< |
try { |
1387 |
< |
p.setKeepAliveTime(-1,MILLISECONDS); |
1388 |
< |
shouldThrow(); |
1389 |
< |
} catch (IllegalArgumentException success) { |
1390 |
< |
} finally { |
1376 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1386 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1387 |
> |
try { |
1388 |
> |
p.setKeepAliveTime(-1, MILLISECONDS); |
1389 |
> |
shouldThrow(); |
1390 |
> |
} catch (IllegalArgumentException success) {} |
1391 |
|
} |
1378 |
– |
joinPool(p); |
1392 |
|
} |
1393 |
|
|
1394 |
|
/** |
1396 |
|
*/ |
1397 |
|
public void testTerminated() { |
1398 |
|
ExtendedTPE p = new ExtendedTPE(); |
1399 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1400 |
< |
assertTrue(p.terminatedCalled()); |
1401 |
< |
joinPool(p); |
1399 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1400 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1401 |
> |
assertTrue(p.terminatedCalled()); |
1402 |
> |
assertTrue(p.isShutdown()); |
1403 |
> |
} |
1404 |
|
} |
1405 |
|
|
1406 |
|
/** |
1408 |
|
*/ |
1409 |
|
public void testBeforeAfter() throws InterruptedException { |
1410 |
|
ExtendedTPE p = new ExtendedTPE(); |
1411 |
< |
try { |
1411 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1412 |
|
final CountDownLatch done = new CountDownLatch(1); |
1413 |
|
p.execute(new CheckedRunnable() { |
1414 |
|
public void realRun() { |
1418 |
|
assertEquals(0, done.getCount()); |
1419 |
|
assertTrue(p.afterCalled()); |
1420 |
|
assertTrue(p.beforeCalled()); |
1406 |
– |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1407 |
– |
} finally { |
1408 |
– |
joinPool(p); |
1421 |
|
} |
1422 |
|
} |
1423 |
|
|
1425 |
|
* completed submit of callable returns result |
1426 |
|
*/ |
1427 |
|
public void testSubmitCallable() throws Exception { |
1428 |
< |
ExecutorService e = |
1428 |
> |
final ExecutorService e = |
1429 |
|
new ThreadPoolExecutor(2, 2, |
1430 |
|
LONG_DELAY_MS, MILLISECONDS, |
1431 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1432 |
< |
try { |
1432 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1433 |
|
Future<String> future = e.submit(new StringTask()); |
1434 |
|
String result = future.get(); |
1435 |
|
assertSame(TEST_STRING, result); |
1424 |
– |
} finally { |
1425 |
– |
joinPool(e); |
1436 |
|
} |
1437 |
|
} |
1438 |
|
|
1440 |
|
* completed submit of runnable returns successfully |
1441 |
|
*/ |
1442 |
|
public void testSubmitRunnable() throws Exception { |
1443 |
< |
ExecutorService e = |
1443 |
> |
final ExecutorService e = |
1444 |
|
new ThreadPoolExecutor(2, 2, |
1445 |
|
LONG_DELAY_MS, MILLISECONDS, |
1446 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1447 |
< |
try { |
1447 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1448 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1449 |
|
future.get(); |
1450 |
|
assertTrue(future.isDone()); |
1441 |
– |
} finally { |
1442 |
– |
joinPool(e); |
1451 |
|
} |
1452 |
|
} |
1453 |
|
|
1455 |
|
* completed submit of (runnable, result) returns result |
1456 |
|
*/ |
1457 |
|
public void testSubmitRunnable2() throws Exception { |
1458 |
< |
ExecutorService e = |
1458 |
> |
final ExecutorService e = |
1459 |
|
new ThreadPoolExecutor(2, 2, |
1460 |
|
LONG_DELAY_MS, MILLISECONDS, |
1461 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1462 |
< |
try { |
1462 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1463 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1464 |
|
String result = future.get(); |
1465 |
|
assertSame(TEST_STRING, result); |
1458 |
– |
} finally { |
1459 |
– |
joinPool(e); |
1466 |
|
} |
1467 |
|
} |
1468 |
|
|
1470 |
|
* invokeAny(null) throws NPE |
1471 |
|
*/ |
1472 |
|
public void testInvokeAny1() throws Exception { |
1473 |
< |
ExecutorService e = |
1473 |
> |
final ExecutorService e = |
1474 |
|
new ThreadPoolExecutor(2, 2, |
1475 |
|
LONG_DELAY_MS, MILLISECONDS, |
1476 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1477 |
< |
try { |
1478 |
< |
e.invokeAny(null); |
1479 |
< |
shouldThrow(); |
1480 |
< |
} catch (NullPointerException success) { |
1481 |
< |
} finally { |
1476 |
< |
joinPool(e); |
1477 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1478 |
> |
try { |
1479 |
> |
e.invokeAny(null); |
1480 |
> |
shouldThrow(); |
1481 |
> |
} catch (NullPointerException success) {} |
1482 |
|
} |
1483 |
|
} |
1484 |
|
|
1486 |
|
* invokeAny(empty collection) throws IAE |
1487 |
|
*/ |
1488 |
|
public void testInvokeAny2() throws Exception { |
1489 |
< |
ExecutorService e = |
1489 |
> |
final ExecutorService e = |
1490 |
|
new ThreadPoolExecutor(2, 2, |
1491 |
|
LONG_DELAY_MS, MILLISECONDS, |
1492 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1493 |
< |
try { |
1494 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
1495 |
< |
shouldThrow(); |
1496 |
< |
} catch (IllegalArgumentException success) { |
1497 |
< |
} finally { |
1493 |
< |
joinPool(e); |
1493 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1494 |
> |
try { |
1495 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
1496 |
> |
shouldThrow(); |
1497 |
> |
} catch (IllegalArgumentException success) {} |
1498 |
|
} |
1499 |
|
} |
1500 |
|
|
1507 |
|
new ThreadPoolExecutor(2, 2, |
1508 |
|
LONG_DELAY_MS, MILLISECONDS, |
1509 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1510 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1511 |
< |
l.add(latchAwaitingStringTask(latch)); |
1512 |
< |
l.add(null); |
1513 |
< |
try { |
1514 |
< |
e.invokeAny(l); |
1515 |
< |
shouldThrow(); |
1516 |
< |
} catch (NullPointerException success) { |
1517 |
< |
} finally { |
1510 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1511 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1512 |
> |
l.add(latchAwaitingStringTask(latch)); |
1513 |
> |
l.add(null); |
1514 |
> |
try { |
1515 |
> |
e.invokeAny(l); |
1516 |
> |
shouldThrow(); |
1517 |
> |
} catch (NullPointerException success) {} |
1518 |
|
latch.countDown(); |
1515 |
– |
joinPool(e); |
1519 |
|
} |
1520 |
|
} |
1521 |
|
|
1523 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1524 |
|
*/ |
1525 |
|
public void testInvokeAny4() throws Exception { |
1526 |
< |
ExecutorService e = |
1526 |
> |
final ExecutorService e = |
1527 |
|
new ThreadPoolExecutor(2, 2, |
1528 |
|
LONG_DELAY_MS, MILLISECONDS, |
1529 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1530 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1531 |
< |
l.add(new NPETask()); |
1532 |
< |
try { |
1533 |
< |
e.invokeAny(l); |
1534 |
< |
shouldThrow(); |
1535 |
< |
} catch (ExecutionException success) { |
1536 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1537 |
< |
} finally { |
1538 |
< |
joinPool(e); |
1530 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1531 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1532 |
> |
l.add(new NPETask()); |
1533 |
> |
try { |
1534 |
> |
e.invokeAny(l); |
1535 |
> |
shouldThrow(); |
1536 |
> |
} catch (ExecutionException success) { |
1537 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1538 |
> |
} |
1539 |
|
} |
1540 |
|
} |
1541 |
|
|
1543 |
|
* invokeAny(c) returns result of some task |
1544 |
|
*/ |
1545 |
|
public void testInvokeAny5() throws Exception { |
1546 |
< |
ExecutorService e = |
1546 |
> |
final ExecutorService e = |
1547 |
|
new ThreadPoolExecutor(2, 2, |
1548 |
|
LONG_DELAY_MS, MILLISECONDS, |
1549 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1550 |
< |
try { |
1550 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1551 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1552 |
|
l.add(new StringTask()); |
1553 |
|
l.add(new StringTask()); |
1554 |
|
String result = e.invokeAny(l); |
1555 |
|
assertSame(TEST_STRING, result); |
1553 |
– |
} finally { |
1554 |
– |
joinPool(e); |
1556 |
|
} |
1557 |
|
} |
1558 |
|
|
1560 |
|
* invokeAll(null) throws NPE |
1561 |
|
*/ |
1562 |
|
public void testInvokeAll1() throws Exception { |
1563 |
< |
ExecutorService e = |
1563 |
> |
final ExecutorService e = |
1564 |
|
new ThreadPoolExecutor(2, 2, |
1565 |
|
LONG_DELAY_MS, MILLISECONDS, |
1566 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1567 |
< |
try { |
1568 |
< |
e.invokeAll(null); |
1569 |
< |
shouldThrow(); |
1570 |
< |
} catch (NullPointerException success) { |
1571 |
< |
} finally { |
1571 |
< |
joinPool(e); |
1567 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1568 |
> |
try { |
1569 |
> |
e.invokeAll(null); |
1570 |
> |
shouldThrow(); |
1571 |
> |
} catch (NullPointerException success) {} |
1572 |
|
} |
1573 |
|
} |
1574 |
|
|
1576 |
|
* invokeAll(empty collection) returns empty collection |
1577 |
|
*/ |
1578 |
|
public void testInvokeAll2() throws InterruptedException { |
1579 |
< |
ExecutorService e = |
1579 |
> |
final ExecutorService e = |
1580 |
|
new ThreadPoolExecutor(2, 2, |
1581 |
|
LONG_DELAY_MS, MILLISECONDS, |
1582 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1583 |
< |
try { |
1583 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1584 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1585 |
|
assertTrue(r.isEmpty()); |
1586 |
– |
} finally { |
1587 |
– |
joinPool(e); |
1586 |
|
} |
1587 |
|
} |
1588 |
|
|
1590 |
|
* invokeAll(c) throws NPE if c has null elements |
1591 |
|
*/ |
1592 |
|
public void testInvokeAll3() throws Exception { |
1593 |
< |
ExecutorService e = |
1593 |
> |
final ExecutorService e = |
1594 |
|
new ThreadPoolExecutor(2, 2, |
1595 |
|
LONG_DELAY_MS, MILLISECONDS, |
1596 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1597 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1598 |
< |
l.add(new StringTask()); |
1599 |
< |
l.add(null); |
1600 |
< |
try { |
1601 |
< |
e.invokeAll(l); |
1602 |
< |
shouldThrow(); |
1603 |
< |
} catch (NullPointerException success) { |
1604 |
< |
} finally { |
1607 |
< |
joinPool(e); |
1597 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1598 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1599 |
> |
l.add(new StringTask()); |
1600 |
> |
l.add(null); |
1601 |
> |
try { |
1602 |
> |
e.invokeAll(l); |
1603 |
> |
shouldThrow(); |
1604 |
> |
} catch (NullPointerException success) {} |
1605 |
|
} |
1606 |
|
} |
1607 |
|
|
1609 |
|
* get of element of invokeAll(c) throws exception on failed task |
1610 |
|
*/ |
1611 |
|
public void testInvokeAll4() throws Exception { |
1612 |
< |
ExecutorService e = |
1612 |
> |
final ExecutorService e = |
1613 |
|
new ThreadPoolExecutor(2, 2, |
1614 |
|
LONG_DELAY_MS, MILLISECONDS, |
1615 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1616 |
< |
try { |
1616 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1617 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1618 |
|
l.add(new NPETask()); |
1619 |
|
List<Future<String>> futures = e.invokeAll(l); |
1624 |
|
} catch (ExecutionException success) { |
1625 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1626 |
|
} |
1630 |
– |
} finally { |
1631 |
– |
joinPool(e); |
1627 |
|
} |
1628 |
|
} |
1629 |
|
|
1631 |
|
* invokeAll(c) returns results of all completed tasks |
1632 |
|
*/ |
1633 |
|
public void testInvokeAll5() throws Exception { |
1634 |
< |
ExecutorService e = |
1634 |
> |
final ExecutorService e = |
1635 |
|
new ThreadPoolExecutor(2, 2, |
1636 |
|
LONG_DELAY_MS, MILLISECONDS, |
1637 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1638 |
< |
try { |
1638 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1639 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1640 |
|
l.add(new StringTask()); |
1641 |
|
l.add(new StringTask()); |
1643 |
|
assertEquals(2, futures.size()); |
1644 |
|
for (Future<String> future : futures) |
1645 |
|
assertSame(TEST_STRING, future.get()); |
1651 |
– |
} finally { |
1652 |
– |
joinPool(e); |
1646 |
|
} |
1647 |
|
} |
1648 |
|
|
1650 |
|
* timed invokeAny(null) throws NPE |
1651 |
|
*/ |
1652 |
|
public void testTimedInvokeAny1() throws Exception { |
1653 |
< |
ExecutorService e = |
1653 |
> |
final ExecutorService e = |
1654 |
|
new ThreadPoolExecutor(2, 2, |
1655 |
|
LONG_DELAY_MS, MILLISECONDS, |
1656 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1657 |
< |
try { |
1658 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1659 |
< |
shouldThrow(); |
1660 |
< |
} catch (NullPointerException success) { |
1661 |
< |
} finally { |
1669 |
< |
joinPool(e); |
1657 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1658 |
> |
try { |
1659 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1660 |
> |
shouldThrow(); |
1661 |
> |
} catch (NullPointerException success) {} |
1662 |
|
} |
1663 |
|
} |
1664 |
|
|
1666 |
|
* timed invokeAny(,,null) throws NPE |
1667 |
|
*/ |
1668 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1669 |
< |
ExecutorService e = |
1669 |
> |
final ExecutorService e = |
1670 |
|
new ThreadPoolExecutor(2, 2, |
1671 |
|
LONG_DELAY_MS, MILLISECONDS, |
1672 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1673 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1674 |
< |
l.add(new StringTask()); |
1675 |
< |
try { |
1676 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1677 |
< |
shouldThrow(); |
1678 |
< |
} catch (NullPointerException success) { |
1679 |
< |
} finally { |
1688 |
< |
joinPool(e); |
1673 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1674 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1675 |
> |
l.add(new StringTask()); |
1676 |
> |
try { |
1677 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1678 |
> |
shouldThrow(); |
1679 |
> |
} catch (NullPointerException success) {} |
1680 |
|
} |
1681 |
|
} |
1682 |
|
|
1684 |
|
* timed invokeAny(empty collection) throws IAE |
1685 |
|
*/ |
1686 |
|
public void testTimedInvokeAny2() throws Exception { |
1687 |
< |
ExecutorService e = |
1687 |
> |
final ExecutorService e = |
1688 |
|
new ThreadPoolExecutor(2, 2, |
1689 |
|
LONG_DELAY_MS, MILLISECONDS, |
1690 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1691 |
< |
try { |
1692 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1693 |
< |
shouldThrow(); |
1694 |
< |
} catch (IllegalArgumentException success) { |
1695 |
< |
} finally { |
1696 |
< |
joinPool(e); |
1691 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1692 |
> |
try { |
1693 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
1694 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1695 |
> |
shouldThrow(); |
1696 |
> |
} catch (IllegalArgumentException success) {} |
1697 |
|
} |
1698 |
|
} |
1699 |
|
|
1706 |
|
new ThreadPoolExecutor(2, 2, |
1707 |
|
LONG_DELAY_MS, MILLISECONDS, |
1708 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1709 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1710 |
< |
l.add(latchAwaitingStringTask(latch)); |
1711 |
< |
l.add(null); |
1712 |
< |
try { |
1713 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1714 |
< |
shouldThrow(); |
1715 |
< |
} catch (NullPointerException success) { |
1716 |
< |
} finally { |
1709 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1710 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1711 |
> |
l.add(latchAwaitingStringTask(latch)); |
1712 |
> |
l.add(null); |
1713 |
> |
try { |
1714 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1715 |
> |
shouldThrow(); |
1716 |
> |
} catch (NullPointerException success) {} |
1717 |
|
latch.countDown(); |
1727 |
– |
joinPool(e); |
1718 |
|
} |
1719 |
|
} |
1720 |
|
|
1722 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1723 |
|
*/ |
1724 |
|
public void testTimedInvokeAny4() throws Exception { |
1725 |
< |
ExecutorService e = |
1725 |
> |
final ExecutorService e = |
1726 |
|
new ThreadPoolExecutor(2, 2, |
1727 |
|
LONG_DELAY_MS, MILLISECONDS, |
1728 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1729 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1730 |
< |
l.add(new NPETask()); |
1731 |
< |
try { |
1732 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1733 |
< |
shouldThrow(); |
1734 |
< |
} catch (ExecutionException success) { |
1735 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1736 |
< |
} finally { |
1737 |
< |
joinPool(e); |
1729 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1730 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1731 |
> |
l.add(new NPETask()); |
1732 |
> |
try { |
1733 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1734 |
> |
shouldThrow(); |
1735 |
> |
} catch (ExecutionException success) { |
1736 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1737 |
> |
} |
1738 |
|
} |
1739 |
|
} |
1740 |
|
|
1742 |
|
* timed invokeAny(c) returns result of some task |
1743 |
|
*/ |
1744 |
|
public void testTimedInvokeAny5() throws Exception { |
1745 |
< |
ExecutorService e = |
1745 |
> |
final ExecutorService e = |
1746 |
|
new ThreadPoolExecutor(2, 2, |
1747 |
|
LONG_DELAY_MS, MILLISECONDS, |
1748 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1749 |
< |
try { |
1749 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1750 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1751 |
|
l.add(new StringTask()); |
1752 |
|
l.add(new StringTask()); |
1753 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1754 |
|
assertSame(TEST_STRING, result); |
1765 |
– |
} finally { |
1766 |
– |
joinPool(e); |
1755 |
|
} |
1756 |
|
} |
1757 |
|
|
1759 |
|
* timed invokeAll(null) throws NPE |
1760 |
|
*/ |
1761 |
|
public void testTimedInvokeAll1() throws Exception { |
1762 |
< |
ExecutorService e = |
1762 |
> |
final ExecutorService e = |
1763 |
|
new ThreadPoolExecutor(2, 2, |
1764 |
|
LONG_DELAY_MS, MILLISECONDS, |
1765 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1766 |
< |
try { |
1767 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1768 |
< |
shouldThrow(); |
1769 |
< |
} catch (NullPointerException success) { |
1770 |
< |
} finally { |
1783 |
< |
joinPool(e); |
1766 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1767 |
> |
try { |
1768 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1769 |
> |
shouldThrow(); |
1770 |
> |
} catch (NullPointerException success) {} |
1771 |
|
} |
1772 |
|
} |
1773 |
|
|
1775 |
|
* timed invokeAll(,,null) throws NPE |
1776 |
|
*/ |
1777 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1778 |
< |
ExecutorService e = |
1778 |
> |
final ExecutorService e = |
1779 |
|
new ThreadPoolExecutor(2, 2, |
1780 |
|
LONG_DELAY_MS, MILLISECONDS, |
1781 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1782 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1783 |
< |
l.add(new StringTask()); |
1784 |
< |
try { |
1785 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1786 |
< |
shouldThrow(); |
1787 |
< |
} catch (NullPointerException success) { |
1788 |
< |
} finally { |
1802 |
< |
joinPool(e); |
1782 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1783 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1784 |
> |
l.add(new StringTask()); |
1785 |
> |
try { |
1786 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1787 |
> |
shouldThrow(); |
1788 |
> |
} catch (NullPointerException success) {} |
1789 |
|
} |
1790 |
|
} |
1791 |
|
|
1793 |
|
* timed invokeAll(empty collection) returns empty collection |
1794 |
|
*/ |
1795 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1796 |
< |
ExecutorService e = |
1796 |
> |
final ExecutorService e = |
1797 |
|
new ThreadPoolExecutor(2, 2, |
1798 |
|
LONG_DELAY_MS, MILLISECONDS, |
1799 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1800 |
< |
try { |
1801 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1800 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1801 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1802 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1803 |
|
assertTrue(r.isEmpty()); |
1817 |
– |
} finally { |
1818 |
– |
joinPool(e); |
1804 |
|
} |
1805 |
|
} |
1806 |
|
|
1808 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1809 |
|
*/ |
1810 |
|
public void testTimedInvokeAll3() throws Exception { |
1811 |
< |
ExecutorService e = |
1811 |
> |
final ExecutorService e = |
1812 |
|
new ThreadPoolExecutor(2, 2, |
1813 |
|
LONG_DELAY_MS, MILLISECONDS, |
1814 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1815 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1816 |
< |
l.add(new StringTask()); |
1817 |
< |
l.add(null); |
1818 |
< |
try { |
1819 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1820 |
< |
shouldThrow(); |
1821 |
< |
} catch (NullPointerException success) { |
1822 |
< |
} finally { |
1838 |
< |
joinPool(e); |
1815 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1816 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1817 |
> |
l.add(new StringTask()); |
1818 |
> |
l.add(null); |
1819 |
> |
try { |
1820 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1821 |
> |
shouldThrow(); |
1822 |
> |
} catch (NullPointerException success) {} |
1823 |
|
} |
1824 |
|
} |
1825 |
|
|
1827 |
|
* get of element of invokeAll(c) throws exception on failed task |
1828 |
|
*/ |
1829 |
|
public void testTimedInvokeAll4() throws Exception { |
1830 |
< |
ExecutorService e = |
1830 |
> |
final ExecutorService e = |
1831 |
|
new ThreadPoolExecutor(2, 2, |
1832 |
|
LONG_DELAY_MS, MILLISECONDS, |
1833 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1834 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1835 |
< |
l.add(new NPETask()); |
1836 |
< |
List<Future<String>> futures = |
1837 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1838 |
< |
assertEquals(1, futures.size()); |
1839 |
< |
try { |
1840 |
< |
futures.get(0).get(); |
1841 |
< |
shouldThrow(); |
1842 |
< |
} catch (ExecutionException success) { |
1843 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1844 |
< |
} finally { |
1845 |
< |
joinPool(e); |
1834 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1835 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1836 |
> |
l.add(new NPETask()); |
1837 |
> |
List<Future<String>> futures = |
1838 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1839 |
> |
assertEquals(1, futures.size()); |
1840 |
> |
try { |
1841 |
> |
futures.get(0).get(); |
1842 |
> |
shouldThrow(); |
1843 |
> |
} catch (ExecutionException success) { |
1844 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1845 |
> |
} |
1846 |
|
} |
1847 |
|
} |
1848 |
|
|
1850 |
|
* timed invokeAll(c) returns results of all completed tasks |
1851 |
|
*/ |
1852 |
|
public void testTimedInvokeAll5() throws Exception { |
1853 |
< |
ExecutorService e = |
1853 |
> |
final ExecutorService e = |
1854 |
|
new ThreadPoolExecutor(2, 2, |
1855 |
|
LONG_DELAY_MS, MILLISECONDS, |
1856 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1857 |
< |
try { |
1857 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1858 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1859 |
|
l.add(new StringTask()); |
1860 |
|
l.add(new StringTask()); |
1861 |
|
List<Future<String>> futures = |
1862 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1862 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1863 |
|
assertEquals(2, futures.size()); |
1864 |
|
for (Future<String> future : futures) |
1865 |
|
assertSame(TEST_STRING, future.get()); |
1882 |
– |
} finally { |
1883 |
– |
joinPool(e); |
1866 |
|
} |
1867 |
|
} |
1868 |
|
|
1870 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1871 |
|
*/ |
1872 |
|
public void testTimedInvokeAll6() throws Exception { |
1873 |
< |
ExecutorService e = |
1873 |
> |
final ExecutorService e = |
1874 |
|
new ThreadPoolExecutor(2, 2, |
1875 |
|
LONG_DELAY_MS, MILLISECONDS, |
1876 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1877 |
< |
try { |
1877 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1878 |
|
for (long timeout = timeoutMillis();;) { |
1879 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1880 |
< |
tasks.add(new StringTask()); |
1880 |
> |
tasks.add(new StringTask("0")); |
1881 |
|
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1882 |
< |
tasks.add(new StringTask()); |
1882 |
> |
tasks.add(new StringTask("2")); |
1883 |
|
long startTime = System.nanoTime(); |
1884 |
|
List<Future<String>> futures = |
1885 |
|
e.invokeAll(tasks, timeout, MILLISECONDS); |
1889 |
|
assertTrue(future.isDone()); |
1890 |
|
assertTrue(futures.get(1).isCancelled()); |
1891 |
|
try { |
1892 |
< |
assertEquals(TEST_STRING, futures.get(0).get()); |
1893 |
< |
assertEquals(TEST_STRING, futures.get(2).get()); |
1892 |
> |
assertEquals("0", futures.get(0).get()); |
1893 |
> |
assertEquals("2", futures.get(2).get()); |
1894 |
|
break; |
1895 |
|
} catch (CancellationException retryWithLongerTimeout) { |
1896 |
|
timeout *= 2; |
1898 |
|
fail("expected exactly one task to be cancelled"); |
1899 |
|
} |
1900 |
|
} |
1919 |
– |
} finally { |
1920 |
– |
joinPool(e); |
1901 |
|
} |
1902 |
|
} |
1903 |
|
|
1911 |
|
LONG_DELAY_MS, MILLISECONDS, |
1912 |
|
new LinkedBlockingQueue<Runnable>(), |
1913 |
|
new FailingThreadFactory()); |
1914 |
< |
try { |
1914 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1915 |
|
final int TASKS = 100; |
1916 |
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1917 |
|
for (int k = 0; k < TASKS; ++k) |
1920 |
|
done.countDown(); |
1921 |
|
}}); |
1922 |
|
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1943 |
– |
} finally { |
1944 |
– |
joinPool(e); |
1923 |
|
} |
1924 |
|
} |
1925 |
|
|
1931 |
|
new ThreadPoolExecutor(2, 2, |
1932 |
|
1000, MILLISECONDS, |
1933 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1934 |
< |
assertFalse(p.allowsCoreThreadTimeOut()); |
1935 |
< |
joinPool(p); |
1934 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1935 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1936 |
> |
} |
1937 |
|
} |
1938 |
|
|
1939 |
|
/** |
1940 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1941 |
|
*/ |
1942 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1943 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1943 |
> |
long keepAliveTime = timeoutMillis(); |
1944 |
|
final ThreadPoolExecutor p = |
1945 |
|
new ThreadPoolExecutor(2, 10, |
1946 |
< |
coreThreadTimeOut, MILLISECONDS, |
1946 |
> |
keepAliveTime, MILLISECONDS, |
1947 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1948 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1949 |
< |
try { |
1948 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1949 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1950 |
|
p.allowCoreThreadTimeOut(true); |
1951 |
|
p.execute(new CheckedRunnable() { |
1952 |
|
public void realRun() { |
1954 |
|
assertEquals(1, p.getPoolSize()); |
1955 |
|
}}); |
1956 |
|
await(threadStarted); |
1957 |
< |
delay(coreThreadTimeOut); |
1957 |
> |
delay(keepAliveTime); |
1958 |
|
long startTime = System.nanoTime(); |
1959 |
|
while (p.getPoolSize() > 0 |
1960 |
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1961 |
|
Thread.yield(); |
1962 |
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1963 |
|
assertEquals(0, p.getPoolSize()); |
1985 |
– |
} finally { |
1986 |
– |
joinPool(p); |
1964 |
|
} |
1965 |
|
} |
1966 |
|
|
1968 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1969 |
|
*/ |
1970 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1971 |
< |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1971 |
> |
long keepAliveTime = timeoutMillis(); |
1972 |
|
final ThreadPoolExecutor p = |
1973 |
|
new ThreadPoolExecutor(2, 10, |
1974 |
< |
coreThreadTimeOut, MILLISECONDS, |
1974 |
> |
keepAliveTime, MILLISECONDS, |
1975 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1976 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1977 |
< |
try { |
1976 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1977 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1978 |
|
p.allowCoreThreadTimeOut(false); |
1979 |
|
p.execute(new CheckedRunnable() { |
1980 |
|
public void realRun() throws InterruptedException { |
1981 |
|
threadStarted.countDown(); |
1982 |
|
assertTrue(p.getPoolSize() >= 1); |
1983 |
|
}}); |
1984 |
< |
delay(2 * coreThreadTimeOut); |
1984 |
> |
delay(2 * keepAliveTime); |
1985 |
|
assertTrue(p.getPoolSize() >= 1); |
2009 |
– |
} finally { |
2010 |
– |
joinPool(p); |
1986 |
|
} |
1987 |
|
} |
1988 |
|
|
2001 |
|
new ThreadPoolExecutor(1, 30, |
2002 |
|
60, SECONDS, |
2003 |
|
new ArrayBlockingQueue(30)); |
2004 |
< |
try { |
2004 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
2005 |
|
for (int i = 0; i < nTasks; ++i) { |
2006 |
|
for (;;) { |
2007 |
|
try { |
2013 |
|
} |
2014 |
|
// enough time to run all tasks |
2015 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2016 |
< |
} finally { |
2017 |
< |
joinPool(p); |
2016 |
> |
} |
2017 |
> |
} |
2018 |
> |
|
2019 |
> |
/** |
2020 |
> |
* get(cancelled task) throws CancellationException |
2021 |
> |
*/ |
2022 |
> |
public void testGet_cancelled() throws Exception { |
2023 |
> |
final CountDownLatch done = new CountDownLatch(1); |
2024 |
> |
final ExecutorService e = |
2025 |
> |
new ThreadPoolExecutor(1, 1, |
2026 |
> |
LONG_DELAY_MS, MILLISECONDS, |
2027 |
> |
new LinkedBlockingQueue<Runnable>()); |
2028 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2029 |
> |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2030 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
2031 |
> |
for (int i = 0; i < 2; i++) { |
2032 |
> |
Runnable r = new CheckedRunnable() { public void realRun() |
2033 |
> |
throws Throwable { |
2034 |
> |
blockerStarted.countDown(); |
2035 |
> |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2036 |
> |
}}; |
2037 |
> |
futures.add(e.submit(r)); |
2038 |
> |
} |
2039 |
> |
await(blockerStarted); |
2040 |
> |
for (Future<?> future : futures) future.cancel(false); |
2041 |
> |
for (Future<?> future : futures) { |
2042 |
> |
try { |
2043 |
> |
future.get(); |
2044 |
> |
shouldThrow(); |
2045 |
> |
} catch (CancellationException success) {} |
2046 |
> |
try { |
2047 |
> |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2048 |
> |
shouldThrow(); |
2049 |
> |
} catch (CancellationException success) {} |
2050 |
> |
assertTrue(future.isCancelled()); |
2051 |
> |
assertTrue(future.isDone()); |
2052 |
> |
} |
2053 |
|
} |
2054 |
|
} |
2055 |
|
|