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