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