1 |
dl |
1.1 |
/* |
2 |
|
|
* Written by members of JCP JSR-166 Expert Group and released to the |
3 |
|
|
* public domain. Use, modify, and redistribute this code in any way |
4 |
|
|
* without acknowledgement. Other contributors include Andrew Wright, |
5 |
|
|
* Jeffrey Hayes, Pat Fischer, Mike Judd. |
6 |
|
|
*/ |
7 |
|
|
|
8 |
|
|
import java.util.concurrent.*; |
9 |
|
|
import junit.framework.*; |
10 |
|
|
import java.util.List; |
11 |
|
|
|
12 |
dl |
1.3 |
public class ThreadPoolExecutorTest extends JSR166TestCase { |
13 |
dl |
1.1 |
public static void main(String[] args) { |
14 |
|
|
junit.textui.TestRunner.run (suite()); |
15 |
|
|
} |
16 |
|
|
public static Test suite() { |
17 |
|
|
return new TestSuite(ThreadPoolExecutorTest.class); |
18 |
|
|
} |
19 |
|
|
|
20 |
dl |
1.8 |
static class ExtendedTPE extends ThreadPoolExecutor { |
21 |
|
|
volatile boolean beforeCalled = false; |
22 |
|
|
volatile boolean afterCalled = false; |
23 |
|
|
volatile boolean terminatedCalled = false; |
24 |
|
|
public ExtendedTPE() { |
25 |
|
|
super(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new SynchronousQueue<Runnable>()); |
26 |
|
|
} |
27 |
|
|
protected void beforeExecute(Thread t, Runnable r) { |
28 |
|
|
beforeCalled = true; |
29 |
|
|
} |
30 |
|
|
protected void afterExecute(Runnable r, Throwable t) { |
31 |
|
|
afterCalled = true; |
32 |
|
|
} |
33 |
|
|
protected void terminated() { |
34 |
|
|
terminatedCalled = true; |
35 |
|
|
} |
36 |
|
|
} |
37 |
dl |
1.1 |
|
38 |
dl |
1.3 |
/** |
39 |
dl |
1.8 |
* execute successfully executes a runnable |
40 |
dl |
1.1 |
*/ |
41 |
dl |
1.5 |
public void testExecute() { |
42 |
dl |
1.8 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
43 |
dl |
1.1 |
try { |
44 |
dl |
1.5 |
p1.execute(new Runnable() { |
45 |
|
|
public void run() { |
46 |
|
|
try { |
47 |
dl |
1.1 |
Thread.sleep(SHORT_DELAY_MS); |
48 |
dl |
1.2 |
} catch(InterruptedException e){ |
49 |
dl |
1.5 |
threadUnexpectedException(); |
50 |
dl |
1.1 |
} |
51 |
|
|
} |
52 |
|
|
}); |
53 |
dl |
1.3 |
Thread.sleep(SMALL_DELAY_MS); |
54 |
dl |
1.2 |
} catch(InterruptedException e){ |
55 |
dl |
1.5 |
unexpectedException(); |
56 |
dl |
1.3 |
} |
57 |
dl |
1.5 |
joinPool(p1); |
58 |
dl |
1.1 |
} |
59 |
|
|
|
60 |
|
|
/** |
61 |
dl |
1.6 |
* getActiveCount increases but doesn't overestimate, when a |
62 |
|
|
* thread becomes active |
63 |
dl |
1.1 |
*/ |
64 |
dl |
1.5 |
public void testGetActiveCount() { |
65 |
dl |
1.8 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
66 |
dl |
1.5 |
assertEquals(0, p2.getActiveCount()); |
67 |
|
|
p2.execute(new MediumRunnable()); |
68 |
|
|
try { |
69 |
dl |
1.3 |
Thread.sleep(SHORT_DELAY_MS); |
70 |
|
|
} catch(Exception e){ |
71 |
dl |
1.5 |
unexpectedException(); |
72 |
dl |
1.1 |
} |
73 |
dl |
1.5 |
assertEquals(1, p2.getActiveCount()); |
74 |
|
|
joinPool(p2); |
75 |
dl |
1.1 |
} |
76 |
dl |
1.8 |
|
77 |
|
|
/** |
78 |
|
|
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
79 |
|
|
*/ |
80 |
|
|
public void testPrestartCoreThread() { |
81 |
|
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
82 |
|
|
assertEquals(0, p2.getPoolSize()); |
83 |
|
|
assertTrue(p2.prestartCoreThread()); |
84 |
|
|
assertEquals(1, p2.getPoolSize()); |
85 |
|
|
assertTrue(p2.prestartCoreThread()); |
86 |
|
|
assertEquals(2, p2.getPoolSize()); |
87 |
|
|
assertFalse(p2.prestartCoreThread()); |
88 |
|
|
assertEquals(2, p2.getPoolSize()); |
89 |
|
|
joinPool(p2); |
90 |
|
|
} |
91 |
|
|
|
92 |
|
|
/** |
93 |
|
|
* prestartAllCoreThreads starts all corePoolSize threads |
94 |
|
|
*/ |
95 |
|
|
public void testPrestartAllCoreThreads() { |
96 |
|
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
97 |
|
|
assertEquals(0, p2.getPoolSize()); |
98 |
|
|
p2.prestartAllCoreThreads(); |
99 |
|
|
assertEquals(2, p2.getPoolSize()); |
100 |
|
|
p2.prestartAllCoreThreads(); |
101 |
|
|
assertEquals(2, p2.getPoolSize()); |
102 |
|
|
joinPool(p2); |
103 |
|
|
} |
104 |
dl |
1.1 |
|
105 |
|
|
/** |
106 |
dl |
1.6 |
* getCompletedTaskCount increases, but doesn't overestimate, |
107 |
|
|
* when tasks complete |
108 |
dl |
1.1 |
*/ |
109 |
dl |
1.5 |
public void testGetCompletedTaskCount() { |
110 |
dl |
1.8 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
111 |
dl |
1.5 |
assertEquals(0, p2.getCompletedTaskCount()); |
112 |
|
|
p2.execute(new ShortRunnable()); |
113 |
|
|
try { |
114 |
dl |
1.8 |
Thread.sleep(SMALL_DELAY_MS); |
115 |
dl |
1.3 |
} catch(Exception e){ |
116 |
dl |
1.5 |
unexpectedException(); |
117 |
dl |
1.1 |
} |
118 |
dl |
1.5 |
assertEquals(1, p2.getCompletedTaskCount()); |
119 |
|
|
p2.shutdown(); |
120 |
|
|
joinPool(p2); |
121 |
dl |
1.1 |
} |
122 |
|
|
|
123 |
|
|
/** |
124 |
dl |
1.6 |
* getCorePoolSize returns size given in constructor if not otherwise set |
125 |
dl |
1.1 |
*/ |
126 |
dl |
1.5 |
public void testGetCorePoolSize() { |
127 |
dl |
1.8 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
128 |
dl |
1.5 |
assertEquals(1, p1.getCorePoolSize()); |
129 |
|
|
joinPool(p1); |
130 |
dl |
1.1 |
} |
131 |
|
|
|
132 |
|
|
/** |
133 |
dl |
1.6 |
* getKeepAliveTime returns value given in constructor if not otherwise set |
134 |
dl |
1.1 |
*/ |
135 |
dl |
1.5 |
public void testGetKeepAliveTime() { |
136 |
|
|
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
137 |
|
|
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
138 |
|
|
joinPool(p2); |
139 |
dl |
1.1 |
} |
140 |
dl |
1.8 |
|
141 |
|
|
|
142 |
|
|
/** |
143 |
|
|
* getThreadFactory returns factory in constructor if not set |
144 |
|
|
*/ |
145 |
|
|
public void testGetThreadFactory() { |
146 |
|
|
ThreadFactory tf = new SimpleThreadFactory(); |
147 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
148 |
|
|
assertSame(tf, p.getThreadFactory()); |
149 |
|
|
p.shutdown(); |
150 |
|
|
joinPool(p); |
151 |
|
|
} |
152 |
|
|
|
153 |
|
|
/** |
154 |
|
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
155 |
|
|
*/ |
156 |
|
|
public void testSetThreadFactory() { |
157 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
158 |
|
|
ThreadFactory tf = new SimpleThreadFactory(); |
159 |
|
|
p.setThreadFactory(tf); |
160 |
|
|
assertSame(tf, p.getThreadFactory()); |
161 |
|
|
p.shutdown(); |
162 |
|
|
joinPool(p); |
163 |
|
|
} |
164 |
|
|
|
165 |
|
|
|
166 |
|
|
/** |
167 |
|
|
* setThreadFactory(null) throws NPE |
168 |
|
|
*/ |
169 |
|
|
public void testSetThreadFactoryNull() { |
170 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
171 |
|
|
try { |
172 |
|
|
p.setThreadFactory(null); |
173 |
|
|
shouldThrow(); |
174 |
|
|
} catch (NullPointerException success) { |
175 |
|
|
} finally { |
176 |
|
|
joinPool(p); |
177 |
|
|
} |
178 |
|
|
} |
179 |
|
|
|
180 |
dl |
1.9 |
/** |
181 |
|
|
* getRejectedExecutionHandler returns handler in constructor if not set |
182 |
|
|
*/ |
183 |
|
|
public void testGetRejectedExecutionHandler() { |
184 |
|
|
RejectedExecutionHandler h = new NoOpREHandler(); |
185 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
186 |
|
|
assertSame(h, p.getRejectedExecutionHandler()); |
187 |
|
|
p.shutdown(); |
188 |
|
|
joinPool(p); |
189 |
|
|
} |
190 |
|
|
|
191 |
|
|
/** |
192 |
|
|
* setRejectedExecutionHandler sets the handler returned by |
193 |
|
|
* getRejectedExecutionHandler |
194 |
|
|
*/ |
195 |
|
|
public void testSetRejectedExecutionHandler() { |
196 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
197 |
|
|
RejectedExecutionHandler h = new NoOpREHandler(); |
198 |
|
|
p.setRejectedExecutionHandler(h); |
199 |
|
|
assertSame(h, p.getRejectedExecutionHandler()); |
200 |
|
|
p.shutdown(); |
201 |
|
|
joinPool(p); |
202 |
|
|
} |
203 |
|
|
|
204 |
|
|
|
205 |
|
|
/** |
206 |
|
|
* setRejectedExecutionHandler(null) throws NPE |
207 |
|
|
*/ |
208 |
|
|
public void testSetRejectedExecutionHandlerNull() { |
209 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
210 |
|
|
try { |
211 |
|
|
p.setRejectedExecutionHandler(null); |
212 |
|
|
shouldThrow(); |
213 |
|
|
} catch (NullPointerException success) { |
214 |
|
|
} finally { |
215 |
|
|
joinPool(p); |
216 |
|
|
} |
217 |
|
|
} |
218 |
|
|
|
219 |
dl |
1.1 |
|
220 |
|
|
/** |
221 |
dl |
1.6 |
* getLargestPoolSize increases, but doesn't overestimate, when |
222 |
|
|
* multiple threads active |
223 |
dl |
1.1 |
*/ |
224 |
dl |
1.5 |
public void testGetLargestPoolSize() { |
225 |
dl |
1.8 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
226 |
dl |
1.1 |
try { |
227 |
dl |
1.5 |
assertEquals(0, p2.getLargestPoolSize()); |
228 |
|
|
p2.execute(new MediumRunnable()); |
229 |
|
|
p2.execute(new MediumRunnable()); |
230 |
dl |
1.3 |
Thread.sleep(SHORT_DELAY_MS); |
231 |
dl |
1.5 |
assertEquals(2, p2.getLargestPoolSize()); |
232 |
dl |
1.3 |
} catch(Exception e){ |
233 |
dl |
1.5 |
unexpectedException(); |
234 |
dl |
1.3 |
} |
235 |
dl |
1.5 |
joinPool(p2); |
236 |
dl |
1.1 |
} |
237 |
|
|
|
238 |
|
|
/** |
239 |
dl |
1.6 |
* getMaximumPoolSize returns value given in constructor if not |
240 |
|
|
* otherwise set |
241 |
dl |
1.1 |
*/ |
242 |
dl |
1.5 |
public void testGetMaximumPoolSize() { |
243 |
dl |
1.8 |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
244 |
dl |
1.5 |
assertEquals(2, p2.getMaximumPoolSize()); |
245 |
|
|
joinPool(p2); |
246 |
dl |
1.1 |
} |
247 |
|
|
|
248 |
|
|
/** |
249 |
dl |
1.6 |
* getPoolSize increases, but doesn't overestimate, when threads |
250 |
|
|
* become active |
251 |
dl |
1.1 |
*/ |
252 |
dl |
1.5 |
public void testGetPoolSize() { |
253 |
dl |
1.8 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
254 |
dl |
1.5 |
assertEquals(0, p1.getPoolSize()); |
255 |
|
|
p1.execute(new MediumRunnable()); |
256 |
|
|
assertEquals(1, p1.getPoolSize()); |
257 |
|
|
joinPool(p1); |
258 |
dl |
1.1 |
} |
259 |
|
|
|
260 |
|
|
/** |
261 |
dl |
1.8 |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
262 |
dl |
1.1 |
*/ |
263 |
dl |
1.5 |
public void testGetTaskCount() { |
264 |
dl |
1.8 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
265 |
dl |
1.1 |
try { |
266 |
dl |
1.5 |
assertEquals(0, p1.getTaskCount()); |
267 |
|
|
p1.execute(new MediumRunnable()); |
268 |
dl |
1.3 |
Thread.sleep(SHORT_DELAY_MS); |
269 |
dl |
1.5 |
assertEquals(1, p1.getTaskCount()); |
270 |
dl |
1.3 |
} catch(Exception e){ |
271 |
dl |
1.5 |
unexpectedException(); |
272 |
dl |
1.3 |
} |
273 |
dl |
1.5 |
joinPool(p1); |
274 |
dl |
1.1 |
} |
275 |
|
|
|
276 |
|
|
/** |
277 |
dl |
1.6 |
* isShutDown is false before shutdown, true after |
278 |
dl |
1.1 |
*/ |
279 |
dl |
1.5 |
public void testIsShutdown() { |
280 |
dl |
1.1 |
|
281 |
dl |
1.8 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
282 |
dl |
1.5 |
assertFalse(p1.isShutdown()); |
283 |
|
|
p1.shutdown(); |
284 |
|
|
assertTrue(p1.isShutdown()); |
285 |
|
|
joinPool(p1); |
286 |
dl |
1.1 |
} |
287 |
|
|
|
288 |
|
|
|
289 |
|
|
/** |
290 |
dl |
1.6 |
* isTerminated is false before termination, true after |
291 |
dl |
1.1 |
*/ |
292 |
dl |
1.5 |
public void testIsTerminated() { |
293 |
dl |
1.8 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
294 |
dl |
1.6 |
assertFalse(p1.isTerminated()); |
295 |
dl |
1.1 |
try { |
296 |
dl |
1.5 |
p1.execute(new MediumRunnable()); |
297 |
dl |
1.1 |
} finally { |
298 |
dl |
1.5 |
p1.shutdown(); |
299 |
dl |
1.1 |
} |
300 |
dl |
1.3 |
try { |
301 |
dl |
1.5 |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
302 |
|
|
assertTrue(p1.isTerminated()); |
303 |
dl |
1.3 |
} catch(Exception e){ |
304 |
dl |
1.5 |
unexpectedException(); |
305 |
|
|
} |
306 |
|
|
} |
307 |
|
|
|
308 |
|
|
/** |
309 |
dl |
1.6 |
* isTerminating is not true when running or when terminated |
310 |
dl |
1.5 |
*/ |
311 |
|
|
public void testIsTerminating() { |
312 |
dl |
1.8 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
313 |
dl |
1.5 |
assertFalse(p1.isTerminating()); |
314 |
|
|
try { |
315 |
|
|
p1.execute(new SmallRunnable()); |
316 |
|
|
assertFalse(p1.isTerminating()); |
317 |
|
|
} finally { |
318 |
|
|
p1.shutdown(); |
319 |
|
|
} |
320 |
|
|
try { |
321 |
|
|
assertTrue(p1.awaitTermination(LONG_DELAY_MS, TimeUnit.MILLISECONDS)); |
322 |
|
|
assertTrue(p1.isTerminated()); |
323 |
|
|
assertFalse(p1.isTerminating()); |
324 |
|
|
} catch(Exception e){ |
325 |
|
|
unexpectedException(); |
326 |
dl |
1.3 |
} |
327 |
dl |
1.1 |
} |
328 |
|
|
|
329 |
|
|
/** |
330 |
dl |
1.8 |
* getQueue returns the work queue, which contains queued tasks |
331 |
|
|
*/ |
332 |
|
|
public void testGetQueue() { |
333 |
|
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
334 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
335 |
dl |
1.11 |
FutureTask[] tasks = new FutureTask[5]; |
336 |
dl |
1.8 |
for(int i = 0; i < 5; i++){ |
337 |
dl |
1.11 |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
338 |
dl |
1.8 |
p1.execute(tasks[i]); |
339 |
|
|
} |
340 |
|
|
try { |
341 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
342 |
|
|
BlockingQueue<Runnable> wq = p1.getQueue(); |
343 |
|
|
assertSame(q, wq); |
344 |
|
|
assertFalse(wq.contains(tasks[0])); |
345 |
|
|
assertTrue(wq.contains(tasks[4])); |
346 |
|
|
p1.shutdownNow(); |
347 |
|
|
} catch(Exception e) { |
348 |
|
|
unexpectedException(); |
349 |
|
|
} finally { |
350 |
|
|
joinPool(p1); |
351 |
|
|
} |
352 |
|
|
} |
353 |
|
|
|
354 |
|
|
/** |
355 |
|
|
* remove(task) removes queued task, and fails to remove active task |
356 |
|
|
*/ |
357 |
|
|
public void testRemove() { |
358 |
|
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
359 |
|
|
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, q); |
360 |
dl |
1.11 |
FutureTask[] tasks = new FutureTask[5]; |
361 |
dl |
1.8 |
for(int i = 0; i < 5; i++){ |
362 |
dl |
1.11 |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
363 |
dl |
1.8 |
p1.execute(tasks[i]); |
364 |
|
|
} |
365 |
|
|
try { |
366 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
367 |
|
|
assertFalse(p1.remove(tasks[0])); |
368 |
|
|
assertTrue(q.contains(tasks[4])); |
369 |
|
|
assertTrue(q.contains(tasks[3])); |
370 |
|
|
assertTrue(p1.remove(tasks[4])); |
371 |
|
|
assertFalse(p1.remove(tasks[4])); |
372 |
|
|
assertFalse(q.contains(tasks[4])); |
373 |
|
|
assertTrue(q.contains(tasks[3])); |
374 |
|
|
assertTrue(p1.remove(tasks[3])); |
375 |
|
|
assertFalse(q.contains(tasks[3])); |
376 |
|
|
p1.shutdownNow(); |
377 |
|
|
} catch(Exception e) { |
378 |
|
|
unexpectedException(); |
379 |
|
|
} finally { |
380 |
|
|
joinPool(p1); |
381 |
|
|
} |
382 |
|
|
} |
383 |
|
|
|
384 |
|
|
/** |
385 |
dl |
1.6 |
* purge removes cancelled tasks from the queue |
386 |
dl |
1.1 |
*/ |
387 |
dl |
1.5 |
public void testPurge() { |
388 |
dl |
1.8 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
389 |
dl |
1.11 |
FutureTask[] tasks = new FutureTask[5]; |
390 |
dl |
1.3 |
for(int i = 0; i < 5; i++){ |
391 |
dl |
1.11 |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
392 |
dl |
1.5 |
p1.execute(tasks[i]); |
393 |
dl |
1.3 |
} |
394 |
|
|
tasks[4].cancel(true); |
395 |
|
|
tasks[3].cancel(true); |
396 |
dl |
1.5 |
p1.purge(); |
397 |
|
|
long count = p1.getTaskCount(); |
398 |
dl |
1.3 |
assertTrue(count >= 2 && count < 5); |
399 |
dl |
1.8 |
p1.shutdownNow(); |
400 |
dl |
1.5 |
joinPool(p1); |
401 |
dl |
1.1 |
} |
402 |
|
|
|
403 |
|
|
/** |
404 |
dl |
1.6 |
* shutDownNow returns a list containing tasks that were not run |
405 |
dl |
1.1 |
*/ |
406 |
dl |
1.5 |
public void testShutDownNow() { |
407 |
dl |
1.8 |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
408 |
dl |
1.1 |
List l; |
409 |
|
|
try { |
410 |
|
|
for(int i = 0; i < 5; i++) |
411 |
dl |
1.5 |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
412 |
dl |
1.1 |
} |
413 |
|
|
finally { |
414 |
dl |
1.5 |
l = p1.shutdownNow(); |
415 |
dl |
1.1 |
} |
416 |
dl |
1.5 |
assertTrue(p1.isShutdown()); |
417 |
dl |
1.1 |
assertTrue(l.size() <= 4); |
418 |
|
|
} |
419 |
|
|
|
420 |
|
|
// Exception Tests |
421 |
|
|
|
422 |
|
|
|
423 |
dl |
1.6 |
/** |
424 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
425 |
|
|
*/ |
426 |
dl |
1.1 |
public void testConstructor1() { |
427 |
dl |
1.5 |
try { |
428 |
dl |
1.3 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
429 |
dl |
1.5 |
shouldThrow(); |
430 |
dl |
1.1 |
} |
431 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
432 |
dl |
1.1 |
} |
433 |
|
|
|
434 |
dl |
1.6 |
/** |
435 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
436 |
|
|
*/ |
437 |
dl |
1.1 |
public void testConstructor2() { |
438 |
dl |
1.5 |
try { |
439 |
dl |
1.3 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
440 |
dl |
1.5 |
shouldThrow(); |
441 |
dl |
1.1 |
} |
442 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
443 |
dl |
1.1 |
} |
444 |
|
|
|
445 |
dl |
1.6 |
/** |
446 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
447 |
|
|
*/ |
448 |
dl |
1.1 |
public void testConstructor3() { |
449 |
dl |
1.5 |
try { |
450 |
dl |
1.3 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
451 |
dl |
1.5 |
shouldThrow(); |
452 |
dl |
1.1 |
} |
453 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
454 |
dl |
1.1 |
} |
455 |
|
|
|
456 |
dl |
1.6 |
/** |
457 |
|
|
* Constructor throws if keepAliveTime is less than zero |
458 |
|
|
*/ |
459 |
dl |
1.1 |
public void testConstructor4() { |
460 |
dl |
1.5 |
try { |
461 |
dl |
1.1 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
462 |
dl |
1.5 |
shouldThrow(); |
463 |
dl |
1.1 |
} |
464 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
465 |
dl |
1.1 |
} |
466 |
|
|
|
467 |
dl |
1.6 |
/** |
468 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
469 |
|
|
*/ |
470 |
dl |
1.1 |
public void testConstructor5() { |
471 |
dl |
1.5 |
try { |
472 |
dl |
1.3 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
473 |
dl |
1.5 |
shouldThrow(); |
474 |
dl |
1.1 |
} |
475 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
476 |
dl |
1.1 |
} |
477 |
|
|
|
478 |
dl |
1.6 |
/** |
479 |
|
|
* Constructor throws if workQueue is set to null |
480 |
|
|
*/ |
481 |
dl |
1.8 |
public void testConstructorNullPointerException() { |
482 |
dl |
1.5 |
try { |
483 |
dl |
1.8 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null); |
484 |
dl |
1.5 |
shouldThrow(); |
485 |
dl |
1.1 |
} |
486 |
dl |
1.3 |
catch (NullPointerException success){} |
487 |
dl |
1.1 |
} |
488 |
|
|
|
489 |
|
|
|
490 |
|
|
|
491 |
dl |
1.6 |
/** |
492 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
493 |
|
|
*/ |
494 |
dl |
1.1 |
public void testConstructor6() { |
495 |
dl |
1.5 |
try { |
496 |
dl |
1.7 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
497 |
dl |
1.5 |
shouldThrow(); |
498 |
dl |
1.3 |
} catch (IllegalArgumentException success){} |
499 |
dl |
1.1 |
} |
500 |
|
|
|
501 |
dl |
1.6 |
/** |
502 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
503 |
|
|
*/ |
504 |
dl |
1.1 |
public void testConstructor7() { |
505 |
dl |
1.5 |
try { |
506 |
dl |
1.8 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
507 |
dl |
1.5 |
shouldThrow(); |
508 |
dl |
1.1 |
} |
509 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
510 |
dl |
1.1 |
} |
511 |
|
|
|
512 |
dl |
1.6 |
/** |
513 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
514 |
|
|
*/ |
515 |
dl |
1.1 |
public void testConstructor8() { |
516 |
dl |
1.5 |
try { |
517 |
dl |
1.8 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
518 |
dl |
1.5 |
shouldThrow(); |
519 |
dl |
1.1 |
} |
520 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
521 |
dl |
1.1 |
} |
522 |
|
|
|
523 |
dl |
1.6 |
/** |
524 |
|
|
* Constructor throws if keepAliveTime is less than zero |
525 |
|
|
*/ |
526 |
dl |
1.1 |
public void testConstructor9() { |
527 |
dl |
1.5 |
try { |
528 |
dl |
1.7 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
529 |
dl |
1.5 |
shouldThrow(); |
530 |
dl |
1.1 |
} |
531 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
532 |
dl |
1.1 |
} |
533 |
|
|
|
534 |
dl |
1.6 |
/** |
535 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
536 |
|
|
*/ |
537 |
dl |
1.1 |
public void testConstructor10() { |
538 |
dl |
1.5 |
try { |
539 |
dl |
1.8 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
540 |
dl |
1.5 |
shouldThrow(); |
541 |
dl |
1.1 |
} |
542 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
543 |
dl |
1.1 |
} |
544 |
|
|
|
545 |
dl |
1.6 |
/** |
546 |
|
|
* Constructor throws if workQueue is set to null |
547 |
|
|
*/ |
548 |
dl |
1.8 |
public void testConstructorNullPointerException2() { |
549 |
dl |
1.5 |
try { |
550 |
dl |
1.8 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory()); |
551 |
dl |
1.5 |
shouldThrow(); |
552 |
dl |
1.1 |
} |
553 |
dl |
1.3 |
catch (NullPointerException success){} |
554 |
dl |
1.1 |
} |
555 |
|
|
|
556 |
dl |
1.6 |
/** |
557 |
|
|
* Constructor throws if threadFactory is set to null |
558 |
|
|
*/ |
559 |
dl |
1.8 |
public void testConstructorNullPointerException3() { |
560 |
dl |
1.5 |
try { |
561 |
dl |
1.1 |
ThreadFactory f = null; |
562 |
dl |
1.8 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
563 |
dl |
1.5 |
shouldThrow(); |
564 |
dl |
1.1 |
} |
565 |
dl |
1.3 |
catch (NullPointerException success){} |
566 |
dl |
1.1 |
} |
567 |
|
|
|
568 |
|
|
|
569 |
dl |
1.6 |
/** |
570 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
571 |
|
|
*/ |
572 |
dl |
1.1 |
public void testConstructor11() { |
573 |
dl |
1.5 |
try { |
574 |
dl |
1.8 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
575 |
dl |
1.5 |
shouldThrow(); |
576 |
dl |
1.1 |
} |
577 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
578 |
dl |
1.1 |
} |
579 |
|
|
|
580 |
dl |
1.6 |
/** |
581 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
582 |
|
|
*/ |
583 |
dl |
1.1 |
public void testConstructor12() { |
584 |
dl |
1.5 |
try { |
585 |
dl |
1.8 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
586 |
dl |
1.5 |
shouldThrow(); |
587 |
dl |
1.1 |
} |
588 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
589 |
dl |
1.1 |
} |
590 |
|
|
|
591 |
dl |
1.6 |
/** |
592 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
593 |
|
|
*/ |
594 |
dl |
1.1 |
public void testConstructor13() { |
595 |
dl |
1.5 |
try { |
596 |
dl |
1.8 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
597 |
dl |
1.5 |
shouldThrow(); |
598 |
dl |
1.1 |
} |
599 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
600 |
dl |
1.1 |
} |
601 |
|
|
|
602 |
dl |
1.6 |
/** |
603 |
|
|
* Constructor throws if keepAliveTime is less than zero |
604 |
|
|
*/ |
605 |
dl |
1.1 |
public void testConstructor14() { |
606 |
dl |
1.5 |
try { |
607 |
dl |
1.7 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
608 |
dl |
1.5 |
shouldThrow(); |
609 |
dl |
1.1 |
} |
610 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
611 |
dl |
1.1 |
} |
612 |
|
|
|
613 |
dl |
1.6 |
/** |
614 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
615 |
|
|
*/ |
616 |
dl |
1.1 |
public void testConstructor15() { |
617 |
dl |
1.5 |
try { |
618 |
dl |
1.8 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
619 |
dl |
1.5 |
shouldThrow(); |
620 |
dl |
1.1 |
} |
621 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
622 |
dl |
1.1 |
} |
623 |
|
|
|
624 |
dl |
1.6 |
/** |
625 |
|
|
* Constructor throws if workQueue is set to null |
626 |
|
|
*/ |
627 |
dl |
1.8 |
public void testConstructorNullPointerException4() { |
628 |
dl |
1.5 |
try { |
629 |
dl |
1.8 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new NoOpREHandler()); |
630 |
dl |
1.5 |
shouldThrow(); |
631 |
dl |
1.1 |
} |
632 |
dl |
1.3 |
catch (NullPointerException success){} |
633 |
dl |
1.1 |
} |
634 |
|
|
|
635 |
dl |
1.6 |
/** |
636 |
|
|
* Constructor throws if handler is set to null |
637 |
|
|
*/ |
638 |
dl |
1.8 |
public void testConstructorNullPointerException5() { |
639 |
dl |
1.5 |
try { |
640 |
dl |
1.1 |
RejectedExecutionHandler r = null; |
641 |
dl |
1.8 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
642 |
dl |
1.5 |
shouldThrow(); |
643 |
dl |
1.1 |
} |
644 |
dl |
1.3 |
catch (NullPointerException success){} |
645 |
dl |
1.1 |
} |
646 |
|
|
|
647 |
|
|
|
648 |
dl |
1.6 |
/** |
649 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
650 |
|
|
*/ |
651 |
dl |
1.1 |
public void testConstructor16() { |
652 |
dl |
1.5 |
try { |
653 |
dl |
1.8 |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
654 |
dl |
1.5 |
shouldThrow(); |
655 |
dl |
1.1 |
} |
656 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
657 |
dl |
1.1 |
} |
658 |
|
|
|
659 |
dl |
1.6 |
/** |
660 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
661 |
|
|
*/ |
662 |
dl |
1.1 |
public void testConstructor17() { |
663 |
dl |
1.5 |
try { |
664 |
dl |
1.8 |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
665 |
dl |
1.5 |
shouldThrow(); |
666 |
dl |
1.1 |
} |
667 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
668 |
dl |
1.1 |
} |
669 |
|
|
|
670 |
dl |
1.6 |
/** |
671 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
672 |
|
|
*/ |
673 |
dl |
1.1 |
public void testConstructor18() { |
674 |
dl |
1.5 |
try { |
675 |
dl |
1.8 |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
676 |
dl |
1.5 |
shouldThrow(); |
677 |
dl |
1.1 |
} |
678 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
679 |
dl |
1.1 |
} |
680 |
|
|
|
681 |
dl |
1.6 |
/** |
682 |
|
|
* Constructor throws if keepAliveTime is less than zero |
683 |
|
|
*/ |
684 |
dl |
1.1 |
public void testConstructor19() { |
685 |
dl |
1.5 |
try { |
686 |
dl |
1.7 |
new ThreadPoolExecutor(1,2,-1L,TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
687 |
dl |
1.5 |
shouldThrow(); |
688 |
dl |
1.1 |
} |
689 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
690 |
dl |
1.1 |
} |
691 |
|
|
|
692 |
dl |
1.6 |
/** |
693 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
694 |
|
|
*/ |
695 |
dl |
1.1 |
public void testConstructor20() { |
696 |
dl |
1.5 |
try { |
697 |
dl |
1.8 |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
698 |
dl |
1.5 |
shouldThrow(); |
699 |
dl |
1.1 |
} |
700 |
dl |
1.3 |
catch (IllegalArgumentException success){} |
701 |
dl |
1.1 |
} |
702 |
|
|
|
703 |
dl |
1.6 |
/** |
704 |
|
|
* Constructor throws if workQueue is set to null |
705 |
|
|
*/ |
706 |
dl |
1.8 |
public void testConstructorNullPointerException6() { |
707 |
dl |
1.5 |
try { |
708 |
dl |
1.8 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
709 |
dl |
1.5 |
shouldThrow(); |
710 |
dl |
1.1 |
} |
711 |
dl |
1.3 |
catch (NullPointerException success){} |
712 |
dl |
1.1 |
} |
713 |
|
|
|
714 |
dl |
1.6 |
/** |
715 |
|
|
* Constructor throws if handler is set to null |
716 |
|
|
*/ |
717 |
dl |
1.8 |
public void testConstructorNullPointerException7() { |
718 |
dl |
1.5 |
try { |
719 |
dl |
1.1 |
RejectedExecutionHandler r = null; |
720 |
dl |
1.8 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
721 |
dl |
1.5 |
shouldThrow(); |
722 |
dl |
1.1 |
} |
723 |
dl |
1.3 |
catch (NullPointerException success){} |
724 |
dl |
1.1 |
} |
725 |
|
|
|
726 |
dl |
1.6 |
/** |
727 |
|
|
* Constructor throws if ThreadFactory is set top null |
728 |
|
|
*/ |
729 |
dl |
1.8 |
public void testConstructorNullPointerException8() { |
730 |
dl |
1.5 |
try { |
731 |
dl |
1.1 |
ThreadFactory f = null; |
732 |
dl |
1.8 |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
733 |
dl |
1.5 |
shouldThrow(); |
734 |
dl |
1.1 |
} |
735 |
dl |
1.3 |
catch (NullPointerException successdn8){} |
736 |
dl |
1.1 |
} |
737 |
|
|
|
738 |
|
|
|
739 |
|
|
/** |
740 |
dl |
1.8 |
* execute throws RejectedExecutionException |
741 |
|
|
* if saturated. |
742 |
|
|
*/ |
743 |
|
|
public void testSaturatedExecute() { |
744 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1)); |
745 |
|
|
try { |
746 |
|
|
|
747 |
|
|
for(int i = 0; i < 5; ++i){ |
748 |
|
|
p.execute(new MediumRunnable()); |
749 |
|
|
} |
750 |
|
|
shouldThrow(); |
751 |
|
|
} catch(RejectedExecutionException success){} |
752 |
|
|
joinPool(p); |
753 |
|
|
} |
754 |
|
|
|
755 |
|
|
/** |
756 |
|
|
* executor using CallerRunsPolicy runs task if saturated. |
757 |
|
|
*/ |
758 |
|
|
public void testSaturatedExecute2() { |
759 |
|
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
760 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
761 |
|
|
try { |
762 |
|
|
|
763 |
|
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
764 |
|
|
for(int i = 0; i < 5; ++i){ |
765 |
|
|
tasks[i] = new TrackedNoOpRunnable(); |
766 |
|
|
} |
767 |
|
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
768 |
|
|
p.execute(mr); |
769 |
|
|
for(int i = 0; i < 5; ++i){ |
770 |
|
|
p.execute(tasks[i]); |
771 |
|
|
} |
772 |
|
|
for(int i = 1; i < 5; ++i) { |
773 |
|
|
assertTrue(tasks[i].done); |
774 |
|
|
} |
775 |
|
|
p.shutdownNow(); |
776 |
|
|
} catch(RejectedExecutionException ex){ |
777 |
|
|
unexpectedException(); |
778 |
|
|
} finally { |
779 |
|
|
joinPool(p); |
780 |
|
|
} |
781 |
|
|
} |
782 |
|
|
|
783 |
|
|
/** |
784 |
|
|
* executor using DiscardPolicy drops task if saturated. |
785 |
|
|
*/ |
786 |
|
|
public void testSaturatedExecute3() { |
787 |
|
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
788 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
789 |
|
|
try { |
790 |
|
|
|
791 |
|
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
792 |
|
|
for(int i = 0; i < 5; ++i){ |
793 |
|
|
tasks[i] = new TrackedNoOpRunnable(); |
794 |
|
|
} |
795 |
|
|
p.execute(new TrackedLongRunnable()); |
796 |
|
|
for(int i = 0; i < 5; ++i){ |
797 |
|
|
p.execute(tasks[i]); |
798 |
|
|
} |
799 |
|
|
for(int i = 0; i < 5; ++i){ |
800 |
|
|
assertFalse(tasks[i].done); |
801 |
|
|
} |
802 |
|
|
p.shutdownNow(); |
803 |
|
|
} catch(RejectedExecutionException ex){ |
804 |
|
|
unexpectedException(); |
805 |
|
|
} finally { |
806 |
|
|
joinPool(p); |
807 |
|
|
} |
808 |
|
|
} |
809 |
|
|
|
810 |
|
|
/** |
811 |
|
|
* executor using DiscardOldestPolicy drops oldest task if saturated. |
812 |
|
|
*/ |
813 |
|
|
public void testSaturatedExecute4() { |
814 |
|
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
815 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
816 |
|
|
try { |
817 |
|
|
p.execute(new TrackedLongRunnable()); |
818 |
|
|
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
819 |
|
|
p.execute(r2); |
820 |
|
|
assertTrue(p.getQueue().contains(r2)); |
821 |
|
|
TrackedNoOpRunnable r3 = new TrackedNoOpRunnable(); |
822 |
|
|
p.execute(r3); |
823 |
|
|
assertFalse(p.getQueue().contains(r2)); |
824 |
|
|
assertTrue(p.getQueue().contains(r3)); |
825 |
|
|
p.shutdownNow(); |
826 |
|
|
} catch(RejectedExecutionException ex){ |
827 |
|
|
unexpectedException(); |
828 |
|
|
} finally { |
829 |
|
|
joinPool(p); |
830 |
|
|
} |
831 |
|
|
} |
832 |
|
|
|
833 |
|
|
/** |
834 |
dl |
1.6 |
* execute throws RejectedExecutionException if shutdown |
835 |
dl |
1.1 |
*/ |
836 |
dl |
1.8 |
public void testRejectedExecutionExceptionOnShutdown() { |
837 |
|
|
ThreadPoolExecutor tpe = |
838 |
|
|
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
839 |
dl |
1.1 |
tpe.shutdown(); |
840 |
dl |
1.5 |
try { |
841 |
dl |
1.3 |
tpe.execute(new NoOpRunnable()); |
842 |
dl |
1.5 |
shouldThrow(); |
843 |
dl |
1.2 |
} catch(RejectedExecutionException success){} |
844 |
dl |
1.1 |
|
845 |
dl |
1.3 |
joinPool(tpe); |
846 |
dl |
1.1 |
} |
847 |
dl |
1.6 |
|
848 |
|
|
/** |
849 |
dl |
1.8 |
* execute using CallerRunsPolicy drops task on shutdown |
850 |
|
|
*/ |
851 |
|
|
public void testCallerRunsOnShutdown() { |
852 |
|
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
853 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
854 |
|
|
|
855 |
|
|
p.shutdown(); |
856 |
|
|
try { |
857 |
|
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
858 |
|
|
p.execute(r); |
859 |
|
|
assertFalse(r.done); |
860 |
|
|
} catch(RejectedExecutionException success){ |
861 |
|
|
unexpectedException(); |
862 |
|
|
} finally { |
863 |
|
|
joinPool(p); |
864 |
|
|
} |
865 |
|
|
} |
866 |
|
|
|
867 |
|
|
/** |
868 |
|
|
* execute using DiscardPolicy drops task on shutdown |
869 |
|
|
*/ |
870 |
|
|
public void testDiscardOnShutdown() { |
871 |
|
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
872 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
873 |
|
|
|
874 |
|
|
p.shutdown(); |
875 |
|
|
try { |
876 |
|
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
877 |
|
|
p.execute(r); |
878 |
|
|
assertFalse(r.done); |
879 |
|
|
} catch(RejectedExecutionException success){ |
880 |
|
|
unexpectedException(); |
881 |
|
|
} finally { |
882 |
|
|
joinPool(p); |
883 |
|
|
} |
884 |
|
|
} |
885 |
|
|
|
886 |
|
|
|
887 |
|
|
/** |
888 |
|
|
* execute using DiscardOldestPolicy drops task on shutdown |
889 |
dl |
1.6 |
*/ |
890 |
dl |
1.8 |
public void testDiscardOldestOnShutdown() { |
891 |
|
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
892 |
|
|
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
893 |
|
|
|
894 |
|
|
p.shutdown(); |
895 |
|
|
try { |
896 |
|
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
897 |
|
|
p.execute(r); |
898 |
|
|
assertFalse(r.done); |
899 |
|
|
} catch(RejectedExecutionException success){ |
900 |
|
|
unexpectedException(); |
901 |
|
|
} finally { |
902 |
|
|
joinPool(p); |
903 |
|
|
} |
904 |
dl |
1.6 |
} |
905 |
|
|
|
906 |
dl |
1.8 |
|
907 |
dl |
1.6 |
/** |
908 |
|
|
* execute (null) throws NPE |
909 |
|
|
*/ |
910 |
|
|
public void testExecuteNull() { |
911 |
|
|
ThreadPoolExecutor tpe = null; |
912 |
|
|
try { |
913 |
dl |
1.8 |
tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
914 |
dl |
1.6 |
tpe.execute(null); |
915 |
|
|
shouldThrow(); |
916 |
|
|
} catch(NullPointerException success){} |
917 |
|
|
|
918 |
|
|
joinPool(tpe); |
919 |
|
|
} |
920 |
dl |
1.1 |
|
921 |
|
|
/** |
922 |
dl |
1.8 |
* setCorePoolSize of negative value throws IllegalArgumentException |
923 |
dl |
1.1 |
*/ |
924 |
dl |
1.5 |
public void testCorePoolSizeIllegalArgumentException() { |
925 |
dl |
1.1 |
ThreadPoolExecutor tpe = null; |
926 |
dl |
1.5 |
try { |
927 |
dl |
1.8 |
tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
928 |
dl |
1.2 |
} catch(Exception e){} |
929 |
dl |
1.5 |
try { |
930 |
dl |
1.1 |
tpe.setCorePoolSize(-1); |
931 |
dl |
1.5 |
shouldThrow(); |
932 |
dl |
1.2 |
} catch(IllegalArgumentException success){ |
933 |
dl |
1.1 |
} finally { |
934 |
|
|
tpe.shutdown(); |
935 |
|
|
} |
936 |
dl |
1.3 |
joinPool(tpe); |
937 |
dl |
1.1 |
} |
938 |
|
|
|
939 |
|
|
/** |
940 |
dl |
1.6 |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
941 |
|
|
* given a value less the core pool size |
942 |
dl |
1.1 |
*/ |
943 |
dl |
1.5 |
public void testMaximumPoolSizeIllegalArgumentException() { |
944 |
dl |
1.1 |
ThreadPoolExecutor tpe = null; |
945 |
dl |
1.5 |
try { |
946 |
dl |
1.8 |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
947 |
dl |
1.2 |
} catch(Exception e){} |
948 |
dl |
1.5 |
try { |
949 |
dl |
1.1 |
tpe.setMaximumPoolSize(1); |
950 |
dl |
1.5 |
shouldThrow(); |
951 |
dl |
1.2 |
} catch(IllegalArgumentException success){ |
952 |
dl |
1.1 |
} finally { |
953 |
|
|
tpe.shutdown(); |
954 |
|
|
} |
955 |
dl |
1.3 |
joinPool(tpe); |
956 |
dl |
1.1 |
} |
957 |
|
|
|
958 |
|
|
/** |
959 |
dl |
1.6 |
* setMaximumPoolSize throws IllegalArgumentException |
960 |
|
|
* if given a negative value |
961 |
dl |
1.1 |
*/ |
962 |
dl |
1.5 |
public void testMaximumPoolSizeIllegalArgumentException2() { |
963 |
dl |
1.1 |
ThreadPoolExecutor tpe = null; |
964 |
dl |
1.5 |
try { |
965 |
dl |
1.8 |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
966 |
dl |
1.2 |
} catch(Exception e){} |
967 |
dl |
1.5 |
try { |
968 |
dl |
1.1 |
tpe.setMaximumPoolSize(-1); |
969 |
dl |
1.5 |
shouldThrow(); |
970 |
dl |
1.2 |
} catch(IllegalArgumentException success){ |
971 |
dl |
1.1 |
} finally { |
972 |
|
|
tpe.shutdown(); |
973 |
|
|
} |
974 |
dl |
1.3 |
joinPool(tpe); |
975 |
dl |
1.1 |
} |
976 |
|
|
|
977 |
|
|
|
978 |
|
|
/** |
979 |
dl |
1.6 |
* setKeepAliveTime throws IllegalArgumentException |
980 |
dl |
1.1 |
* when given a negative value |
981 |
|
|
*/ |
982 |
dl |
1.5 |
public void testKeepAliveTimeIllegalArgumentException() { |
983 |
dl |
1.1 |
ThreadPoolExecutor tpe = null; |
984 |
dl |
1.5 |
try { |
985 |
dl |
1.8 |
tpe = new ThreadPoolExecutor(2,3,LONG_DELAY_MS, TimeUnit.MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
986 |
dl |
1.2 |
} catch(Exception e){} |
987 |
dl |
1.1 |
|
988 |
dl |
1.5 |
try { |
989 |
dl |
1.1 |
tpe.setKeepAliveTime(-1,TimeUnit.MILLISECONDS); |
990 |
dl |
1.5 |
shouldThrow(); |
991 |
dl |
1.2 |
} catch(IllegalArgumentException success){ |
992 |
dl |
1.1 |
} finally { |
993 |
|
|
tpe.shutdown(); |
994 |
|
|
} |
995 |
dl |
1.3 |
joinPool(tpe); |
996 |
dl |
1.1 |
} |
997 |
dl |
1.8 |
|
998 |
|
|
/** |
999 |
|
|
* terminated() is called on termination |
1000 |
|
|
*/ |
1001 |
|
|
public void testTerminated() { |
1002 |
|
|
ExtendedTPE tpe = new ExtendedTPE(); |
1003 |
|
|
tpe.shutdown(); |
1004 |
|
|
assertTrue(tpe.terminatedCalled); |
1005 |
|
|
joinPool(tpe); |
1006 |
|
|
} |
1007 |
|
|
|
1008 |
|
|
/** |
1009 |
|
|
* beforeExecute and afterExecute are called when executing task |
1010 |
|
|
*/ |
1011 |
|
|
public void testBeforeAfter() { |
1012 |
|
|
ExtendedTPE tpe = new ExtendedTPE(); |
1013 |
|
|
try { |
1014 |
|
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1015 |
|
|
tpe.execute(r); |
1016 |
|
|
Thread.sleep(SHORT_DELAY_MS); |
1017 |
|
|
assertTrue(r.done); |
1018 |
|
|
assertTrue(tpe.beforeCalled); |
1019 |
|
|
assertTrue(tpe.afterCalled); |
1020 |
|
|
tpe.shutdown(); |
1021 |
|
|
} |
1022 |
|
|
catch(Exception ex) { |
1023 |
|
|
unexpectedException(); |
1024 |
|
|
} finally { |
1025 |
|
|
joinPool(tpe); |
1026 |
|
|
} |
1027 |
|
|
} |
1028 |
dl |
1.1 |
} |