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 |
jsr166 |
1.19 |
* execute successfully executes a runnable |
196 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* getActiveCount increases but doesn't overestimate, when a |
209 |
|
|
* thread becomes active |
210 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
222 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* prestartAllCoreThreads starts all corePoolSize threads |
237 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* getCompletedTaskCount increases, but doesn't overestimate, |
250 |
|
|
* when tasks complete |
251 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* getCorePoolSize returns size given in constructor if not otherwise set |
264 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* getKeepAliveTime returns value given in constructor if not otherwise set |
273 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* getLargestPoolSize increases, but doesn't overestimate, when |
357 |
|
|
* multiple threads active |
358 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* getMaximumPoolSize returns value given in constructor if not |
371 |
|
|
* otherwise set |
372 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* getPoolSize increases, but doesn't overestimate, when threads |
381 |
|
|
* become active |
382 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
393 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* isShutDown is false before shutdown, true after |
405 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* isTerminated is false before termination, true after |
418 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* isTerminating is not true when running or when terminated |
433 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* purge removes cancelled tasks from the queue |
502 |
dl |
1.1 |
*/ |
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 |
jsr166 |
1.19 |
* shutDownNow returns a list containing tasks that were not run |
520 |
dl |
1.1 |
*/ |
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 |
|
|
} |
533 |
jsr166 |
1.8 |
assertTrue(p1.isShutdown()); |
534 |
|
|
assertTrue(l.size() <= 4); |
535 |
dl |
1.1 |
} |
536 |
|
|
|
537 |
|
|
// Exception Tests |
538 |
|
|
|
539 |
jsr166 |
1.2 |
|
540 |
|
|
/** |
541 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
542 |
dl |
1.1 |
*/ |
543 |
|
|
public void testConstructor1() { |
544 |
|
|
try { |
545 |
jsr166 |
1.9 |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
546 |
dl |
1.1 |
shouldThrow(); |
547 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
548 |
dl |
1.1 |
} |
549 |
jsr166 |
1.2 |
|
550 |
|
|
/** |
551 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
552 |
dl |
1.1 |
*/ |
553 |
|
|
public void testConstructor2() { |
554 |
|
|
try { |
555 |
jsr166 |
1.9 |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
556 |
dl |
1.1 |
shouldThrow(); |
557 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
558 |
dl |
1.1 |
} |
559 |
jsr166 |
1.2 |
|
560 |
|
|
/** |
561 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
562 |
dl |
1.1 |
*/ |
563 |
|
|
public void testConstructor3() { |
564 |
|
|
try { |
565 |
jsr166 |
1.9 |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
566 |
dl |
1.1 |
shouldThrow(); |
567 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
568 |
dl |
1.1 |
} |
569 |
|
|
|
570 |
jsr166 |
1.2 |
/** |
571 |
|
|
* Constructor throws if keepAliveTime is less than zero |
572 |
dl |
1.1 |
*/ |
573 |
|
|
public void testConstructor4() { |
574 |
|
|
try { |
575 |
jsr166 |
1.9 |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
576 |
dl |
1.1 |
shouldThrow(); |
577 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
578 |
dl |
1.1 |
} |
579 |
|
|
|
580 |
jsr166 |
1.2 |
/** |
581 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
582 |
dl |
1.1 |
*/ |
583 |
|
|
public void testConstructor5() { |
584 |
|
|
try { |
585 |
jsr166 |
1.9 |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
586 |
dl |
1.1 |
shouldThrow(); |
587 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
588 |
dl |
1.1 |
} |
589 |
jsr166 |
1.2 |
|
590 |
|
|
/** |
591 |
|
|
* Constructor throws if workQueue is set to null |
592 |
dl |
1.1 |
*/ |
593 |
|
|
public void testConstructorNullPointerException() { |
594 |
|
|
try { |
595 |
jsr166 |
1.9 |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
596 |
dl |
1.1 |
shouldThrow(); |
597 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
598 |
dl |
1.1 |
} |
599 |
|
|
|
600 |
jsr166 |
1.2 |
|
601 |
|
|
|
602 |
|
|
/** |
603 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
604 |
dl |
1.1 |
*/ |
605 |
|
|
public void testConstructor6() { |
606 |
|
|
try { |
607 |
jsr166 |
1.9 |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
608 |
dl |
1.1 |
shouldThrow(); |
609 |
jsr166 |
1.4 |
} catch (IllegalArgumentException success) {} |
610 |
dl |
1.1 |
} |
611 |
jsr166 |
1.2 |
|
612 |
|
|
/** |
613 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
614 |
dl |
1.1 |
*/ |
615 |
|
|
public void testConstructor7() { |
616 |
|
|
try { |
617 |
jsr166 |
1.9 |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
618 |
dl |
1.1 |
shouldThrow(); |
619 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
620 |
dl |
1.1 |
} |
621 |
|
|
|
622 |
jsr166 |
1.2 |
/** |
623 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
624 |
dl |
1.1 |
*/ |
625 |
|
|
public void testConstructor8() { |
626 |
|
|
try { |
627 |
jsr166 |
1.9 |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
628 |
dl |
1.1 |
shouldThrow(); |
629 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
630 |
dl |
1.1 |
} |
631 |
|
|
|
632 |
jsr166 |
1.2 |
/** |
633 |
|
|
* Constructor throws if keepAliveTime is less than zero |
634 |
dl |
1.1 |
*/ |
635 |
|
|
public void testConstructor9() { |
636 |
|
|
try { |
637 |
jsr166 |
1.9 |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
638 |
dl |
1.1 |
shouldThrow(); |
639 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
640 |
dl |
1.1 |
} |
641 |
|
|
|
642 |
jsr166 |
1.2 |
/** |
643 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
644 |
dl |
1.1 |
*/ |
645 |
|
|
public void testConstructor10() { |
646 |
|
|
try { |
647 |
jsr166 |
1.9 |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
648 |
dl |
1.1 |
shouldThrow(); |
649 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
650 |
dl |
1.1 |
} |
651 |
|
|
|
652 |
jsr166 |
1.2 |
/** |
653 |
|
|
* Constructor throws if workQueue is set to null |
654 |
dl |
1.1 |
*/ |
655 |
|
|
public void testConstructorNullPointerException2() { |
656 |
|
|
try { |
657 |
jsr166 |
1.9 |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
658 |
dl |
1.1 |
shouldThrow(); |
659 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
660 |
dl |
1.1 |
} |
661 |
|
|
|
662 |
jsr166 |
1.2 |
/** |
663 |
|
|
* Constructor throws if threadFactory is set to null |
664 |
dl |
1.1 |
*/ |
665 |
|
|
public void testConstructorNullPointerException3() { |
666 |
|
|
try { |
667 |
|
|
ThreadFactory f = null; |
668 |
jsr166 |
1.9 |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
669 |
dl |
1.1 |
shouldThrow(); |
670 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
671 |
dl |
1.1 |
} |
672 |
jsr166 |
1.2 |
|
673 |
|
|
|
674 |
|
|
/** |
675 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
676 |
dl |
1.1 |
*/ |
677 |
|
|
public void testConstructor11() { |
678 |
|
|
try { |
679 |
jsr166 |
1.9 |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
680 |
dl |
1.1 |
shouldThrow(); |
681 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
682 |
dl |
1.1 |
} |
683 |
|
|
|
684 |
jsr166 |
1.2 |
/** |
685 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
686 |
dl |
1.1 |
*/ |
687 |
|
|
public void testConstructor12() { |
688 |
|
|
try { |
689 |
jsr166 |
1.9 |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
690 |
dl |
1.1 |
shouldThrow(); |
691 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
692 |
dl |
1.1 |
} |
693 |
|
|
|
694 |
jsr166 |
1.2 |
/** |
695 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
696 |
dl |
1.1 |
*/ |
697 |
|
|
public void testConstructor13() { |
698 |
|
|
try { |
699 |
jsr166 |
1.9 |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
700 |
dl |
1.1 |
shouldThrow(); |
701 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
702 |
dl |
1.1 |
} |
703 |
|
|
|
704 |
jsr166 |
1.2 |
/** |
705 |
|
|
* Constructor throws if keepAliveTime is less than zero |
706 |
dl |
1.1 |
*/ |
707 |
|
|
public void testConstructor14() { |
708 |
|
|
try { |
709 |
jsr166 |
1.9 |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
710 |
dl |
1.1 |
shouldThrow(); |
711 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
712 |
dl |
1.1 |
} |
713 |
|
|
|
714 |
jsr166 |
1.2 |
/** |
715 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
716 |
dl |
1.1 |
*/ |
717 |
|
|
public void testConstructor15() { |
718 |
|
|
try { |
719 |
jsr166 |
1.9 |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
720 |
dl |
1.1 |
shouldThrow(); |
721 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
722 |
dl |
1.1 |
} |
723 |
|
|
|
724 |
jsr166 |
1.2 |
/** |
725 |
|
|
* Constructor throws if workQueue is set to null |
726 |
dl |
1.1 |
*/ |
727 |
|
|
public void testConstructorNullPointerException4() { |
728 |
|
|
try { |
729 |
jsr166 |
1.9 |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
730 |
dl |
1.1 |
shouldThrow(); |
731 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
732 |
dl |
1.1 |
} |
733 |
|
|
|
734 |
jsr166 |
1.2 |
/** |
735 |
|
|
* Constructor throws if handler is set to null |
736 |
dl |
1.1 |
*/ |
737 |
|
|
public void testConstructorNullPointerException5() { |
738 |
|
|
try { |
739 |
|
|
RejectedExecutionHandler r = null; |
740 |
jsr166 |
1.9 |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
741 |
dl |
1.1 |
shouldThrow(); |
742 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
743 |
dl |
1.1 |
} |
744 |
|
|
|
745 |
jsr166 |
1.2 |
|
746 |
|
|
/** |
747 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
748 |
dl |
1.1 |
*/ |
749 |
|
|
public void testConstructor16() { |
750 |
|
|
try { |
751 |
jsr166 |
1.9 |
new CustomTPE(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
752 |
dl |
1.1 |
shouldThrow(); |
753 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
754 |
dl |
1.1 |
} |
755 |
|
|
|
756 |
jsr166 |
1.2 |
/** |
757 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
758 |
dl |
1.1 |
*/ |
759 |
|
|
public void testConstructor17() { |
760 |
|
|
try { |
761 |
jsr166 |
1.9 |
new CustomTPE(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
762 |
dl |
1.1 |
shouldThrow(); |
763 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
764 |
dl |
1.1 |
} |
765 |
|
|
|
766 |
jsr166 |
1.2 |
/** |
767 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
768 |
dl |
1.1 |
*/ |
769 |
|
|
public void testConstructor18() { |
770 |
|
|
try { |
771 |
jsr166 |
1.9 |
new CustomTPE(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
772 |
dl |
1.1 |
shouldThrow(); |
773 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
774 |
dl |
1.1 |
} |
775 |
|
|
|
776 |
jsr166 |
1.2 |
/** |
777 |
|
|
* Constructor throws if keepAliveTime is less than zero |
778 |
dl |
1.1 |
*/ |
779 |
|
|
public void testConstructor19() { |
780 |
|
|
try { |
781 |
jsr166 |
1.9 |
new CustomTPE(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
782 |
dl |
1.1 |
shouldThrow(); |
783 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
784 |
dl |
1.1 |
} |
785 |
|
|
|
786 |
jsr166 |
1.2 |
/** |
787 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
788 |
dl |
1.1 |
*/ |
789 |
|
|
public void testConstructor20() { |
790 |
|
|
try { |
791 |
jsr166 |
1.9 |
new CustomTPE(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
792 |
dl |
1.1 |
shouldThrow(); |
793 |
jsr166 |
1.5 |
} catch (IllegalArgumentException success) {} |
794 |
dl |
1.1 |
} |
795 |
|
|
|
796 |
jsr166 |
1.2 |
/** |
797 |
jsr166 |
1.20 |
* Constructor throws if workQueue is null |
798 |
dl |
1.1 |
*/ |
799 |
|
|
public void testConstructorNullPointerException6() { |
800 |
|
|
try { |
801 |
jsr166 |
1.9 |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
802 |
dl |
1.1 |
shouldThrow(); |
803 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
804 |
dl |
1.1 |
} |
805 |
|
|
|
806 |
jsr166 |
1.2 |
/** |
807 |
jsr166 |
1.20 |
* Constructor throws if handler is null |
808 |
dl |
1.1 |
*/ |
809 |
|
|
public void testConstructorNullPointerException7() { |
810 |
|
|
try { |
811 |
|
|
RejectedExecutionHandler r = null; |
812 |
jsr166 |
1.9 |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
813 |
dl |
1.1 |
shouldThrow(); |
814 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
815 |
dl |
1.1 |
} |
816 |
|
|
|
817 |
jsr166 |
1.2 |
/** |
818 |
jsr166 |
1.20 |
* Constructor throws if ThreadFactory is null |
819 |
dl |
1.1 |
*/ |
820 |
|
|
public void testConstructorNullPointerException8() { |
821 |
|
|
try { |
822 |
|
|
ThreadFactory f = null; |
823 |
jsr166 |
1.9 |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
824 |
dl |
1.1 |
shouldThrow(); |
825 |
jsr166 |
1.5 |
} catch (NullPointerException success) {} |
826 |
dl |
1.1 |
} |
827 |
jsr166 |
1.2 |
|
828 |
dl |
1.1 |
|
829 |
|
|
/** |
830 |
jsr166 |
1.19 |
* execute throws RejectedExecutionException if saturated. |
831 |
dl |
1.1 |
*/ |
832 |
|
|
public void testSaturatedExecute() { |
833 |
jsr166 |
1.9 |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
834 |
dl |
1.1 |
try { |
835 |
jsr166 |
1.2 |
|
836 |
jsr166 |
1.4 |
for (int i = 0; i < 5; ++i) { |
837 |
dl |
1.1 |
p.execute(new MediumRunnable()); |
838 |
|
|
} |
839 |
|
|
shouldThrow(); |
840 |
jsr166 |
1.4 |
} catch (RejectedExecutionException success) {} |
841 |
dl |
1.1 |
joinPool(p); |
842 |
|
|
} |
843 |
|
|
|
844 |
|
|
/** |
845 |
jsr166 |
1.19 |
* executor using CallerRunsPolicy runs task if saturated. |
846 |
dl |
1.1 |
*/ |
847 |
|
|
public void testSaturatedExecute2() { |
848 |
|
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
849 |
jsr166 |
1.9 |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
850 |
dl |
1.1 |
try { |
851 |
jsr166 |
1.2 |
|
852 |
dl |
1.1 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
853 |
jsr166 |
1.4 |
for (int i = 0; i < 5; ++i) { |
854 |
dl |
1.1 |
tasks[i] = new TrackedNoOpRunnable(); |
855 |
|
|
} |
856 |
|
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
857 |
|
|
p.execute(mr); |
858 |
jsr166 |
1.4 |
for (int i = 0; i < 5; ++i) { |
859 |
dl |
1.1 |
p.execute(tasks[i]); |
860 |
|
|
} |
861 |
jsr166 |
1.3 |
for (int i = 1; i < 5; ++i) { |
862 |
dl |
1.1 |
assertTrue(tasks[i].done); |
863 |
|
|
} |
864 |
jsr166 |
1.3 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
865 |
dl |
1.1 |
} finally { |
866 |
|
|
joinPool(p); |
867 |
|
|
} |
868 |
|
|
} |
869 |
|
|
|
870 |
|
|
/** |
871 |
jsr166 |
1.19 |
* executor using DiscardPolicy drops task if saturated. |
872 |
dl |
1.1 |
*/ |
873 |
|
|
public void testSaturatedExecute3() { |
874 |
|
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
875 |
jsr166 |
1.9 |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
876 |
dl |
1.1 |
try { |
877 |
jsr166 |
1.2 |
|
878 |
dl |
1.1 |
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
879 |
jsr166 |
1.4 |
for (int i = 0; i < 5; ++i) { |
880 |
dl |
1.1 |
tasks[i] = new TrackedNoOpRunnable(); |
881 |
|
|
} |
882 |
|
|
p.execute(new TrackedLongRunnable()); |
883 |
jsr166 |
1.4 |
for (int i = 0; i < 5; ++i) { |
884 |
dl |
1.1 |
p.execute(tasks[i]); |
885 |
|
|
} |
886 |
jsr166 |
1.4 |
for (int i = 0; i < 5; ++i) { |
887 |
dl |
1.1 |
assertFalse(tasks[i].done); |
888 |
|
|
} |
889 |
jsr166 |
1.3 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
890 |
dl |
1.1 |
} finally { |
891 |
|
|
joinPool(p); |
892 |
|
|
} |
893 |
|
|
} |
894 |
|
|
|
895 |
|
|
/** |
896 |
jsr166 |
1.19 |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
897 |
dl |
1.1 |
*/ |
898 |
|
|
public void testSaturatedExecute4() { |
899 |
|
|
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
900 |
jsr166 |
1.9 |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
901 |
dl |
1.1 |
try { |
902 |
|
|
p.execute(new TrackedLongRunnable()); |
903 |
|
|
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
904 |
|
|
p.execute(r2); |
905 |
|
|
assertTrue(p.getQueue().contains(r2)); |
906 |
|
|
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
907 |
|
|
p.execute(r3); |
908 |
|
|
assertFalse(p.getQueue().contains(r2)); |
909 |
|
|
assertTrue(p.getQueue().contains(r3)); |
910 |
jsr166 |
1.3 |
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
911 |
dl |
1.1 |
} finally { |
912 |
|
|
joinPool(p); |
913 |
|
|
} |
914 |
|
|
} |
915 |
|
|
|
916 |
|
|
/** |
917 |
jsr166 |
1.19 |
* execute throws RejectedExecutionException if shutdown |
918 |
dl |
1.1 |
*/ |
919 |
|
|
public void testRejectedExecutionExceptionOnShutdown() { |
920 |
jsr166 |
1.2 |
ThreadPoolExecutor tpe = |
921 |
jsr166 |
1.9 |
new CustomTPE(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
922 |
jsr166 |
1.3 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
923 |
jsr166 |
1.8 |
try { |
924 |
|
|
tpe.execute(new NoOpRunnable()); |
925 |
|
|
shouldThrow(); |
926 |
|
|
} catch (RejectedExecutionException success) {} |
927 |
jsr166 |
1.2 |
|
928 |
jsr166 |
1.8 |
joinPool(tpe); |
929 |
dl |
1.1 |
} |
930 |
|
|
|
931 |
|
|
/** |
932 |
jsr166 |
1.19 |
* execute using CallerRunsPolicy drops task on shutdown |
933 |
dl |
1.1 |
*/ |
934 |
|
|
public void testCallerRunsOnShutdown() { |
935 |
|
|
RejectedExecutionHandler h = new CustomTPE.CallerRunsPolicy(); |
936 |
jsr166 |
1.9 |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
937 |
dl |
1.1 |
|
938 |
jsr166 |
1.3 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
939 |
jsr166 |
1.8 |
try { |
940 |
dl |
1.1 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
941 |
jsr166 |
1.8 |
p.execute(r); |
942 |
dl |
1.1 |
assertFalse(r.done); |
943 |
|
|
} finally { |
944 |
|
|
joinPool(p); |
945 |
|
|
} |
946 |
|
|
} |
947 |
|
|
|
948 |
|
|
/** |
949 |
jsr166 |
1.19 |
* execute using DiscardPolicy drops task on shutdown |
950 |
dl |
1.1 |
*/ |
951 |
|
|
public void testDiscardOnShutdown() { |
952 |
|
|
RejectedExecutionHandler h = new CustomTPE.DiscardPolicy(); |
953 |
jsr166 |
1.9 |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
954 |
dl |
1.1 |
|
955 |
jsr166 |
1.3 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
956 |
jsr166 |
1.8 |
try { |
957 |
dl |
1.1 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
958 |
jsr166 |
1.8 |
p.execute(r); |
959 |
dl |
1.1 |
assertFalse(r.done); |
960 |
|
|
} finally { |
961 |
|
|
joinPool(p); |
962 |
|
|
} |
963 |
|
|
} |
964 |
|
|
|
965 |
|
|
|
966 |
|
|
/** |
967 |
jsr166 |
1.19 |
* execute using DiscardOldestPolicy drops task on shutdown |
968 |
dl |
1.1 |
*/ |
969 |
|
|
public void testDiscardOldestOnShutdown() { |
970 |
|
|
RejectedExecutionHandler h = new CustomTPE.DiscardOldestPolicy(); |
971 |
jsr166 |
1.9 |
ThreadPoolExecutor p = new CustomTPE(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
972 |
dl |
1.1 |
|
973 |
jsr166 |
1.3 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
974 |
jsr166 |
1.8 |
try { |
975 |
dl |
1.1 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
976 |
jsr166 |
1.8 |
p.execute(r); |
977 |
dl |
1.1 |
assertFalse(r.done); |
978 |
|
|
} finally { |
979 |
|
|
joinPool(p); |
980 |
|
|
} |
981 |
|
|
} |
982 |
|
|
|
983 |
|
|
|
984 |
|
|
/** |
985 |
jsr166 |
1.18 |
* execute(null) throws NPE |
986 |
dl |
1.1 |
*/ |
987 |
|
|
public void testExecuteNull() { |
988 |
|
|
ThreadPoolExecutor tpe = null; |
989 |
|
|
try { |
990 |
jsr166 |
1.9 |
tpe = new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
991 |
jsr166 |
1.8 |
tpe.execute(null); |
992 |
dl |
1.1 |
shouldThrow(); |
993 |
jsr166 |
1.8 |
} catch (NullPointerException success) {} |
994 |
jsr166 |
1.2 |
|
995 |
jsr166 |
1.8 |
joinPool(tpe); |
996 |
dl |
1.1 |
} |
997 |
jsr166 |
1.2 |
|
998 |
dl |
1.1 |
/** |
999 |
jsr166 |
1.19 |
* setCorePoolSize of negative value throws IllegalArgumentException |
1000 |
dl |
1.1 |
*/ |
1001 |
|
|
public void testCorePoolSizeIllegalArgumentException() { |
1002 |
jsr166 |
1.8 |
ThreadPoolExecutor tpe = |
1003 |
jsr166 |
1.9 |
new CustomTPE(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1004 |
jsr166 |
1.8 |
try { |
1005 |
|
|
tpe.setCorePoolSize(-1); |
1006 |
|
|
shouldThrow(); |
1007 |
|
|
} catch (IllegalArgumentException success) { |
1008 |
dl |
1.1 |
} finally { |
1009 |
jsr166 |
1.3 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1010 |
dl |
1.1 |
} |
1011 |
|
|
joinPool(tpe); |
1012 |
jsr166 |
1.2 |
} |
1013 |
dl |
1.1 |
|
1014 |
|
|
/** |
1015 |
jsr166 |
1.19 |
* setMaximumPoolSize(int) throws IllegalArgumentException |
1016 |
|
|
* if given a value less the core pool size |
1017 |
jsr166 |
1.2 |
*/ |
1018 |
dl |
1.1 |
public void testMaximumPoolSizeIllegalArgumentException() { |
1019 |
jsr166 |
1.10 |
ThreadPoolExecutor tpe = |
1020 |
|
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1021 |
dl |
1.1 |
try { |
1022 |
|
|
tpe.setMaximumPoolSize(1); |
1023 |
|
|
shouldThrow(); |
1024 |
jsr166 |
1.4 |
} catch (IllegalArgumentException success) { |
1025 |
dl |
1.1 |
} finally { |
1026 |
jsr166 |
1.3 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1027 |
dl |
1.1 |
} |
1028 |
|
|
joinPool(tpe); |
1029 |
|
|
} |
1030 |
jsr166 |
1.2 |
|
1031 |
dl |
1.1 |
/** |
1032 |
jsr166 |
1.19 |
* setMaximumPoolSize throws IllegalArgumentException |
1033 |
|
|
* if given a negative value |
1034 |
dl |
1.1 |
*/ |
1035 |
|
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1036 |
jsr166 |
1.10 |
ThreadPoolExecutor tpe = |
1037 |
|
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1038 |
dl |
1.1 |
try { |
1039 |
|
|
tpe.setMaximumPoolSize(-1); |
1040 |
|
|
shouldThrow(); |
1041 |
jsr166 |
1.4 |
} catch (IllegalArgumentException success) { |
1042 |
dl |
1.1 |
} finally { |
1043 |
jsr166 |
1.3 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1044 |
dl |
1.1 |
} |
1045 |
|
|
joinPool(tpe); |
1046 |
|
|
} |
1047 |
jsr166 |
1.2 |
|
1048 |
dl |
1.1 |
|
1049 |
|
|
/** |
1050 |
jsr166 |
1.19 |
* setKeepAliveTime throws IllegalArgumentException |
1051 |
|
|
* when given a negative value |
1052 |
dl |
1.1 |
*/ |
1053 |
|
|
public void testKeepAliveTimeIllegalArgumentException() { |
1054 |
jsr166 |
1.10 |
ThreadPoolExecutor tpe = |
1055 |
|
|
new CustomTPE(2,3,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1056 |
jsr166 |
1.2 |
|
1057 |
jsr166 |
1.8 |
try { |
1058 |
jsr166 |
1.9 |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
1059 |
dl |
1.1 |
shouldThrow(); |
1060 |
jsr166 |
1.4 |
} catch (IllegalArgumentException success) { |
1061 |
dl |
1.1 |
} finally { |
1062 |
jsr166 |
1.3 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1063 |
dl |
1.1 |
} |
1064 |
|
|
joinPool(tpe); |
1065 |
|
|
} |
1066 |
|
|
|
1067 |
|
|
/** |
1068 |
|
|
* terminated() is called on termination |
1069 |
|
|
*/ |
1070 |
|
|
public void testTerminated() { |
1071 |
|
|
CustomTPE tpe = new CustomTPE(); |
1072 |
jsr166 |
1.3 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1073 |
dl |
1.1 |
assertTrue(tpe.terminatedCalled); |
1074 |
|
|
joinPool(tpe); |
1075 |
|
|
} |
1076 |
|
|
|
1077 |
|
|
/** |
1078 |
|
|
* beforeExecute and afterExecute are called when executing task |
1079 |
|
|
*/ |
1080 |
jsr166 |
1.5 |
public void testBeforeAfter() throws InterruptedException { |
1081 |
dl |
1.1 |
CustomTPE tpe = new CustomTPE(); |
1082 |
|
|
try { |
1083 |
|
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1084 |
|
|
tpe.execute(r); |
1085 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1086 |
|
|
assertTrue(r.done); |
1087 |
|
|
assertTrue(tpe.beforeCalled); |
1088 |
|
|
assertTrue(tpe.afterCalled); |
1089 |
jsr166 |
1.3 |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1090 |
dl |
1.1 |
} finally { |
1091 |
|
|
joinPool(tpe); |
1092 |
|
|
} |
1093 |
|
|
} |
1094 |
|
|
|
1095 |
|
|
/** |
1096 |
|
|
* completed submit of callable returns result |
1097 |
|
|
*/ |
1098 |
jsr166 |
1.5 |
public void testSubmitCallable() throws Exception { |
1099 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1100 |
dl |
1.1 |
try { |
1101 |
|
|
Future<String> future = e.submit(new StringTask()); |
1102 |
|
|
String result = future.get(); |
1103 |
|
|
assertSame(TEST_STRING, result); |
1104 |
|
|
} finally { |
1105 |
|
|
joinPool(e); |
1106 |
|
|
} |
1107 |
|
|
} |
1108 |
|
|
|
1109 |
|
|
/** |
1110 |
|
|
* completed submit of runnable returns successfully |
1111 |
|
|
*/ |
1112 |
jsr166 |
1.5 |
public void testSubmitRunnable() throws Exception { |
1113 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1114 |
dl |
1.1 |
try { |
1115 |
|
|
Future<?> future = e.submit(new NoOpRunnable()); |
1116 |
|
|
future.get(); |
1117 |
|
|
assertTrue(future.isDone()); |
1118 |
|
|
} finally { |
1119 |
|
|
joinPool(e); |
1120 |
|
|
} |
1121 |
|
|
} |
1122 |
|
|
|
1123 |
|
|
/** |
1124 |
|
|
* completed submit of (runnable, result) returns result |
1125 |
|
|
*/ |
1126 |
jsr166 |
1.5 |
public void testSubmitRunnable2() throws Exception { |
1127 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1128 |
dl |
1.1 |
try { |
1129 |
|
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1130 |
|
|
String result = future.get(); |
1131 |
|
|
assertSame(TEST_STRING, result); |
1132 |
|
|
} finally { |
1133 |
|
|
joinPool(e); |
1134 |
|
|
} |
1135 |
|
|
} |
1136 |
|
|
|
1137 |
|
|
|
1138 |
|
|
/** |
1139 |
|
|
* invokeAny(null) throws NPE |
1140 |
|
|
*/ |
1141 |
jsr166 |
1.5 |
public void testInvokeAny1() throws Exception { |
1142 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1143 |
dl |
1.1 |
try { |
1144 |
|
|
e.invokeAny(null); |
1145 |
jsr166 |
1.5 |
shouldThrow(); |
1146 |
dl |
1.1 |
} catch (NullPointerException success) { |
1147 |
|
|
} finally { |
1148 |
|
|
joinPool(e); |
1149 |
|
|
} |
1150 |
|
|
} |
1151 |
|
|
|
1152 |
|
|
/** |
1153 |
|
|
* invokeAny(empty collection) throws IAE |
1154 |
|
|
*/ |
1155 |
jsr166 |
1.5 |
public void testInvokeAny2() throws Exception { |
1156 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1157 |
dl |
1.1 |
try { |
1158 |
|
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1159 |
jsr166 |
1.5 |
shouldThrow(); |
1160 |
dl |
1.1 |
} catch (IllegalArgumentException success) { |
1161 |
|
|
} finally { |
1162 |
|
|
joinPool(e); |
1163 |
|
|
} |
1164 |
|
|
} |
1165 |
|
|
|
1166 |
|
|
/** |
1167 |
|
|
* invokeAny(c) throws NPE if c has null elements |
1168 |
|
|
*/ |
1169 |
jsr166 |
1.5 |
public void testInvokeAny3() throws Exception { |
1170 |
jsr166 |
1.17 |
CountDownLatch latch = new CountDownLatch(1); |
1171 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1172 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1173 |
|
|
l.add(latchAwaitingStringTask(latch)); |
1174 |
|
|
l.add(null); |
1175 |
dl |
1.1 |
try { |
1176 |
|
|
e.invokeAny(l); |
1177 |
jsr166 |
1.5 |
shouldThrow(); |
1178 |
dl |
1.1 |
} catch (NullPointerException success) { |
1179 |
|
|
} finally { |
1180 |
jsr166 |
1.5 |
latch.countDown(); |
1181 |
dl |
1.1 |
joinPool(e); |
1182 |
|
|
} |
1183 |
|
|
} |
1184 |
|
|
|
1185 |
|
|
/** |
1186 |
|
|
* invokeAny(c) throws ExecutionException if no task completes |
1187 |
|
|
*/ |
1188 |
jsr166 |
1.5 |
public void testInvokeAny4() throws Exception { |
1189 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1190 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1191 |
|
|
l.add(new NPETask()); |
1192 |
dl |
1.1 |
try { |
1193 |
|
|
e.invokeAny(l); |
1194 |
jsr166 |
1.5 |
shouldThrow(); |
1195 |
dl |
1.1 |
} catch (ExecutionException success) { |
1196 |
jsr166 |
1.13 |
assertTrue(success.getCause() instanceof NullPointerException); |
1197 |
dl |
1.1 |
} finally { |
1198 |
|
|
joinPool(e); |
1199 |
|
|
} |
1200 |
|
|
} |
1201 |
|
|
|
1202 |
|
|
/** |
1203 |
|
|
* invokeAny(c) returns result of some task |
1204 |
|
|
*/ |
1205 |
jsr166 |
1.5 |
public void testInvokeAny5() throws Exception { |
1206 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1207 |
dl |
1.1 |
try { |
1208 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1209 |
dl |
1.1 |
l.add(new StringTask()); |
1210 |
|
|
l.add(new StringTask()); |
1211 |
|
|
String result = e.invokeAny(l); |
1212 |
|
|
assertSame(TEST_STRING, result); |
1213 |
|
|
} finally { |
1214 |
|
|
joinPool(e); |
1215 |
|
|
} |
1216 |
|
|
} |
1217 |
|
|
|
1218 |
|
|
/** |
1219 |
|
|
* invokeAll(null) throws NPE |
1220 |
|
|
*/ |
1221 |
jsr166 |
1.5 |
public void testInvokeAll1() throws Exception { |
1222 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1223 |
dl |
1.1 |
try { |
1224 |
|
|
e.invokeAll(null); |
1225 |
jsr166 |
1.5 |
shouldThrow(); |
1226 |
dl |
1.1 |
} catch (NullPointerException success) { |
1227 |
|
|
} finally { |
1228 |
|
|
joinPool(e); |
1229 |
|
|
} |
1230 |
|
|
} |
1231 |
|
|
|
1232 |
|
|
/** |
1233 |
|
|
* invokeAll(empty collection) returns empty collection |
1234 |
|
|
*/ |
1235 |
jsr166 |
1.5 |
public void testInvokeAll2() throws Exception { |
1236 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1237 |
dl |
1.1 |
try { |
1238 |
|
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1239 |
|
|
assertTrue(r.isEmpty()); |
1240 |
|
|
} finally { |
1241 |
|
|
joinPool(e); |
1242 |
|
|
} |
1243 |
|
|
} |
1244 |
|
|
|
1245 |
|
|
/** |
1246 |
|
|
* invokeAll(c) throws NPE if c has null elements |
1247 |
|
|
*/ |
1248 |
jsr166 |
1.5 |
public void testInvokeAll3() throws Exception { |
1249 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1250 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1251 |
|
|
l.add(new StringTask()); |
1252 |
|
|
l.add(null); |
1253 |
dl |
1.1 |
try { |
1254 |
|
|
e.invokeAll(l); |
1255 |
jsr166 |
1.5 |
shouldThrow(); |
1256 |
dl |
1.1 |
} catch (NullPointerException success) { |
1257 |
|
|
} finally { |
1258 |
|
|
joinPool(e); |
1259 |
|
|
} |
1260 |
|
|
} |
1261 |
|
|
|
1262 |
|
|
/** |
1263 |
|
|
* get of element of invokeAll(c) throws exception on failed task |
1264 |
|
|
*/ |
1265 |
jsr166 |
1.5 |
public void testInvokeAll4() throws Exception { |
1266 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1267 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1268 |
|
|
l.add(new NPETask()); |
1269 |
|
|
List<Future<String>> futures = e.invokeAll(l); |
1270 |
|
|
assertEquals(1, futures.size()); |
1271 |
dl |
1.1 |
try { |
1272 |
jsr166 |
1.17 |
futures.get(0).get(); |
1273 |
jsr166 |
1.5 |
shouldThrow(); |
1274 |
jsr166 |
1.3 |
} catch (ExecutionException success) { |
1275 |
jsr166 |
1.15 |
assertTrue(success.getCause() instanceof NullPointerException); |
1276 |
dl |
1.1 |
} finally { |
1277 |
|
|
joinPool(e); |
1278 |
|
|
} |
1279 |
|
|
} |
1280 |
|
|
|
1281 |
|
|
/** |
1282 |
|
|
* invokeAll(c) returns results of all completed tasks |
1283 |
|
|
*/ |
1284 |
jsr166 |
1.5 |
public void testInvokeAll5() throws Exception { |
1285 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1286 |
dl |
1.1 |
try { |
1287 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1288 |
dl |
1.1 |
l.add(new StringTask()); |
1289 |
|
|
l.add(new StringTask()); |
1290 |
jsr166 |
1.17 |
List<Future<String>> futures = e.invokeAll(l); |
1291 |
|
|
assertEquals(2, futures.size()); |
1292 |
|
|
for (Future<String> future : futures) |
1293 |
jsr166 |
1.6 |
assertSame(TEST_STRING, future.get()); |
1294 |
dl |
1.1 |
} finally { |
1295 |
|
|
joinPool(e); |
1296 |
|
|
} |
1297 |
|
|
} |
1298 |
|
|
|
1299 |
|
|
|
1300 |
|
|
|
1301 |
|
|
/** |
1302 |
|
|
* timed invokeAny(null) throws NPE |
1303 |
|
|
*/ |
1304 |
jsr166 |
1.5 |
public void testTimedInvokeAny1() throws Exception { |
1305 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1306 |
dl |
1.1 |
try { |
1307 |
jsr166 |
1.9 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1308 |
jsr166 |
1.5 |
shouldThrow(); |
1309 |
dl |
1.1 |
} catch (NullPointerException success) { |
1310 |
|
|
} finally { |
1311 |
|
|
joinPool(e); |
1312 |
|
|
} |
1313 |
|
|
} |
1314 |
|
|
|
1315 |
|
|
/** |
1316 |
|
|
* timed invokeAny(,,null) throws NPE |
1317 |
|
|
*/ |
1318 |
jsr166 |
1.5 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1319 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1320 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1321 |
|
|
l.add(new StringTask()); |
1322 |
dl |
1.1 |
try { |
1323 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1324 |
jsr166 |
1.5 |
shouldThrow(); |
1325 |
dl |
1.1 |
} catch (NullPointerException success) { |
1326 |
|
|
} finally { |
1327 |
|
|
joinPool(e); |
1328 |
|
|
} |
1329 |
|
|
} |
1330 |
|
|
|
1331 |
|
|
/** |
1332 |
|
|
* timed invokeAny(empty collection) throws IAE |
1333 |
|
|
*/ |
1334 |
jsr166 |
1.5 |
public void testTimedInvokeAny2() throws Exception { |
1335 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1336 |
dl |
1.1 |
try { |
1337 |
jsr166 |
1.9 |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1338 |
jsr166 |
1.5 |
shouldThrow(); |
1339 |
dl |
1.1 |
} catch (IllegalArgumentException success) { |
1340 |
|
|
} finally { |
1341 |
|
|
joinPool(e); |
1342 |
|
|
} |
1343 |
|
|
} |
1344 |
|
|
|
1345 |
|
|
/** |
1346 |
|
|
* timed invokeAny(c) throws NPE if c has null elements |
1347 |
|
|
*/ |
1348 |
jsr166 |
1.5 |
public void testTimedInvokeAny3() throws Exception { |
1349 |
jsr166 |
1.17 |
CountDownLatch latch = new CountDownLatch(1); |
1350 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1351 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1352 |
|
|
l.add(latchAwaitingStringTask(latch)); |
1353 |
|
|
l.add(null); |
1354 |
dl |
1.1 |
try { |
1355 |
jsr166 |
1.9 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1356 |
jsr166 |
1.5 |
shouldThrow(); |
1357 |
dl |
1.1 |
} catch (NullPointerException success) { |
1358 |
|
|
} finally { |
1359 |
jsr166 |
1.12 |
latch.countDown(); |
1360 |
dl |
1.1 |
joinPool(e); |
1361 |
|
|
} |
1362 |
|
|
} |
1363 |
|
|
|
1364 |
|
|
/** |
1365 |
|
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1366 |
|
|
*/ |
1367 |
jsr166 |
1.5 |
public void testTimedInvokeAny4() throws Exception { |
1368 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1369 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1370 |
|
|
l.add(new NPETask()); |
1371 |
dl |
1.1 |
try { |
1372 |
jsr166 |
1.9 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1373 |
jsr166 |
1.5 |
shouldThrow(); |
1374 |
jsr166 |
1.3 |
} catch (ExecutionException success) { |
1375 |
jsr166 |
1.13 |
assertTrue(success.getCause() instanceof NullPointerException); |
1376 |
dl |
1.1 |
} finally { |
1377 |
|
|
joinPool(e); |
1378 |
|
|
} |
1379 |
|
|
} |
1380 |
|
|
|
1381 |
|
|
/** |
1382 |
|
|
* timed invokeAny(c) returns result of some task |
1383 |
|
|
*/ |
1384 |
jsr166 |
1.5 |
public void testTimedInvokeAny5() throws Exception { |
1385 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1386 |
dl |
1.1 |
try { |
1387 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1388 |
dl |
1.1 |
l.add(new StringTask()); |
1389 |
|
|
l.add(new StringTask()); |
1390 |
jsr166 |
1.9 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1391 |
dl |
1.1 |
assertSame(TEST_STRING, result); |
1392 |
|
|
} finally { |
1393 |
|
|
joinPool(e); |
1394 |
|
|
} |
1395 |
|
|
} |
1396 |
|
|
|
1397 |
|
|
/** |
1398 |
|
|
* timed invokeAll(null) throws NPE |
1399 |
|
|
*/ |
1400 |
jsr166 |
1.5 |
public void testTimedInvokeAll1() throws Exception { |
1401 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1402 |
dl |
1.1 |
try { |
1403 |
jsr166 |
1.9 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1404 |
jsr166 |
1.5 |
shouldThrow(); |
1405 |
dl |
1.1 |
} catch (NullPointerException success) { |
1406 |
|
|
} finally { |
1407 |
|
|
joinPool(e); |
1408 |
|
|
} |
1409 |
|
|
} |
1410 |
|
|
|
1411 |
|
|
/** |
1412 |
|
|
* timed invokeAll(,,null) throws NPE |
1413 |
|
|
*/ |
1414 |
jsr166 |
1.5 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1415 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1416 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1417 |
|
|
l.add(new StringTask()); |
1418 |
dl |
1.1 |
try { |
1419 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1420 |
jsr166 |
1.5 |
shouldThrow(); |
1421 |
dl |
1.1 |
} catch (NullPointerException success) { |
1422 |
|
|
} finally { |
1423 |
|
|
joinPool(e); |
1424 |
|
|
} |
1425 |
|
|
} |
1426 |
|
|
|
1427 |
|
|
/** |
1428 |
|
|
* timed invokeAll(empty collection) returns empty collection |
1429 |
|
|
*/ |
1430 |
jsr166 |
1.5 |
public void testTimedInvokeAll2() throws Exception { |
1431 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1432 |
dl |
1.1 |
try { |
1433 |
jsr166 |
1.9 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1434 |
dl |
1.1 |
assertTrue(r.isEmpty()); |
1435 |
|
|
} finally { |
1436 |
|
|
joinPool(e); |
1437 |
|
|
} |
1438 |
|
|
} |
1439 |
|
|
|
1440 |
|
|
/** |
1441 |
|
|
* timed invokeAll(c) throws NPE if c has null elements |
1442 |
|
|
*/ |
1443 |
jsr166 |
1.5 |
public void testTimedInvokeAll3() throws Exception { |
1444 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1445 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1446 |
|
|
l.add(new StringTask()); |
1447 |
|
|
l.add(null); |
1448 |
dl |
1.1 |
try { |
1449 |
jsr166 |
1.9 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1450 |
jsr166 |
1.5 |
shouldThrow(); |
1451 |
dl |
1.1 |
} catch (NullPointerException success) { |
1452 |
|
|
} finally { |
1453 |
|
|
joinPool(e); |
1454 |
|
|
} |
1455 |
|
|
} |
1456 |
|
|
|
1457 |
|
|
/** |
1458 |
|
|
* get of element of invokeAll(c) throws exception on failed task |
1459 |
|
|
*/ |
1460 |
jsr166 |
1.5 |
public void testTimedInvokeAll4() throws Exception { |
1461 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1462 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1463 |
|
|
l.add(new NPETask()); |
1464 |
|
|
List<Future<String>> futures = |
1465 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1466 |
|
|
assertEquals(1, futures.size()); |
1467 |
dl |
1.1 |
try { |
1468 |
jsr166 |
1.17 |
futures.get(0).get(); |
1469 |
jsr166 |
1.5 |
shouldThrow(); |
1470 |
jsr166 |
1.3 |
} catch (ExecutionException success) { |
1471 |
jsr166 |
1.6 |
assertTrue(success.getCause() instanceof NullPointerException); |
1472 |
dl |
1.1 |
} finally { |
1473 |
|
|
joinPool(e); |
1474 |
|
|
} |
1475 |
|
|
} |
1476 |
|
|
|
1477 |
|
|
/** |
1478 |
|
|
* timed invokeAll(c) returns results of all completed tasks |
1479 |
|
|
*/ |
1480 |
jsr166 |
1.5 |
public void testTimedInvokeAll5() throws Exception { |
1481 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1482 |
dl |
1.1 |
try { |
1483 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1484 |
dl |
1.1 |
l.add(new StringTask()); |
1485 |
|
|
l.add(new StringTask()); |
1486 |
jsr166 |
1.17 |
List<Future<String>> futures = |
1487 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1488 |
|
|
assertEquals(2, futures.size()); |
1489 |
|
|
for (Future<String> future : futures) |
1490 |
jsr166 |
1.6 |
assertSame(TEST_STRING, future.get()); |
1491 |
dl |
1.1 |
} finally { |
1492 |
|
|
joinPool(e); |
1493 |
|
|
} |
1494 |
|
|
} |
1495 |
|
|
|
1496 |
|
|
/** |
1497 |
|
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1498 |
|
|
*/ |
1499 |
jsr166 |
1.5 |
public void testTimedInvokeAll6() throws Exception { |
1500 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1501 |
dl |
1.1 |
try { |
1502 |
jsr166 |
1.17 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1503 |
dl |
1.1 |
l.add(new StringTask()); |
1504 |
|
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1505 |
|
|
l.add(new StringTask()); |
1506 |
jsr166 |
1.17 |
List<Future<String>> futures = |
1507 |
|
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1508 |
|
|
assertEquals(3, futures.size()); |
1509 |
|
|
Iterator<Future<String>> it = futures.iterator(); |
1510 |
dl |
1.1 |
Future<String> f1 = it.next(); |
1511 |
|
|
Future<String> f2 = it.next(); |
1512 |
|
|
Future<String> f3 = it.next(); |
1513 |
|
|
assertTrue(f1.isDone()); |
1514 |
|
|
assertTrue(f2.isDone()); |
1515 |
|
|
assertTrue(f3.isDone()); |
1516 |
|
|
assertFalse(f1.isCancelled()); |
1517 |
|
|
assertTrue(f2.isCancelled()); |
1518 |
|
|
} finally { |
1519 |
|
|
joinPool(e); |
1520 |
|
|
} |
1521 |
|
|
} |
1522 |
|
|
|
1523 |
|
|
/** |
1524 |
|
|
* Execution continues if there is at least one thread even if |
1525 |
|
|
* thread factory fails to create more |
1526 |
|
|
*/ |
1527 |
jsr166 |
1.5 |
public void testFailingThreadFactory() throws InterruptedException { |
1528 |
jsr166 |
1.9 |
ExecutorService e = new CustomTPE(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1529 |
dl |
1.1 |
try { |
1530 |
|
|
for (int k = 0; k < 100; ++k) { |
1531 |
|
|
e.execute(new NoOpRunnable()); |
1532 |
|
|
} |
1533 |
|
|
Thread.sleep(LONG_DELAY_MS); |
1534 |
|
|
} finally { |
1535 |
|
|
joinPool(e); |
1536 |
|
|
} |
1537 |
|
|
} |
1538 |
|
|
|
1539 |
|
|
/** |
1540 |
|
|
* allowsCoreThreadTimeOut is by default false. |
1541 |
|
|
*/ |
1542 |
|
|
public void testAllowsCoreThreadTimeOut() { |
1543 |
jsr166 |
1.9 |
ThreadPoolExecutor tpe = new CustomTPE(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1544 |
dl |
1.1 |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1545 |
|
|
joinPool(tpe); |
1546 |
|
|
} |
1547 |
|
|
|
1548 |
|
|
/** |
1549 |
|
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1550 |
|
|
*/ |
1551 |
jsr166 |
1.5 |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1552 |
jsr166 |
1.9 |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1553 |
dl |
1.1 |
tpe.allowCoreThreadTimeOut(true); |
1554 |
|
|
tpe.execute(new NoOpRunnable()); |
1555 |
|
|
try { |
1556 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
1557 |
|
|
assertEquals(0, tpe.getPoolSize()); |
1558 |
|
|
} finally { |
1559 |
|
|
joinPool(tpe); |
1560 |
|
|
} |
1561 |
|
|
} |
1562 |
|
|
|
1563 |
|
|
/** |
1564 |
|
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1565 |
|
|
*/ |
1566 |
jsr166 |
1.5 |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1567 |
jsr166 |
1.9 |
ThreadPoolExecutor tpe = new CustomTPE(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1568 |
dl |
1.1 |
tpe.allowCoreThreadTimeOut(false); |
1569 |
|
|
tpe.execute(new NoOpRunnable()); |
1570 |
|
|
try { |
1571 |
|
|
Thread.sleep(MEDIUM_DELAY_MS); |
1572 |
|
|
assertTrue(tpe.getPoolSize() >= 1); |
1573 |
|
|
} finally { |
1574 |
|
|
joinPool(tpe); |
1575 |
|
|
} |
1576 |
|
|
} |
1577 |
|
|
|
1578 |
|
|
} |