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.atomic.*; |
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
> |
|
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
16 |
> |
import java.util.concurrent.BlockingQueue; |
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; |
22 |
> |
import java.util.concurrent.ExecutorService; |
23 |
> |
import java.util.concurrent.Future; |
24 |
> |
import java.util.concurrent.FutureTask; |
25 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
26 |
> |
import java.util.concurrent.RejectedExecutionException; |
27 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
28 |
> |
import java.util.concurrent.SynchronousQueue; |
29 |
> |
import java.util.concurrent.ThreadFactory; |
30 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
31 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
32 |
> |
|
33 |
> |
import junit.framework.Test; |
34 |
> |
import junit.framework.TestSuite; |
35 |
|
|
36 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
37 |
|
public static void main(String[] args) { |
38 |
< |
junit.textui.TestRunner.run(suite()); |
38 |
> |
main(suite(), args); |
39 |
|
} |
40 |
|
public static Test suite() { |
41 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
42 |
|
} |
43 |
|
|
44 |
|
static class ExtendedTPE extends ThreadPoolExecutor { |
45 |
< |
volatile boolean beforeCalled = false; |
46 |
< |
volatile boolean afterCalled = false; |
47 |
< |
volatile boolean terminatedCalled = false; |
45 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
46 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
47 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
48 |
> |
|
49 |
|
public ExtendedTPE() { |
50 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
51 |
|
} |
52 |
|
protected void beforeExecute(Thread t, Runnable r) { |
53 |
< |
beforeCalled = true; |
53 |
> |
beforeCalled.countDown(); |
54 |
|
} |
55 |
|
protected void afterExecute(Runnable r, Throwable t) { |
56 |
< |
afterCalled = true; |
56 |
> |
afterCalled.countDown(); |
57 |
|
} |
58 |
|
protected void terminated() { |
59 |
< |
terminatedCalled = true; |
59 |
> |
terminatedCalled.countDown(); |
60 |
> |
} |
61 |
> |
|
62 |
> |
public boolean beforeCalled() { |
63 |
> |
return beforeCalled.getCount() == 0; |
64 |
> |
} |
65 |
> |
public boolean afterCalled() { |
66 |
> |
return afterCalled.getCount() == 0; |
67 |
> |
} |
68 |
> |
public boolean terminatedCalled() { |
69 |
> |
return terminatedCalled.getCount() == 0; |
70 |
|
} |
71 |
|
} |
72 |
|
|
78 |
|
} |
79 |
|
} |
80 |
|
|
49 |
– |
|
81 |
|
/** |
82 |
|
* execute successfully executes a runnable |
83 |
|
*/ |
86 |
|
new ThreadPoolExecutor(1, 1, |
87 |
|
LONG_DELAY_MS, MILLISECONDS, |
88 |
|
new ArrayBlockingQueue<Runnable>(10)); |
89 |
< |
final CountDownLatch done = new CountDownLatch(1); |
90 |
< |
final Runnable task = new CheckedRunnable() { |
91 |
< |
public void realRun() { |
92 |
< |
done.countDown(); |
62 |
< |
}}; |
63 |
< |
try { |
89 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
90 |
> |
final CountDownLatch done = new CountDownLatch(1); |
91 |
> |
final Runnable task = new CheckedRunnable() { |
92 |
> |
public void realRun() { done.countDown(); }}; |
93 |
|
p.execute(task); |
94 |
< |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
66 |
< |
} finally { |
67 |
< |
joinPool(p); |
94 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
95 |
|
} |
96 |
|
} |
97 |
|
|
100 |
|
* thread becomes active |
101 |
|
*/ |
102 |
|
public void testGetActiveCount() throws InterruptedException { |
103 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
104 |
|
final ThreadPoolExecutor p = |
105 |
|
new ThreadPoolExecutor(2, 2, |
106 |
|
LONG_DELAY_MS, MILLISECONDS, |
107 |
|
new ArrayBlockingQueue<Runnable>(10)); |
108 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
109 |
< |
final CountDownLatch done = new CountDownLatch(1); |
82 |
< |
try { |
108 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
109 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
110 |
|
assertEquals(0, p.getActiveCount()); |
111 |
|
p.execute(new CheckedRunnable() { |
112 |
|
public void realRun() throws InterruptedException { |
113 |
|
threadStarted.countDown(); |
114 |
|
assertEquals(1, p.getActiveCount()); |
115 |
< |
done.await(); |
115 |
> |
await(done); |
116 |
|
}}); |
117 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
117 |
> |
await(threadStarted); |
118 |
|
assertEquals(1, p.getActiveCount()); |
92 |
– |
} finally { |
93 |
– |
done.countDown(); |
94 |
– |
joinPool(p); |
119 |
|
} |
120 |
|
} |
121 |
|
|
124 |
|
*/ |
125 |
|
public void testPrestartCoreThread() { |
126 |
|
final ThreadPoolExecutor p = |
127 |
< |
new ThreadPoolExecutor(2, 2, |
127 |
> |
new ThreadPoolExecutor(2, 6, |
128 |
|
LONG_DELAY_MS, MILLISECONDS, |
129 |
|
new ArrayBlockingQueue<Runnable>(10)); |
130 |
< |
assertEquals(0, p.getPoolSize()); |
131 |
< |
assertTrue(p.prestartCoreThread()); |
132 |
< |
assertEquals(1, p.getPoolSize()); |
133 |
< |
assertTrue(p.prestartCoreThread()); |
134 |
< |
assertEquals(2, p.getPoolSize()); |
135 |
< |
assertFalse(p.prestartCoreThread()); |
136 |
< |
assertEquals(2, p.getPoolSize()); |
137 |
< |
joinPool(p); |
130 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
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 |
> |
p.setCorePoolSize(4); |
139 |
> |
assertTrue(p.prestartCoreThread()); |
140 |
> |
assertEquals(3, p.getPoolSize()); |
141 |
> |
assertTrue(p.prestartCoreThread()); |
142 |
> |
assertEquals(4, p.getPoolSize()); |
143 |
> |
assertFalse(p.prestartCoreThread()); |
144 |
> |
assertEquals(4, p.getPoolSize()); |
145 |
> |
} |
146 |
|
} |
147 |
|
|
148 |
|
/** |
150 |
|
*/ |
151 |
|
public void testPrestartAllCoreThreads() { |
152 |
|
final ThreadPoolExecutor p = |
153 |
< |
new ThreadPoolExecutor(2, 2, |
153 |
> |
new ThreadPoolExecutor(2, 6, |
154 |
|
LONG_DELAY_MS, MILLISECONDS, |
155 |
|
new ArrayBlockingQueue<Runnable>(10)); |
156 |
< |
assertEquals(0, p.getPoolSize()); |
157 |
< |
p.prestartAllCoreThreads(); |
158 |
< |
assertEquals(2, p.getPoolSize()); |
159 |
< |
p.prestartAllCoreThreads(); |
160 |
< |
assertEquals(2, p.getPoolSize()); |
161 |
< |
joinPool(p); |
156 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
157 |
> |
assertEquals(0, p.getPoolSize()); |
158 |
> |
p.prestartAllCoreThreads(); |
159 |
> |
assertEquals(2, p.getPoolSize()); |
160 |
> |
p.prestartAllCoreThreads(); |
161 |
> |
assertEquals(2, p.getPoolSize()); |
162 |
> |
p.setCorePoolSize(4); |
163 |
> |
p.prestartAllCoreThreads(); |
164 |
> |
assertEquals(4, p.getPoolSize()); |
165 |
> |
p.prestartAllCoreThreads(); |
166 |
> |
assertEquals(4, p.getPoolSize()); |
167 |
> |
} |
168 |
|
} |
169 |
|
|
170 |
|
/** |
176 |
|
new ThreadPoolExecutor(2, 2, |
177 |
|
LONG_DELAY_MS, MILLISECONDS, |
178 |
|
new ArrayBlockingQueue<Runnable>(10)); |
179 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
180 |
< |
final CountDownLatch threadProceed = new CountDownLatch(1); |
181 |
< |
final CountDownLatch threadDone = new CountDownLatch(1); |
182 |
< |
try { |
179 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
180 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
181 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
182 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
183 |
|
assertEquals(0, p.getCompletedTaskCount()); |
184 |
|
p.execute(new CheckedRunnable() { |
185 |
|
public void realRun() throws InterruptedException { |
188 |
|
threadProceed.await(); |
189 |
|
threadDone.countDown(); |
190 |
|
}}); |
191 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
191 |
> |
await(threadStarted); |
192 |
|
assertEquals(0, p.getCompletedTaskCount()); |
193 |
|
threadProceed.countDown(); |
194 |
|
threadDone.await(); |
195 |
< |
Thread.sleep(SHORT_DELAY_MS); |
196 |
< |
assertEquals(1, p.getCompletedTaskCount()); |
197 |
< |
} finally { |
198 |
< |
joinPool(p); |
195 |
> |
long startTime = System.nanoTime(); |
196 |
> |
while (p.getCompletedTaskCount() != 1) { |
197 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
198 |
> |
fail("timed out"); |
199 |
> |
Thread.yield(); |
200 |
> |
} |
201 |
|
} |
202 |
|
} |
203 |
|
|
209 |
|
new ThreadPoolExecutor(1, 1, |
210 |
|
LONG_DELAY_MS, MILLISECONDS, |
211 |
|
new ArrayBlockingQueue<Runnable>(10)); |
212 |
< |
assertEquals(1, p.getCorePoolSize()); |
213 |
< |
joinPool(p); |
212 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
213 |
> |
assertEquals(1, p.getCorePoolSize()); |
214 |
> |
} |
215 |
|
} |
216 |
|
|
217 |
|
/** |
222 |
|
new ThreadPoolExecutor(2, 2, |
223 |
|
1000, MILLISECONDS, |
224 |
|
new ArrayBlockingQueue<Runnable>(10)); |
225 |
< |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
226 |
< |
joinPool(p); |
225 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
226 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
227 |
> |
} |
228 |
|
} |
229 |
|
|
188 |
– |
|
230 |
|
/** |
231 |
|
* getThreadFactory returns factory in constructor if not set |
232 |
|
*/ |
233 |
|
public void testGetThreadFactory() { |
234 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
234 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
235 |
|
final ThreadPoolExecutor p = |
236 |
|
new ThreadPoolExecutor(1, 2, |
237 |
|
LONG_DELAY_MS, MILLISECONDS, |
238 |
|
new ArrayBlockingQueue<Runnable>(10), |
239 |
< |
tf, |
239 |
> |
threadFactory, |
240 |
|
new NoOpREHandler()); |
241 |
< |
assertSame(tf, p.getThreadFactory()); |
242 |
< |
joinPool(p); |
241 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
242 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
243 |
> |
} |
244 |
|
} |
245 |
|
|
246 |
|
/** |
251 |
|
new ThreadPoolExecutor(1, 2, |
252 |
|
LONG_DELAY_MS, MILLISECONDS, |
253 |
|
new ArrayBlockingQueue<Runnable>(10)); |
254 |
< |
ThreadFactory tf = new SimpleThreadFactory(); |
255 |
< |
p.setThreadFactory(tf); |
256 |
< |
assertSame(tf, p.getThreadFactory()); |
257 |
< |
joinPool(p); |
254 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
255 |
> |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
256 |
> |
p.setThreadFactory(threadFactory); |
257 |
> |
assertSame(threadFactory, p.getThreadFactory()); |
258 |
> |
} |
259 |
|
} |
260 |
|
|
218 |
– |
|
261 |
|
/** |
262 |
|
* setThreadFactory(null) throws NPE |
263 |
|
*/ |
266 |
|
new ThreadPoolExecutor(1, 2, |
267 |
|
LONG_DELAY_MS, MILLISECONDS, |
268 |
|
new ArrayBlockingQueue<Runnable>(10)); |
269 |
< |
try { |
270 |
< |
p.setThreadFactory(null); |
271 |
< |
shouldThrow(); |
272 |
< |
} catch (NullPointerException success) { |
273 |
< |
} finally { |
232 |
< |
joinPool(p); |
269 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
270 |
> |
try { |
271 |
> |
p.setThreadFactory(null); |
272 |
> |
shouldThrow(); |
273 |
> |
} catch (NullPointerException success) {} |
274 |
|
} |
275 |
|
} |
276 |
|
|
278 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
279 |
|
*/ |
280 |
|
public void testGetRejectedExecutionHandler() { |
281 |
< |
final RejectedExecutionHandler h = new NoOpREHandler(); |
281 |
> |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
282 |
|
final ThreadPoolExecutor p = |
283 |
|
new ThreadPoolExecutor(1, 2, |
284 |
|
LONG_DELAY_MS, MILLISECONDS, |
285 |
|
new ArrayBlockingQueue<Runnable>(10), |
286 |
< |
h); |
287 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
288 |
< |
joinPool(p); |
286 |
> |
handler); |
287 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
288 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
289 |
> |
} |
290 |
|
} |
291 |
|
|
292 |
|
/** |
298 |
|
new ThreadPoolExecutor(1, 2, |
299 |
|
LONG_DELAY_MS, MILLISECONDS, |
300 |
|
new ArrayBlockingQueue<Runnable>(10)); |
301 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
302 |
< |
p.setRejectedExecutionHandler(h); |
303 |
< |
assertSame(h, p.getRejectedExecutionHandler()); |
304 |
< |
joinPool(p); |
301 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
302 |
> |
RejectedExecutionHandler handler = new NoOpREHandler(); |
303 |
> |
p.setRejectedExecutionHandler(handler); |
304 |
> |
assertSame(handler, p.getRejectedExecutionHandler()); |
305 |
> |
} |
306 |
|
} |
307 |
|
|
265 |
– |
|
308 |
|
/** |
309 |
|
* setRejectedExecutionHandler(null) throws NPE |
310 |
|
*/ |
313 |
|
new ThreadPoolExecutor(1, 2, |
314 |
|
LONG_DELAY_MS, MILLISECONDS, |
315 |
|
new ArrayBlockingQueue<Runnable>(10)); |
316 |
< |
try { |
317 |
< |
p.setRejectedExecutionHandler(null); |
318 |
< |
shouldThrow(); |
319 |
< |
} catch (NullPointerException success) { |
320 |
< |
} finally { |
279 |
< |
joinPool(p); |
316 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
317 |
> |
try { |
318 |
> |
p.setRejectedExecutionHandler(null); |
319 |
> |
shouldThrow(); |
320 |
> |
} catch (NullPointerException success) {} |
321 |
|
} |
322 |
|
} |
323 |
|
|
283 |
– |
|
324 |
|
/** |
325 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
326 |
|
* multiple threads active |
327 |
|
*/ |
328 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
329 |
|
final int THREADS = 3; |
330 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
331 |
|
final ThreadPoolExecutor p = |
332 |
|
new ThreadPoolExecutor(THREADS, THREADS, |
333 |
|
LONG_DELAY_MS, MILLISECONDS, |
334 |
|
new ArrayBlockingQueue<Runnable>(10)); |
335 |
< |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
295 |
< |
final CountDownLatch done = new CountDownLatch(1); |
296 |
< |
try { |
335 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
336 |
|
assertEquals(0, p.getLargestPoolSize()); |
337 |
+ |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
338 |
|
for (int i = 0; i < THREADS; i++) |
339 |
|
p.execute(new CheckedRunnable() { |
340 |
|
public void realRun() throws InterruptedException { |
341 |
|
threadsStarted.countDown(); |
342 |
< |
done.await(); |
342 |
> |
await(done); |
343 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
344 |
|
}}); |
345 |
< |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
306 |
< |
assertEquals(THREADS, p.getLargestPoolSize()); |
307 |
< |
} finally { |
308 |
< |
done.countDown(); |
309 |
< |
joinPool(p); |
345 |
> |
await(threadsStarted); |
346 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
347 |
|
} |
348 |
+ |
assertEquals(THREADS, p.getLargestPoolSize()); |
349 |
|
} |
350 |
|
|
351 |
|
/** |
357 |
|
new ThreadPoolExecutor(2, 3, |
358 |
|
LONG_DELAY_MS, MILLISECONDS, |
359 |
|
new ArrayBlockingQueue<Runnable>(10)); |
360 |
< |
assertEquals(3, p.getMaximumPoolSize()); |
361 |
< |
joinPool(p); |
360 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
361 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
362 |
> |
p.setMaximumPoolSize(5); |
363 |
> |
assertEquals(5, p.getMaximumPoolSize()); |
364 |
> |
p.setMaximumPoolSize(4); |
365 |
> |
assertEquals(4, p.getMaximumPoolSize()); |
366 |
> |
} |
367 |
|
} |
368 |
|
|
369 |
|
/** |
371 |
|
* become active |
372 |
|
*/ |
373 |
|
public void testGetPoolSize() throws InterruptedException { |
374 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
375 |
|
final ThreadPoolExecutor p = |
376 |
|
new ThreadPoolExecutor(1, 1, |
377 |
|
LONG_DELAY_MS, MILLISECONDS, |
378 |
|
new ArrayBlockingQueue<Runnable>(10)); |
379 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
337 |
< |
final CountDownLatch done = new CountDownLatch(1); |
338 |
< |
try { |
379 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
380 |
|
assertEquals(0, p.getPoolSize()); |
381 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
382 |
|
p.execute(new CheckedRunnable() { |
383 |
|
public void realRun() throws InterruptedException { |
384 |
|
threadStarted.countDown(); |
385 |
|
assertEquals(1, p.getPoolSize()); |
386 |
< |
done.await(); |
386 |
> |
await(done); |
387 |
|
}}); |
388 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
388 |
> |
await(threadStarted); |
389 |
|
assertEquals(1, p.getPoolSize()); |
348 |
– |
} finally { |
349 |
– |
done.countDown(); |
350 |
– |
joinPool(p); |
390 |
|
} |
391 |
|
} |
392 |
|
|
394 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
395 |
|
*/ |
396 |
|
public void testGetTaskCount() throws InterruptedException { |
397 |
+ |
final int TASKS = 3; |
398 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
399 |
|
final ThreadPoolExecutor p = |
400 |
|
new ThreadPoolExecutor(1, 1, |
401 |
|
LONG_DELAY_MS, MILLISECONDS, |
402 |
|
new ArrayBlockingQueue<Runnable>(10)); |
403 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
404 |
< |
final CountDownLatch done = new CountDownLatch(1); |
364 |
< |
try { |
403 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
404 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
405 |
|
assertEquals(0, p.getTaskCount()); |
406 |
+ |
assertEquals(0, p.getCompletedTaskCount()); |
407 |
|
p.execute(new CheckedRunnable() { |
408 |
|
public void realRun() throws InterruptedException { |
409 |
|
threadStarted.countDown(); |
410 |
< |
assertEquals(1, p.getTaskCount()); |
370 |
< |
done.await(); |
410 |
> |
await(done); |
411 |
|
}}); |
412 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
412 |
> |
await(threadStarted); |
413 |
|
assertEquals(1, p.getTaskCount()); |
414 |
< |
} finally { |
415 |
< |
done.countDown(); |
416 |
< |
joinPool(p); |
414 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
415 |
> |
for (int i = 0; i < TASKS; i++) { |
416 |
> |
assertEquals(1 + i, p.getTaskCount()); |
417 |
> |
p.execute(new CheckedRunnable() { |
418 |
> |
public void realRun() throws InterruptedException { |
419 |
> |
threadStarted.countDown(); |
420 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
421 |
> |
await(done); |
422 |
> |
}}); |
423 |
> |
} |
424 |
> |
assertEquals(1 + TASKS, p.getTaskCount()); |
425 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
426 |
|
} |
427 |
+ |
assertEquals(1 + TASKS, p.getTaskCount()); |
428 |
+ |
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
429 |
|
} |
430 |
|
|
431 |
|
/** |
432 |
< |
* isShutDown is false before shutdown, true after |
432 |
> |
* isShutdown is false before shutdown, true after |
433 |
|
*/ |
434 |
|
public void testIsShutdown() { |
435 |
|
final ThreadPoolExecutor p = |
436 |
|
new ThreadPoolExecutor(1, 1, |
437 |
|
LONG_DELAY_MS, MILLISECONDS, |
438 |
|
new ArrayBlockingQueue<Runnable>(10)); |
439 |
< |
assertFalse(p.isShutdown()); |
440 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
441 |
< |
assertTrue(p.isShutdown()); |
442 |
< |
joinPool(p); |
439 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
440 |
> |
assertFalse(p.isShutdown()); |
441 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
442 |
> |
assertTrue(p.isShutdown()); |
443 |
> |
} |
444 |
|
} |
445 |
|
|
446 |
+ |
/** |
447 |
+ |
* awaitTermination on a non-shutdown pool times out |
448 |
+ |
*/ |
449 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
450 |
+ |
final ThreadPoolExecutor p = |
451 |
+ |
new ThreadPoolExecutor(1, 1, |
452 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
453 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
454 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
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 |
+ |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
473 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
474 |
+ |
assertTrue(p.isTerminated()); |
475 |
+ |
} |
476 |
+ |
} |
477 |
|
|
478 |
|
/** |
479 |
|
* isTerminated is false before termination, true after |
483 |
|
new ThreadPoolExecutor(1, 1, |
484 |
|
LONG_DELAY_MS, MILLISECONDS, |
485 |
|
new ArrayBlockingQueue<Runnable>(10)); |
486 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
487 |
< |
final CountDownLatch done = new CountDownLatch(1); |
488 |
< |
assertFalse(p.isTerminated()); |
489 |
< |
try { |
486 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
487 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
488 |
> |
final CountDownLatch done = new CountDownLatch(1); |
489 |
> |
assertFalse(p.isTerminating()); |
490 |
|
p.execute(new CheckedRunnable() { |
491 |
|
public void realRun() throws InterruptedException { |
492 |
< |
assertFalse(p.isTerminated()); |
492 |
> |
assertFalse(p.isTerminating()); |
493 |
|
threadStarted.countDown(); |
494 |
< |
done.await(); |
494 |
> |
await(done); |
495 |
|
}}); |
496 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
496 |
> |
await(threadStarted); |
497 |
|
assertFalse(p.isTerminating()); |
498 |
|
done.countDown(); |
416 |
– |
} finally { |
499 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
500 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
501 |
+ |
assertTrue(p.isTerminated()); |
502 |
+ |
assertFalse(p.isTerminating()); |
503 |
|
} |
419 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
420 |
– |
assertTrue(p.isTerminated()); |
504 |
|
} |
505 |
|
|
506 |
|
/** |
511 |
|
new ThreadPoolExecutor(1, 1, |
512 |
|
LONG_DELAY_MS, MILLISECONDS, |
513 |
|
new ArrayBlockingQueue<Runnable>(10)); |
514 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
515 |
< |
final CountDownLatch done = new CountDownLatch(1); |
516 |
< |
try { |
514 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
515 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
516 |
> |
final CountDownLatch done = new CountDownLatch(1); |
517 |
|
assertFalse(p.isTerminating()); |
518 |
|
p.execute(new CheckedRunnable() { |
519 |
|
public void realRun() throws InterruptedException { |
520 |
|
assertFalse(p.isTerminating()); |
521 |
|
threadStarted.countDown(); |
522 |
< |
done.await(); |
522 |
> |
await(done); |
523 |
|
}}); |
524 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
524 |
> |
await(threadStarted); |
525 |
|
assertFalse(p.isTerminating()); |
526 |
|
done.countDown(); |
444 |
– |
} finally { |
527 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
528 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
529 |
+ |
assertTrue(p.isTerminated()); |
530 |
+ |
assertFalse(p.isTerminating()); |
531 |
|
} |
447 |
– |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
448 |
– |
assertTrue(p.isTerminated()); |
449 |
– |
assertFalse(p.isTerminating()); |
532 |
|
} |
533 |
|
|
534 |
|
/** |
535 |
|
* getQueue returns the work queue, which contains queued tasks |
536 |
|
*/ |
537 |
|
public void testGetQueue() throws InterruptedException { |
538 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
538 |
> |
final CountDownLatch done = new CountDownLatch(1); |
539 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
540 |
|
final ThreadPoolExecutor p = |
541 |
|
new ThreadPoolExecutor(1, 1, |
542 |
|
LONG_DELAY_MS, MILLISECONDS, |
543 |
|
q); |
544 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
545 |
< |
final CountDownLatch done = new CountDownLatch(1); |
463 |
< |
try { |
544 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
545 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
546 |
|
FutureTask[] tasks = new FutureTask[5]; |
547 |
|
for (int i = 0; i < tasks.length; i++) { |
548 |
|
Callable task = new CheckedCallable<Boolean>() { |
549 |
|
public Boolean realCall() throws InterruptedException { |
550 |
|
threadStarted.countDown(); |
551 |
|
assertSame(q, p.getQueue()); |
552 |
< |
done.await(); |
552 |
> |
await(done); |
553 |
|
return Boolean.TRUE; |
554 |
|
}}; |
555 |
|
tasks[i] = new FutureTask(task); |
556 |
|
p.execute(tasks[i]); |
557 |
|
} |
558 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
558 |
> |
await(threadStarted); |
559 |
|
assertSame(q, p.getQueue()); |
560 |
|
assertFalse(q.contains(tasks[0])); |
561 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
562 |
|
assertEquals(tasks.length - 1, q.size()); |
481 |
– |
} finally { |
482 |
– |
done.countDown(); |
483 |
– |
joinPool(p); |
563 |
|
} |
564 |
|
} |
565 |
|
|
567 |
|
* remove(task) removes queued task, and fails to remove active task |
568 |
|
*/ |
569 |
|
public void testRemove() throws InterruptedException { |
570 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
570 |
> |
final CountDownLatch done = new CountDownLatch(1); |
571 |
> |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
572 |
|
final ThreadPoolExecutor p = |
573 |
|
new ThreadPoolExecutor(1, 1, |
574 |
|
LONG_DELAY_MS, MILLISECONDS, |
575 |
|
q); |
576 |
< |
Runnable[] tasks = new Runnable[5]; |
577 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
578 |
< |
final CountDownLatch done = new CountDownLatch(1); |
499 |
< |
try { |
576 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
577 |
> |
Runnable[] tasks = new Runnable[6]; |
578 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
579 |
|
for (int i = 0; i < tasks.length; i++) { |
580 |
|
tasks[i] = new CheckedRunnable() { |
581 |
|
public void realRun() throws InterruptedException { |
582 |
|
threadStarted.countDown(); |
583 |
< |
done.await(); |
583 |
> |
await(done); |
584 |
|
}}; |
585 |
|
p.execute(tasks[i]); |
586 |
|
} |
587 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
587 |
> |
await(threadStarted); |
588 |
|
assertFalse(p.remove(tasks[0])); |
589 |
|
assertTrue(q.contains(tasks[4])); |
590 |
|
assertTrue(q.contains(tasks[3])); |
594 |
|
assertTrue(q.contains(tasks[3])); |
595 |
|
assertTrue(p.remove(tasks[3])); |
596 |
|
assertFalse(q.contains(tasks[3])); |
518 |
– |
} finally { |
519 |
– |
done.countDown(); |
520 |
– |
joinPool(p); |
597 |
|
} |
598 |
|
} |
599 |
|
|
603 |
|
public void testPurge() throws InterruptedException { |
604 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
605 |
|
final CountDownLatch done = new CountDownLatch(1); |
606 |
< |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
606 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
607 |
|
final ThreadPoolExecutor p = |
608 |
|
new ThreadPoolExecutor(1, 1, |
609 |
|
LONG_DELAY_MS, MILLISECONDS, |
610 |
|
q); |
611 |
< |
FutureTask[] tasks = new FutureTask[5]; |
612 |
< |
try { |
611 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
612 |
> |
FutureTask[] tasks = new FutureTask[5]; |
613 |
|
for (int i = 0; i < tasks.length; i++) { |
614 |
|
Callable task = new CheckedCallable<Boolean>() { |
615 |
|
public Boolean realCall() throws InterruptedException { |
616 |
|
threadStarted.countDown(); |
617 |
< |
done.await(); |
617 |
> |
await(done); |
618 |
|
return Boolean.TRUE; |
619 |
|
}}; |
620 |
|
tasks[i] = new FutureTask(task); |
621 |
|
p.execute(tasks[i]); |
622 |
|
} |
623 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
623 |
> |
await(threadStarted); |
624 |
|
assertEquals(tasks.length, p.getTaskCount()); |
625 |
|
assertEquals(tasks.length - 1, q.size()); |
626 |
|
assertEquals(1L, p.getActiveCount()); |
633 |
|
p.purge(); // Nothing to do |
634 |
|
assertEquals(tasks.length - 3, q.size()); |
635 |
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
560 |
– |
} finally { |
561 |
– |
done.countDown(); |
562 |
– |
joinPool(p); |
636 |
|
} |
637 |
|
} |
638 |
|
|
639 |
|
/** |
640 |
< |
* shutDownNow returns a list containing tasks that were not run |
640 |
> |
* shutdownNow returns a list containing tasks that were not run, |
641 |
> |
* and those tasks are drained from the queue |
642 |
|
*/ |
643 |
< |
public void testShutDownNow() { |
643 |
> |
public void testShutdownNow() throws InterruptedException { |
644 |
> |
final int poolSize = 2; |
645 |
> |
final int count = 5; |
646 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
647 |
|
final ThreadPoolExecutor p = |
648 |
< |
new ThreadPoolExecutor(1, 1, |
648 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
649 |
|
LONG_DELAY_MS, MILLISECONDS, |
650 |
|
new ArrayBlockingQueue<Runnable>(10)); |
651 |
< |
List l; |
652 |
< |
try { |
653 |
< |
for (int i = 0; i < 5; i++) |
577 |
< |
p.execute(new MediumPossiblyInterruptedRunnable()); |
578 |
< |
} |
579 |
< |
finally { |
651 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
652 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
653 |
> |
threadsStarted.countDown(); |
654 |
|
try { |
655 |
< |
l = p.shutdownNow(); |
656 |
< |
} catch (SecurityException ok) { return; } |
655 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
656 |
> |
} catch (InterruptedException success) {} |
657 |
> |
ran.getAndIncrement(); |
658 |
> |
}}; |
659 |
> |
for (int i = 0; i < count; i++) |
660 |
> |
p.execute(waiter); |
661 |
> |
await(threadsStarted); |
662 |
> |
assertEquals(poolSize, p.getActiveCount()); |
663 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
664 |
> |
final List<Runnable> queuedTasks; |
665 |
> |
try { |
666 |
> |
queuedTasks = p.shutdownNow(); |
667 |
> |
} catch (SecurityException ok) { |
668 |
> |
return; // Allowed in case test doesn't have privs |
669 |
|
} |
670 |
|
assertTrue(p.isShutdown()); |
671 |
< |
assertTrue(l.size() <= 4); |
671 |
> |
assertTrue(p.getQueue().isEmpty()); |
672 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
673 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
674 |
> |
assertTrue(p.isTerminated()); |
675 |
> |
assertEquals(poolSize, ran.get()); |
676 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
677 |
|
} |
678 |
|
|
679 |
|
// Exception Tests |
680 |
|
|
590 |
– |
|
681 |
|
/** |
682 |
|
* Constructor throws if corePoolSize argument is less than zero |
683 |
|
*/ |
684 |
|
public void testConstructor1() { |
685 |
|
try { |
686 |
< |
new ThreadPoolExecutor(-1, 1, |
597 |
< |
LONG_DELAY_MS, MILLISECONDS, |
686 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
687 |
|
new ArrayBlockingQueue<Runnable>(10)); |
688 |
|
shouldThrow(); |
689 |
|
} catch (IllegalArgumentException success) {} |
694 |
|
*/ |
695 |
|
public void testConstructor2() { |
696 |
|
try { |
697 |
< |
new ThreadPoolExecutor(1, -1, |
609 |
< |
LONG_DELAY_MS, MILLISECONDS, |
697 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
698 |
|
new ArrayBlockingQueue<Runnable>(10)); |
699 |
|
shouldThrow(); |
700 |
|
} catch (IllegalArgumentException success) {} |
705 |
|
*/ |
706 |
|
public void testConstructor3() { |
707 |
|
try { |
708 |
< |
new ThreadPoolExecutor(1, 0, |
621 |
< |
LONG_DELAY_MS, MILLISECONDS, |
708 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
709 |
|
new ArrayBlockingQueue<Runnable>(10)); |
710 |
|
shouldThrow(); |
711 |
|
} catch (IllegalArgumentException success) {} |
716 |
|
*/ |
717 |
|
public void testConstructor4() { |
718 |
|
try { |
719 |
< |
new ThreadPoolExecutor(1, 2, |
633 |
< |
-1L, MILLISECONDS, |
719 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
720 |
|
new ArrayBlockingQueue<Runnable>(10)); |
721 |
|
shouldThrow(); |
722 |
|
} catch (IllegalArgumentException success) {} |
727 |
|
*/ |
728 |
|
public void testConstructor5() { |
729 |
|
try { |
730 |
< |
new ThreadPoolExecutor(2, 1, |
645 |
< |
LONG_DELAY_MS, MILLISECONDS, |
730 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
731 |
|
new ArrayBlockingQueue<Runnable>(10)); |
732 |
|
shouldThrow(); |
733 |
|
} catch (IllegalArgumentException success) {} |
738 |
|
*/ |
739 |
|
public void testConstructorNullPointerException() { |
740 |
|
try { |
741 |
< |
new ThreadPoolExecutor(1, 2, |
657 |
< |
LONG_DELAY_MS, MILLISECONDS, |
741 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
742 |
|
(BlockingQueue) null); |
743 |
|
shouldThrow(); |
744 |
|
} catch (NullPointerException success) {} |
745 |
|
} |
746 |
|
|
663 |
– |
|
664 |
– |
|
747 |
|
/** |
748 |
|
* Constructor throws if corePoolSize argument is less than zero |
749 |
|
*/ |
750 |
|
public void testConstructor6() { |
751 |
|
try { |
752 |
< |
new ThreadPoolExecutor(-1, 1, |
671 |
< |
LONG_DELAY_MS, MILLISECONDS, |
752 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
753 |
|
new ArrayBlockingQueue<Runnable>(10), |
754 |
|
new SimpleThreadFactory()); |
755 |
|
shouldThrow(); |
761 |
|
*/ |
762 |
|
public void testConstructor7() { |
763 |
|
try { |
764 |
< |
new ThreadPoolExecutor(1, -1, |
684 |
< |
LONG_DELAY_MS, MILLISECONDS, |
764 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
765 |
|
new ArrayBlockingQueue<Runnable>(10), |
766 |
|
new SimpleThreadFactory()); |
767 |
|
shouldThrow(); |
773 |
|
*/ |
774 |
|
public void testConstructor8() { |
775 |
|
try { |
776 |
< |
new ThreadPoolExecutor(1, 0, |
697 |
< |
LONG_DELAY_MS, MILLISECONDS, |
776 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
777 |
|
new ArrayBlockingQueue<Runnable>(10), |
778 |
|
new SimpleThreadFactory()); |
779 |
|
shouldThrow(); |
785 |
|
*/ |
786 |
|
public void testConstructor9() { |
787 |
|
try { |
788 |
< |
new ThreadPoolExecutor(1, 2, |
710 |
< |
-1L, MILLISECONDS, |
788 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
789 |
|
new ArrayBlockingQueue<Runnable>(10), |
790 |
|
new SimpleThreadFactory()); |
791 |
|
shouldThrow(); |
797 |
|
*/ |
798 |
|
public void testConstructor10() { |
799 |
|
try { |
800 |
< |
new ThreadPoolExecutor(2, 1, |
723 |
< |
LONG_DELAY_MS, MILLISECONDS, |
800 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
801 |
|
new ArrayBlockingQueue<Runnable>(10), |
802 |
|
new SimpleThreadFactory()); |
803 |
|
shouldThrow(); |
809 |
|
*/ |
810 |
|
public void testConstructorNullPointerException2() { |
811 |
|
try { |
812 |
< |
new ThreadPoolExecutor(1, 2, |
736 |
< |
LONG_DELAY_MS, MILLISECONDS, |
812 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
813 |
|
(BlockingQueue) null, |
814 |
|
new SimpleThreadFactory()); |
815 |
|
shouldThrow(); |
821 |
|
*/ |
822 |
|
public void testConstructorNullPointerException3() { |
823 |
|
try { |
824 |
< |
new ThreadPoolExecutor(1, 2, |
749 |
< |
LONG_DELAY_MS, MILLISECONDS, |
824 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
825 |
|
new ArrayBlockingQueue<Runnable>(10), |
826 |
|
(ThreadFactory) null); |
827 |
|
shouldThrow(); |
828 |
|
} catch (NullPointerException success) {} |
829 |
|
} |
830 |
|
|
756 |
– |
|
831 |
|
/** |
832 |
|
* Constructor throws if corePoolSize argument is less than zero |
833 |
|
*/ |
834 |
|
public void testConstructor11() { |
835 |
|
try { |
836 |
< |
new ThreadPoolExecutor(-1, 1, |
763 |
< |
LONG_DELAY_MS, MILLISECONDS, |
836 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
837 |
|
new ArrayBlockingQueue<Runnable>(10), |
838 |
|
new NoOpREHandler()); |
839 |
|
shouldThrow(); |
845 |
|
*/ |
846 |
|
public void testConstructor12() { |
847 |
|
try { |
848 |
< |
new ThreadPoolExecutor(1, -1, |
776 |
< |
LONG_DELAY_MS, MILLISECONDS, |
848 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
849 |
|
new ArrayBlockingQueue<Runnable>(10), |
850 |
|
new NoOpREHandler()); |
851 |
|
shouldThrow(); |
857 |
|
*/ |
858 |
|
public void testConstructor13() { |
859 |
|
try { |
860 |
< |
new ThreadPoolExecutor(1, 0, |
789 |
< |
LONG_DELAY_MS, MILLISECONDS, |
860 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
861 |
|
new ArrayBlockingQueue<Runnable>(10), |
862 |
|
new NoOpREHandler()); |
863 |
|
shouldThrow(); |
869 |
|
*/ |
870 |
|
public void testConstructor14() { |
871 |
|
try { |
872 |
< |
new ThreadPoolExecutor(1, 2, |
802 |
< |
-1L, MILLISECONDS, |
872 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
873 |
|
new ArrayBlockingQueue<Runnable>(10), |
874 |
|
new NoOpREHandler()); |
875 |
|
shouldThrow(); |
881 |
|
*/ |
882 |
|
public void testConstructor15() { |
883 |
|
try { |
884 |
< |
new ThreadPoolExecutor(2, 1, |
815 |
< |
LONG_DELAY_MS, MILLISECONDS, |
884 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
885 |
|
new ArrayBlockingQueue<Runnable>(10), |
886 |
|
new NoOpREHandler()); |
887 |
|
shouldThrow(); |
893 |
|
*/ |
894 |
|
public void testConstructorNullPointerException4() { |
895 |
|
try { |
896 |
< |
new ThreadPoolExecutor(1, 2, |
828 |
< |
LONG_DELAY_MS, MILLISECONDS, |
896 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
897 |
|
(BlockingQueue) null, |
898 |
|
new NoOpREHandler()); |
899 |
|
shouldThrow(); |
905 |
|
*/ |
906 |
|
public void testConstructorNullPointerException5() { |
907 |
|
try { |
908 |
< |
new ThreadPoolExecutor(1, 2, |
841 |
< |
LONG_DELAY_MS, MILLISECONDS, |
908 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
909 |
|
new ArrayBlockingQueue<Runnable>(10), |
910 |
|
(RejectedExecutionHandler) null); |
911 |
|
shouldThrow(); |
912 |
|
} catch (NullPointerException success) {} |
913 |
|
} |
914 |
|
|
848 |
– |
|
915 |
|
/** |
916 |
|
* Constructor throws if corePoolSize argument is less than zero |
917 |
|
*/ |
918 |
|
public void testConstructor16() { |
919 |
|
try { |
920 |
< |
new ThreadPoolExecutor(-1, 1, |
855 |
< |
LONG_DELAY_MS, MILLISECONDS, |
920 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
921 |
|
new ArrayBlockingQueue<Runnable>(10), |
922 |
|
new SimpleThreadFactory(), |
923 |
|
new NoOpREHandler()); |
930 |
|
*/ |
931 |
|
public void testConstructor17() { |
932 |
|
try { |
933 |
< |
new ThreadPoolExecutor(1, -1, |
869 |
< |
LONG_DELAY_MS, MILLISECONDS, |
933 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
934 |
|
new ArrayBlockingQueue<Runnable>(10), |
935 |
|
new SimpleThreadFactory(), |
936 |
|
new NoOpREHandler()); |
943 |
|
*/ |
944 |
|
public void testConstructor18() { |
945 |
|
try { |
946 |
< |
new ThreadPoolExecutor(1, 0, |
883 |
< |
LONG_DELAY_MS, MILLISECONDS, |
946 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
947 |
|
new ArrayBlockingQueue<Runnable>(10), |
948 |
|
new SimpleThreadFactory(), |
949 |
|
new NoOpREHandler()); |
956 |
|
*/ |
957 |
|
public void testConstructor19() { |
958 |
|
try { |
959 |
< |
new ThreadPoolExecutor(1, 2, |
897 |
< |
-1L, MILLISECONDS, |
959 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
960 |
|
new ArrayBlockingQueue<Runnable>(10), |
961 |
|
new SimpleThreadFactory(), |
962 |
|
new NoOpREHandler()); |
969 |
|
*/ |
970 |
|
public void testConstructor20() { |
971 |
|
try { |
972 |
< |
new ThreadPoolExecutor(2, 1, |
911 |
< |
LONG_DELAY_MS, MILLISECONDS, |
972 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
973 |
|
new ArrayBlockingQueue<Runnable>(10), |
974 |
|
new SimpleThreadFactory(), |
975 |
|
new NoOpREHandler()); |
982 |
|
*/ |
983 |
|
public void testConstructorNullPointerException6() { |
984 |
|
try { |
985 |
< |
new ThreadPoolExecutor(1, 2, |
925 |
< |
LONG_DELAY_MS, MILLISECONDS, |
985 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
986 |
|
(BlockingQueue) null, |
987 |
|
new SimpleThreadFactory(), |
988 |
|
new NoOpREHandler()); |
995 |
|
*/ |
996 |
|
public void testConstructorNullPointerException7() { |
997 |
|
try { |
998 |
< |
new ThreadPoolExecutor(1, 2, |
939 |
< |
LONG_DELAY_MS, MILLISECONDS, |
998 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
999 |
|
new ArrayBlockingQueue<Runnable>(10), |
1000 |
|
new SimpleThreadFactory(), |
1001 |
|
(RejectedExecutionHandler) null); |
1008 |
|
*/ |
1009 |
|
public void testConstructorNullPointerException8() { |
1010 |
|
try { |
1011 |
< |
new ThreadPoolExecutor(1, 2, |
953 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1011 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1012 |
|
new ArrayBlockingQueue<Runnable>(10), |
1013 |
|
(ThreadFactory) null, |
1014 |
|
new NoOpREHandler()); |
1020 |
|
* get of submitted callable throws InterruptedException if interrupted |
1021 |
|
*/ |
1022 |
|
public void testInterruptedSubmit() throws InterruptedException { |
1023 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1024 |
|
final ThreadPoolExecutor p = |
1025 |
|
new ThreadPoolExecutor(1, 1, |
1026 |
< |
60, TimeUnit.SECONDS, |
1026 |
> |
60, SECONDS, |
1027 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1028 |
|
|
1029 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1030 |
< |
final CountDownLatch done = new CountDownLatch(1); |
972 |
< |
try { |
1029 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1030 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1031 |
|
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1032 |
|
public void realRun() throws Exception { |
1033 |
|
Callable task = new CheckedCallable<Boolean>() { |
1034 |
|
public Boolean realCall() throws InterruptedException { |
1035 |
|
threadStarted.countDown(); |
1036 |
< |
done.await(); |
1036 |
> |
await(done); |
1037 |
|
return Boolean.TRUE; |
1038 |
|
}}; |
1039 |
|
p.submit(task).get(); |
1040 |
|
}}); |
1041 |
|
|
1042 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1042 |
> |
await(threadStarted); |
1043 |
|
t.interrupt(); |
1044 |
< |
awaitTermination(t, MEDIUM_DELAY_MS); |
987 |
< |
} finally { |
988 |
< |
done.countDown(); |
989 |
< |
joinPool(p); |
1044 |
> |
awaitTermination(t); |
1045 |
|
} |
1046 |
|
} |
1047 |
|
|
1049 |
|
* execute throws RejectedExecutionException if saturated. |
1050 |
|
*/ |
1051 |
|
public void testSaturatedExecute() { |
1052 |
< |
ThreadPoolExecutor p = |
1052 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1053 |
> |
final ThreadPoolExecutor p = |
1054 |
|
new ThreadPoolExecutor(1, 1, |
1055 |
|
LONG_DELAY_MS, MILLISECONDS, |
1056 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1057 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1002 |
< |
try { |
1057 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1058 |
|
Runnable task = new CheckedRunnable() { |
1059 |
|
public void realRun() throws InterruptedException { |
1060 |
< |
done.await(); |
1060 |
> |
await(done); |
1061 |
|
}}; |
1062 |
|
for (int i = 0; i < 2; ++i) |
1063 |
|
p.execute(task); |
1068 |
|
} catch (RejectedExecutionException success) {} |
1069 |
|
assertTrue(p.getTaskCount() <= 2); |
1070 |
|
} |
1016 |
– |
} finally { |
1017 |
– |
done.countDown(); |
1018 |
– |
joinPool(p); |
1071 |
|
} |
1072 |
|
} |
1073 |
|
|
1075 |
|
* submit(runnable) throws RejectedExecutionException if saturated. |
1076 |
|
*/ |
1077 |
|
public void testSaturatedSubmitRunnable() { |
1078 |
< |
ThreadPoolExecutor p = |
1078 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1079 |
> |
final ThreadPoolExecutor p = |
1080 |
|
new ThreadPoolExecutor(1, 1, |
1081 |
|
LONG_DELAY_MS, MILLISECONDS, |
1082 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1083 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1031 |
< |
try { |
1083 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1084 |
|
Runnable task = new CheckedRunnable() { |
1085 |
|
public void realRun() throws InterruptedException { |
1086 |
< |
done.await(); |
1086 |
> |
await(done); |
1087 |
|
}}; |
1088 |
|
for (int i = 0; i < 2; ++i) |
1089 |
|
p.submit(task); |
1094 |
|
} catch (RejectedExecutionException success) {} |
1095 |
|
assertTrue(p.getTaskCount() <= 2); |
1096 |
|
} |
1045 |
– |
} finally { |
1046 |
– |
done.countDown(); |
1047 |
– |
joinPool(p); |
1097 |
|
} |
1098 |
|
} |
1099 |
|
|
1101 |
|
* submit(callable) throws RejectedExecutionException if saturated. |
1102 |
|
*/ |
1103 |
|
public void testSaturatedSubmitCallable() { |
1104 |
< |
ThreadPoolExecutor p = |
1104 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1105 |
> |
final ThreadPoolExecutor p = |
1106 |
|
new ThreadPoolExecutor(1, 1, |
1107 |
|
LONG_DELAY_MS, MILLISECONDS, |
1108 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1109 |
< |
final CountDownLatch done = new CountDownLatch(1); |
1060 |
< |
try { |
1109 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1110 |
|
Runnable task = new CheckedRunnable() { |
1111 |
|
public void realRun() throws InterruptedException { |
1112 |
< |
done.await(); |
1112 |
> |
await(done); |
1113 |
|
}}; |
1114 |
|
for (int i = 0; i < 2; ++i) |
1115 |
|
p.submit(Executors.callable(task)); |
1120 |
|
} catch (RejectedExecutionException success) {} |
1121 |
|
assertTrue(p.getTaskCount() <= 2); |
1122 |
|
} |
1074 |
– |
} finally { |
1075 |
– |
done.countDown(); |
1076 |
– |
joinPool(p); |
1123 |
|
} |
1124 |
|
} |
1125 |
|
|
1127 |
|
* executor using CallerRunsPolicy runs task if saturated. |
1128 |
|
*/ |
1129 |
|
public void testSaturatedExecute2() { |
1084 |
– |
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1130 |
|
final ThreadPoolExecutor p = |
1131 |
|
new ThreadPoolExecutor(1, 1, |
1132 |
|
LONG_DELAY_MS, |
1133 |
|
MILLISECONDS, |
1134 |
|
new ArrayBlockingQueue<Runnable>(1), |
1135 |
< |
h); |
1136 |
< |
try { |
1135 |
> |
new ThreadPoolExecutor.CallerRunsPolicy()); |
1136 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1137 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1138 |
> |
Runnable blocker = new CheckedRunnable() { |
1139 |
> |
public void realRun() throws InterruptedException { |
1140 |
> |
await(done); |
1141 |
> |
}}; |
1142 |
> |
p.execute(blocker); |
1143 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1144 |
< |
for (int i = 0; i < tasks.length; ++i) |
1144 |
> |
for (int i = 0; i < tasks.length; i++) |
1145 |
|
tasks[i] = new TrackedNoOpRunnable(); |
1146 |
< |
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1096 |
< |
p.execute(mr); |
1097 |
< |
for (int i = 0; i < tasks.length; ++i) |
1146 |
> |
for (int i = 0; i < tasks.length; i++) |
1147 |
|
p.execute(tasks[i]); |
1148 |
< |
for (int i = 1; i < tasks.length; ++i) |
1148 |
> |
for (int i = 1; i < tasks.length; i++) |
1149 |
|
assertTrue(tasks[i].done); |
1150 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1151 |
< |
} finally { |
1103 |
< |
joinPool(p); |
1150 |
> |
assertFalse(tasks[0].done); // waiting in queue |
1151 |
> |
done.countDown(); |
1152 |
|
} |
1153 |
|
} |
1154 |
|
|
1156 |
|
* executor using DiscardPolicy drops task if saturated. |
1157 |
|
*/ |
1158 |
|
public void testSaturatedExecute3() { |
1159 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1159 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1160 |
> |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1161 |
> |
for (int i = 0; i < tasks.length; ++i) |
1162 |
> |
tasks[i] = new TrackedNoOpRunnable(); |
1163 |
|
final ThreadPoolExecutor p = |
1164 |
|
new ThreadPoolExecutor(1, 1, |
1165 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1166 |
< |
new ArrayBlockingQueue<Runnable>(1), |
1167 |
< |
h); |
1168 |
< |
try { |
1169 |
< |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1170 |
< |
for (int i = 0; i < tasks.length; ++i) |
1120 |
< |
tasks[i] = new TrackedNoOpRunnable(); |
1121 |
< |
p.execute(new TrackedLongRunnable()); |
1165 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1166 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1167 |
> |
new ThreadPoolExecutor.DiscardPolicy()); |
1168 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1169 |
> |
p.execute(awaiter(done)); |
1170 |
> |
|
1171 |
|
for (TrackedNoOpRunnable task : tasks) |
1172 |
|
p.execute(task); |
1173 |
< |
for (TrackedNoOpRunnable task : tasks) |
1174 |
< |
assertFalse(task.done); |
1126 |
< |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1127 |
< |
} finally { |
1128 |
< |
joinPool(p); |
1173 |
> |
for (int i = 1; i < tasks.length; i++) |
1174 |
> |
assertFalse(tasks[i].done); |
1175 |
|
} |
1176 |
+ |
for (int i = 1; i < tasks.length; i++) |
1177 |
+ |
assertFalse(tasks[i].done); |
1178 |
+ |
assertTrue(tasks[0].done); // was waiting in queue |
1179 |
|
} |
1180 |
|
|
1181 |
|
/** |
1182 |
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1183 |
|
*/ |
1184 |
|
public void testSaturatedExecute4() { |
1185 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1185 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1186 |
> |
LatchAwaiter r1 = awaiter(done); |
1187 |
> |
LatchAwaiter r2 = awaiter(done); |
1188 |
> |
LatchAwaiter r3 = awaiter(done); |
1189 |
|
final ThreadPoolExecutor p = |
1190 |
|
new ThreadPoolExecutor(1, 1, |
1191 |
|
LONG_DELAY_MS, MILLISECONDS, |
1192 |
|
new ArrayBlockingQueue<Runnable>(1), |
1193 |
< |
h); |
1194 |
< |
try { |
1195 |
< |
p.execute(new TrackedLongRunnable()); |
1196 |
< |
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1193 |
> |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1194 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1195 |
> |
assertEquals(LatchAwaiter.NEW, r1.state); |
1196 |
> |
assertEquals(LatchAwaiter.NEW, r2.state); |
1197 |
> |
assertEquals(LatchAwaiter.NEW, r3.state); |
1198 |
> |
p.execute(r1); |
1199 |
|
p.execute(r2); |
1200 |
|
assertTrue(p.getQueue().contains(r2)); |
1147 |
– |
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
1201 |
|
p.execute(r3); |
1202 |
|
assertFalse(p.getQueue().contains(r2)); |
1203 |
|
assertTrue(p.getQueue().contains(r3)); |
1151 |
– |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1152 |
– |
} finally { |
1153 |
– |
joinPool(p); |
1204 |
|
} |
1205 |
+ |
assertEquals(LatchAwaiter.DONE, r1.state); |
1206 |
+ |
assertEquals(LatchAwaiter.NEW, r2.state); |
1207 |
+ |
assertEquals(LatchAwaiter.DONE, r3.state); |
1208 |
|
} |
1209 |
|
|
1210 |
|
/** |
1211 |
|
* execute throws RejectedExecutionException if shutdown |
1212 |
|
*/ |
1213 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1214 |
< |
ThreadPoolExecutor p = |
1214 |
> |
final ThreadPoolExecutor p = |
1215 |
|
new ThreadPoolExecutor(1, 1, |
1216 |
|
LONG_DELAY_MS, MILLISECONDS, |
1217 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1218 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1219 |
< |
try { |
1220 |
< |
p.execute(new NoOpRunnable()); |
1221 |
< |
shouldThrow(); |
1222 |
< |
} catch (RejectedExecutionException success) {} |
1223 |
< |
|
1224 |
< |
joinPool(p); |
1219 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1220 |
> |
try { |
1221 |
> |
p.execute(new NoOpRunnable()); |
1222 |
> |
shouldThrow(); |
1223 |
> |
} catch (RejectedExecutionException success) {} |
1224 |
> |
} |
1225 |
|
} |
1226 |
|
|
1227 |
|
/** |
1235 |
|
new ArrayBlockingQueue<Runnable>(1), h); |
1236 |
|
|
1237 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1238 |
< |
try { |
1238 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1239 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1240 |
|
p.execute(r); |
1241 |
|
assertFalse(r.done); |
1189 |
– |
} finally { |
1190 |
– |
joinPool(p); |
1242 |
|
} |
1243 |
|
} |
1244 |
|
|
1246 |
|
* execute using DiscardPolicy drops task on shutdown |
1247 |
|
*/ |
1248 |
|
public void testDiscardOnShutdown() { |
1249 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1199 |
< |
ThreadPoolExecutor p = |
1249 |
> |
final ThreadPoolExecutor p = |
1250 |
|
new ThreadPoolExecutor(1, 1, |
1251 |
|
LONG_DELAY_MS, MILLISECONDS, |
1252 |
|
new ArrayBlockingQueue<Runnable>(1), |
1253 |
< |
h); |
1253 |
> |
new ThreadPoolExecutor.DiscardPolicy()); |
1254 |
|
|
1255 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1256 |
< |
try { |
1256 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1257 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1258 |
|
p.execute(r); |
1259 |
|
assertFalse(r.done); |
1210 |
– |
} finally { |
1211 |
– |
joinPool(p); |
1260 |
|
} |
1261 |
|
} |
1262 |
|
|
1215 |
– |
|
1263 |
|
/** |
1264 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1265 |
|
*/ |
1266 |
|
public void testDiscardOldestOnShutdown() { |
1267 |
< |
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1221 |
< |
ThreadPoolExecutor p = |
1267 |
> |
final ThreadPoolExecutor p = |
1268 |
|
new ThreadPoolExecutor(1, 1, |
1269 |
|
LONG_DELAY_MS, MILLISECONDS, |
1270 |
|
new ArrayBlockingQueue<Runnable>(1), |
1271 |
< |
h); |
1271 |
> |
new ThreadPoolExecutor.DiscardOldestPolicy()); |
1272 |
|
|
1273 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1274 |
< |
try { |
1274 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1275 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1276 |
|
p.execute(r); |
1277 |
|
assertFalse(r.done); |
1232 |
– |
} finally { |
1233 |
– |
joinPool(p); |
1278 |
|
} |
1279 |
|
} |
1280 |
|
|
1237 |
– |
|
1281 |
|
/** |
1282 |
|
* execute(null) throws NPE |
1283 |
|
*/ |
1284 |
|
public void testExecuteNull() { |
1285 |
< |
ThreadPoolExecutor p = |
1285 |
> |
final ThreadPoolExecutor p = |
1286 |
|
new ThreadPoolExecutor(1, 2, |
1287 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1287 |
> |
1L, SECONDS, |
1288 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1289 |
< |
try { |
1290 |
< |
p.execute(null); |
1291 |
< |
shouldThrow(); |
1292 |
< |
} catch (NullPointerException success) {} |
1293 |
< |
|
1294 |
< |
joinPool(p); |
1289 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1290 |
> |
try { |
1291 |
> |
p.execute(null); |
1292 |
> |
shouldThrow(); |
1293 |
> |
} catch (NullPointerException success) {} |
1294 |
> |
} |
1295 |
|
} |
1296 |
|
|
1297 |
|
/** |
1298 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1299 |
|
*/ |
1300 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1301 |
< |
ThreadPoolExecutor p = |
1301 |
> |
final ThreadPoolExecutor p = |
1302 |
|
new ThreadPoolExecutor(1, 2, |
1303 |
|
LONG_DELAY_MS, MILLISECONDS, |
1304 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1305 |
< |
try { |
1306 |
< |
p.setCorePoolSize(-1); |
1307 |
< |
shouldThrow(); |
1308 |
< |
} catch (IllegalArgumentException success) { |
1309 |
< |
} finally { |
1267 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1305 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1306 |
> |
try { |
1307 |
> |
p.setCorePoolSize(-1); |
1308 |
> |
shouldThrow(); |
1309 |
> |
} catch (IllegalArgumentException success) {} |
1310 |
|
} |
1269 |
– |
joinPool(p); |
1311 |
|
} |
1312 |
|
|
1313 |
|
/** |
1315 |
|
* given a value less the core pool size |
1316 |
|
*/ |
1317 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1318 |
< |
ThreadPoolExecutor p = |
1318 |
> |
final ThreadPoolExecutor p = |
1319 |
|
new ThreadPoolExecutor(2, 3, |
1320 |
|
LONG_DELAY_MS, MILLISECONDS, |
1321 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1322 |
< |
try { |
1323 |
< |
p.setMaximumPoolSize(1); |
1324 |
< |
shouldThrow(); |
1325 |
< |
} catch (IllegalArgumentException success) { |
1326 |
< |
} finally { |
1286 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1322 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1323 |
> |
try { |
1324 |
> |
p.setMaximumPoolSize(1); |
1325 |
> |
shouldThrow(); |
1326 |
> |
} catch (IllegalArgumentException success) {} |
1327 |
|
} |
1288 |
– |
joinPool(p); |
1328 |
|
} |
1329 |
|
|
1330 |
|
/** |
1332 |
|
* if given a negative value |
1333 |
|
*/ |
1334 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1335 |
< |
ThreadPoolExecutor p = |
1335 |
> |
final ThreadPoolExecutor p = |
1336 |
|
new ThreadPoolExecutor(2, 3, |
1337 |
|
LONG_DELAY_MS, MILLISECONDS, |
1338 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1339 |
< |
try { |
1340 |
< |
p.setMaximumPoolSize(-1); |
1341 |
< |
shouldThrow(); |
1342 |
< |
} catch (IllegalArgumentException success) { |
1343 |
< |
} finally { |
1305 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1339 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1340 |
> |
try { |
1341 |
> |
p.setMaximumPoolSize(-1); |
1342 |
> |
shouldThrow(); |
1343 |
> |
} catch (IllegalArgumentException success) {} |
1344 |
|
} |
1307 |
– |
joinPool(p); |
1345 |
|
} |
1346 |
|
|
1347 |
+ |
/** |
1348 |
+ |
* Configuration changes that allow core pool size greater than |
1349 |
+ |
* max pool size result in IllegalArgumentException. |
1350 |
+ |
*/ |
1351 |
+ |
public void testPoolSizeInvariants() { |
1352 |
+ |
final ThreadPoolExecutor p = |
1353 |
+ |
new ThreadPoolExecutor(1, 1, |
1354 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1355 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1356 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
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()); |
1372 |
+ |
} |
1373 |
+ |
} |
1374 |
+ |
} |
1375 |
|
|
1376 |
|
/** |
1377 |
|
* setKeepAliveTime throws IllegalArgumentException |
1378 |
|
* when given a negative value |
1379 |
|
*/ |
1380 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1381 |
< |
ThreadPoolExecutor p = |
1381 |
> |
final ThreadPoolExecutor p = |
1382 |
|
new ThreadPoolExecutor(2, 3, |
1383 |
|
LONG_DELAY_MS, MILLISECONDS, |
1384 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1385 |
< |
try { |
1386 |
< |
p.setKeepAliveTime(-1,MILLISECONDS); |
1387 |
< |
shouldThrow(); |
1388 |
< |
} catch (IllegalArgumentException success) { |
1389 |
< |
} finally { |
1325 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1385 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1386 |
> |
try { |
1387 |
> |
p.setKeepAliveTime(-1, MILLISECONDS); |
1388 |
> |
shouldThrow(); |
1389 |
> |
} catch (IllegalArgumentException success) {} |
1390 |
|
} |
1327 |
– |
joinPool(p); |
1391 |
|
} |
1392 |
|
|
1393 |
|
/** |
1395 |
|
*/ |
1396 |
|
public void testTerminated() { |
1397 |
|
ExtendedTPE p = new ExtendedTPE(); |
1398 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1399 |
< |
assertTrue(p.terminatedCalled); |
1400 |
< |
joinPool(p); |
1398 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1399 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1400 |
> |
assertTrue(p.terminatedCalled()); |
1401 |
> |
assertTrue(p.isShutdown()); |
1402 |
> |
} |
1403 |
|
} |
1404 |
|
|
1405 |
|
/** |
1407 |
|
*/ |
1408 |
|
public void testBeforeAfter() throws InterruptedException { |
1409 |
|
ExtendedTPE p = new ExtendedTPE(); |
1410 |
< |
try { |
1411 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1412 |
< |
p.execute(r); |
1413 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1414 |
< |
assertTrue(r.done); |
1415 |
< |
assertTrue(p.beforeCalled); |
1416 |
< |
assertTrue(p.afterCalled); |
1417 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1418 |
< |
} finally { |
1419 |
< |
joinPool(p); |
1410 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1411 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1412 |
> |
p.execute(new CheckedRunnable() { |
1413 |
> |
public void realRun() { |
1414 |
> |
done.countDown(); |
1415 |
> |
}}); |
1416 |
> |
await(p.afterCalled); |
1417 |
> |
assertEquals(0, done.getCount()); |
1418 |
> |
assertTrue(p.afterCalled()); |
1419 |
> |
assertTrue(p.beforeCalled()); |
1420 |
|
} |
1421 |
|
} |
1422 |
|
|
1424 |
|
* completed submit of callable returns result |
1425 |
|
*/ |
1426 |
|
public void testSubmitCallable() throws Exception { |
1427 |
< |
ExecutorService e = |
1427 |
> |
final ExecutorService e = |
1428 |
|
new ThreadPoolExecutor(2, 2, |
1429 |
|
LONG_DELAY_MS, MILLISECONDS, |
1430 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1431 |
< |
try { |
1431 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1432 |
|
Future<String> future = e.submit(new StringTask()); |
1433 |
|
String result = future.get(); |
1434 |
|
assertSame(TEST_STRING, result); |
1370 |
– |
} finally { |
1371 |
– |
joinPool(e); |
1435 |
|
} |
1436 |
|
} |
1437 |
|
|
1439 |
|
* completed submit of runnable returns successfully |
1440 |
|
*/ |
1441 |
|
public void testSubmitRunnable() throws Exception { |
1442 |
< |
ExecutorService e = |
1442 |
> |
final ExecutorService e = |
1443 |
|
new ThreadPoolExecutor(2, 2, |
1444 |
|
LONG_DELAY_MS, MILLISECONDS, |
1445 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1446 |
< |
try { |
1446 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1447 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1448 |
|
future.get(); |
1449 |
|
assertTrue(future.isDone()); |
1387 |
– |
} finally { |
1388 |
– |
joinPool(e); |
1450 |
|
} |
1451 |
|
} |
1452 |
|
|
1454 |
|
* completed submit of (runnable, result) returns result |
1455 |
|
*/ |
1456 |
|
public void testSubmitRunnable2() throws Exception { |
1457 |
< |
ExecutorService e = |
1457 |
> |
final ExecutorService e = |
1458 |
|
new ThreadPoolExecutor(2, 2, |
1459 |
|
LONG_DELAY_MS, MILLISECONDS, |
1460 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1461 |
< |
try { |
1461 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1462 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1463 |
|
String result = future.get(); |
1464 |
|
assertSame(TEST_STRING, result); |
1404 |
– |
} finally { |
1405 |
– |
joinPool(e); |
1465 |
|
} |
1466 |
|
} |
1467 |
|
|
1409 |
– |
|
1468 |
|
/** |
1469 |
|
* invokeAny(null) throws NPE |
1470 |
|
*/ |
1471 |
|
public void testInvokeAny1() throws Exception { |
1472 |
< |
ExecutorService e = |
1472 |
> |
final ExecutorService e = |
1473 |
|
new ThreadPoolExecutor(2, 2, |
1474 |
|
LONG_DELAY_MS, MILLISECONDS, |
1475 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1476 |
< |
try { |
1477 |
< |
e.invokeAny(null); |
1478 |
< |
shouldThrow(); |
1479 |
< |
} catch (NullPointerException success) { |
1480 |
< |
} finally { |
1423 |
< |
joinPool(e); |
1476 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1477 |
> |
try { |
1478 |
> |
e.invokeAny(null); |
1479 |
> |
shouldThrow(); |
1480 |
> |
} catch (NullPointerException success) {} |
1481 |
|
} |
1482 |
|
} |
1483 |
|
|
1485 |
|
* invokeAny(empty collection) throws IAE |
1486 |
|
*/ |
1487 |
|
public void testInvokeAny2() throws Exception { |
1488 |
< |
ExecutorService e = |
1488 |
> |
final ExecutorService e = |
1489 |
|
new ThreadPoolExecutor(2, 2, |
1490 |
|
LONG_DELAY_MS, MILLISECONDS, |
1491 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1492 |
< |
try { |
1493 |
< |
e.invokeAny(new ArrayList<Callable<String>>()); |
1494 |
< |
shouldThrow(); |
1495 |
< |
} catch (IllegalArgumentException success) { |
1496 |
< |
} finally { |
1440 |
< |
joinPool(e); |
1492 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1493 |
> |
try { |
1494 |
> |
e.invokeAny(new ArrayList<Callable<String>>()); |
1495 |
> |
shouldThrow(); |
1496 |
> |
} catch (IllegalArgumentException success) {} |
1497 |
|
} |
1498 |
|
} |
1499 |
|
|
1506 |
|
new ThreadPoolExecutor(2, 2, |
1507 |
|
LONG_DELAY_MS, MILLISECONDS, |
1508 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1509 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1510 |
< |
l.add(latchAwaitingStringTask(latch)); |
1511 |
< |
l.add(null); |
1512 |
< |
try { |
1513 |
< |
e.invokeAny(l); |
1514 |
< |
shouldThrow(); |
1515 |
< |
} catch (NullPointerException success) { |
1516 |
< |
} finally { |
1509 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1510 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1511 |
> |
l.add(latchAwaitingStringTask(latch)); |
1512 |
> |
l.add(null); |
1513 |
> |
try { |
1514 |
> |
e.invokeAny(l); |
1515 |
> |
shouldThrow(); |
1516 |
> |
} catch (NullPointerException success) {} |
1517 |
|
latch.countDown(); |
1462 |
– |
joinPool(e); |
1518 |
|
} |
1519 |
|
} |
1520 |
|
|
1522 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1523 |
|
*/ |
1524 |
|
public void testInvokeAny4() throws Exception { |
1525 |
< |
ExecutorService e = |
1525 |
> |
final ExecutorService e = |
1526 |
|
new ThreadPoolExecutor(2, 2, |
1527 |
|
LONG_DELAY_MS, MILLISECONDS, |
1528 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1529 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1530 |
< |
l.add(new NPETask()); |
1531 |
< |
try { |
1532 |
< |
e.invokeAny(l); |
1533 |
< |
shouldThrow(); |
1534 |
< |
} catch (ExecutionException success) { |
1535 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1536 |
< |
} finally { |
1537 |
< |
joinPool(e); |
1529 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1530 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1531 |
> |
l.add(new NPETask()); |
1532 |
> |
try { |
1533 |
> |
e.invokeAny(l); |
1534 |
> |
shouldThrow(); |
1535 |
> |
} catch (ExecutionException success) { |
1536 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1537 |
> |
} |
1538 |
|
} |
1539 |
|
} |
1540 |
|
|
1542 |
|
* invokeAny(c) returns result of some task |
1543 |
|
*/ |
1544 |
|
public void testInvokeAny5() throws Exception { |
1545 |
< |
ExecutorService e = |
1545 |
> |
final ExecutorService e = |
1546 |
|
new ThreadPoolExecutor(2, 2, |
1547 |
|
LONG_DELAY_MS, MILLISECONDS, |
1548 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1549 |
< |
try { |
1550 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1549 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1550 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1551 |
|
l.add(new StringTask()); |
1552 |
|
l.add(new StringTask()); |
1553 |
|
String result = e.invokeAny(l); |
1554 |
|
assertSame(TEST_STRING, result); |
1500 |
– |
} finally { |
1501 |
– |
joinPool(e); |
1555 |
|
} |
1556 |
|
} |
1557 |
|
|
1559 |
|
* invokeAll(null) throws NPE |
1560 |
|
*/ |
1561 |
|
public void testInvokeAll1() throws Exception { |
1562 |
< |
ExecutorService e = |
1562 |
> |
final ExecutorService e = |
1563 |
|
new ThreadPoolExecutor(2, 2, |
1564 |
|
LONG_DELAY_MS, MILLISECONDS, |
1565 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1566 |
< |
try { |
1567 |
< |
e.invokeAll(null); |
1568 |
< |
shouldThrow(); |
1569 |
< |
} catch (NullPointerException success) { |
1570 |
< |
} finally { |
1518 |
< |
joinPool(e); |
1566 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1567 |
> |
try { |
1568 |
> |
e.invokeAll(null); |
1569 |
> |
shouldThrow(); |
1570 |
> |
} catch (NullPointerException success) {} |
1571 |
|
} |
1572 |
|
} |
1573 |
|
|
1575 |
|
* invokeAll(empty collection) returns empty collection |
1576 |
|
*/ |
1577 |
|
public void testInvokeAll2() throws InterruptedException { |
1578 |
< |
ExecutorService e = |
1578 |
> |
final ExecutorService e = |
1579 |
|
new ThreadPoolExecutor(2, 2, |
1580 |
|
LONG_DELAY_MS, MILLISECONDS, |
1581 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1582 |
< |
try { |
1582 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1583 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1584 |
|
assertTrue(r.isEmpty()); |
1533 |
– |
} finally { |
1534 |
– |
joinPool(e); |
1585 |
|
} |
1586 |
|
} |
1587 |
|
|
1589 |
|
* invokeAll(c) throws NPE if c has null elements |
1590 |
|
*/ |
1591 |
|
public void testInvokeAll3() throws Exception { |
1592 |
< |
ExecutorService e = |
1592 |
> |
final ExecutorService e = |
1593 |
|
new ThreadPoolExecutor(2, 2, |
1594 |
|
LONG_DELAY_MS, MILLISECONDS, |
1595 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1596 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1597 |
< |
l.add(new StringTask()); |
1598 |
< |
l.add(null); |
1599 |
< |
try { |
1600 |
< |
e.invokeAll(l); |
1601 |
< |
shouldThrow(); |
1602 |
< |
} catch (NullPointerException success) { |
1603 |
< |
} finally { |
1554 |
< |
joinPool(e); |
1596 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1597 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1598 |
> |
l.add(new StringTask()); |
1599 |
> |
l.add(null); |
1600 |
> |
try { |
1601 |
> |
e.invokeAll(l); |
1602 |
> |
shouldThrow(); |
1603 |
> |
} catch (NullPointerException success) {} |
1604 |
|
} |
1605 |
|
} |
1606 |
|
|
1608 |
|
* get of element of invokeAll(c) throws exception on failed task |
1609 |
|
*/ |
1610 |
|
public void testInvokeAll4() throws Exception { |
1611 |
< |
ExecutorService e = |
1611 |
> |
final ExecutorService e = |
1612 |
|
new ThreadPoolExecutor(2, 2, |
1613 |
|
LONG_DELAY_MS, MILLISECONDS, |
1614 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1615 |
< |
try { |
1616 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1615 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1616 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1617 |
|
l.add(new NPETask()); |
1618 |
|
List<Future<String>> futures = e.invokeAll(l); |
1619 |
|
assertEquals(1, futures.size()); |
1623 |
|
} catch (ExecutionException success) { |
1624 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1625 |
|
} |
1577 |
– |
} finally { |
1578 |
– |
joinPool(e); |
1626 |
|
} |
1627 |
|
} |
1628 |
|
|
1630 |
|
* invokeAll(c) returns results of all completed tasks |
1631 |
|
*/ |
1632 |
|
public void testInvokeAll5() throws Exception { |
1633 |
< |
ExecutorService e = |
1633 |
> |
final ExecutorService e = |
1634 |
|
new ThreadPoolExecutor(2, 2, |
1635 |
|
LONG_DELAY_MS, MILLISECONDS, |
1636 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1637 |
< |
try { |
1638 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1637 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1638 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1639 |
|
l.add(new StringTask()); |
1640 |
|
l.add(new StringTask()); |
1641 |
|
List<Future<String>> futures = e.invokeAll(l); |
1642 |
|
assertEquals(2, futures.size()); |
1643 |
|
for (Future<String> future : futures) |
1644 |
|
assertSame(TEST_STRING, future.get()); |
1598 |
– |
} finally { |
1599 |
– |
joinPool(e); |
1645 |
|
} |
1646 |
|
} |
1647 |
|
|
1603 |
– |
|
1604 |
– |
|
1648 |
|
/** |
1649 |
|
* timed invokeAny(null) throws NPE |
1650 |
|
*/ |
1651 |
|
public void testTimedInvokeAny1() throws Exception { |
1652 |
< |
ExecutorService e = |
1652 |
> |
final ExecutorService e = |
1653 |
|
new ThreadPoolExecutor(2, 2, |
1654 |
|
LONG_DELAY_MS, MILLISECONDS, |
1655 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1656 |
< |
try { |
1657 |
< |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1658 |
< |
shouldThrow(); |
1659 |
< |
} catch (NullPointerException success) { |
1660 |
< |
} finally { |
1618 |
< |
joinPool(e); |
1656 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1657 |
> |
try { |
1658 |
> |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1659 |
> |
shouldThrow(); |
1660 |
> |
} catch (NullPointerException success) {} |
1661 |
|
} |
1662 |
|
} |
1663 |
|
|
1665 |
|
* timed invokeAny(,,null) throws NPE |
1666 |
|
*/ |
1667 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1668 |
< |
ExecutorService e = |
1668 |
> |
final ExecutorService e = |
1669 |
|
new ThreadPoolExecutor(2, 2, |
1670 |
|
LONG_DELAY_MS, MILLISECONDS, |
1671 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1672 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1673 |
< |
l.add(new StringTask()); |
1674 |
< |
try { |
1675 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1676 |
< |
shouldThrow(); |
1677 |
< |
} catch (NullPointerException success) { |
1678 |
< |
} finally { |
1637 |
< |
joinPool(e); |
1672 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1673 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1674 |
> |
l.add(new StringTask()); |
1675 |
> |
try { |
1676 |
> |
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1677 |
> |
shouldThrow(); |
1678 |
> |
} catch (NullPointerException success) {} |
1679 |
|
} |
1680 |
|
} |
1681 |
|
|
1683 |
|
* timed invokeAny(empty collection) throws IAE |
1684 |
|
*/ |
1685 |
|
public void testTimedInvokeAny2() throws Exception { |
1686 |
< |
ExecutorService e = |
1686 |
> |
final ExecutorService e = |
1687 |
|
new ThreadPoolExecutor(2, 2, |
1688 |
|
LONG_DELAY_MS, MILLISECONDS, |
1689 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1690 |
< |
try { |
1691 |
< |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1692 |
< |
shouldThrow(); |
1693 |
< |
} catch (IllegalArgumentException success) { |
1694 |
< |
} finally { |
1695 |
< |
joinPool(e); |
1690 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1691 |
> |
try { |
1692 |
> |
e.invokeAny(new ArrayList<Callable<String>>(), |
1693 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1694 |
> |
shouldThrow(); |
1695 |
> |
} catch (IllegalArgumentException success) {} |
1696 |
|
} |
1697 |
|
} |
1698 |
|
|
1705 |
|
new ThreadPoolExecutor(2, 2, |
1706 |
|
LONG_DELAY_MS, MILLISECONDS, |
1707 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1708 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1709 |
< |
l.add(latchAwaitingStringTask(latch)); |
1710 |
< |
l.add(null); |
1711 |
< |
try { |
1712 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1713 |
< |
shouldThrow(); |
1714 |
< |
} catch (NullPointerException success) { |
1715 |
< |
} finally { |
1708 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1709 |
> |
List<Callable<String>> l = new ArrayList<>(); |
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 |
|
latch.countDown(); |
1676 |
– |
joinPool(e); |
1717 |
|
} |
1718 |
|
} |
1719 |
|
|
1721 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1722 |
|
*/ |
1723 |
|
public void testTimedInvokeAny4() throws Exception { |
1724 |
< |
ExecutorService e = |
1724 |
> |
final ExecutorService e = |
1725 |
|
new ThreadPoolExecutor(2, 2, |
1726 |
|
LONG_DELAY_MS, MILLISECONDS, |
1727 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1728 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1729 |
< |
l.add(new NPETask()); |
1730 |
< |
try { |
1731 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1732 |
< |
shouldThrow(); |
1733 |
< |
} catch (ExecutionException success) { |
1734 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1735 |
< |
} finally { |
1736 |
< |
joinPool(e); |
1728 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1729 |
> |
long startTime = System.nanoTime(); |
1730 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1731 |
> |
l.add(new NPETask()); |
1732 |
> |
try { |
1733 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1734 |
> |
shouldThrow(); |
1735 |
> |
} catch (ExecutionException success) { |
1736 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1737 |
> |
} |
1738 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1739 |
|
} |
1740 |
|
} |
1741 |
|
|
1743 |
|
* timed invokeAny(c) returns result of some task |
1744 |
|
*/ |
1745 |
|
public void testTimedInvokeAny5() throws Exception { |
1746 |
< |
ExecutorService e = |
1746 |
> |
final ExecutorService e = |
1747 |
|
new ThreadPoolExecutor(2, 2, |
1748 |
|
LONG_DELAY_MS, MILLISECONDS, |
1749 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1750 |
< |
try { |
1751 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1750 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1751 |
> |
long startTime = System.nanoTime(); |
1752 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1753 |
|
l.add(new StringTask()); |
1754 |
|
l.add(new StringTask()); |
1755 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1755 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1756 |
|
assertSame(TEST_STRING, result); |
1757 |
< |
} finally { |
1715 |
< |
joinPool(e); |
1757 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1758 |
|
} |
1759 |
|
} |
1760 |
|
|
1762 |
|
* timed invokeAll(null) throws NPE |
1763 |
|
*/ |
1764 |
|
public void testTimedInvokeAll1() throws Exception { |
1765 |
< |
ExecutorService e = |
1765 |
> |
final ExecutorService e = |
1766 |
|
new ThreadPoolExecutor(2, 2, |
1767 |
|
LONG_DELAY_MS, MILLISECONDS, |
1768 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1769 |
< |
try { |
1770 |
< |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1771 |
< |
shouldThrow(); |
1772 |
< |
} catch (NullPointerException success) { |
1773 |
< |
} finally { |
1732 |
< |
joinPool(e); |
1769 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1770 |
> |
try { |
1771 |
> |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1772 |
> |
shouldThrow(); |
1773 |
> |
} catch (NullPointerException success) {} |
1774 |
|
} |
1775 |
|
} |
1776 |
|
|
1778 |
|
* timed invokeAll(,,null) throws NPE |
1779 |
|
*/ |
1780 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1781 |
< |
ExecutorService e = |
1781 |
> |
final ExecutorService e = |
1782 |
|
new ThreadPoolExecutor(2, 2, |
1783 |
|
LONG_DELAY_MS, MILLISECONDS, |
1784 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1785 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1786 |
< |
l.add(new StringTask()); |
1787 |
< |
try { |
1788 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1789 |
< |
shouldThrow(); |
1790 |
< |
} catch (NullPointerException success) { |
1791 |
< |
} finally { |
1751 |
< |
joinPool(e); |
1785 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1786 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1787 |
> |
l.add(new StringTask()); |
1788 |
> |
try { |
1789 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1790 |
> |
shouldThrow(); |
1791 |
> |
} catch (NullPointerException success) {} |
1792 |
|
} |
1793 |
|
} |
1794 |
|
|
1796 |
|
* timed invokeAll(empty collection) returns empty collection |
1797 |
|
*/ |
1798 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1799 |
< |
ExecutorService e = |
1799 |
> |
final ExecutorService e = |
1800 |
|
new ThreadPoolExecutor(2, 2, |
1801 |
|
LONG_DELAY_MS, MILLISECONDS, |
1802 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1803 |
< |
try { |
1804 |
< |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1803 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1804 |
> |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1805 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
1806 |
|
assertTrue(r.isEmpty()); |
1766 |
– |
} finally { |
1767 |
– |
joinPool(e); |
1807 |
|
} |
1808 |
|
} |
1809 |
|
|
1811 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1812 |
|
*/ |
1813 |
|
public void testTimedInvokeAll3() throws Exception { |
1814 |
< |
ExecutorService e = |
1814 |
> |
final ExecutorService e = |
1815 |
|
new ThreadPoolExecutor(2, 2, |
1816 |
|
LONG_DELAY_MS, MILLISECONDS, |
1817 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1818 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1819 |
< |
l.add(new StringTask()); |
1820 |
< |
l.add(null); |
1821 |
< |
try { |
1822 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1823 |
< |
shouldThrow(); |
1824 |
< |
} catch (NullPointerException success) { |
1825 |
< |
} finally { |
1787 |
< |
joinPool(e); |
1818 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1819 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1820 |
> |
l.add(new StringTask()); |
1821 |
> |
l.add(null); |
1822 |
> |
try { |
1823 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1824 |
> |
shouldThrow(); |
1825 |
> |
} catch (NullPointerException success) {} |
1826 |
|
} |
1827 |
|
} |
1828 |
|
|
1830 |
|
* get of element of invokeAll(c) throws exception on failed task |
1831 |
|
*/ |
1832 |
|
public void testTimedInvokeAll4() throws Exception { |
1833 |
< |
ExecutorService e = |
1833 |
> |
final ExecutorService e = |
1834 |
|
new ThreadPoolExecutor(2, 2, |
1835 |
|
LONG_DELAY_MS, MILLISECONDS, |
1836 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1837 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1838 |
< |
l.add(new NPETask()); |
1839 |
< |
List<Future<String>> futures = |
1840 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1841 |
< |
assertEquals(1, futures.size()); |
1842 |
< |
try { |
1843 |
< |
futures.get(0).get(); |
1844 |
< |
shouldThrow(); |
1845 |
< |
} catch (ExecutionException success) { |
1846 |
< |
assertTrue(success.getCause() instanceof NullPointerException); |
1847 |
< |
} finally { |
1848 |
< |
joinPool(e); |
1837 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1838 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1839 |
> |
l.add(new NPETask()); |
1840 |
> |
List<Future<String>> futures = |
1841 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1842 |
> |
assertEquals(1, futures.size()); |
1843 |
> |
try { |
1844 |
> |
futures.get(0).get(); |
1845 |
> |
shouldThrow(); |
1846 |
> |
} catch (ExecutionException success) { |
1847 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1848 |
> |
} |
1849 |
|
} |
1850 |
|
} |
1851 |
|
|
1853 |
|
* timed invokeAll(c) returns results of all completed tasks |
1854 |
|
*/ |
1855 |
|
public void testTimedInvokeAll5() throws Exception { |
1856 |
< |
ExecutorService e = |
1856 |
> |
final ExecutorService e = |
1857 |
|
new ThreadPoolExecutor(2, 2, |
1858 |
|
LONG_DELAY_MS, MILLISECONDS, |
1859 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1860 |
< |
try { |
1861 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1860 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1861 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1862 |
|
l.add(new StringTask()); |
1863 |
|
l.add(new StringTask()); |
1864 |
|
List<Future<String>> futures = |
1865 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1865 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1866 |
|
assertEquals(2, futures.size()); |
1867 |
|
for (Future<String> future : futures) |
1868 |
|
assertSame(TEST_STRING, future.get()); |
1831 |
– |
} finally { |
1832 |
– |
joinPool(e); |
1869 |
|
} |
1870 |
|
} |
1871 |
|
|
1873 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1874 |
|
*/ |
1875 |
|
public void testTimedInvokeAll6() throws Exception { |
1876 |
< |
ExecutorService e = |
1877 |
< |
new ThreadPoolExecutor(2, 2, |
1878 |
< |
LONG_DELAY_MS, MILLISECONDS, |
1879 |
< |
new ArrayBlockingQueue<Runnable>(10)); |
1880 |
< |
try { |
1881 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1882 |
< |
l.add(new StringTask()); |
1883 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1884 |
< |
l.add(new StringTask()); |
1885 |
< |
List<Future<String>> futures = |
1886 |
< |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1887 |
< |
assertEquals(3, futures.size()); |
1888 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1889 |
< |
Future<String> f1 = it.next(); |
1890 |
< |
Future<String> f2 = it.next(); |
1891 |
< |
Future<String> f3 = it.next(); |
1892 |
< |
assertTrue(f1.isDone()); |
1893 |
< |
assertTrue(f2.isDone()); |
1894 |
< |
assertTrue(f3.isDone()); |
1895 |
< |
assertFalse(f1.isCancelled()); |
1896 |
< |
assertTrue(f2.isCancelled()); |
1897 |
< |
} finally { |
1898 |
< |
joinPool(e); |
1876 |
> |
for (long timeout = timeoutMillis();;) { |
1877 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1878 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1879 |
> |
public String realCall() { |
1880 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1881 |
> |
catch (InterruptedException ok) {} |
1882 |
> |
return "1"; }}; |
1883 |
> |
final ExecutorService p = |
1884 |
> |
new ThreadPoolExecutor(2, 2, |
1885 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1886 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1887 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1888 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1889 |
> |
tasks.add(new StringTask("0")); |
1890 |
> |
tasks.add(waiter); |
1891 |
> |
tasks.add(new StringTask("2")); |
1892 |
> |
long startTime = System.nanoTime(); |
1893 |
> |
List<Future<String>> futures = |
1894 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1895 |
> |
assertEquals(tasks.size(), futures.size()); |
1896 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1897 |
> |
for (Future future : futures) |
1898 |
> |
assertTrue(future.isDone()); |
1899 |
> |
assertTrue(futures.get(1).isCancelled()); |
1900 |
> |
try { |
1901 |
> |
assertEquals("0", futures.get(0).get()); |
1902 |
> |
assertEquals("2", futures.get(2).get()); |
1903 |
> |
break; |
1904 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1905 |
> |
timeout *= 2; |
1906 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1907 |
> |
fail("expected exactly one task to be cancelled"); |
1908 |
> |
} |
1909 |
> |
} |
1910 |
|
} |
1911 |
|
} |
1912 |
|
|
1920 |
|
LONG_DELAY_MS, MILLISECONDS, |
1921 |
|
new LinkedBlockingQueue<Runnable>(), |
1922 |
|
new FailingThreadFactory()); |
1923 |
< |
try { |
1923 |
> |
try (PoolCleaner cleaner = cleaner(e)) { |
1924 |
|
final int TASKS = 100; |
1925 |
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1926 |
|
for (int k = 0; k < TASKS; ++k) |
1929 |
|
done.countDown(); |
1930 |
|
}}); |
1931 |
|
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1885 |
– |
} finally { |
1886 |
– |
joinPool(e); |
1932 |
|
} |
1933 |
|
} |
1934 |
|
|
1940 |
|
new ThreadPoolExecutor(2, 2, |
1941 |
|
1000, MILLISECONDS, |
1942 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1943 |
< |
assertFalse(p.allowsCoreThreadTimeOut()); |
1944 |
< |
joinPool(p); |
1943 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1944 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1945 |
> |
} |
1946 |
|
} |
1947 |
|
|
1948 |
|
/** |
1949 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1950 |
|
*/ |
1951 |
|
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1952 |
+ |
long keepAliveTime = timeoutMillis(); |
1953 |
|
final ThreadPoolExecutor p = |
1954 |
|
new ThreadPoolExecutor(2, 10, |
1955 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1955 |
> |
keepAliveTime, MILLISECONDS, |
1956 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1957 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1958 |
< |
try { |
1957 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1958 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1959 |
|
p.allowCoreThreadTimeOut(true); |
1960 |
|
p.execute(new CheckedRunnable() { |
1961 |
< |
public void realRun() throws InterruptedException { |
1961 |
> |
public void realRun() { |
1962 |
|
threadStarted.countDown(); |
1963 |
|
assertEquals(1, p.getPoolSize()); |
1964 |
|
}}); |
1965 |
< |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1966 |
< |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1967 |
< |
if (p.getPoolSize() == 0) |
1968 |
< |
break; |
1969 |
< |
Thread.sleep(10); |
1970 |
< |
} |
1965 |
> |
await(threadStarted); |
1966 |
> |
delay(keepAliveTime); |
1967 |
> |
long startTime = System.nanoTime(); |
1968 |
> |
while (p.getPoolSize() > 0 |
1969 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1970 |
> |
Thread.yield(); |
1971 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1972 |
|
assertEquals(0, p.getPoolSize()); |
1925 |
– |
} finally { |
1926 |
– |
joinPool(p); |
1973 |
|
} |
1974 |
|
} |
1975 |
|
|
1977 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1978 |
|
*/ |
1979 |
|
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1980 |
+ |
long keepAliveTime = timeoutMillis(); |
1981 |
|
final ThreadPoolExecutor p = |
1982 |
|
new ThreadPoolExecutor(2, 10, |
1983 |
< |
SHORT_DELAY_MS, MILLISECONDS, |
1983 |
> |
keepAliveTime, MILLISECONDS, |
1984 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1985 |
< |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1986 |
< |
try { |
1985 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
1986 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1987 |
|
p.allowCoreThreadTimeOut(false); |
1988 |
|
p.execute(new CheckedRunnable() { |
1989 |
|
public void realRun() throws InterruptedException { |
1990 |
|
threadStarted.countDown(); |
1991 |
|
assertTrue(p.getPoolSize() >= 1); |
1992 |
|
}}); |
1993 |
< |
Thread.sleep(SMALL_DELAY_MS); |
1993 |
> |
delay(2 * keepAliveTime); |
1994 |
|
assertTrue(p.getPoolSize() >= 1); |
1948 |
– |
} finally { |
1949 |
– |
joinPool(p); |
1995 |
|
} |
1996 |
|
} |
1997 |
|
|
2007 |
|
done.countDown(); |
2008 |
|
}}; |
2009 |
|
final ThreadPoolExecutor p = |
2010 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2010 |
> |
new ThreadPoolExecutor(1, 30, |
2011 |
> |
60, SECONDS, |
2012 |
|
new ArrayBlockingQueue(30)); |
2013 |
< |
try { |
2013 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
2014 |
|
for (int i = 0; i < nTasks; ++i) { |
2015 |
|
for (;;) { |
2016 |
|
try { |
2022 |
|
} |
2023 |
|
// enough time to run all tasks |
2024 |
|
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2025 |
< |
} finally { |
2026 |
< |
p.shutdown(); |
2025 |
> |
} |
2026 |
> |
} |
2027 |
> |
|
2028 |
> |
/** |
2029 |
> |
* get(cancelled task) throws CancellationException |
2030 |
> |
*/ |
2031 |
> |
public void testGet_cancelled() throws Exception { |
2032 |
> |
final CountDownLatch done = new CountDownLatch(1); |
2033 |
> |
final ExecutorService e = |
2034 |
> |
new ThreadPoolExecutor(1, 1, |
2035 |
> |
LONG_DELAY_MS, MILLISECONDS, |
2036 |
> |
new LinkedBlockingQueue<Runnable>()); |
2037 |
> |
try (PoolCleaner cleaner = cleaner(e, done)) { |
2038 |
> |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2039 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
2040 |
> |
for (int i = 0; i < 2; i++) { |
2041 |
> |
Runnable r = new CheckedRunnable() { public void realRun() |
2042 |
> |
throws Throwable { |
2043 |
> |
blockerStarted.countDown(); |
2044 |
> |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2045 |
> |
}}; |
2046 |
> |
futures.add(e.submit(r)); |
2047 |
> |
} |
2048 |
> |
await(blockerStarted); |
2049 |
> |
for (Future<?> future : futures) future.cancel(false); |
2050 |
> |
for (Future<?> future : futures) { |
2051 |
> |
try { |
2052 |
> |
future.get(); |
2053 |
> |
shouldThrow(); |
2054 |
> |
} catch (CancellationException success) {} |
2055 |
> |
try { |
2056 |
> |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2057 |
> |
shouldThrow(); |
2058 |
> |
} catch (CancellationException success) {} |
2059 |
> |
assertTrue(future.isCancelled()); |
2060 |
> |
assertTrue(future.isDone()); |
2061 |
> |
} |
2062 |
|
} |
2063 |
|
} |
2064 |
|
|