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