1 |
dl |
1.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 |
jsr166 |
1.25 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.2 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.9 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
jsr166 |
1.36 |
import static java.util.concurrent.TimeUnit.SECONDS; |
11 |
jsr166 |
1.33 |
|
12 |
|
|
import java.util.ArrayList; |
13 |
|
|
import java.util.List; |
14 |
|
|
import java.util.concurrent.ArrayBlockingQueue; |
15 |
|
|
import java.util.concurrent.BlockingQueue; |
16 |
|
|
import java.util.concurrent.Callable; |
17 |
jsr166 |
1.39 |
import java.util.concurrent.CancellationException; |
18 |
jsr166 |
1.33 |
import java.util.concurrent.CountDownLatch; |
19 |
|
|
import java.util.concurrent.ExecutionException; |
20 |
|
|
import java.util.concurrent.Executors; |
21 |
|
|
import java.util.concurrent.ExecutorService; |
22 |
|
|
import java.util.concurrent.Future; |
23 |
|
|
import java.util.concurrent.FutureTask; |
24 |
|
|
import java.util.concurrent.LinkedBlockingQueue; |
25 |
|
|
import java.util.concurrent.RejectedExecutionException; |
26 |
|
|
import java.util.concurrent.RejectedExecutionHandler; |
27 |
|
|
import java.util.concurrent.RunnableFuture; |
28 |
|
|
import java.util.concurrent.SynchronousQueue; |
29 |
|
|
import java.util.concurrent.ThreadFactory; |
30 |
|
|
import java.util.concurrent.ThreadPoolExecutor; |
31 |
|
|
import java.util.concurrent.TimeoutException; |
32 |
|
|
import java.util.concurrent.TimeUnit; |
33 |
jsr166 |
1.41 |
import java.util.concurrent.atomic.AtomicInteger; |
34 |
jsr166 |
1.31 |
import java.util.concurrent.locks.Condition; |
35 |
|
|
import java.util.concurrent.locks.ReentrantLock; |
36 |
jsr166 |
1.33 |
|
37 |
|
|
import junit.framework.Test; |
38 |
|
|
import junit.framework.TestSuite; |
39 |
dl |
1.1 |
|
40 |
|
|
public class ThreadPoolExecutorSubclassTest extends JSR166TestCase { |
41 |
|
|
public static void main(String[] args) { |
42 |
jsr166 |
1.35 |
main(suite(), args); |
43 |
dl |
1.1 |
} |
44 |
|
|
public static Test suite() { |
45 |
jsr166 |
1.6 |
return new TestSuite(ThreadPoolExecutorSubclassTest.class); |
46 |
dl |
1.1 |
} |
47 |
|
|
|
48 |
|
|
static class CustomTask<V> implements RunnableFuture<V> { |
49 |
|
|
final Callable<V> callable; |
50 |
|
|
final ReentrantLock lock = new ReentrantLock(); |
51 |
|
|
final Condition cond = lock.newCondition(); |
52 |
|
|
boolean done; |
53 |
|
|
boolean cancelled; |
54 |
|
|
V result; |
55 |
|
|
Thread thread; |
56 |
|
|
Exception exception; |
57 |
jsr166 |
1.6 |
CustomTask(Callable<V> c) { |
58 |
|
|
if (c == null) throw new NullPointerException(); |
59 |
|
|
callable = c; |
60 |
|
|
} |
61 |
|
|
CustomTask(final Runnable r, final V res) { |
62 |
|
|
if (r == null) throw new NullPointerException(); |
63 |
|
|
callable = new Callable<V>() { |
64 |
jsr166 |
1.34 |
public V call() throws Exception { r.run(); return res; }}; |
65 |
dl |
1.1 |
} |
66 |
|
|
public boolean isDone() { |
67 |
|
|
lock.lock(); try { return done; } finally { lock.unlock() ; } |
68 |
|
|
} |
69 |
|
|
public boolean isCancelled() { |
70 |
|
|
lock.lock(); try { return cancelled; } finally { lock.unlock() ; } |
71 |
|
|
} |
72 |
|
|
public boolean cancel(boolean mayInterrupt) { |
73 |
|
|
lock.lock(); |
74 |
|
|
try { |
75 |
|
|
if (!done) { |
76 |
|
|
cancelled = true; |
77 |
|
|
done = true; |
78 |
jsr166 |
1.2 |
if (mayInterrupt && thread != null) |
79 |
dl |
1.1 |
thread.interrupt(); |
80 |
|
|
return true; |
81 |
|
|
} |
82 |
|
|
return false; |
83 |
|
|
} |
84 |
|
|
finally { lock.unlock() ; } |
85 |
|
|
} |
86 |
|
|
public void run() { |
87 |
|
|
lock.lock(); |
88 |
|
|
try { |
89 |
jsr166 |
1.22 |
if (done) |
90 |
|
|
return; |
91 |
|
|
thread = Thread.currentThread(); |
92 |
dl |
1.1 |
} |
93 |
|
|
finally { lock.unlock() ; } |
94 |
|
|
V v = null; |
95 |
|
|
Exception e = null; |
96 |
|
|
try { |
97 |
|
|
v = callable.call(); |
98 |
|
|
} |
99 |
jsr166 |
1.3 |
catch (Exception ex) { |
100 |
dl |
1.1 |
e = ex; |
101 |
|
|
} |
102 |
|
|
lock.lock(); |
103 |
|
|
try { |
104 |
jsr166 |
1.44 |
if (!done) { |
105 |
|
|
result = v; |
106 |
|
|
exception = e; |
107 |
|
|
done = true; |
108 |
|
|
thread = null; |
109 |
|
|
cond.signalAll(); |
110 |
|
|
} |
111 |
dl |
1.1 |
} |
112 |
|
|
finally { lock.unlock(); } |
113 |
|
|
} |
114 |
|
|
public V get() throws InterruptedException, ExecutionException { |
115 |
|
|
lock.lock(); |
116 |
|
|
try { |
117 |
jsr166 |
1.2 |
while (!done) |
118 |
dl |
1.1 |
cond.await(); |
119 |
jsr166 |
1.44 |
if (cancelled) |
120 |
|
|
throw new CancellationException(); |
121 |
dl |
1.1 |
if (exception != null) |
122 |
|
|
throw new ExecutionException(exception); |
123 |
|
|
return result; |
124 |
|
|
} |
125 |
|
|
finally { lock.unlock(); } |
126 |
|
|
} |
127 |
|
|
public V get(long timeout, TimeUnit unit) |
128 |
jsr166 |
1.4 |
throws InterruptedException, ExecutionException, TimeoutException { |
129 |
dl |
1.1 |
long nanos = unit.toNanos(timeout); |
130 |
|
|
lock.lock(); |
131 |
|
|
try { |
132 |
jsr166 |
1.44 |
while (!done) { |
133 |
jsr166 |
1.47 |
if (nanos <= 0L) |
134 |
dl |
1.1 |
throw new TimeoutException(); |
135 |
|
|
nanos = cond.awaitNanos(nanos); |
136 |
|
|
} |
137 |
jsr166 |
1.44 |
if (cancelled) |
138 |
|
|
throw new CancellationException(); |
139 |
dl |
1.1 |
if (exception != null) |
140 |
|
|
throw new ExecutionException(exception); |
141 |
|
|
return result; |
142 |
|
|
} |
143 |
|
|
finally { lock.unlock(); } |
144 |
|
|
} |
145 |
jsr166 |
1.2 |
} |
146 |
|
|
|
147 |
dl |
1.1 |
static class CustomTPE extends ThreadPoolExecutor { |
148 |
|
|
protected <V> RunnableFuture<V> newTaskFor(Callable<V> c) { |
149 |
|
|
return new CustomTask<V>(c); |
150 |
|
|
} |
151 |
|
|
protected <V> RunnableFuture<V> newTaskFor(Runnable r, V v) { |
152 |
|
|
return new CustomTask<V>(r, v); |
153 |
jsr166 |
1.2 |
} |
154 |
|
|
|
155 |
dl |
1.1 |
CustomTPE(int corePoolSize, |
156 |
|
|
int maximumPoolSize, |
157 |
|
|
long keepAliveTime, |
158 |
|
|
TimeUnit unit, |
159 |
|
|
BlockingQueue<Runnable> workQueue) { |
160 |
jsr166 |
1.2 |
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, |
161 |
dl |
1.1 |
workQueue); |
162 |
|
|
} |
163 |
|
|
CustomTPE(int corePoolSize, |
164 |
|
|
int maximumPoolSize, |
165 |
|
|
long keepAliveTime, |
166 |
|
|
TimeUnit unit, |
167 |
|
|
BlockingQueue<Runnable> workQueue, |
168 |
|
|
ThreadFactory threadFactory) { |
169 |
|
|
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, |
170 |
|
|
threadFactory); |
171 |
|
|
} |
172 |
|
|
|
173 |
|
|
CustomTPE(int corePoolSize, |
174 |
|
|
int maximumPoolSize, |
175 |
|
|
long keepAliveTime, |
176 |
|
|
TimeUnit unit, |
177 |
|
|
BlockingQueue<Runnable> workQueue, |
178 |
|
|
RejectedExecutionHandler handler) { |
179 |
|
|
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, workQueue, |
180 |
|
|
handler); |
181 |
|
|
} |
182 |
|
|
CustomTPE(int corePoolSize, |
183 |
|
|
int maximumPoolSize, |
184 |
|
|
long keepAliveTime, |
185 |
|
|
TimeUnit unit, |
186 |
|
|
BlockingQueue<Runnable> workQueue, |
187 |
|
|
ThreadFactory threadFactory, |
188 |
|
|
RejectedExecutionHandler handler) { |
189 |
jsr166 |
1.2 |
super(corePoolSize, maximumPoolSize, keepAliveTime, unit, |
190 |
dl |
1.1 |
workQueue, threadFactory, handler); |
191 |
|
|
} |
192 |
|
|
|
193 |
jsr166 |
1.30 |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
194 |
|
|
final CountDownLatch afterCalled = new CountDownLatch(1); |
195 |
|
|
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
196 |
|
|
|
197 |
dl |
1.1 |
public CustomTPE() { |
198 |
jsr166 |
1.9 |
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
199 |
dl |
1.1 |
} |
200 |
|
|
protected void beforeExecute(Thread t, Runnable r) { |
201 |
jsr166 |
1.30 |
beforeCalled.countDown(); |
202 |
dl |
1.1 |
} |
203 |
|
|
protected void afterExecute(Runnable r, Throwable t) { |
204 |
jsr166 |
1.30 |
afterCalled.countDown(); |
205 |
dl |
1.1 |
} |
206 |
|
|
protected void terminated() { |
207 |
jsr166 |
1.30 |
terminatedCalled.countDown(); |
208 |
dl |
1.1 |
} |
209 |
jsr166 |
1.2 |
|
210 |
jsr166 |
1.30 |
public boolean beforeCalled() { |
211 |
|
|
return beforeCalled.getCount() == 0; |
212 |
|
|
} |
213 |
|
|
public boolean afterCalled() { |
214 |
|
|
return afterCalled.getCount() == 0; |
215 |
|
|
} |
216 |
|
|
public boolean terminatedCalled() { |
217 |
|
|
return terminatedCalled.getCount() == 0; |
218 |
|
|
} |
219 |
dl |
1.1 |
} |
220 |
|
|
|
221 |
jsr166 |
1.4 |
static class FailingThreadFactory implements ThreadFactory { |
222 |
dl |
1.1 |
int calls = 0; |
223 |
jsr166 |
1.4 |
public Thread newThread(Runnable r) { |
224 |
dl |
1.1 |
if (++calls > 1) return null; |
225 |
|
|
return new Thread(r); |
226 |
jsr166 |
1.2 |
} |
227 |
dl |
1.1 |
} |
228 |
jsr166 |
1.2 |
|
229 |
dl |
1.1 |
/** |
230 |
jsr166 |
1.19 |
* execute successfully executes a runnable |
231 |
dl |
1.1 |
*/ |
232 |
jsr166 |
1.5 |
public void testExecute() throws InterruptedException { |
233 |
jsr166 |
1.50 |
final ThreadPoolExecutor p = |
234 |
|
|
new CustomTPE(1, 1, |
235 |
|
|
2 * LONG_DELAY_MS, MILLISECONDS, |
236 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
237 |
jsr166 |
1.51 |
try (PoolCleaner cleaner = cleaner(p)) { |
238 |
jsr166 |
1.48 |
final CountDownLatch done = new CountDownLatch(1); |
239 |
|
|
final Runnable task = new CheckedRunnable() { |
240 |
jsr166 |
1.50 |
public void realRun() { done.countDown(); }}; |
241 |
jsr166 |
1.21 |
p.execute(task); |
242 |
jsr166 |
1.48 |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
243 |
jsr166 |
1.2 |
} |
244 |
dl |
1.1 |
} |
245 |
|
|
|
246 |
|
|
/** |
247 |
jsr166 |
1.19 |
* getActiveCount increases but doesn't overestimate, when a |
248 |
|
|
* thread becomes active |
249 |
dl |
1.1 |
*/ |
250 |
jsr166 |
1.5 |
public void testGetActiveCount() throws InterruptedException { |
251 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
252 |
|
|
new CustomTPE(2, 2, |
253 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
254 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
255 |
jsr166 |
1.51 |
try (PoolCleaner cleaner = cleaner(p)) { |
256 |
jsr166 |
1.66 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
257 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
258 |
jsr166 |
1.21 |
assertEquals(0, p.getActiveCount()); |
259 |
|
|
p.execute(new CheckedRunnable() { |
260 |
|
|
public void realRun() throws InterruptedException { |
261 |
|
|
threadStarted.countDown(); |
262 |
|
|
assertEquals(1, p.getActiveCount()); |
263 |
|
|
done.await(); |
264 |
|
|
}}); |
265 |
jsr166 |
1.86 |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
266 |
jsr166 |
1.21 |
assertEquals(1, p.getActiveCount()); |
267 |
|
|
done.countDown(); |
268 |
|
|
} |
269 |
dl |
1.1 |
} |
270 |
|
|
|
271 |
|
|
/** |
272 |
jsr166 |
1.19 |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
273 |
dl |
1.1 |
*/ |
274 |
|
|
public void testPrestartCoreThread() { |
275 |
jsr166 |
1.80 |
final ThreadPoolExecutor p = |
276 |
jsr166 |
1.52 |
new CustomTPE(2, 6, |
277 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
278 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
279 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
280 |
|
|
assertEquals(0, p.getPoolSize()); |
281 |
|
|
assertTrue(p.prestartCoreThread()); |
282 |
|
|
assertEquals(1, p.getPoolSize()); |
283 |
|
|
assertTrue(p.prestartCoreThread()); |
284 |
|
|
assertEquals(2, p.getPoolSize()); |
285 |
|
|
assertFalse(p.prestartCoreThread()); |
286 |
|
|
assertEquals(2, p.getPoolSize()); |
287 |
|
|
p.setCorePoolSize(4); |
288 |
|
|
assertTrue(p.prestartCoreThread()); |
289 |
|
|
assertEquals(3, p.getPoolSize()); |
290 |
|
|
assertTrue(p.prestartCoreThread()); |
291 |
|
|
assertEquals(4, p.getPoolSize()); |
292 |
|
|
assertFalse(p.prestartCoreThread()); |
293 |
|
|
assertEquals(4, p.getPoolSize()); |
294 |
|
|
} |
295 |
dl |
1.1 |
} |
296 |
|
|
|
297 |
|
|
/** |
298 |
jsr166 |
1.19 |
* prestartAllCoreThreads starts all corePoolSize threads |
299 |
dl |
1.1 |
*/ |
300 |
|
|
public void testPrestartAllCoreThreads() { |
301 |
jsr166 |
1.80 |
final ThreadPoolExecutor p = |
302 |
jsr166 |
1.53 |
new CustomTPE(2, 6, |
303 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
304 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
305 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
306 |
|
|
assertEquals(0, p.getPoolSize()); |
307 |
|
|
p.prestartAllCoreThreads(); |
308 |
|
|
assertEquals(2, p.getPoolSize()); |
309 |
|
|
p.prestartAllCoreThreads(); |
310 |
|
|
assertEquals(2, p.getPoolSize()); |
311 |
|
|
p.setCorePoolSize(4); |
312 |
|
|
p.prestartAllCoreThreads(); |
313 |
|
|
assertEquals(4, p.getPoolSize()); |
314 |
|
|
p.prestartAllCoreThreads(); |
315 |
|
|
assertEquals(4, p.getPoolSize()); |
316 |
|
|
} |
317 |
dl |
1.1 |
} |
318 |
jsr166 |
1.2 |
|
319 |
dl |
1.1 |
/** |
320 |
jsr166 |
1.19 |
* getCompletedTaskCount increases, but doesn't overestimate, |
321 |
|
|
* when tasks complete |
322 |
dl |
1.1 |
*/ |
323 |
jsr166 |
1.5 |
public void testGetCompletedTaskCount() throws InterruptedException { |
324 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
325 |
|
|
new CustomTPE(2, 2, |
326 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
327 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
328 |
jsr166 |
1.54 |
try (PoolCleaner cleaner = cleaner(p)) { |
329 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
330 |
|
|
final CountDownLatch threadProceed = new CountDownLatch(1); |
331 |
|
|
final CountDownLatch threadDone = new CountDownLatch(1); |
332 |
jsr166 |
1.21 |
assertEquals(0, p.getCompletedTaskCount()); |
333 |
|
|
p.execute(new CheckedRunnable() { |
334 |
|
|
public void realRun() throws InterruptedException { |
335 |
|
|
threadStarted.countDown(); |
336 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
337 |
|
|
threadProceed.await(); |
338 |
|
|
threadDone.countDown(); |
339 |
|
|
}}); |
340 |
jsr166 |
1.30 |
await(threadStarted); |
341 |
jsr166 |
1.21 |
assertEquals(0, p.getCompletedTaskCount()); |
342 |
|
|
threadProceed.countDown(); |
343 |
|
|
threadDone.await(); |
344 |
jsr166 |
1.30 |
long startTime = System.nanoTime(); |
345 |
|
|
while (p.getCompletedTaskCount() != 1) { |
346 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
347 |
|
|
fail("timed out"); |
348 |
|
|
Thread.yield(); |
349 |
|
|
} |
350 |
jsr166 |
1.21 |
} |
351 |
dl |
1.1 |
} |
352 |
jsr166 |
1.2 |
|
353 |
dl |
1.1 |
/** |
354 |
jsr166 |
1.19 |
* getCorePoolSize returns size given in constructor if not otherwise set |
355 |
dl |
1.1 |
*/ |
356 |
|
|
public void testGetCorePoolSize() { |
357 |
jsr166 |
1.59 |
final ThreadPoolExecutor p = |
358 |
jsr166 |
1.55 |
new CustomTPE(1, 1, |
359 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
360 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
361 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
362 |
|
|
assertEquals(1, p.getCorePoolSize()); |
363 |
|
|
} |
364 |
dl |
1.1 |
} |
365 |
jsr166 |
1.2 |
|
366 |
dl |
1.1 |
/** |
367 |
jsr166 |
1.19 |
* getKeepAliveTime returns value given in constructor if not otherwise set |
368 |
dl |
1.1 |
*/ |
369 |
|
|
public void testGetKeepAliveTime() { |
370 |
jsr166 |
1.59 |
final ThreadPoolExecutor p = |
371 |
jsr166 |
1.56 |
new CustomTPE(2, 2, |
372 |
|
|
1000, MILLISECONDS, |
373 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
374 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
375 |
|
|
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
376 |
|
|
} |
377 |
dl |
1.1 |
} |
378 |
|
|
|
379 |
jsr166 |
1.2 |
/** |
380 |
dl |
1.1 |
* getThreadFactory returns factory in constructor if not set |
381 |
|
|
*/ |
382 |
|
|
public void testGetThreadFactory() { |
383 |
jsr166 |
1.59 |
final ThreadFactory threadFactory = new SimpleThreadFactory(); |
384 |
|
|
final ThreadPoolExecutor p = |
385 |
jsr166 |
1.57 |
new CustomTPE(1, 2, |
386 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
387 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
388 |
|
|
threadFactory, |
389 |
|
|
new NoOpREHandler()); |
390 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
391 |
|
|
assertSame(threadFactory, p.getThreadFactory()); |
392 |
|
|
} |
393 |
dl |
1.1 |
} |
394 |
|
|
|
395 |
jsr166 |
1.2 |
/** |
396 |
dl |
1.1 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
397 |
|
|
*/ |
398 |
|
|
public void testSetThreadFactory() { |
399 |
jsr166 |
1.59 |
final ThreadPoolExecutor p = |
400 |
jsr166 |
1.57 |
new CustomTPE(1, 2, |
401 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
402 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
403 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
404 |
|
|
ThreadFactory threadFactory = new SimpleThreadFactory(); |
405 |
|
|
p.setThreadFactory(threadFactory); |
406 |
|
|
assertSame(threadFactory, p.getThreadFactory()); |
407 |
|
|
} |
408 |
dl |
1.1 |
} |
409 |
|
|
|
410 |
jsr166 |
1.2 |
/** |
411 |
dl |
1.1 |
* setThreadFactory(null) throws NPE |
412 |
|
|
*/ |
413 |
|
|
public void testSetThreadFactoryNull() { |
414 |
jsr166 |
1.59 |
final ThreadPoolExecutor p = |
415 |
jsr166 |
1.58 |
new CustomTPE(1, 2, |
416 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
417 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
418 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
419 |
|
|
try { |
420 |
|
|
p.setThreadFactory(null); |
421 |
|
|
shouldThrow(); |
422 |
|
|
} catch (NullPointerException success) {} |
423 |
dl |
1.1 |
} |
424 |
|
|
} |
425 |
|
|
|
426 |
jsr166 |
1.2 |
/** |
427 |
dl |
1.1 |
* getRejectedExecutionHandler returns handler in constructor if not set |
428 |
|
|
*/ |
429 |
|
|
public void testGetRejectedExecutionHandler() { |
430 |
jsr166 |
1.60 |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
431 |
jsr166 |
1.59 |
final ThreadPoolExecutor p = |
432 |
|
|
new CustomTPE(1, 2, |
433 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
434 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
435 |
jsr166 |
1.60 |
handler); |
436 |
jsr166 |
1.59 |
try (PoolCleaner cleaner = cleaner(p)) { |
437 |
jsr166 |
1.60 |
assertSame(handler, p.getRejectedExecutionHandler()); |
438 |
jsr166 |
1.59 |
} |
439 |
dl |
1.1 |
} |
440 |
|
|
|
441 |
jsr166 |
1.2 |
/** |
442 |
dl |
1.1 |
* setRejectedExecutionHandler sets the handler returned by |
443 |
|
|
* getRejectedExecutionHandler |
444 |
|
|
*/ |
445 |
|
|
public void testSetRejectedExecutionHandler() { |
446 |
jsr166 |
1.60 |
final ThreadPoolExecutor p = |
447 |
|
|
new CustomTPE(1, 2, |
448 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
449 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
450 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
451 |
|
|
RejectedExecutionHandler handler = new NoOpREHandler(); |
452 |
|
|
p.setRejectedExecutionHandler(handler); |
453 |
|
|
assertSame(handler, p.getRejectedExecutionHandler()); |
454 |
|
|
} |
455 |
dl |
1.1 |
} |
456 |
|
|
|
457 |
jsr166 |
1.2 |
/** |
458 |
dl |
1.1 |
* setRejectedExecutionHandler(null) throws NPE |
459 |
|
|
*/ |
460 |
|
|
public void testSetRejectedExecutionHandlerNull() { |
461 |
jsr166 |
1.61 |
final ThreadPoolExecutor p = |
462 |
|
|
new CustomTPE(1, 2, |
463 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
464 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
465 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
466 |
|
|
try { |
467 |
|
|
p.setRejectedExecutionHandler(null); |
468 |
|
|
shouldThrow(); |
469 |
|
|
} catch (NullPointerException success) {} |
470 |
dl |
1.1 |
} |
471 |
|
|
} |
472 |
|
|
|
473 |
|
|
/** |
474 |
jsr166 |
1.19 |
* getLargestPoolSize increases, but doesn't overestimate, when |
475 |
|
|
* multiple threads active |
476 |
dl |
1.1 |
*/ |
477 |
jsr166 |
1.5 |
public void testGetLargestPoolSize() throws InterruptedException { |
478 |
jsr166 |
1.21 |
final int THREADS = 3; |
479 |
jsr166 |
1.87 |
final CountDownLatch done = new CountDownLatch(1); |
480 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
481 |
|
|
new CustomTPE(THREADS, THREADS, |
482 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
483 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
484 |
jsr166 |
1.87 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
485 |
|
|
assertEquals(0, p.getLargestPoolSize()); |
486 |
jsr166 |
1.62 |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
487 |
jsr166 |
1.21 |
for (int i = 0; i < THREADS; i++) |
488 |
|
|
p.execute(new CheckedRunnable() { |
489 |
|
|
public void realRun() throws InterruptedException { |
490 |
|
|
threadsStarted.countDown(); |
491 |
|
|
done.await(); |
492 |
|
|
assertEquals(THREADS, p.getLargestPoolSize()); |
493 |
|
|
}}); |
494 |
jsr166 |
1.86 |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
495 |
jsr166 |
1.21 |
assertEquals(THREADS, p.getLargestPoolSize()); |
496 |
|
|
} |
497 |
jsr166 |
1.62 |
assertEquals(THREADS, p.getLargestPoolSize()); |
498 |
dl |
1.1 |
} |
499 |
jsr166 |
1.2 |
|
500 |
dl |
1.1 |
/** |
501 |
jsr166 |
1.19 |
* getMaximumPoolSize returns value given in constructor if not |
502 |
|
|
* otherwise set |
503 |
dl |
1.1 |
*/ |
504 |
|
|
public void testGetMaximumPoolSize() { |
505 |
jsr166 |
1.63 |
final ThreadPoolExecutor p = |
506 |
|
|
new CustomTPE(2, 3, |
507 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
508 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
509 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
510 |
|
|
assertEquals(3, p.getMaximumPoolSize()); |
511 |
|
|
p.setMaximumPoolSize(5); |
512 |
|
|
assertEquals(5, p.getMaximumPoolSize()); |
513 |
|
|
p.setMaximumPoolSize(4); |
514 |
|
|
assertEquals(4, p.getMaximumPoolSize()); |
515 |
|
|
} |
516 |
dl |
1.1 |
} |
517 |
jsr166 |
1.2 |
|
518 |
dl |
1.1 |
/** |
519 |
jsr166 |
1.19 |
* getPoolSize increases, but doesn't overestimate, when threads |
520 |
|
|
* become active |
521 |
dl |
1.1 |
*/ |
522 |
jsr166 |
1.21 |
public void testGetPoolSize() throws InterruptedException { |
523 |
jsr166 |
1.87 |
final CountDownLatch done = new CountDownLatch(1); |
524 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
525 |
|
|
new CustomTPE(1, 1, |
526 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
527 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
528 |
jsr166 |
1.87 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
529 |
|
|
assertEquals(0, p.getPoolSize()); |
530 |
jsr166 |
1.64 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
531 |
jsr166 |
1.21 |
p.execute(new CheckedRunnable() { |
532 |
|
|
public void realRun() throws InterruptedException { |
533 |
|
|
threadStarted.countDown(); |
534 |
|
|
assertEquals(1, p.getPoolSize()); |
535 |
|
|
done.await(); |
536 |
|
|
}}); |
537 |
jsr166 |
1.86 |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
538 |
jsr166 |
1.21 |
assertEquals(1, p.getPoolSize()); |
539 |
|
|
} |
540 |
dl |
1.1 |
} |
541 |
jsr166 |
1.2 |
|
542 |
dl |
1.1 |
/** |
543 |
jsr166 |
1.19 |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
544 |
dl |
1.1 |
*/ |
545 |
jsr166 |
1.5 |
public void testGetTaskCount() throws InterruptedException { |
546 |
jsr166 |
1.84 |
final int TASKS = 3; |
547 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
548 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
549 |
|
|
new CustomTPE(1, 1, |
550 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
551 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
552 |
jsr166 |
1.84 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
553 |
jsr166 |
1.67 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
554 |
jsr166 |
1.21 |
assertEquals(0, p.getTaskCount()); |
555 |
jsr166 |
1.84 |
assertEquals(0, p.getCompletedTaskCount()); |
556 |
jsr166 |
1.21 |
p.execute(new CheckedRunnable() { |
557 |
|
|
public void realRun() throws InterruptedException { |
558 |
|
|
threadStarted.countDown(); |
559 |
|
|
done.await(); |
560 |
|
|
}}); |
561 |
jsr166 |
1.84 |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
562 |
jsr166 |
1.21 |
assertEquals(1, p.getTaskCount()); |
563 |
jsr166 |
1.84 |
assertEquals(0, p.getCompletedTaskCount()); |
564 |
|
|
for (int i = 0; i < TASKS; i++) { |
565 |
|
|
assertEquals(1 + i, p.getTaskCount()); |
566 |
|
|
p.execute(new CheckedRunnable() { |
567 |
|
|
public void realRun() throws InterruptedException { |
568 |
|
|
threadStarted.countDown(); |
569 |
|
|
assertEquals(1 + TASKS, p.getTaskCount()); |
570 |
|
|
done.await(); |
571 |
|
|
}}); |
572 |
|
|
} |
573 |
|
|
assertEquals(1 + TASKS, p.getTaskCount()); |
574 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
575 |
jsr166 |
1.21 |
} |
576 |
jsr166 |
1.84 |
assertEquals(1 + TASKS, p.getTaskCount()); |
577 |
|
|
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
578 |
dl |
1.1 |
} |
579 |
jsr166 |
1.2 |
|
580 |
dl |
1.1 |
/** |
581 |
jsr166 |
1.28 |
* isShutdown is false before shutdown, true after |
582 |
dl |
1.1 |
*/ |
583 |
|
|
public void testIsShutdown() { |
584 |
jsr166 |
1.68 |
final ThreadPoolExecutor p = |
585 |
|
|
new CustomTPE(1, 1, |
586 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
587 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
588 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
589 |
|
|
assertFalse(p.isShutdown()); |
590 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
591 |
|
|
assertTrue(p.isShutdown()); |
592 |
|
|
} |
593 |
dl |
1.1 |
} |
594 |
|
|
|
595 |
|
|
/** |
596 |
jsr166 |
1.19 |
* isTerminated is false before termination, true after |
597 |
dl |
1.1 |
*/ |
598 |
jsr166 |
1.5 |
public void testIsTerminated() throws InterruptedException { |
599 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
600 |
|
|
new CustomTPE(1, 1, |
601 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
602 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
603 |
jsr166 |
1.69 |
try (PoolCleaner cleaner = cleaner(p)) { |
604 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
605 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
606 |
jsr166 |
1.21 |
assertFalse(p.isTerminating()); |
607 |
|
|
p.execute(new CheckedRunnable() { |
608 |
|
|
public void realRun() throws InterruptedException { |
609 |
jsr166 |
1.24 |
assertFalse(p.isTerminating()); |
610 |
jsr166 |
1.21 |
threadStarted.countDown(); |
611 |
|
|
done.await(); |
612 |
|
|
}}); |
613 |
jsr166 |
1.86 |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
614 |
jsr166 |
1.21 |
assertFalse(p.isTerminating()); |
615 |
|
|
done.countDown(); |
616 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
617 |
jsr166 |
1.69 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
618 |
|
|
assertTrue(p.isTerminated()); |
619 |
|
|
assertFalse(p.isTerminating()); |
620 |
jsr166 |
1.21 |
} |
621 |
dl |
1.1 |
} |
622 |
|
|
|
623 |
|
|
/** |
624 |
jsr166 |
1.19 |
* isTerminating is not true when running or when terminated |
625 |
dl |
1.1 |
*/ |
626 |
jsr166 |
1.5 |
public void testIsTerminating() throws InterruptedException { |
627 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
628 |
|
|
new CustomTPE(1, 1, |
629 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
630 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
631 |
jsr166 |
1.70 |
try (PoolCleaner cleaner = cleaner(p)) { |
632 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
633 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
634 |
jsr166 |
1.21 |
assertFalse(p.isTerminating()); |
635 |
|
|
p.execute(new CheckedRunnable() { |
636 |
|
|
public void realRun() throws InterruptedException { |
637 |
jsr166 |
1.23 |
assertFalse(p.isTerminating()); |
638 |
jsr166 |
1.21 |
threadStarted.countDown(); |
639 |
|
|
done.await(); |
640 |
|
|
}}); |
641 |
jsr166 |
1.86 |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
642 |
jsr166 |
1.21 |
assertFalse(p.isTerminating()); |
643 |
|
|
done.countDown(); |
644 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
645 |
jsr166 |
1.70 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
646 |
|
|
assertTrue(p.isTerminated()); |
647 |
|
|
assertFalse(p.isTerminating()); |
648 |
jsr166 |
1.21 |
} |
649 |
dl |
1.1 |
} |
650 |
|
|
|
651 |
|
|
/** |
652 |
|
|
* getQueue returns the work queue, which contains queued tasks |
653 |
|
|
*/ |
654 |
jsr166 |
1.5 |
public void testGetQueue() throws InterruptedException { |
655 |
jsr166 |
1.21 |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
656 |
|
|
final ThreadPoolExecutor p = |
657 |
|
|
new CustomTPE(1, 1, |
658 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
659 |
|
|
q); |
660 |
jsr166 |
1.71 |
try (PoolCleaner cleaner = cleaner(p)) { |
661 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
662 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
663 |
jsr166 |
1.21 |
FutureTask[] tasks = new FutureTask[5]; |
664 |
|
|
for (int i = 0; i < tasks.length; i++) { |
665 |
|
|
Callable task = new CheckedCallable<Boolean>() { |
666 |
|
|
public Boolean realCall() throws InterruptedException { |
667 |
|
|
threadStarted.countDown(); |
668 |
|
|
assertSame(q, p.getQueue()); |
669 |
|
|
done.await(); |
670 |
|
|
return Boolean.TRUE; |
671 |
|
|
}}; |
672 |
|
|
tasks[i] = new FutureTask(task); |
673 |
|
|
p.execute(tasks[i]); |
674 |
|
|
} |
675 |
jsr166 |
1.86 |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
676 |
jsr166 |
1.21 |
assertSame(q, p.getQueue()); |
677 |
|
|
assertFalse(q.contains(tasks[0])); |
678 |
|
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
679 |
|
|
assertEquals(tasks.length - 1, q.size()); |
680 |
|
|
done.countDown(); |
681 |
dl |
1.1 |
} |
682 |
|
|
} |
683 |
|
|
|
684 |
|
|
/** |
685 |
|
|
* remove(task) removes queued task, and fails to remove active task |
686 |
|
|
*/ |
687 |
jsr166 |
1.5 |
public void testRemove() throws InterruptedException { |
688 |
dl |
1.1 |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
689 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
690 |
|
|
new CustomTPE(1, 1, |
691 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
692 |
|
|
q); |
693 |
jsr166 |
1.72 |
try (PoolCleaner cleaner = cleaner(p)) { |
694 |
|
|
Runnable[] tasks = new Runnable[6]; |
695 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
696 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
697 |
jsr166 |
1.21 |
for (int i = 0; i < tasks.length; i++) { |
698 |
|
|
tasks[i] = new CheckedRunnable() { |
699 |
jsr166 |
1.72 |
public void realRun() throws InterruptedException { |
700 |
|
|
threadStarted.countDown(); |
701 |
|
|
done.await(); |
702 |
|
|
}}; |
703 |
jsr166 |
1.21 |
p.execute(tasks[i]); |
704 |
|
|
} |
705 |
jsr166 |
1.86 |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
706 |
jsr166 |
1.21 |
assertFalse(p.remove(tasks[0])); |
707 |
dl |
1.1 |
assertTrue(q.contains(tasks[4])); |
708 |
|
|
assertTrue(q.contains(tasks[3])); |
709 |
jsr166 |
1.21 |
assertTrue(p.remove(tasks[4])); |
710 |
|
|
assertFalse(p.remove(tasks[4])); |
711 |
dl |
1.1 |
assertFalse(q.contains(tasks[4])); |
712 |
|
|
assertTrue(q.contains(tasks[3])); |
713 |
jsr166 |
1.21 |
assertTrue(p.remove(tasks[3])); |
714 |
dl |
1.1 |
assertFalse(q.contains(tasks[3])); |
715 |
jsr166 |
1.21 |
done.countDown(); |
716 |
dl |
1.1 |
} |
717 |
|
|
} |
718 |
|
|
|
719 |
|
|
/** |
720 |
jsr166 |
1.19 |
* purge removes cancelled tasks from the queue |
721 |
dl |
1.1 |
*/ |
722 |
jsr166 |
1.21 |
public void testPurge() throws InterruptedException { |
723 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
724 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
725 |
|
|
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
726 |
|
|
final ThreadPoolExecutor p = |
727 |
|
|
new CustomTPE(1, 1, |
728 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
729 |
|
|
q); |
730 |
jsr166 |
1.85 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
731 |
jsr166 |
1.72 |
FutureTask[] tasks = new FutureTask[5]; |
732 |
jsr166 |
1.21 |
for (int i = 0; i < tasks.length; i++) { |
733 |
|
|
Callable task = new CheckedCallable<Boolean>() { |
734 |
|
|
public Boolean realCall() throws InterruptedException { |
735 |
|
|
threadStarted.countDown(); |
736 |
|
|
done.await(); |
737 |
|
|
return Boolean.TRUE; |
738 |
|
|
}}; |
739 |
|
|
tasks[i] = new FutureTask(task); |
740 |
|
|
p.execute(tasks[i]); |
741 |
|
|
} |
742 |
jsr166 |
1.85 |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
743 |
jsr166 |
1.21 |
assertEquals(tasks.length, p.getTaskCount()); |
744 |
|
|
assertEquals(tasks.length - 1, q.size()); |
745 |
|
|
assertEquals(1L, p.getActiveCount()); |
746 |
|
|
assertEquals(0L, p.getCompletedTaskCount()); |
747 |
|
|
tasks[4].cancel(true); |
748 |
|
|
tasks[3].cancel(false); |
749 |
|
|
p.purge(); |
750 |
|
|
assertEquals(tasks.length - 3, q.size()); |
751 |
|
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
752 |
|
|
p.purge(); // Nothing to do |
753 |
|
|
assertEquals(tasks.length - 3, q.size()); |
754 |
|
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
755 |
|
|
} |
756 |
dl |
1.1 |
} |
757 |
|
|
|
758 |
|
|
/** |
759 |
jsr166 |
1.40 |
* shutdownNow returns a list containing tasks that were not run, |
760 |
|
|
* and those tasks are drained from the queue |
761 |
dl |
1.1 |
*/ |
762 |
jsr166 |
1.41 |
public void testShutdownNow() throws InterruptedException { |
763 |
|
|
final int poolSize = 2; |
764 |
|
|
final int count = 5; |
765 |
|
|
final AtomicInteger ran = new AtomicInteger(0); |
766 |
jsr166 |
1.80 |
final ThreadPoolExecutor p = |
767 |
jsr166 |
1.81 |
new CustomTPE(poolSize, poolSize, |
768 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
769 |
jsr166 |
1.41 |
new ArrayBlockingQueue<Runnable>(10)); |
770 |
jsr166 |
1.83 |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
771 |
jsr166 |
1.43 |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
772 |
jsr166 |
1.41 |
threadsStarted.countDown(); |
773 |
dl |
1.1 |
try { |
774 |
jsr166 |
1.41 |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
775 |
|
|
} catch (InterruptedException success) {} |
776 |
|
|
ran.getAndIncrement(); |
777 |
|
|
}}; |
778 |
|
|
for (int i = 0; i < count; i++) |
779 |
|
|
p.execute(waiter); |
780 |
|
|
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
781 |
jsr166 |
1.42 |
assertEquals(poolSize, p.getActiveCount()); |
782 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
783 |
jsr166 |
1.41 |
final List<Runnable> queuedTasks; |
784 |
|
|
try { |
785 |
|
|
queuedTasks = p.shutdownNow(); |
786 |
|
|
} catch (SecurityException ok) { |
787 |
|
|
return; // Allowed in case test doesn't have privs |
788 |
dl |
1.1 |
} |
789 |
jsr166 |
1.21 |
assertTrue(p.isShutdown()); |
790 |
jsr166 |
1.40 |
assertTrue(p.getQueue().isEmpty()); |
791 |
jsr166 |
1.41 |
assertEquals(count - poolSize, queuedTasks.size()); |
792 |
|
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
793 |
|
|
assertTrue(p.isTerminated()); |
794 |
|
|
assertEquals(poolSize, ran.get()); |
795 |
jsr166 |
1.42 |
assertEquals(poolSize, p.getCompletedTaskCount()); |
796 |
dl |
1.1 |
} |
797 |
|
|
|
798 |
|
|
// Exception Tests |
799 |
|
|
|
800 |
jsr166 |
1.2 |
/** |
801 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
802 |
dl |
1.1 |
*/ |
803 |
|
|
public void testConstructor1() { |
804 |
|
|
try { |
805 |
jsr166 |
1.36 |
new CustomTPE(-1, 1, 1L, SECONDS, |
806 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
807 |
dl |
1.1 |
shouldThrow(); |
808 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
809 |
dl |
1.1 |
} |
810 |
jsr166 |
1.2 |
|
811 |
|
|
/** |
812 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
813 |
dl |
1.1 |
*/ |
814 |
|
|
public void testConstructor2() { |
815 |
|
|
try { |
816 |
jsr166 |
1.36 |
new CustomTPE(1, -1, 1L, SECONDS, |
817 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
818 |
dl |
1.1 |
shouldThrow(); |
819 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
820 |
dl |
1.1 |
} |
821 |
jsr166 |
1.2 |
|
822 |
|
|
/** |
823 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
824 |
dl |
1.1 |
*/ |
825 |
|
|
public void testConstructor3() { |
826 |
|
|
try { |
827 |
jsr166 |
1.36 |
new CustomTPE(1, 0, 1L, SECONDS, |
828 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
829 |
dl |
1.1 |
shouldThrow(); |
830 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
831 |
dl |
1.1 |
} |
832 |
|
|
|
833 |
jsr166 |
1.2 |
/** |
834 |
|
|
* Constructor throws if keepAliveTime is less than zero |
835 |
dl |
1.1 |
*/ |
836 |
|
|
public void testConstructor4() { |
837 |
|
|
try { |
838 |
jsr166 |
1.36 |
new CustomTPE(1, 2, -1L, SECONDS, |
839 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
840 |
dl |
1.1 |
shouldThrow(); |
841 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
842 |
dl |
1.1 |
} |
843 |
|
|
|
844 |
jsr166 |
1.2 |
/** |
845 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
846 |
dl |
1.1 |
*/ |
847 |
|
|
public void testConstructor5() { |
848 |
|
|
try { |
849 |
jsr166 |
1.36 |
new CustomTPE(2, 1, 1L, SECONDS, |
850 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
851 |
dl |
1.1 |
shouldThrow(); |
852 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
853 |
dl |
1.1 |
} |
854 |
jsr166 |
1.2 |
|
855 |
|
|
/** |
856 |
|
|
* Constructor throws if workQueue is set to null |
857 |
dl |
1.1 |
*/ |
858 |
|
|
public void testConstructorNullPointerException() { |
859 |
|
|
try { |
860 |
jsr166 |
1.36 |
new CustomTPE(1, 2, 1L, SECONDS, null); |
861 |
dl |
1.1 |
shouldThrow(); |
862 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
863 |
dl |
1.1 |
} |
864 |
|
|
|
865 |
jsr166 |
1.2 |
/** |
866 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
867 |
dl |
1.1 |
*/ |
868 |
|
|
public void testConstructor6() { |
869 |
|
|
try { |
870 |
jsr166 |
1.36 |
new CustomTPE(-1, 1, 1L, SECONDS, |
871 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
872 |
|
|
new SimpleThreadFactory()); |
873 |
dl |
1.1 |
shouldThrow(); |
874 |
jsr166 |
1.4 |
} catch (IllegalArgumentException success) {} |
875 |
dl |
1.1 |
} |
876 |
jsr166 |
1.2 |
|
877 |
|
|
/** |
878 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
879 |
dl |
1.1 |
*/ |
880 |
|
|
public void testConstructor7() { |
881 |
|
|
try { |
882 |
jsr166 |
1.36 |
new CustomTPE(1,-1, 1L, SECONDS, |
883 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
884 |
|
|
new SimpleThreadFactory()); |
885 |
dl |
1.1 |
shouldThrow(); |
886 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
887 |
dl |
1.1 |
} |
888 |
|
|
|
889 |
jsr166 |
1.2 |
/** |
890 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
891 |
dl |
1.1 |
*/ |
892 |
|
|
public void testConstructor8() { |
893 |
|
|
try { |
894 |
jsr166 |
1.36 |
new CustomTPE(1, 0, 1L, SECONDS, |
895 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
896 |
|
|
new SimpleThreadFactory()); |
897 |
dl |
1.1 |
shouldThrow(); |
898 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
899 |
dl |
1.1 |
} |
900 |
|
|
|
901 |
jsr166 |
1.2 |
/** |
902 |
|
|
* Constructor throws if keepAliveTime is less than zero |
903 |
dl |
1.1 |
*/ |
904 |
|
|
public void testConstructor9() { |
905 |
|
|
try { |
906 |
jsr166 |
1.36 |
new CustomTPE(1, 2, -1L, SECONDS, |
907 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
908 |
|
|
new SimpleThreadFactory()); |
909 |
dl |
1.1 |
shouldThrow(); |
910 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
911 |
dl |
1.1 |
} |
912 |
|
|
|
913 |
jsr166 |
1.2 |
/** |
914 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
915 |
dl |
1.1 |
*/ |
916 |
|
|
public void testConstructor10() { |
917 |
|
|
try { |
918 |
jsr166 |
1.36 |
new CustomTPE(2, 1, 1L, SECONDS, |
919 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
920 |
|
|
new SimpleThreadFactory()); |
921 |
dl |
1.1 |
shouldThrow(); |
922 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
923 |
dl |
1.1 |
} |
924 |
|
|
|
925 |
jsr166 |
1.2 |
/** |
926 |
|
|
* Constructor throws if workQueue is set to null |
927 |
dl |
1.1 |
*/ |
928 |
|
|
public void testConstructorNullPointerException2() { |
929 |
|
|
try { |
930 |
jsr166 |
1.36 |
new CustomTPE(1, 2, 1L, SECONDS, null, new SimpleThreadFactory()); |
931 |
dl |
1.1 |
shouldThrow(); |
932 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
933 |
dl |
1.1 |
} |
934 |
|
|
|
935 |
jsr166 |
1.2 |
/** |
936 |
|
|
* Constructor throws if threadFactory is set to null |
937 |
dl |
1.1 |
*/ |
938 |
|
|
public void testConstructorNullPointerException3() { |
939 |
|
|
try { |
940 |
jsr166 |
1.36 |
new CustomTPE(1, 2, 1L, SECONDS, |
941 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
942 |
|
|
(ThreadFactory) null); |
943 |
dl |
1.1 |
shouldThrow(); |
944 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
945 |
dl |
1.1 |
} |
946 |
jsr166 |
1.2 |
|
947 |
|
|
/** |
948 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
949 |
dl |
1.1 |
*/ |
950 |
|
|
public void testConstructor11() { |
951 |
|
|
try { |
952 |
jsr166 |
1.36 |
new CustomTPE(-1, 1, 1L, SECONDS, |
953 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
954 |
|
|
new NoOpREHandler()); |
955 |
dl |
1.1 |
shouldThrow(); |
956 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
957 |
dl |
1.1 |
} |
958 |
|
|
|
959 |
jsr166 |
1.2 |
/** |
960 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
961 |
dl |
1.1 |
*/ |
962 |
|
|
public void testConstructor12() { |
963 |
|
|
try { |
964 |
jsr166 |
1.36 |
new CustomTPE(1, -1, 1L, SECONDS, |
965 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
966 |
|
|
new NoOpREHandler()); |
967 |
dl |
1.1 |
shouldThrow(); |
968 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
969 |
dl |
1.1 |
} |
970 |
|
|
|
971 |
jsr166 |
1.2 |
/** |
972 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
973 |
dl |
1.1 |
*/ |
974 |
|
|
public void testConstructor13() { |
975 |
|
|
try { |
976 |
jsr166 |
1.36 |
new CustomTPE(1, 0, 1L, SECONDS, |
977 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
978 |
|
|
new NoOpREHandler()); |
979 |
dl |
1.1 |
shouldThrow(); |
980 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
981 |
dl |
1.1 |
} |
982 |
|
|
|
983 |
jsr166 |
1.2 |
/** |
984 |
|
|
* Constructor throws if keepAliveTime is less than zero |
985 |
dl |
1.1 |
*/ |
986 |
|
|
public void testConstructor14() { |
987 |
|
|
try { |
988 |
jsr166 |
1.36 |
new CustomTPE(1, 2, -1L, SECONDS, |
989 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
990 |
|
|
new NoOpREHandler()); |
991 |
dl |
1.1 |
shouldThrow(); |
992 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
993 |
dl |
1.1 |
} |
994 |
|
|
|
995 |
jsr166 |
1.2 |
/** |
996 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
997 |
dl |
1.1 |
*/ |
998 |
|
|
public void testConstructor15() { |
999 |
|
|
try { |
1000 |
jsr166 |
1.36 |
new CustomTPE(2, 1, 1L, SECONDS, |
1001 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
1002 |
|
|
new NoOpREHandler()); |
1003 |
dl |
1.1 |
shouldThrow(); |
1004 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
1005 |
dl |
1.1 |
} |
1006 |
|
|
|
1007 |
jsr166 |
1.2 |
/** |
1008 |
|
|
* Constructor throws if workQueue is set to null |
1009 |
dl |
1.1 |
*/ |
1010 |
|
|
public void testConstructorNullPointerException4() { |
1011 |
|
|
try { |
1012 |
jsr166 |
1.36 |
new CustomTPE(1, 2, 1L, SECONDS, |
1013 |
|
|
null, |
1014 |
|
|
new NoOpREHandler()); |
1015 |
dl |
1.1 |
shouldThrow(); |
1016 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
1017 |
dl |
1.1 |
} |
1018 |
|
|
|
1019 |
jsr166 |
1.2 |
/** |
1020 |
|
|
* Constructor throws if handler is set to null |
1021 |
dl |
1.1 |
*/ |
1022 |
|
|
public void testConstructorNullPointerException5() { |
1023 |
|
|
try { |
1024 |
jsr166 |
1.36 |
new CustomTPE(1, 2, 1L, SECONDS, |
1025 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
1026 |
|
|
(RejectedExecutionHandler) null); |
1027 |
dl |
1.1 |
shouldThrow(); |
1028 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
1029 |
dl |
1.1 |
} |
1030 |
|
|
|
1031 |
jsr166 |
1.2 |
/** |
1032 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
1033 |
dl |
1.1 |
*/ |
1034 |
|
|
public void testConstructor16() { |
1035 |
|
|
try { |
1036 |
jsr166 |
1.36 |
new CustomTPE(-1, 1, 1L, SECONDS, |
1037 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
1038 |
|
|
new SimpleThreadFactory(), |
1039 |
|
|
new NoOpREHandler()); |
1040 |
dl |
1.1 |
shouldThrow(); |
1041 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
1042 |
dl |
1.1 |
} |
1043 |
|
|
|
1044 |
jsr166 |
1.2 |
/** |
1045 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
1046 |
dl |
1.1 |
*/ |
1047 |
|
|
public void testConstructor17() { |
1048 |
|
|
try { |
1049 |
jsr166 |
1.36 |
new CustomTPE(1, -1, 1L, SECONDS, |
1050 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
1051 |
|
|
new SimpleThreadFactory(), |
1052 |
|
|
new NoOpREHandler()); |
1053 |
dl |
1.1 |
shouldThrow(); |
1054 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
1055 |
dl |
1.1 |
} |
1056 |
|
|
|
1057 |
jsr166 |
1.2 |
/** |
1058 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
1059 |
dl |
1.1 |
*/ |
1060 |
|
|
public void testConstructor18() { |
1061 |
|
|
try { |
1062 |
jsr166 |
1.36 |
new CustomTPE(1, 0, 1L, SECONDS, |
1063 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
1064 |
|
|
new SimpleThreadFactory(), |
1065 |
|
|
new NoOpREHandler()); |
1066 |
dl |
1.1 |
shouldThrow(); |
1067 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
1068 |
dl |
1.1 |
} |
1069 |
|
|
|
1070 |
jsr166 |
1.2 |
/** |
1071 |
|
|
* Constructor throws if keepAliveTime is less than zero |
1072 |
dl |
1.1 |
*/ |
1073 |
|
|
public void testConstructor19() { |
1074 |
|
|
try { |
1075 |
jsr166 |
1.36 |
new CustomTPE(1, 2, -1L, SECONDS, |
1076 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
1077 |
|
|
new SimpleThreadFactory(), |
1078 |
|
|
new NoOpREHandler()); |
1079 |
dl |
1.1 |
shouldThrow(); |
1080 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
1081 |
dl |
1.1 |
} |
1082 |
|
|
|
1083 |
jsr166 |
1.2 |
/** |
1084 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
1085 |
dl |
1.1 |
*/ |
1086 |
|
|
public void testConstructor20() { |
1087 |
|
|
try { |
1088 |
jsr166 |
1.36 |
new CustomTPE(2, 1, 1L, SECONDS, |
1089 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
1090 |
|
|
new SimpleThreadFactory(), |
1091 |
|
|
new NoOpREHandler()); |
1092 |
dl |
1.1 |
shouldThrow(); |
1093 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
1094 |
dl |
1.1 |
} |
1095 |
|
|
|
1096 |
jsr166 |
1.2 |
/** |
1097 |
jsr166 |
1.20 |
* Constructor throws if workQueue is null |
1098 |
dl |
1.1 |
*/ |
1099 |
|
|
public void testConstructorNullPointerException6() { |
1100 |
|
|
try { |
1101 |
jsr166 |
1.36 |
new CustomTPE(1, 2, 1L, SECONDS, |
1102 |
|
|
null, |
1103 |
|
|
new SimpleThreadFactory(), |
1104 |
|
|
new NoOpREHandler()); |
1105 |
dl |
1.1 |
shouldThrow(); |
1106 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
1107 |
dl |
1.1 |
} |
1108 |
|
|
|
1109 |
jsr166 |
1.2 |
/** |
1110 |
jsr166 |
1.20 |
* Constructor throws if handler is null |
1111 |
dl |
1.1 |
*/ |
1112 |
|
|
public void testConstructorNullPointerException7() { |
1113 |
|
|
try { |
1114 |
jsr166 |
1.36 |
new CustomTPE(1, 2, 1L, SECONDS, |
1115 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
1116 |
|
|
new SimpleThreadFactory(), |
1117 |
|
|
(RejectedExecutionHandler) null); |
1118 |
dl |
1.1 |
shouldThrow(); |
1119 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
1120 |
dl |
1.1 |
} |
1121 |
|
|
|
1122 |
jsr166 |
1.2 |
/** |
1123 |
jsr166 |
1.20 |
* Constructor throws if ThreadFactory is null |
1124 |
dl |
1.1 |
*/ |
1125 |
|
|
public void testConstructorNullPointerException8() { |
1126 |
|
|
try { |
1127 |
jsr166 |
1.36 |
new CustomTPE(1, 2, 1L, SECONDS, |
1128 |
jsr166 |
1.21 |
new ArrayBlockingQueue<Runnable>(10), |
1129 |
|
|
(ThreadFactory) null, |
1130 |
|
|
new NoOpREHandler()); |
1131 |
dl |
1.1 |
shouldThrow(); |
1132 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
1133 |
dl |
1.1 |
} |
1134 |
jsr166 |
1.2 |
|
1135 |
dl |
1.1 |
/** |
1136 |
jsr166 |
1.19 |
* execute throws RejectedExecutionException if saturated. |
1137 |
dl |
1.1 |
*/ |
1138 |
|
|
public void testSaturatedExecute() { |
1139 |
jsr166 |
1.73 |
final ThreadPoolExecutor p = |
1140 |
jsr166 |
1.21 |
new CustomTPE(1, 1, |
1141 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1142 |
|
|
new ArrayBlockingQueue<Runnable>(1)); |
1143 |
jsr166 |
1.73 |
try (PoolCleaner cleaner = cleaner(p)) { |
1144 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
1145 |
jsr166 |
1.21 |
Runnable task = new CheckedRunnable() { |
1146 |
|
|
public void realRun() throws InterruptedException { |
1147 |
|
|
done.await(); |
1148 |
|
|
}}; |
1149 |
|
|
for (int i = 0; i < 2; ++i) |
1150 |
|
|
p.execute(task); |
1151 |
|
|
for (int i = 0; i < 2; ++i) { |
1152 |
|
|
try { |
1153 |
|
|
p.execute(task); |
1154 |
|
|
shouldThrow(); |
1155 |
|
|
} catch (RejectedExecutionException success) {} |
1156 |
|
|
assertTrue(p.getTaskCount() <= 2); |
1157 |
dl |
1.1 |
} |
1158 |
jsr166 |
1.21 |
done.countDown(); |
1159 |
|
|
} |
1160 |
dl |
1.1 |
} |
1161 |
|
|
|
1162 |
|
|
/** |
1163 |
jsr166 |
1.19 |
* executor using CallerRunsPolicy runs task if saturated. |
1164 |
dl |
1.1 |
*/ |
1165 |
|
|
public void testSaturatedExecute2() { |
1166 |
jsr166 |
1.74 |
final ThreadPoolExecutor p = |
1167 |
|
|
new CustomTPE(1, 1, |
1168 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1169 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
1170 |
|
|
new CustomTPE.CallerRunsPolicy()); |
1171 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
1172 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
1173 |
|
|
Runnable blocker = new CheckedRunnable() { |
1174 |
|
|
public void realRun() throws InterruptedException { |
1175 |
|
|
done.await(); |
1176 |
|
|
}}; |
1177 |
|
|
p.execute(blocker); |
1178 |
dl |
1.1 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1179 |
jsr166 |
1.74 |
for (int i = 0; i < tasks.length; i++) |
1180 |
dl |
1.1 |
tasks[i] = new TrackedNoOpRunnable(); |
1181 |
jsr166 |
1.74 |
for (int i = 0; i < tasks.length; i++) |
1182 |
dl |
1.1 |
p.execute(tasks[i]); |
1183 |
jsr166 |
1.74 |
for (int i = 1; i < tasks.length; i++) |
1184 |
dl |
1.1 |
assertTrue(tasks[i].done); |
1185 |
jsr166 |
1.75 |
assertFalse(tasks[0].done); // waiting in queue |
1186 |
jsr166 |
1.74 |
done.countDown(); |
1187 |
dl |
1.1 |
} |
1188 |
|
|
} |
1189 |
|
|
|
1190 |
|
|
/** |
1191 |
jsr166 |
1.19 |
* executor using DiscardPolicy drops task if saturated. |
1192 |
dl |
1.1 |
*/ |
1193 |
|
|
public void testSaturatedExecute3() { |
1194 |
jsr166 |
1.75 |
final TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1195 |
|
|
for (int i = 0; i < tasks.length; ++i) |
1196 |
|
|
tasks[i] = new TrackedNoOpRunnable(); |
1197 |
|
|
final ThreadPoolExecutor p = |
1198 |
jsr166 |
1.21 |
new CustomTPE(1, 1, |
1199 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1200 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
1201 |
jsr166 |
1.75 |
new CustomTPE.DiscardPolicy()); |
1202 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
1203 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
1204 |
|
|
p.execute(awaiter(done)); |
1205 |
|
|
|
1206 |
jsr166 |
1.21 |
for (TrackedNoOpRunnable task : tasks) |
1207 |
|
|
p.execute(task); |
1208 |
jsr166 |
1.75 |
for (int i = 1; i < tasks.length; i++) |
1209 |
|
|
assertFalse(tasks[i].done); |
1210 |
|
|
done.countDown(); |
1211 |
dl |
1.1 |
} |
1212 |
jsr166 |
1.75 |
for (int i = 1; i < tasks.length; i++) |
1213 |
|
|
assertFalse(tasks[i].done); |
1214 |
|
|
assertTrue(tasks[0].done); // was waiting in queue |
1215 |
dl |
1.1 |
} |
1216 |
|
|
|
1217 |
|
|
/** |
1218 |
jsr166 |
1.19 |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1219 |
dl |
1.1 |
*/ |
1220 |
|
|
public void testSaturatedExecute4() { |
1221 |
jsr166 |
1.76 |
final CountDownLatch done = new CountDownLatch(1); |
1222 |
|
|
LatchAwaiter r1 = awaiter(done); |
1223 |
|
|
LatchAwaiter r2 = awaiter(done); |
1224 |
|
|
LatchAwaiter r3 = awaiter(done); |
1225 |
|
|
final ThreadPoolExecutor p = |
1226 |
|
|
new CustomTPE(1, 1, |
1227 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1228 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
1229 |
|
|
new CustomTPE.DiscardOldestPolicy()); |
1230 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
1231 |
|
|
assertEquals(LatchAwaiter.NEW, r1.state); |
1232 |
|
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1233 |
|
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1234 |
|
|
p.execute(r1); |
1235 |
dl |
1.1 |
p.execute(r2); |
1236 |
|
|
assertTrue(p.getQueue().contains(r2)); |
1237 |
|
|
p.execute(r3); |
1238 |
|
|
assertFalse(p.getQueue().contains(r2)); |
1239 |
|
|
assertTrue(p.getQueue().contains(r3)); |
1240 |
jsr166 |
1.76 |
done.countDown(); |
1241 |
dl |
1.1 |
} |
1242 |
jsr166 |
1.76 |
assertEquals(LatchAwaiter.DONE, r1.state); |
1243 |
|
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1244 |
|
|
assertEquals(LatchAwaiter.DONE, r3.state); |
1245 |
dl |
1.1 |
} |
1246 |
|
|
|
1247 |
|
|
/** |
1248 |
jsr166 |
1.19 |
* execute throws RejectedExecutionException if shutdown |
1249 |
dl |
1.1 |
*/ |
1250 |
|
|
public void testRejectedExecutionExceptionOnShutdown() { |
1251 |
jsr166 |
1.77 |
final ThreadPoolExecutor p = |
1252 |
|
|
new CustomTPE(1, 1, |
1253 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1254 |
|
|
new ArrayBlockingQueue<Runnable>(1)); |
1255 |
jsr166 |
1.21 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1256 |
jsr166 |
1.77 |
try (PoolCleaner cleaner = cleaner(p)) { |
1257 |
|
|
try { |
1258 |
|
|
p.execute(new NoOpRunnable()); |
1259 |
|
|
shouldThrow(); |
1260 |
|
|
} catch (RejectedExecutionException success) {} |
1261 |
|
|
} |
1262 |
dl |
1.1 |
} |
1263 |
|
|
|
1264 |
|
|
/** |
1265 |
jsr166 |
1.19 |
* execute using CallerRunsPolicy drops task on shutdown |
1266 |
dl |
1.1 |
*/ |
1267 |
|
|
public void testCallerRunsOnShutdown() { |
1268 |
jsr166 |
1.79 |
final ThreadPoolExecutor p = |
1269 |
|
|
new CustomTPE(1, 1, |
1270 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1271 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
1272 |
|
|
new CustomTPE.CallerRunsPolicy()); |
1273 |
jsr166 |
1.3 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1274 |
jsr166 |
1.78 |
try (PoolCleaner cleaner = cleaner(p)) { |
1275 |
dl |
1.1 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1276 |
jsr166 |
1.8 |
p.execute(r); |
1277 |
dl |
1.1 |
assertFalse(r.done); |
1278 |
|
|
} |
1279 |
|
|
} |
1280 |
|
|
|
1281 |
|
|
/** |
1282 |
jsr166 |
1.19 |
* execute using DiscardPolicy drops task on shutdown |
1283 |
dl |
1.1 |
*/ |
1284 |
|
|
public void testDiscardOnShutdown() { |
1285 |
jsr166 |
1.79 |
final ThreadPoolExecutor p = |
1286 |
|
|
new CustomTPE(1, 1, |
1287 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1288 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
1289 |
|
|
new CustomTPE.DiscardPolicy()); |
1290 |
jsr166 |
1.3 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1291 |
jsr166 |
1.79 |
try (PoolCleaner cleaner = cleaner(p)) { |
1292 |
dl |
1.1 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1293 |
jsr166 |
1.8 |
p.execute(r); |
1294 |
dl |
1.1 |
assertFalse(r.done); |
1295 |
|
|
} |
1296 |
|
|
} |
1297 |
|
|
|
1298 |
|
|
/** |
1299 |
jsr166 |
1.19 |
* execute using DiscardOldestPolicy drops task on shutdown |
1300 |
dl |
1.1 |
*/ |
1301 |
|
|
public void testDiscardOldestOnShutdown() { |
1302 |
jsr166 |
1.79 |
final ThreadPoolExecutor p = |
1303 |
|
|
new CustomTPE(1, 1, |
1304 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1305 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
1306 |
|
|
new CustomTPE.DiscardOldestPolicy()); |
1307 |
dl |
1.1 |
|
1308 |
jsr166 |
1.3 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1309 |
jsr166 |
1.79 |
try (PoolCleaner cleaner = cleaner(p)) { |
1310 |
dl |
1.1 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1311 |
jsr166 |
1.8 |
p.execute(r); |
1312 |
dl |
1.1 |
assertFalse(r.done); |
1313 |
|
|
} |
1314 |
|
|
} |
1315 |
|
|
|
1316 |
|
|
/** |
1317 |
jsr166 |
1.18 |
* execute(null) throws NPE |
1318 |
dl |
1.1 |
*/ |
1319 |
|
|
public void testExecuteNull() { |
1320 |
jsr166 |
1.79 |
final ThreadPoolExecutor p = |
1321 |
|
|
new CustomTPE(1, 2, |
1322 |
|
|
1L, SECONDS, |
1323 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
1324 |
jsr166 |
1.79 |
try (PoolCleaner cleaner = cleaner(p)) { |
1325 |
|
|
try { |
1326 |
|
|
p.execute(null); |
1327 |
|
|
shouldThrow(); |
1328 |
|
|
} catch (NullPointerException success) {} |
1329 |
|
|
} |
1330 |
dl |
1.1 |
} |
1331 |
jsr166 |
1.2 |
|
1332 |
dl |
1.1 |
/** |
1333 |
jsr166 |
1.19 |
* setCorePoolSize of negative value throws IllegalArgumentException |
1334 |
dl |
1.1 |
*/ |
1335 |
|
|
public void testCorePoolSizeIllegalArgumentException() { |
1336 |
jsr166 |
1.80 |
final ThreadPoolExecutor p = |
1337 |
jsr166 |
1.81 |
new CustomTPE(1, 2, |
1338 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1339 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1340 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
1341 |
|
|
try { |
1342 |
|
|
p.setCorePoolSize(-1); |
1343 |
|
|
shouldThrow(); |
1344 |
|
|
} catch (IllegalArgumentException success) {} |
1345 |
dl |
1.1 |
} |
1346 |
jsr166 |
1.2 |
} |
1347 |
dl |
1.1 |
|
1348 |
|
|
/** |
1349 |
jsr166 |
1.19 |
* setMaximumPoolSize(int) throws IllegalArgumentException |
1350 |
|
|
* if given a value less the core pool size |
1351 |
jsr166 |
1.2 |
*/ |
1352 |
dl |
1.1 |
public void testMaximumPoolSizeIllegalArgumentException() { |
1353 |
jsr166 |
1.80 |
final ThreadPoolExecutor p = |
1354 |
jsr166 |
1.81 |
new CustomTPE(2, 3, |
1355 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1356 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1357 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(p)) { |
1358 |
|
|
try { |
1359 |
|
|
p.setMaximumPoolSize(1); |
1360 |
|
|
shouldThrow(); |
1361 |
|
|
} catch (IllegalArgumentException success) {} |
1362 |
dl |
1.1 |
} |
1363 |
|
|
} |
1364 |
jsr166 |
1.2 |
|
1365 |
dl |
1.1 |
/** |
1366 |
jsr166 |
1.19 |
* setMaximumPoolSize throws IllegalArgumentException |
1367 |
|
|
* if given a negative value |
1368 |
dl |
1.1 |
*/ |
1369 |
|
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1370 |
jsr166 |
1.80 |
final ThreadPoolExecutor p = |
1371 |
jsr166 |
1.81 |
new CustomTPE(2, 3, |
1372 |
|
|
LONG_DELAY_MS, |
1373 |
|
|
MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1374 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(p)) { |
1375 |
|
|
try { |
1376 |
|
|
p.setMaximumPoolSize(-1); |
1377 |
|
|
shouldThrow(); |
1378 |
|
|
} catch (IllegalArgumentException success) {} |
1379 |
dl |
1.1 |
} |
1380 |
|
|
} |
1381 |
jsr166 |
1.2 |
|
1382 |
dl |
1.1 |
/** |
1383 |
jsr166 |
1.19 |
* setKeepAliveTime throws IllegalArgumentException |
1384 |
|
|
* when given a negative value |
1385 |
dl |
1.1 |
*/ |
1386 |
|
|
public void testKeepAliveTimeIllegalArgumentException() { |
1387 |
jsr166 |
1.80 |
final ThreadPoolExecutor p = |
1388 |
jsr166 |
1.81 |
new CustomTPE(2, 3, |
1389 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1390 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1391 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(p)) { |
1392 |
|
|
try { |
1393 |
|
|
p.setKeepAliveTime(-1, MILLISECONDS); |
1394 |
|
|
shouldThrow(); |
1395 |
|
|
} catch (IllegalArgumentException success) {} |
1396 |
dl |
1.1 |
} |
1397 |
|
|
} |
1398 |
|
|
|
1399 |
|
|
/** |
1400 |
|
|
* terminated() is called on termination |
1401 |
|
|
*/ |
1402 |
|
|
public void testTerminated() { |
1403 |
jsr166 |
1.21 |
CustomTPE p = new CustomTPE(); |
1404 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(p)) { |
1405 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1406 |
|
|
assertTrue(p.terminatedCalled()); |
1407 |
|
|
assertTrue(p.isShutdown()); |
1408 |
|
|
} |
1409 |
dl |
1.1 |
} |
1410 |
|
|
|
1411 |
|
|
/** |
1412 |
|
|
* beforeExecute and afterExecute are called when executing task |
1413 |
|
|
*/ |
1414 |
jsr166 |
1.5 |
public void testBeforeAfter() throws InterruptedException { |
1415 |
jsr166 |
1.21 |
CustomTPE p = new CustomTPE(); |
1416 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(p)) { |
1417 |
jsr166 |
1.30 |
final CountDownLatch done = new CountDownLatch(1); |
1418 |
jsr166 |
1.32 |
p.execute(new CheckedRunnable() { |
1419 |
jsr166 |
1.30 |
public void realRun() { |
1420 |
|
|
done.countDown(); |
1421 |
jsr166 |
1.32 |
}}); |
1422 |
jsr166 |
1.30 |
await(p.afterCalled); |
1423 |
|
|
assertEquals(0, done.getCount()); |
1424 |
|
|
assertTrue(p.afterCalled()); |
1425 |
|
|
assertTrue(p.beforeCalled()); |
1426 |
dl |
1.1 |
} |
1427 |
|
|
} |
1428 |
|
|
|
1429 |
|
|
/** |
1430 |
|
|
* completed submit of callable returns result |
1431 |
|
|
*/ |
1432 |
jsr166 |
1.5 |
public void testSubmitCallable() throws Exception { |
1433 |
jsr166 |
1.80 |
final ExecutorService e = |
1434 |
|
|
new CustomTPE(2, 2, |
1435 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1436 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1437 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1438 |
dl |
1.1 |
Future<String> future = e.submit(new StringTask()); |
1439 |
|
|
String result = future.get(); |
1440 |
|
|
assertSame(TEST_STRING, result); |
1441 |
|
|
} |
1442 |
|
|
} |
1443 |
|
|
|
1444 |
|
|
/** |
1445 |
|
|
* completed submit of runnable returns successfully |
1446 |
|
|
*/ |
1447 |
jsr166 |
1.5 |
public void testSubmitRunnable() throws Exception { |
1448 |
jsr166 |
1.80 |
final ExecutorService e = |
1449 |
|
|
new CustomTPE(2, 2, |
1450 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1451 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1452 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1453 |
dl |
1.1 |
Future<?> future = e.submit(new NoOpRunnable()); |
1454 |
|
|
future.get(); |
1455 |
|
|
assertTrue(future.isDone()); |
1456 |
|
|
} |
1457 |
|
|
} |
1458 |
|
|
|
1459 |
|
|
/** |
1460 |
|
|
* completed submit of (runnable, result) returns result |
1461 |
|
|
*/ |
1462 |
jsr166 |
1.5 |
public void testSubmitRunnable2() throws Exception { |
1463 |
jsr166 |
1.80 |
final ExecutorService e = |
1464 |
|
|
new CustomTPE(2, 2, |
1465 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1466 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1467 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1468 |
dl |
1.1 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1469 |
|
|
String result = future.get(); |
1470 |
|
|
assertSame(TEST_STRING, result); |
1471 |
|
|
} |
1472 |
|
|
} |
1473 |
|
|
|
1474 |
|
|
/** |
1475 |
|
|
* invokeAny(null) throws NPE |
1476 |
|
|
*/ |
1477 |
jsr166 |
1.5 |
public void testInvokeAny1() throws Exception { |
1478 |
jsr166 |
1.80 |
final ExecutorService e = |
1479 |
|
|
new CustomTPE(2, 2, |
1480 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1481 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1482 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1483 |
|
|
try { |
1484 |
|
|
e.invokeAny(null); |
1485 |
|
|
shouldThrow(); |
1486 |
|
|
} catch (NullPointerException success) {} |
1487 |
dl |
1.1 |
} |
1488 |
|
|
} |
1489 |
|
|
|
1490 |
|
|
/** |
1491 |
|
|
* invokeAny(empty collection) throws IAE |
1492 |
|
|
*/ |
1493 |
jsr166 |
1.5 |
public void testInvokeAny2() throws Exception { |
1494 |
jsr166 |
1.80 |
final ExecutorService e = |
1495 |
|
|
new CustomTPE(2, 2, |
1496 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1497 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1498 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1499 |
|
|
try { |
1500 |
|
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1501 |
|
|
shouldThrow(); |
1502 |
|
|
} catch (IllegalArgumentException success) {} |
1503 |
dl |
1.1 |
} |
1504 |
|
|
} |
1505 |
|
|
|
1506 |
|
|
/** |
1507 |
|
|
* invokeAny(c) throws NPE if c has null elements |
1508 |
|
|
*/ |
1509 |
jsr166 |
1.5 |
public void testInvokeAny3() throws Exception { |
1510 |
jsr166 |
1.17 |
CountDownLatch latch = new CountDownLatch(1); |
1511 |
jsr166 |
1.80 |
final ExecutorService e = |
1512 |
|
|
new CustomTPE(2, 2, |
1513 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1514 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1515 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1516 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1517 |
|
|
l.add(latchAwaitingStringTask(latch)); |
1518 |
|
|
l.add(null); |
1519 |
|
|
try { |
1520 |
|
|
e.invokeAny(l); |
1521 |
|
|
shouldThrow(); |
1522 |
|
|
} catch (NullPointerException success) {} |
1523 |
jsr166 |
1.5 |
latch.countDown(); |
1524 |
dl |
1.1 |
} |
1525 |
|
|
} |
1526 |
|
|
|
1527 |
|
|
/** |
1528 |
|
|
* invokeAny(c) throws ExecutionException if no task completes |
1529 |
|
|
*/ |
1530 |
jsr166 |
1.5 |
public void testInvokeAny4() throws Exception { |
1531 |
jsr166 |
1.80 |
final ExecutorService e = |
1532 |
|
|
new CustomTPE(2, 2, |
1533 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1534 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1535 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1536 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1537 |
|
|
l.add(new NPETask()); |
1538 |
|
|
try { |
1539 |
|
|
e.invokeAny(l); |
1540 |
|
|
shouldThrow(); |
1541 |
|
|
} catch (ExecutionException success) { |
1542 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
1543 |
|
|
} |
1544 |
dl |
1.1 |
} |
1545 |
|
|
} |
1546 |
|
|
|
1547 |
|
|
/** |
1548 |
|
|
* invokeAny(c) returns result of some task |
1549 |
|
|
*/ |
1550 |
jsr166 |
1.5 |
public void testInvokeAny5() throws Exception { |
1551 |
jsr166 |
1.80 |
final ExecutorService e = |
1552 |
|
|
new CustomTPE(2, 2, |
1553 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1554 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1555 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1556 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1557 |
dl |
1.1 |
l.add(new StringTask()); |
1558 |
|
|
l.add(new StringTask()); |
1559 |
|
|
String result = e.invokeAny(l); |
1560 |
|
|
assertSame(TEST_STRING, result); |
1561 |
|
|
} |
1562 |
|
|
} |
1563 |
|
|
|
1564 |
|
|
/** |
1565 |
|
|
* invokeAll(null) throws NPE |
1566 |
|
|
*/ |
1567 |
jsr166 |
1.5 |
public void testInvokeAll1() throws Exception { |
1568 |
jsr166 |
1.80 |
final ExecutorService e = |
1569 |
|
|
new CustomTPE(2, 2, |
1570 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1571 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1572 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1573 |
|
|
try { |
1574 |
|
|
e.invokeAll(null); |
1575 |
|
|
shouldThrow(); |
1576 |
|
|
} catch (NullPointerException success) {} |
1577 |
dl |
1.1 |
} |
1578 |
|
|
} |
1579 |
|
|
|
1580 |
|
|
/** |
1581 |
|
|
* invokeAll(empty collection) returns empty collection |
1582 |
|
|
*/ |
1583 |
jsr166 |
1.5 |
public void testInvokeAll2() throws Exception { |
1584 |
jsr166 |
1.80 |
final ExecutorService e = |
1585 |
|
|
new CustomTPE(2, 2, |
1586 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1587 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1588 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1589 |
dl |
1.1 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1590 |
|
|
assertTrue(r.isEmpty()); |
1591 |
|
|
} |
1592 |
|
|
} |
1593 |
|
|
|
1594 |
|
|
/** |
1595 |
|
|
* invokeAll(c) throws NPE if c has null elements |
1596 |
|
|
*/ |
1597 |
jsr166 |
1.5 |
public void testInvokeAll3() throws Exception { |
1598 |
jsr166 |
1.80 |
final ExecutorService e = |
1599 |
|
|
new CustomTPE(2, 2, |
1600 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1601 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1602 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1603 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1604 |
|
|
l.add(new StringTask()); |
1605 |
|
|
l.add(null); |
1606 |
|
|
try { |
1607 |
|
|
e.invokeAll(l); |
1608 |
|
|
shouldThrow(); |
1609 |
|
|
} catch (NullPointerException success) {} |
1610 |
dl |
1.1 |
} |
1611 |
|
|
} |
1612 |
|
|
|
1613 |
|
|
/** |
1614 |
|
|
* get of element of invokeAll(c) throws exception on failed task |
1615 |
|
|
*/ |
1616 |
jsr166 |
1.5 |
public void testInvokeAll4() throws Exception { |
1617 |
jsr166 |
1.80 |
final ExecutorService e = |
1618 |
|
|
new CustomTPE(2, 2, |
1619 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1620 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1621 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1622 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1623 |
|
|
l.add(new NPETask()); |
1624 |
|
|
List<Future<String>> futures = e.invokeAll(l); |
1625 |
|
|
assertEquals(1, futures.size()); |
1626 |
|
|
try { |
1627 |
|
|
futures.get(0).get(); |
1628 |
|
|
shouldThrow(); |
1629 |
|
|
} catch (ExecutionException success) { |
1630 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
1631 |
|
|
} |
1632 |
dl |
1.1 |
} |
1633 |
|
|
} |
1634 |
|
|
|
1635 |
|
|
/** |
1636 |
|
|
* invokeAll(c) returns results of all completed tasks |
1637 |
|
|
*/ |
1638 |
jsr166 |
1.5 |
public void testInvokeAll5() throws Exception { |
1639 |
jsr166 |
1.80 |
final ExecutorService e = |
1640 |
|
|
new CustomTPE(2, 2, |
1641 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1642 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1643 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1644 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1645 |
dl |
1.1 |
l.add(new StringTask()); |
1646 |
|
|
l.add(new StringTask()); |
1647 |
jsr166 |
1.17 |
List<Future<String>> futures = e.invokeAll(l); |
1648 |
|
|
assertEquals(2, futures.size()); |
1649 |
|
|
for (Future<String> future : futures) |
1650 |
jsr166 |
1.6 |
assertSame(TEST_STRING, future.get()); |
1651 |
dl |
1.1 |
} |
1652 |
|
|
} |
1653 |
|
|
|
1654 |
|
|
/** |
1655 |
|
|
* timed invokeAny(null) throws NPE |
1656 |
|
|
*/ |
1657 |
jsr166 |
1.5 |
public void testTimedInvokeAny1() throws Exception { |
1658 |
jsr166 |
1.80 |
final ExecutorService e = |
1659 |
|
|
new CustomTPE(2, 2, |
1660 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1661 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1662 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1663 |
|
|
try { |
1664 |
|
|
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1665 |
|
|
shouldThrow(); |
1666 |
|
|
} catch (NullPointerException success) {} |
1667 |
dl |
1.1 |
} |
1668 |
|
|
} |
1669 |
|
|
|
1670 |
|
|
/** |
1671 |
|
|
* timed invokeAny(,,null) throws NPE |
1672 |
|
|
*/ |
1673 |
jsr166 |
1.5 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1674 |
jsr166 |
1.80 |
final ExecutorService e = |
1675 |
|
|
new CustomTPE(2, 2, |
1676 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1677 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1678 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1679 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1680 |
|
|
l.add(new StringTask()); |
1681 |
|
|
try { |
1682 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1683 |
|
|
shouldThrow(); |
1684 |
|
|
} catch (NullPointerException success) {} |
1685 |
dl |
1.1 |
} |
1686 |
|
|
} |
1687 |
|
|
|
1688 |
|
|
/** |
1689 |
|
|
* timed invokeAny(empty collection) throws IAE |
1690 |
|
|
*/ |
1691 |
jsr166 |
1.5 |
public void testTimedInvokeAny2() throws Exception { |
1692 |
jsr166 |
1.80 |
final ExecutorService e = |
1693 |
|
|
new CustomTPE(2, 2, |
1694 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1695 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1696 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1697 |
|
|
try { |
1698 |
|
|
e.invokeAny(new ArrayList<Callable<String>>(), |
1699 |
|
|
MEDIUM_DELAY_MS, MILLISECONDS); |
1700 |
|
|
shouldThrow(); |
1701 |
|
|
} catch (IllegalArgumentException success) {} |
1702 |
dl |
1.1 |
} |
1703 |
|
|
} |
1704 |
|
|
|
1705 |
|
|
/** |
1706 |
|
|
* timed invokeAny(c) throws NPE if c has null elements |
1707 |
|
|
*/ |
1708 |
jsr166 |
1.5 |
public void testTimedInvokeAny3() throws Exception { |
1709 |
jsr166 |
1.17 |
CountDownLatch latch = new CountDownLatch(1); |
1710 |
jsr166 |
1.80 |
final ExecutorService e = |
1711 |
|
|
new CustomTPE(2, 2, |
1712 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1713 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1714 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1715 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1716 |
|
|
l.add(latchAwaitingStringTask(latch)); |
1717 |
|
|
l.add(null); |
1718 |
|
|
try { |
1719 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1720 |
|
|
shouldThrow(); |
1721 |
|
|
} catch (NullPointerException success) {} |
1722 |
jsr166 |
1.12 |
latch.countDown(); |
1723 |
dl |
1.1 |
} |
1724 |
|
|
} |
1725 |
|
|
|
1726 |
|
|
/** |
1727 |
|
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1728 |
|
|
*/ |
1729 |
jsr166 |
1.5 |
public void testTimedInvokeAny4() throws Exception { |
1730 |
jsr166 |
1.80 |
final ExecutorService e = |
1731 |
|
|
new CustomTPE(2, 2, |
1732 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1733 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1734 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1735 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1736 |
|
|
l.add(new NPETask()); |
1737 |
|
|
try { |
1738 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1739 |
|
|
shouldThrow(); |
1740 |
|
|
} catch (ExecutionException success) { |
1741 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
1742 |
|
|
} |
1743 |
dl |
1.1 |
} |
1744 |
|
|
} |
1745 |
|
|
|
1746 |
|
|
/** |
1747 |
|
|
* timed invokeAny(c) returns result of some task |
1748 |
|
|
*/ |
1749 |
jsr166 |
1.5 |
public void testTimedInvokeAny5() throws Exception { |
1750 |
jsr166 |
1.80 |
final ExecutorService e = |
1751 |
|
|
new CustomTPE(2, 2, |
1752 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1753 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1754 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1755 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1756 |
dl |
1.1 |
l.add(new StringTask()); |
1757 |
|
|
l.add(new StringTask()); |
1758 |
jsr166 |
1.9 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1759 |
dl |
1.1 |
assertSame(TEST_STRING, result); |
1760 |
|
|
} |
1761 |
|
|
} |
1762 |
|
|
|
1763 |
|
|
/** |
1764 |
|
|
* timed invokeAll(null) throws NPE |
1765 |
|
|
*/ |
1766 |
jsr166 |
1.5 |
public void testTimedInvokeAll1() throws Exception { |
1767 |
jsr166 |
1.80 |
final ExecutorService e = |
1768 |
|
|
new CustomTPE(2, 2, |
1769 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1770 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1771 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1772 |
|
|
try { |
1773 |
|
|
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1774 |
|
|
shouldThrow(); |
1775 |
|
|
} catch (NullPointerException success) {} |
1776 |
dl |
1.1 |
} |
1777 |
|
|
} |
1778 |
|
|
|
1779 |
|
|
/** |
1780 |
|
|
* timed invokeAll(,,null) throws NPE |
1781 |
|
|
*/ |
1782 |
jsr166 |
1.5 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1783 |
jsr166 |
1.80 |
final ExecutorService e = |
1784 |
|
|
new CustomTPE(2, 2, |
1785 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1786 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1787 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1788 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1789 |
|
|
l.add(new StringTask()); |
1790 |
|
|
try { |
1791 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1792 |
|
|
shouldThrow(); |
1793 |
|
|
} catch (NullPointerException success) {} |
1794 |
dl |
1.1 |
} |
1795 |
|
|
} |
1796 |
|
|
|
1797 |
|
|
/** |
1798 |
|
|
* timed invokeAll(empty collection) returns empty collection |
1799 |
|
|
*/ |
1800 |
jsr166 |
1.5 |
public void testTimedInvokeAll2() throws Exception { |
1801 |
jsr166 |
1.80 |
final ExecutorService e = |
1802 |
|
|
new CustomTPE(2, 2, |
1803 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1804 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1805 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1806 |
|
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), |
1807 |
|
|
MEDIUM_DELAY_MS, MILLISECONDS); |
1808 |
dl |
1.1 |
assertTrue(r.isEmpty()); |
1809 |
|
|
} |
1810 |
|
|
} |
1811 |
|
|
|
1812 |
|
|
/** |
1813 |
|
|
* timed invokeAll(c) throws NPE if c has null elements |
1814 |
|
|
*/ |
1815 |
jsr166 |
1.5 |
public void testTimedInvokeAll3() throws Exception { |
1816 |
jsr166 |
1.80 |
final ExecutorService e = |
1817 |
|
|
new CustomTPE(2, 2, |
1818 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1819 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1820 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1821 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1822 |
|
|
l.add(new StringTask()); |
1823 |
|
|
l.add(null); |
1824 |
|
|
try { |
1825 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1826 |
|
|
shouldThrow(); |
1827 |
|
|
} catch (NullPointerException success) {} |
1828 |
dl |
1.1 |
} |
1829 |
|
|
} |
1830 |
|
|
|
1831 |
|
|
/** |
1832 |
|
|
* get of element of invokeAll(c) throws exception on failed task |
1833 |
|
|
*/ |
1834 |
jsr166 |
1.5 |
public void testTimedInvokeAll4() throws Exception { |
1835 |
jsr166 |
1.80 |
final ExecutorService e = |
1836 |
|
|
new CustomTPE(2, 2, |
1837 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1838 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1839 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1840 |
|
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1841 |
|
|
l.add(new NPETask()); |
1842 |
|
|
List<Future<String>> futures = |
1843 |
jsr166 |
1.88 |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1844 |
jsr166 |
1.82 |
assertEquals(1, futures.size()); |
1845 |
|
|
try { |
1846 |
|
|
futures.get(0).get(); |
1847 |
|
|
shouldThrow(); |
1848 |
|
|
} catch (ExecutionException success) { |
1849 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
1850 |
|
|
} |
1851 |
dl |
1.1 |
} |
1852 |
|
|
} |
1853 |
|
|
|
1854 |
|
|
/** |
1855 |
|
|
* timed invokeAll(c) returns results of all completed tasks |
1856 |
|
|
*/ |
1857 |
jsr166 |
1.5 |
public void testTimedInvokeAll5() throws Exception { |
1858 |
jsr166 |
1.80 |
final ExecutorService e = |
1859 |
|
|
new CustomTPE(2, 2, |
1860 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1861 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1862 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1863 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1864 |
dl |
1.1 |
l.add(new StringTask()); |
1865 |
|
|
l.add(new StringTask()); |
1866 |
jsr166 |
1.17 |
List<Future<String>> futures = |
1867 |
jsr166 |
1.45 |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1868 |
jsr166 |
1.17 |
assertEquals(2, futures.size()); |
1869 |
|
|
for (Future<String> future : futures) |
1870 |
jsr166 |
1.6 |
assertSame(TEST_STRING, future.get()); |
1871 |
dl |
1.1 |
} |
1872 |
|
|
} |
1873 |
|
|
|
1874 |
|
|
/** |
1875 |
|
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1876 |
|
|
*/ |
1877 |
jsr166 |
1.5 |
public void testTimedInvokeAll6() throws Exception { |
1878 |
jsr166 |
1.80 |
final ExecutorService e = |
1879 |
|
|
new CustomTPE(2, 2, |
1880 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1881 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1882 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1883 |
jsr166 |
1.39 |
for (long timeout = timeoutMillis();;) { |
1884 |
|
|
List<Callable<String>> tasks = new ArrayList<>(); |
1885 |
|
|
tasks.add(new StringTask("0")); |
1886 |
|
|
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1887 |
|
|
tasks.add(new StringTask("2")); |
1888 |
|
|
long startTime = System.nanoTime(); |
1889 |
|
|
List<Future<String>> futures = |
1890 |
|
|
e.invokeAll(tasks, timeout, MILLISECONDS); |
1891 |
|
|
assertEquals(tasks.size(), futures.size()); |
1892 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1893 |
|
|
for (Future future : futures) |
1894 |
|
|
assertTrue(future.isDone()); |
1895 |
|
|
assertTrue(futures.get(1).isCancelled()); |
1896 |
|
|
try { |
1897 |
|
|
assertEquals("0", futures.get(0).get()); |
1898 |
|
|
assertEquals("2", futures.get(2).get()); |
1899 |
|
|
break; |
1900 |
|
|
} catch (CancellationException retryWithLongerTimeout) { |
1901 |
|
|
timeout *= 2; |
1902 |
|
|
if (timeout >= LONG_DELAY_MS / 2) |
1903 |
|
|
fail("expected exactly one task to be cancelled"); |
1904 |
|
|
} |
1905 |
|
|
} |
1906 |
dl |
1.1 |
} |
1907 |
|
|
} |
1908 |
|
|
|
1909 |
|
|
/** |
1910 |
|
|
* Execution continues if there is at least one thread even if |
1911 |
|
|
* thread factory fails to create more |
1912 |
|
|
*/ |
1913 |
jsr166 |
1.5 |
public void testFailingThreadFactory() throws InterruptedException { |
1914 |
jsr166 |
1.21 |
final ExecutorService e = |
1915 |
|
|
new CustomTPE(100, 100, |
1916 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1917 |
|
|
new LinkedBlockingQueue<Runnable>(), |
1918 |
|
|
new FailingThreadFactory()); |
1919 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
1920 |
jsr166 |
1.21 |
final int TASKS = 100; |
1921 |
|
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1922 |
|
|
for (int k = 0; k < TASKS; ++k) |
1923 |
|
|
e.execute(new CheckedRunnable() { |
1924 |
|
|
public void realRun() { |
1925 |
|
|
done.countDown(); |
1926 |
|
|
}}); |
1927 |
|
|
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1928 |
dl |
1.1 |
} |
1929 |
|
|
} |
1930 |
|
|
|
1931 |
|
|
/** |
1932 |
|
|
* allowsCoreThreadTimeOut is by default false. |
1933 |
|
|
*/ |
1934 |
|
|
public void testAllowsCoreThreadTimeOut() { |
1935 |
jsr166 |
1.80 |
final ThreadPoolExecutor p = |
1936 |
|
|
new CustomTPE(2, 2, |
1937 |
|
|
1000, MILLISECONDS, |
1938 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1939 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(p)) { |
1940 |
|
|
assertFalse(p.allowsCoreThreadTimeOut()); |
1941 |
|
|
} |
1942 |
dl |
1.1 |
} |
1943 |
|
|
|
1944 |
|
|
/** |
1945 |
|
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1946 |
|
|
*/ |
1947 |
jsr166 |
1.21 |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1948 |
jsr166 |
1.39 |
long keepAliveTime = timeoutMillis(); |
1949 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
1950 |
|
|
new CustomTPE(2, 10, |
1951 |
jsr166 |
1.39 |
keepAliveTime, MILLISECONDS, |
1952 |
jsr166 |
1.21 |
new ArrayBlockingQueue<Runnable>(10)); |
1953 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(p)) { |
1954 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1955 |
jsr166 |
1.21 |
p.allowCoreThreadTimeOut(true); |
1956 |
|
|
p.execute(new CheckedRunnable() { |
1957 |
jsr166 |
1.39 |
public void realRun() { |
1958 |
jsr166 |
1.21 |
threadStarted.countDown(); |
1959 |
|
|
assertEquals(1, p.getPoolSize()); |
1960 |
|
|
}}); |
1961 |
jsr166 |
1.29 |
await(threadStarted); |
1962 |
jsr166 |
1.39 |
delay(keepAliveTime); |
1963 |
jsr166 |
1.29 |
long startTime = System.nanoTime(); |
1964 |
|
|
while (p.getPoolSize() > 0 |
1965 |
|
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1966 |
|
|
Thread.yield(); |
1967 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1968 |
jsr166 |
1.21 |
assertEquals(0, p.getPoolSize()); |
1969 |
dl |
1.1 |
} |
1970 |
|
|
} |
1971 |
|
|
|
1972 |
|
|
/** |
1973 |
|
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1974 |
|
|
*/ |
1975 |
jsr166 |
1.21 |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1976 |
jsr166 |
1.39 |
long keepAliveTime = timeoutMillis(); |
1977 |
jsr166 |
1.21 |
final ThreadPoolExecutor p = |
1978 |
|
|
new CustomTPE(2, 10, |
1979 |
jsr166 |
1.39 |
keepAliveTime, MILLISECONDS, |
1980 |
jsr166 |
1.21 |
new ArrayBlockingQueue<Runnable>(10)); |
1981 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(p)) { |
1982 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1983 |
jsr166 |
1.21 |
p.allowCoreThreadTimeOut(false); |
1984 |
|
|
p.execute(new CheckedRunnable() { |
1985 |
|
|
public void realRun() throws InterruptedException { |
1986 |
|
|
threadStarted.countDown(); |
1987 |
|
|
assertTrue(p.getPoolSize() >= 1); |
1988 |
|
|
}}); |
1989 |
jsr166 |
1.39 |
delay(2 * keepAliveTime); |
1990 |
jsr166 |
1.21 |
assertTrue(p.getPoolSize() >= 1); |
1991 |
dl |
1.1 |
} |
1992 |
|
|
} |
1993 |
|
|
|
1994 |
jsr166 |
1.44 |
/** |
1995 |
|
|
* get(cancelled task) throws CancellationException |
1996 |
|
|
* (in part, a test of CustomTPE itself) |
1997 |
|
|
*/ |
1998 |
|
|
public void testGet_cancelled() throws Exception { |
1999 |
|
|
final ExecutorService e = |
2000 |
|
|
new CustomTPE(1, 1, |
2001 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
2002 |
|
|
new LinkedBlockingQueue<Runnable>()); |
2003 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(e)) { |
2004 |
jsr166 |
1.44 |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2005 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
2006 |
|
|
final List<Future<?>> futures = new ArrayList<>(); |
2007 |
|
|
for (int i = 0; i < 2; i++) { |
2008 |
|
|
Runnable r = new CheckedRunnable() { public void realRun() |
2009 |
|
|
throws Throwable { |
2010 |
|
|
blockerStarted.countDown(); |
2011 |
|
|
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2012 |
|
|
}}; |
2013 |
|
|
futures.add(e.submit(r)); |
2014 |
|
|
} |
2015 |
|
|
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2016 |
|
|
for (Future<?> future : futures) future.cancel(false); |
2017 |
|
|
for (Future<?> future : futures) { |
2018 |
|
|
try { |
2019 |
|
|
future.get(); |
2020 |
|
|
shouldThrow(); |
2021 |
|
|
} catch (CancellationException success) {} |
2022 |
|
|
try { |
2023 |
|
|
future.get(LONG_DELAY_MS, MILLISECONDS); |
2024 |
|
|
shouldThrow(); |
2025 |
|
|
} catch (CancellationException success) {} |
2026 |
|
|
assertTrue(future.isCancelled()); |
2027 |
|
|
assertTrue(future.isDone()); |
2028 |
|
|
} |
2029 |
|
|
done.countDown(); |
2030 |
|
|
} |
2031 |
|
|
} |
2032 |
|
|
|
2033 |
dl |
1.1 |
} |