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 |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
14 |
|
|
15 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
17 |
< |
junit.textui.TestRunner.run (suite()); |
17 |
> |
junit.textui.TestRunner.run(suite()); |
18 |
|
} |
19 |
|
public static Test suite() { |
20 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
48 |
|
|
49 |
|
|
50 |
|
/** |
51 |
< |
* execute successfully executes a runnable |
51 |
> |
* execute successfully executes a runnable |
52 |
|
*/ |
53 |
|
public void testExecute() throws InterruptedException { |
54 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
54 |
> |
final ThreadPoolExecutor p = |
55 |
> |
new ThreadPoolExecutor(1, 1, |
56 |
> |
LONG_DELAY_MS, MILLISECONDS, |
57 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
58 |
> |
final CountDownLatch done = new CountDownLatch(1); |
59 |
> |
final Runnable task = new CheckedRunnable() { |
60 |
> |
public void realRun() { |
61 |
> |
done.countDown(); |
62 |
> |
}}; |
63 |
|
try { |
64 |
< |
p1.execute(new ShortRunnable()); |
65 |
< |
Thread.sleep(SMALL_DELAY_MS); |
64 |
> |
p.execute(task); |
65 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
66 |
|
} finally { |
67 |
< |
joinPool(p1); |
67 |
> |
joinPool(p); |
68 |
|
} |
69 |
|
} |
70 |
|
|
71 |
|
/** |
72 |
< |
* getActiveCount increases but doesn't overestimate, when a |
73 |
< |
* thread becomes active |
72 |
> |
* getActiveCount increases but doesn't overestimate, when a |
73 |
> |
* thread becomes active |
74 |
|
*/ |
75 |
|
public void testGetActiveCount() throws InterruptedException { |
76 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
77 |
< |
assertEquals(0, p2.getActiveCount()); |
78 |
< |
p2.execute(new MediumRunnable()); |
79 |
< |
Thread.sleep(SHORT_DELAY_MS); |
80 |
< |
assertEquals(1, p2.getActiveCount()); |
81 |
< |
joinPool(p2); |
76 |
> |
final ThreadPoolExecutor p = |
77 |
> |
new ThreadPoolExecutor(2, 2, |
78 |
> |
LONG_DELAY_MS, MILLISECONDS, |
79 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
80 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
81 |
> |
final CountDownLatch done = new CountDownLatch(1); |
82 |
> |
try { |
83 |
> |
assertEquals(0, p.getActiveCount()); |
84 |
> |
p.execute(new CheckedRunnable() { |
85 |
> |
public void realRun() throws InterruptedException { |
86 |
> |
threadStarted.countDown(); |
87 |
> |
assertEquals(1, p.getActiveCount()); |
88 |
> |
done.await(); |
89 |
> |
}}); |
90 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
91 |
> |
assertEquals(1, p.getActiveCount()); |
92 |
> |
} finally { |
93 |
> |
done.countDown(); |
94 |
> |
joinPool(p); |
95 |
> |
} |
96 |
|
} |
97 |
|
|
98 |
|
/** |
99 |
< |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
99 |
> |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
100 |
|
*/ |
101 |
|
public void testPrestartCoreThread() { |
102 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
103 |
< |
assertEquals(0, p2.getPoolSize()); |
104 |
< |
assertTrue(p2.prestartCoreThread()); |
105 |
< |
assertEquals(1, p2.getPoolSize()); |
106 |
< |
assertTrue(p2.prestartCoreThread()); |
107 |
< |
assertEquals(2, p2.getPoolSize()); |
108 |
< |
assertFalse(p2.prestartCoreThread()); |
109 |
< |
assertEquals(2, p2.getPoolSize()); |
110 |
< |
joinPool(p2); |
102 |
> |
final ThreadPoolExecutor p = |
103 |
> |
new ThreadPoolExecutor(2, 2, |
104 |
> |
LONG_DELAY_MS, MILLISECONDS, |
105 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
106 |
> |
assertEquals(0, p.getPoolSize()); |
107 |
> |
assertTrue(p.prestartCoreThread()); |
108 |
> |
assertEquals(1, p.getPoolSize()); |
109 |
> |
assertTrue(p.prestartCoreThread()); |
110 |
> |
assertEquals(2, p.getPoolSize()); |
111 |
> |
assertFalse(p.prestartCoreThread()); |
112 |
> |
assertEquals(2, p.getPoolSize()); |
113 |
> |
joinPool(p); |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
< |
* prestartAllCoreThreads starts all corePoolSize threads |
117 |
> |
* prestartAllCoreThreads starts all corePoolSize threads |
118 |
|
*/ |
119 |
|
public void testPrestartAllCoreThreads() { |
120 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
121 |
< |
assertEquals(0, p2.getPoolSize()); |
122 |
< |
p2.prestartAllCoreThreads(); |
123 |
< |
assertEquals(2, p2.getPoolSize()); |
124 |
< |
p2.prestartAllCoreThreads(); |
125 |
< |
assertEquals(2, p2.getPoolSize()); |
126 |
< |
joinPool(p2); |
120 |
> |
final ThreadPoolExecutor p = |
121 |
> |
new ThreadPoolExecutor(2, 2, |
122 |
> |
LONG_DELAY_MS, MILLISECONDS, |
123 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
124 |
> |
assertEquals(0, p.getPoolSize()); |
125 |
> |
p.prestartAllCoreThreads(); |
126 |
> |
assertEquals(2, p.getPoolSize()); |
127 |
> |
p.prestartAllCoreThreads(); |
128 |
> |
assertEquals(2, p.getPoolSize()); |
129 |
> |
joinPool(p); |
130 |
|
} |
131 |
|
|
132 |
|
/** |
133 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
134 |
< |
* when tasks complete |
133 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
134 |
> |
* when tasks complete |
135 |
|
*/ |
136 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
137 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
138 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
139 |
< |
p2.execute(new ShortRunnable()); |
140 |
< |
Thread.sleep(SMALL_DELAY_MS); |
141 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
142 |
< |
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
143 |
< |
joinPool(p2); |
137 |
> |
final ThreadPoolExecutor p = |
138 |
> |
new ThreadPoolExecutor(2, 2, |
139 |
> |
LONG_DELAY_MS, MILLISECONDS, |
140 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
141 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
142 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
143 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
144 |
> |
try { |
145 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
146 |
> |
p.execute(new CheckedRunnable() { |
147 |
> |
public void realRun() throws InterruptedException { |
148 |
> |
threadStarted.countDown(); |
149 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
150 |
> |
threadProceed.await(); |
151 |
> |
threadDone.countDown(); |
152 |
> |
}}); |
153 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
154 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
155 |
> |
threadProceed.countDown(); |
156 |
> |
threadDone.await(); |
157 |
> |
delay(SHORT_DELAY_MS); |
158 |
> |
assertEquals(1, p.getCompletedTaskCount()); |
159 |
> |
} finally { |
160 |
> |
joinPool(p); |
161 |
> |
} |
162 |
|
} |
163 |
|
|
164 |
|
/** |
165 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
165 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
166 |
|
*/ |
167 |
|
public void testGetCorePoolSize() { |
168 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
169 |
< |
assertEquals(1, p1.getCorePoolSize()); |
170 |
< |
joinPool(p1); |
168 |
> |
final ThreadPoolExecutor p = |
169 |
> |
new ThreadPoolExecutor(1, 1, |
170 |
> |
LONG_DELAY_MS, MILLISECONDS, |
171 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
172 |
> |
assertEquals(1, p.getCorePoolSize()); |
173 |
> |
joinPool(p); |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* getKeepAliveTime returns value given in constructor if not otherwise set |
177 |
> |
* getKeepAliveTime returns value given in constructor if not otherwise set |
178 |
|
*/ |
179 |
|
public void testGetKeepAliveTime() { |
180 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
181 |
< |
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
182 |
< |
joinPool(p2); |
180 |
> |
final ThreadPoolExecutor p = |
181 |
> |
new ThreadPoolExecutor(2, 2, |
182 |
> |
1000, MILLISECONDS, |
183 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
184 |
> |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
185 |
> |
joinPool(p); |
186 |
|
} |
187 |
|
|
188 |
|
|
191 |
|
*/ |
192 |
|
public void testGetThreadFactory() { |
193 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
194 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
194 |
> |
final ThreadPoolExecutor p = |
195 |
> |
new ThreadPoolExecutor(1, 2, |
196 |
> |
LONG_DELAY_MS, MILLISECONDS, |
197 |
> |
new ArrayBlockingQueue<Runnable>(10), |
198 |
> |
tf, |
199 |
> |
new NoOpREHandler()); |
200 |
|
assertSame(tf, p.getThreadFactory()); |
201 |
|
joinPool(p); |
202 |
|
} |
205 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
206 |
|
*/ |
207 |
|
public void testSetThreadFactory() { |
208 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
208 |
> |
final ThreadPoolExecutor p = |
209 |
> |
new ThreadPoolExecutor(1, 2, |
210 |
> |
LONG_DELAY_MS, MILLISECONDS, |
211 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
212 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
213 |
|
p.setThreadFactory(tf); |
214 |
|
assertSame(tf, p.getThreadFactory()); |
220 |
|
* setThreadFactory(null) throws NPE |
221 |
|
*/ |
222 |
|
public void testSetThreadFactoryNull() { |
223 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
223 |
> |
final ThreadPoolExecutor p = |
224 |
> |
new ThreadPoolExecutor(1, 2, |
225 |
> |
LONG_DELAY_MS, MILLISECONDS, |
226 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
227 |
|
try { |
228 |
|
p.setThreadFactory(null); |
229 |
|
shouldThrow(); |
237 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
238 |
|
*/ |
239 |
|
public void testGetRejectedExecutionHandler() { |
240 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
241 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
240 |
> |
final RejectedExecutionHandler h = new NoOpREHandler(); |
241 |
> |
final ThreadPoolExecutor p = |
242 |
> |
new ThreadPoolExecutor(1, 2, |
243 |
> |
LONG_DELAY_MS, MILLISECONDS, |
244 |
> |
new ArrayBlockingQueue<Runnable>(10), |
245 |
> |
h); |
246 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
247 |
|
joinPool(p); |
248 |
|
} |
252 |
|
* getRejectedExecutionHandler |
253 |
|
*/ |
254 |
|
public void testSetRejectedExecutionHandler() { |
255 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
255 |
> |
final ThreadPoolExecutor p = |
256 |
> |
new ThreadPoolExecutor(1, 2, |
257 |
> |
LONG_DELAY_MS, MILLISECONDS, |
258 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
259 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
260 |
|
p.setRejectedExecutionHandler(h); |
261 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
267 |
|
* setRejectedExecutionHandler(null) throws NPE |
268 |
|
*/ |
269 |
|
public void testSetRejectedExecutionHandlerNull() { |
270 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
270 |
> |
final ThreadPoolExecutor p = |
271 |
> |
new ThreadPoolExecutor(1, 2, |
272 |
> |
LONG_DELAY_MS, MILLISECONDS, |
273 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
274 |
|
try { |
275 |
|
p.setRejectedExecutionHandler(null); |
276 |
|
shouldThrow(); |
282 |
|
|
283 |
|
|
284 |
|
/** |
285 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
286 |
< |
* multiple threads active |
285 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
286 |
> |
* multiple threads active |
287 |
|
*/ |
288 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
289 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
290 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
291 |
< |
p2.execute(new MediumRunnable()); |
292 |
< |
p2.execute(new MediumRunnable()); |
293 |
< |
Thread.sleep(SHORT_DELAY_MS); |
294 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
295 |
< |
joinPool(p2); |
289 |
> |
final int THREADS = 3; |
290 |
> |
final ThreadPoolExecutor p = |
291 |
> |
new ThreadPoolExecutor(THREADS, THREADS, |
292 |
> |
LONG_DELAY_MS, MILLISECONDS, |
293 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
294 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
295 |
> |
final CountDownLatch done = new CountDownLatch(1); |
296 |
> |
try { |
297 |
> |
assertEquals(0, p.getLargestPoolSize()); |
298 |
> |
for (int i = 0; i < THREADS; i++) |
299 |
> |
p.execute(new CheckedRunnable() { |
300 |
> |
public void realRun() throws InterruptedException { |
301 |
> |
threadsStarted.countDown(); |
302 |
> |
done.await(); |
303 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
304 |
> |
}}); |
305 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
306 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
307 |
> |
} finally { |
308 |
> |
done.countDown(); |
309 |
> |
joinPool(p); |
310 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
311 |
> |
} |
312 |
|
} |
313 |
|
|
314 |
|
/** |
315 |
< |
* getMaximumPoolSize returns value given in constructor if not |
316 |
< |
* otherwise set |
315 |
> |
* getMaximumPoolSize returns value given in constructor if not |
316 |
> |
* otherwise set |
317 |
|
*/ |
318 |
|
public void testGetMaximumPoolSize() { |
319 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
320 |
< |
assertEquals(2, p2.getMaximumPoolSize()); |
321 |
< |
joinPool(p2); |
319 |
> |
final ThreadPoolExecutor p = |
320 |
> |
new ThreadPoolExecutor(2, 3, |
321 |
> |
LONG_DELAY_MS, MILLISECONDS, |
322 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
323 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
324 |
> |
joinPool(p); |
325 |
|
} |
326 |
|
|
327 |
|
/** |
328 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
329 |
< |
* become active |
328 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
329 |
> |
* become active |
330 |
|
*/ |
331 |
< |
public void testGetPoolSize() { |
332 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
333 |
< |
assertEquals(0, p1.getPoolSize()); |
334 |
< |
p1.execute(new MediumRunnable()); |
335 |
< |
assertEquals(1, p1.getPoolSize()); |
336 |
< |
joinPool(p1); |
331 |
> |
public void testGetPoolSize() throws InterruptedException { |
332 |
> |
final ThreadPoolExecutor p = |
333 |
> |
new ThreadPoolExecutor(1, 1, |
334 |
> |
LONG_DELAY_MS, MILLISECONDS, |
335 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
336 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
337 |
> |
final CountDownLatch done = new CountDownLatch(1); |
338 |
> |
try { |
339 |
> |
assertEquals(0, p.getPoolSize()); |
340 |
> |
p.execute(new CheckedRunnable() { |
341 |
> |
public void realRun() throws InterruptedException { |
342 |
> |
threadStarted.countDown(); |
343 |
> |
assertEquals(1, p.getPoolSize()); |
344 |
> |
done.await(); |
345 |
> |
}}); |
346 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
347 |
> |
assertEquals(1, p.getPoolSize()); |
348 |
> |
} finally { |
349 |
> |
done.countDown(); |
350 |
> |
joinPool(p); |
351 |
> |
} |
352 |
|
} |
353 |
|
|
354 |
|
/** |
355 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
355 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
356 |
|
*/ |
357 |
|
public void testGetTaskCount() throws InterruptedException { |
358 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
359 |
< |
assertEquals(0, p1.getTaskCount()); |
360 |
< |
p1.execute(new MediumRunnable()); |
361 |
< |
Thread.sleep(SHORT_DELAY_MS); |
362 |
< |
assertEquals(1, p1.getTaskCount()); |
363 |
< |
joinPool(p1); |
358 |
> |
final ThreadPoolExecutor p = |
359 |
> |
new ThreadPoolExecutor(1, 1, |
360 |
> |
LONG_DELAY_MS, MILLISECONDS, |
361 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
362 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
363 |
> |
final CountDownLatch done = new CountDownLatch(1); |
364 |
> |
try { |
365 |
> |
assertEquals(0, p.getTaskCount()); |
366 |
> |
p.execute(new CheckedRunnable() { |
367 |
> |
public void realRun() throws InterruptedException { |
368 |
> |
threadStarted.countDown(); |
369 |
> |
assertEquals(1, p.getTaskCount()); |
370 |
> |
done.await(); |
371 |
> |
}}); |
372 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
373 |
> |
assertEquals(1, p.getTaskCount()); |
374 |
> |
} finally { |
375 |
> |
done.countDown(); |
376 |
> |
joinPool(p); |
377 |
> |
} |
378 |
|
} |
379 |
|
|
380 |
|
/** |
381 |
< |
* isShutDown is false before shutdown, true after |
381 |
> |
* isShutDown is false before shutdown, true after |
382 |
|
*/ |
383 |
|
public void testIsShutdown() { |
384 |
< |
|
385 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
386 |
< |
assertFalse(p1.isShutdown()); |
387 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
388 |
< |
assertTrue(p1.isShutdown()); |
389 |
< |
joinPool(p1); |
384 |
> |
final ThreadPoolExecutor p = |
385 |
> |
new ThreadPoolExecutor(1, 1, |
386 |
> |
LONG_DELAY_MS, MILLISECONDS, |
387 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
388 |
> |
assertFalse(p.isShutdown()); |
389 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
390 |
> |
assertTrue(p.isShutdown()); |
391 |
> |
joinPool(p); |
392 |
|
} |
393 |
|
|
394 |
|
|
395 |
|
/** |
396 |
< |
* isTerminated is false before termination, true after |
396 |
> |
* isTerminated is false before termination, true after |
397 |
|
*/ |
398 |
|
public void testIsTerminated() throws InterruptedException { |
399 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
400 |
< |
assertFalse(p1.isTerminated()); |
399 |
> |
final ThreadPoolExecutor p = |
400 |
> |
new ThreadPoolExecutor(1, 1, |
401 |
> |
LONG_DELAY_MS, MILLISECONDS, |
402 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
403 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
404 |
> |
final CountDownLatch done = new CountDownLatch(1); |
405 |
> |
assertFalse(p.isTerminated()); |
406 |
|
try { |
407 |
< |
p1.execute(new MediumRunnable()); |
407 |
> |
p.execute(new CheckedRunnable() { |
408 |
> |
public void realRun() throws InterruptedException { |
409 |
> |
assertFalse(p.isTerminated()); |
410 |
> |
threadStarted.countDown(); |
411 |
> |
done.await(); |
412 |
> |
}}); |
413 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
414 |
> |
assertFalse(p.isTerminating()); |
415 |
> |
done.countDown(); |
416 |
|
} finally { |
417 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
417 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
418 |
|
} |
419 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
420 |
< |
assertTrue(p1.isTerminated()); |
419 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
420 |
> |
assertTrue(p.isTerminated()); |
421 |
|
} |
422 |
|
|
423 |
|
/** |
424 |
< |
* isTerminating is not true when running or when terminated |
424 |
> |
* isTerminating is not true when running or when terminated |
425 |
|
*/ |
426 |
|
public void testIsTerminating() throws InterruptedException { |
427 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
428 |
< |
assertFalse(p1.isTerminating()); |
427 |
> |
final ThreadPoolExecutor p = |
428 |
> |
new ThreadPoolExecutor(1, 1, |
429 |
> |
LONG_DELAY_MS, MILLISECONDS, |
430 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
431 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
432 |
> |
final CountDownLatch done = new CountDownLatch(1); |
433 |
|
try { |
434 |
< |
p1.execute(new SmallRunnable()); |
435 |
< |
assertFalse(p1.isTerminating()); |
436 |
< |
} finally { |
437 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
438 |
< |
} |
439 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
440 |
< |
assertTrue(p1.isTerminated()); |
441 |
< |
assertFalse(p1.isTerminating()); |
434 |
> |
assertFalse(p.isTerminating()); |
435 |
> |
p.execute(new CheckedRunnable() { |
436 |
> |
public void realRun() throws InterruptedException { |
437 |
> |
assertFalse(p.isTerminating()); |
438 |
> |
threadStarted.countDown(); |
439 |
> |
done.await(); |
440 |
> |
}}); |
441 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
442 |
> |
assertFalse(p.isTerminating()); |
443 |
> |
done.countDown(); |
444 |
> |
} finally { |
445 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
446 |
> |
} |
447 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
448 |
> |
assertTrue(p.isTerminated()); |
449 |
> |
assertFalse(p.isTerminating()); |
450 |
|
} |
451 |
|
|
452 |
|
/** |
453 |
|
* getQueue returns the work queue, which contains queued tasks |
454 |
|
*/ |
455 |
|
public void testGetQueue() throws InterruptedException { |
456 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
457 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
458 |
< |
FutureTask[] tasks = new FutureTask[5]; |
459 |
< |
for (int i = 0; i < 5; i++) { |
460 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
461 |
< |
p1.execute(tasks[i]); |
462 |
< |
} |
463 |
< |
try { |
464 |
< |
Thread.sleep(SHORT_DELAY_MS); |
465 |
< |
BlockingQueue<Runnable> wq = p1.getQueue(); |
466 |
< |
assertSame(q, wq); |
467 |
< |
assertFalse(wq.contains(tasks[0])); |
468 |
< |
assertTrue(wq.contains(tasks[4])); |
469 |
< |
for (int i = 1; i < 5; ++i) |
470 |
< |
tasks[i].cancel(true); |
471 |
< |
p1.shutdownNow(); |
456 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
457 |
> |
final ThreadPoolExecutor p = |
458 |
> |
new ThreadPoolExecutor(1, 1, |
459 |
> |
LONG_DELAY_MS, MILLISECONDS, |
460 |
> |
q); |
461 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
462 |
> |
final CountDownLatch done = new CountDownLatch(1); |
463 |
> |
try { |
464 |
> |
FutureTask[] tasks = new FutureTask[5]; |
465 |
> |
for (int i = 0; i < tasks.length; i++) { |
466 |
> |
Callable task = new CheckedCallable<Boolean>() { |
467 |
> |
public Boolean realCall() throws InterruptedException { |
468 |
> |
threadStarted.countDown(); |
469 |
> |
assertSame(q, p.getQueue()); |
470 |
> |
done.await(); |
471 |
> |
return Boolean.TRUE; |
472 |
> |
}}; |
473 |
> |
tasks[i] = new FutureTask(task); |
474 |
> |
p.execute(tasks[i]); |
475 |
> |
} |
476 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
477 |
> |
assertSame(q, p.getQueue()); |
478 |
> |
assertFalse(q.contains(tasks[0])); |
479 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
480 |
> |
assertEquals(tasks.length - 1, q.size()); |
481 |
|
} finally { |
482 |
< |
joinPool(p1); |
482 |
> |
done.countDown(); |
483 |
> |
joinPool(p); |
484 |
|
} |
485 |
|
} |
486 |
|
|
489 |
|
*/ |
490 |
|
public void testRemove() throws InterruptedException { |
491 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
492 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
493 |
< |
FutureTask[] tasks = new FutureTask[5]; |
494 |
< |
for (int i = 0; i < 5; i++) { |
495 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
496 |
< |
p1.execute(tasks[i]); |
497 |
< |
} |
498 |
< |
try { |
499 |
< |
Thread.sleep(SHORT_DELAY_MS); |
500 |
< |
assertFalse(p1.remove(tasks[0])); |
492 |
> |
final ThreadPoolExecutor p = |
493 |
> |
new ThreadPoolExecutor(1, 1, |
494 |
> |
LONG_DELAY_MS, MILLISECONDS, |
495 |
> |
q); |
496 |
> |
Runnable[] tasks = new Runnable[5]; |
497 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
498 |
> |
final CountDownLatch done = new CountDownLatch(1); |
499 |
> |
try { |
500 |
> |
for (int i = 0; i < tasks.length; i++) { |
501 |
> |
tasks[i] = new CheckedRunnable() { |
502 |
> |
public void realRun() throws InterruptedException { |
503 |
> |
threadStarted.countDown(); |
504 |
> |
done.await(); |
505 |
> |
}}; |
506 |
> |
p.execute(tasks[i]); |
507 |
> |
} |
508 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
509 |
> |
assertFalse(p.remove(tasks[0])); |
510 |
|
assertTrue(q.contains(tasks[4])); |
511 |
|
assertTrue(q.contains(tasks[3])); |
512 |
< |
assertTrue(p1.remove(tasks[4])); |
513 |
< |
assertFalse(p1.remove(tasks[4])); |
512 |
> |
assertTrue(p.remove(tasks[4])); |
513 |
> |
assertFalse(p.remove(tasks[4])); |
514 |
|
assertFalse(q.contains(tasks[4])); |
515 |
|
assertTrue(q.contains(tasks[3])); |
516 |
< |
assertTrue(p1.remove(tasks[3])); |
516 |
> |
assertTrue(p.remove(tasks[3])); |
517 |
|
assertFalse(q.contains(tasks[3])); |
518 |
|
} finally { |
519 |
< |
joinPool(p1); |
519 |
> |
done.countDown(); |
520 |
> |
joinPool(p); |
521 |
|
} |
522 |
|
} |
523 |
|
|
524 |
|
/** |
525 |
< |
* purge removes cancelled tasks from the queue |
525 |
> |
* purge removes cancelled tasks from the queue |
526 |
|
*/ |
527 |
< |
public void testPurge() { |
528 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
527 |
> |
public void testPurge() throws InterruptedException { |
528 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
529 |
> |
final CountDownLatch done = new CountDownLatch(1); |
530 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
531 |
> |
final ThreadPoolExecutor p = |
532 |
> |
new ThreadPoolExecutor(1, 1, |
533 |
> |
LONG_DELAY_MS, MILLISECONDS, |
534 |
> |
q); |
535 |
|
FutureTask[] tasks = new FutureTask[5]; |
536 |
< |
for (int i = 0; i < 5; i++) { |
537 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
538 |
< |
p1.execute(tasks[i]); |
536 |
> |
try { |
537 |
> |
for (int i = 0; i < tasks.length; i++) { |
538 |
> |
Callable task = new CheckedCallable<Boolean>() { |
539 |
> |
public Boolean realCall() throws InterruptedException { |
540 |
> |
threadStarted.countDown(); |
541 |
> |
done.await(); |
542 |
> |
return Boolean.TRUE; |
543 |
> |
}}; |
544 |
> |
tasks[i] = new FutureTask(task); |
545 |
> |
p.execute(tasks[i]); |
546 |
> |
} |
547 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
548 |
> |
assertEquals(tasks.length, p.getTaskCount()); |
549 |
> |
assertEquals(tasks.length - 1, q.size()); |
550 |
> |
assertEquals(1L, p.getActiveCount()); |
551 |
> |
assertEquals(0L, p.getCompletedTaskCount()); |
552 |
> |
tasks[4].cancel(true); |
553 |
> |
tasks[3].cancel(false); |
554 |
> |
p.purge(); |
555 |
> |
assertEquals(tasks.length - 3, q.size()); |
556 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
557 |
> |
p.purge(); // Nothing to do |
558 |
> |
assertEquals(tasks.length - 3, q.size()); |
559 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
560 |
> |
} finally { |
561 |
> |
done.countDown(); |
562 |
> |
joinPool(p); |
563 |
|
} |
366 |
– |
tasks[4].cancel(true); |
367 |
– |
tasks[3].cancel(true); |
368 |
– |
p1.purge(); |
369 |
– |
long count = p1.getTaskCount(); |
370 |
– |
assertTrue(count >= 2 && count < 5); |
371 |
– |
joinPool(p1); |
564 |
|
} |
565 |
|
|
566 |
|
/** |
567 |
< |
* shutDownNow returns a list containing tasks that were not run |
567 |
> |
* shutDownNow returns a list containing tasks that were not run |
568 |
|
*/ |
569 |
< |
public void testShutDownNow() { |
570 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
569 |
> |
public void testShutdownNow() { |
570 |
> |
final ThreadPoolExecutor p = |
571 |
> |
new ThreadPoolExecutor(1, 1, |
572 |
> |
LONG_DELAY_MS, MILLISECONDS, |
573 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
574 |
|
List l; |
575 |
|
try { |
576 |
|
for (int i = 0; i < 5; i++) |
577 |
< |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
577 |
> |
p.execute(new MediumPossiblyInterruptedRunnable()); |
578 |
|
} |
579 |
|
finally { |
580 |
|
try { |
581 |
< |
l = p1.shutdownNow(); |
581 |
> |
l = p.shutdownNow(); |
582 |
|
} catch (SecurityException ok) { return; } |
388 |
– |
|
583 |
|
} |
584 |
< |
assertTrue(p1.isShutdown()); |
585 |
< |
assertTrue(l.size() <= 4); |
584 |
> |
assertTrue(p.isShutdown()); |
585 |
> |
assertTrue(l.size() <= 4); |
586 |
|
} |
587 |
|
|
588 |
|
// Exception Tests |
593 |
|
*/ |
594 |
|
public void testConstructor1() { |
595 |
|
try { |
596 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
596 |
> |
new ThreadPoolExecutor(-1, 1, |
597 |
> |
LONG_DELAY_MS, MILLISECONDS, |
598 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
599 |
|
shouldThrow(); |
600 |
|
} catch (IllegalArgumentException success) {} |
601 |
|
} |
605 |
|
*/ |
606 |
|
public void testConstructor2() { |
607 |
|
try { |
608 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
608 |
> |
new ThreadPoolExecutor(1, -1, |
609 |
> |
LONG_DELAY_MS, MILLISECONDS, |
610 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
611 |
|
shouldThrow(); |
612 |
|
} catch (IllegalArgumentException success) {} |
613 |
|
} |
617 |
|
*/ |
618 |
|
public void testConstructor3() { |
619 |
|
try { |
620 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
620 |
> |
new ThreadPoolExecutor(1, 0, |
621 |
> |
LONG_DELAY_MS, MILLISECONDS, |
622 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
623 |
|
shouldThrow(); |
624 |
|
} catch (IllegalArgumentException success) {} |
625 |
|
} |
629 |
|
*/ |
630 |
|
public void testConstructor4() { |
631 |
|
try { |
632 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
632 |
> |
new ThreadPoolExecutor(1, 2, |
633 |
> |
-1L, MILLISECONDS, |
634 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
635 |
|
shouldThrow(); |
636 |
|
} catch (IllegalArgumentException success) {} |
637 |
|
} |
641 |
|
*/ |
642 |
|
public void testConstructor5() { |
643 |
|
try { |
644 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
644 |
> |
new ThreadPoolExecutor(2, 1, |
645 |
> |
LONG_DELAY_MS, MILLISECONDS, |
646 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
647 |
|
shouldThrow(); |
648 |
|
} catch (IllegalArgumentException success) {} |
649 |
|
} |
653 |
|
*/ |
654 |
|
public void testConstructorNullPointerException() { |
655 |
|
try { |
656 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
656 |
> |
new ThreadPoolExecutor(1, 2, |
657 |
> |
LONG_DELAY_MS, MILLISECONDS, |
658 |
> |
(BlockingQueue) null); |
659 |
|
shouldThrow(); |
660 |
|
} catch (NullPointerException success) {} |
661 |
|
} |
667 |
|
*/ |
668 |
|
public void testConstructor6() { |
669 |
|
try { |
670 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
670 |
> |
new ThreadPoolExecutor(-1, 1, |
671 |
> |
LONG_DELAY_MS, MILLISECONDS, |
672 |
> |
new ArrayBlockingQueue<Runnable>(10), |
673 |
> |
new SimpleThreadFactory()); |
674 |
|
shouldThrow(); |
675 |
|
} catch (IllegalArgumentException success) {} |
676 |
|
} |
680 |
|
*/ |
681 |
|
public void testConstructor7() { |
682 |
|
try { |
683 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
683 |
> |
new ThreadPoolExecutor(1, -1, |
684 |
> |
LONG_DELAY_MS, MILLISECONDS, |
685 |
> |
new ArrayBlockingQueue<Runnable>(10), |
686 |
> |
new SimpleThreadFactory()); |
687 |
|
shouldThrow(); |
688 |
|
} catch (IllegalArgumentException success) {} |
689 |
|
} |
693 |
|
*/ |
694 |
|
public void testConstructor8() { |
695 |
|
try { |
696 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
696 |
> |
new ThreadPoolExecutor(1, 0, |
697 |
> |
LONG_DELAY_MS, MILLISECONDS, |
698 |
> |
new ArrayBlockingQueue<Runnable>(10), |
699 |
> |
new SimpleThreadFactory()); |
700 |
|
shouldThrow(); |
701 |
|
} catch (IllegalArgumentException success) {} |
702 |
|
} |
706 |
|
*/ |
707 |
|
public void testConstructor9() { |
708 |
|
try { |
709 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
709 |
> |
new ThreadPoolExecutor(1, 2, |
710 |
> |
-1L, MILLISECONDS, |
711 |
> |
new ArrayBlockingQueue<Runnable>(10), |
712 |
> |
new SimpleThreadFactory()); |
713 |
|
shouldThrow(); |
714 |
|
} catch (IllegalArgumentException success) {} |
715 |
|
} |
719 |
|
*/ |
720 |
|
public void testConstructor10() { |
721 |
|
try { |
722 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
722 |
> |
new ThreadPoolExecutor(2, 1, |
723 |
> |
LONG_DELAY_MS, MILLISECONDS, |
724 |
> |
new ArrayBlockingQueue<Runnable>(10), |
725 |
> |
new SimpleThreadFactory()); |
726 |
|
shouldThrow(); |
727 |
|
} catch (IllegalArgumentException success) {} |
728 |
|
} |
732 |
|
*/ |
733 |
|
public void testConstructorNullPointerException2() { |
734 |
|
try { |
735 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
735 |
> |
new ThreadPoolExecutor(1, 2, |
736 |
> |
LONG_DELAY_MS, MILLISECONDS, |
737 |
> |
(BlockingQueue) null, |
738 |
> |
new SimpleThreadFactory()); |
739 |
|
shouldThrow(); |
740 |
|
} catch (NullPointerException success) {} |
741 |
|
} |
745 |
|
*/ |
746 |
|
public void testConstructorNullPointerException3() { |
747 |
|
try { |
748 |
< |
ThreadFactory f = null; |
749 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
748 |
> |
new ThreadPoolExecutor(1, 2, |
749 |
> |
LONG_DELAY_MS, MILLISECONDS, |
750 |
> |
new ArrayBlockingQueue<Runnable>(10), |
751 |
> |
(ThreadFactory) null); |
752 |
|
shouldThrow(); |
753 |
|
} catch (NullPointerException success) {} |
754 |
|
} |
759 |
|
*/ |
760 |
|
public void testConstructor11() { |
761 |
|
try { |
762 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
762 |
> |
new ThreadPoolExecutor(-1, 1, |
763 |
> |
LONG_DELAY_MS, MILLISECONDS, |
764 |
> |
new ArrayBlockingQueue<Runnable>(10), |
765 |
> |
new NoOpREHandler()); |
766 |
|
shouldThrow(); |
767 |
|
} catch (IllegalArgumentException success) {} |
768 |
|
} |
772 |
|
*/ |
773 |
|
public void testConstructor12() { |
774 |
|
try { |
775 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
775 |
> |
new ThreadPoolExecutor(1, -1, |
776 |
> |
LONG_DELAY_MS, MILLISECONDS, |
777 |
> |
new ArrayBlockingQueue<Runnable>(10), |
778 |
> |
new NoOpREHandler()); |
779 |
|
shouldThrow(); |
780 |
|
} catch (IllegalArgumentException success) {} |
781 |
|
} |
785 |
|
*/ |
786 |
|
public void testConstructor13() { |
787 |
|
try { |
788 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
788 |
> |
new ThreadPoolExecutor(1, 0, |
789 |
> |
LONG_DELAY_MS, MILLISECONDS, |
790 |
> |
new ArrayBlockingQueue<Runnable>(10), |
791 |
> |
new NoOpREHandler()); |
792 |
|
shouldThrow(); |
793 |
|
} catch (IllegalArgumentException success) {} |
794 |
|
} |
798 |
|
*/ |
799 |
|
public void testConstructor14() { |
800 |
|
try { |
801 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
801 |
> |
new ThreadPoolExecutor(1, 2, |
802 |
> |
-1L, MILLISECONDS, |
803 |
> |
new ArrayBlockingQueue<Runnable>(10), |
804 |
> |
new NoOpREHandler()); |
805 |
|
shouldThrow(); |
806 |
|
} catch (IllegalArgumentException success) {} |
807 |
|
} |
811 |
|
*/ |
812 |
|
public void testConstructor15() { |
813 |
|
try { |
814 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
814 |
> |
new ThreadPoolExecutor(2, 1, |
815 |
> |
LONG_DELAY_MS, MILLISECONDS, |
816 |
> |
new ArrayBlockingQueue<Runnable>(10), |
817 |
> |
new NoOpREHandler()); |
818 |
|
shouldThrow(); |
819 |
|
} catch (IllegalArgumentException success) {} |
820 |
|
} |
824 |
|
*/ |
825 |
|
public void testConstructorNullPointerException4() { |
826 |
|
try { |
827 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
827 |
> |
new ThreadPoolExecutor(1, 2, |
828 |
> |
LONG_DELAY_MS, MILLISECONDS, |
829 |
> |
(BlockingQueue) null, |
830 |
> |
new NoOpREHandler()); |
831 |
|
shouldThrow(); |
832 |
|
} catch (NullPointerException success) {} |
833 |
|
} |
837 |
|
*/ |
838 |
|
public void testConstructorNullPointerException5() { |
839 |
|
try { |
840 |
< |
RejectedExecutionHandler r = null; |
841 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
840 |
> |
new ThreadPoolExecutor(1, 2, |
841 |
> |
LONG_DELAY_MS, MILLISECONDS, |
842 |
> |
new ArrayBlockingQueue<Runnable>(10), |
843 |
> |
(RejectedExecutionHandler) null); |
844 |
|
shouldThrow(); |
845 |
|
} catch (NullPointerException success) {} |
846 |
|
} |
851 |
|
*/ |
852 |
|
public void testConstructor16() { |
853 |
|
try { |
854 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
854 |
> |
new ThreadPoolExecutor(-1, 1, |
855 |
> |
LONG_DELAY_MS, MILLISECONDS, |
856 |
> |
new ArrayBlockingQueue<Runnable>(10), |
857 |
> |
new SimpleThreadFactory(), |
858 |
> |
new NoOpREHandler()); |
859 |
|
shouldThrow(); |
860 |
|
} catch (IllegalArgumentException success) {} |
861 |
|
} |
865 |
|
*/ |
866 |
|
public void testConstructor17() { |
867 |
|
try { |
868 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
868 |
> |
new ThreadPoolExecutor(1, -1, |
869 |
> |
LONG_DELAY_MS, MILLISECONDS, |
870 |
> |
new ArrayBlockingQueue<Runnable>(10), |
871 |
> |
new SimpleThreadFactory(), |
872 |
> |
new NoOpREHandler()); |
873 |
|
shouldThrow(); |
874 |
|
} catch (IllegalArgumentException success) {} |
875 |
|
} |
879 |
|
*/ |
880 |
|
public void testConstructor18() { |
881 |
|
try { |
882 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
882 |
> |
new ThreadPoolExecutor(1, 0, |
883 |
> |
LONG_DELAY_MS, MILLISECONDS, |
884 |
> |
new ArrayBlockingQueue<Runnable>(10), |
885 |
> |
new SimpleThreadFactory(), |
886 |
> |
new NoOpREHandler()); |
887 |
|
shouldThrow(); |
888 |
|
} catch (IllegalArgumentException success) {} |
889 |
|
} |
893 |
|
*/ |
894 |
|
public void testConstructor19() { |
895 |
|
try { |
896 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
896 |
> |
new ThreadPoolExecutor(1, 2, |
897 |
> |
-1L, MILLISECONDS, |
898 |
> |
new ArrayBlockingQueue<Runnable>(10), |
899 |
> |
new SimpleThreadFactory(), |
900 |
> |
new NoOpREHandler()); |
901 |
|
shouldThrow(); |
902 |
|
} catch (IllegalArgumentException success) {} |
903 |
|
} |
907 |
|
*/ |
908 |
|
public void testConstructor20() { |
909 |
|
try { |
910 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
910 |
> |
new ThreadPoolExecutor(2, 1, |
911 |
> |
LONG_DELAY_MS, MILLISECONDS, |
912 |
> |
new ArrayBlockingQueue<Runnable>(10), |
913 |
> |
new SimpleThreadFactory(), |
914 |
> |
new NoOpREHandler()); |
915 |
|
shouldThrow(); |
916 |
|
} catch (IllegalArgumentException success) {} |
917 |
|
} |
918 |
|
|
919 |
|
/** |
920 |
< |
* Constructor throws if workQueue is set to null |
920 |
> |
* Constructor throws if workQueue is null |
921 |
|
*/ |
922 |
|
public void testConstructorNullPointerException6() { |
923 |
|
try { |
924 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
924 |
> |
new ThreadPoolExecutor(1, 2, |
925 |
> |
LONG_DELAY_MS, MILLISECONDS, |
926 |
> |
(BlockingQueue) null, |
927 |
> |
new SimpleThreadFactory(), |
928 |
> |
new NoOpREHandler()); |
929 |
|
shouldThrow(); |
930 |
|
} catch (NullPointerException success) {} |
931 |
|
} |
932 |
|
|
933 |
|
/** |
934 |
< |
* Constructor throws if handler is set to null |
934 |
> |
* Constructor throws if handler is null |
935 |
|
*/ |
936 |
|
public void testConstructorNullPointerException7() { |
937 |
|
try { |
938 |
< |
RejectedExecutionHandler r = null; |
939 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
938 |
> |
new ThreadPoolExecutor(1, 2, |
939 |
> |
LONG_DELAY_MS, MILLISECONDS, |
940 |
> |
new ArrayBlockingQueue<Runnable>(10), |
941 |
> |
new SimpleThreadFactory(), |
942 |
> |
(RejectedExecutionHandler) null); |
943 |
|
shouldThrow(); |
944 |
|
} catch (NullPointerException success) {} |
945 |
|
} |
946 |
|
|
947 |
|
/** |
948 |
< |
* Constructor throws if ThreadFactory is set top null |
948 |
> |
* Constructor throws if ThreadFactory is null |
949 |
|
*/ |
950 |
|
public void testConstructorNullPointerException8() { |
951 |
|
try { |
952 |
< |
ThreadFactory f = null; |
953 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
952 |
> |
new ThreadPoolExecutor(1, 2, |
953 |
> |
LONG_DELAY_MS, MILLISECONDS, |
954 |
> |
new ArrayBlockingQueue<Runnable>(10), |
955 |
> |
(ThreadFactory) null, |
956 |
> |
new NoOpREHandler()); |
957 |
|
shouldThrow(); |
958 |
|
} catch (NullPointerException success) {} |
959 |
|
} |
960 |
|
|
961 |
+ |
/** |
962 |
+ |
* get of submitted callable throws InterruptedException if interrupted |
963 |
+ |
*/ |
964 |
+ |
public void testInterruptedSubmit() throws InterruptedException { |
965 |
+ |
final ThreadPoolExecutor p = |
966 |
+ |
new ThreadPoolExecutor(1, 1, |
967 |
+ |
60, TimeUnit.SECONDS, |
968 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
969 |
+ |
|
970 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
971 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
972 |
+ |
try { |
973 |
+ |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
974 |
+ |
public void realRun() throws Exception { |
975 |
+ |
Callable task = new CheckedCallable<Boolean>() { |
976 |
+ |
public Boolean realCall() throws InterruptedException { |
977 |
+ |
threadStarted.countDown(); |
978 |
+ |
done.await(); |
979 |
+ |
return Boolean.TRUE; |
980 |
+ |
}}; |
981 |
+ |
p.submit(task).get(); |
982 |
+ |
}}); |
983 |
+ |
|
984 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
985 |
+ |
t.interrupt(); |
986 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
987 |
+ |
} finally { |
988 |
+ |
done.countDown(); |
989 |
+ |
joinPool(p); |
990 |
+ |
} |
991 |
+ |
} |
992 |
|
|
993 |
|
/** |
994 |
< |
* execute throws RejectedExecutionException |
688 |
< |
* if saturated. |
994 |
> |
* execute throws RejectedExecutionException if saturated. |
995 |
|
*/ |
996 |
|
public void testSaturatedExecute() { |
997 |
|
ThreadPoolExecutor p = |
998 |
|
new ThreadPoolExecutor(1, 1, |
999 |
|
LONG_DELAY_MS, MILLISECONDS, |
1000 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1001 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1002 |
+ |
try { |
1003 |
+ |
Runnable task = new CheckedRunnable() { |
1004 |
+ |
public void realRun() throws InterruptedException { |
1005 |
+ |
done.await(); |
1006 |
+ |
}}; |
1007 |
+ |
for (int i = 0; i < 2; ++i) |
1008 |
+ |
p.execute(task); |
1009 |
+ |
for (int i = 0; i < 2; ++i) { |
1010 |
+ |
try { |
1011 |
+ |
p.execute(task); |
1012 |
+ |
shouldThrow(); |
1013 |
+ |
} catch (RejectedExecutionException success) {} |
1014 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1015 |
+ |
} |
1016 |
+ |
} finally { |
1017 |
+ |
done.countDown(); |
1018 |
+ |
joinPool(p); |
1019 |
+ |
} |
1020 |
+ |
} |
1021 |
+ |
|
1022 |
+ |
/** |
1023 |
+ |
* submit(runnable) throws RejectedExecutionException if saturated. |
1024 |
+ |
*/ |
1025 |
+ |
public void testSaturatedSubmitRunnable() { |
1026 |
+ |
ThreadPoolExecutor p = |
1027 |
+ |
new ThreadPoolExecutor(1, 1, |
1028 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1029 |
+ |
new ArrayBlockingQueue<Runnable>(1)); |
1030 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1031 |
|
try { |
1032 |
+ |
Runnable task = new CheckedRunnable() { |
1033 |
+ |
public void realRun() throws InterruptedException { |
1034 |
+ |
done.await(); |
1035 |
+ |
}}; |
1036 |
|
for (int i = 0; i < 2; ++i) |
1037 |
< |
p.execute(new MediumRunnable()); |
1037 |
> |
p.submit(task); |
1038 |
|
for (int i = 0; i < 2; ++i) { |
1039 |
|
try { |
1040 |
< |
p.execute(new MediumRunnable()); |
1040 |
> |
p.execute(task); |
1041 |
|
shouldThrow(); |
1042 |
|
} catch (RejectedExecutionException success) {} |
1043 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1044 |
|
} |
1045 |
|
} finally { |
1046 |
+ |
done.countDown(); |
1047 |
|
joinPool(p); |
1048 |
|
} |
1049 |
|
} |
1050 |
|
|
1051 |
|
/** |
1052 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1052 |
> |
* submit(callable) throws RejectedExecutionException if saturated. |
1053 |
> |
*/ |
1054 |
> |
public void testSaturatedSubmitCallable() { |
1055 |
> |
ThreadPoolExecutor p = |
1056 |
> |
new ThreadPoolExecutor(1, 1, |
1057 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1058 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1059 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1060 |
> |
try { |
1061 |
> |
Runnable task = new CheckedRunnable() { |
1062 |
> |
public void realRun() throws InterruptedException { |
1063 |
> |
done.await(); |
1064 |
> |
}}; |
1065 |
> |
for (int i = 0; i < 2; ++i) |
1066 |
> |
p.submit(Executors.callable(task)); |
1067 |
> |
for (int i = 0; i < 2; ++i) { |
1068 |
> |
try { |
1069 |
> |
p.execute(task); |
1070 |
> |
shouldThrow(); |
1071 |
> |
} catch (RejectedExecutionException success) {} |
1072 |
> |
assertTrue(p.getTaskCount() <= 2); |
1073 |
> |
} |
1074 |
> |
} finally { |
1075 |
> |
done.countDown(); |
1076 |
> |
joinPool(p); |
1077 |
> |
} |
1078 |
> |
} |
1079 |
> |
|
1080 |
> |
/** |
1081 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
1082 |
|
*/ |
1083 |
|
public void testSaturatedExecute2() { |
1084 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1085 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1085 |
> |
final ThreadPoolExecutor p = |
1086 |
> |
new ThreadPoolExecutor(1, 1, |
1087 |
> |
LONG_DELAY_MS, |
1088 |
> |
MILLISECONDS, |
1089 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1090 |
> |
h); |
1091 |
|
try { |
716 |
– |
|
1092 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1093 |
< |
for (int i = 0; i < 5; ++i) { |
1093 |
> |
for (int i = 0; i < tasks.length; ++i) |
1094 |
|
tasks[i] = new TrackedNoOpRunnable(); |
720 |
– |
} |
1095 |
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1096 |
|
p.execute(mr); |
1097 |
< |
for (int i = 0; i < 5; ++i) { |
1097 |
> |
for (int i = 0; i < tasks.length; ++i) |
1098 |
|
p.execute(tasks[i]); |
1099 |
< |
} |
726 |
< |
for (int i = 1; i < 5; ++i) { |
1099 |
> |
for (int i = 1; i < tasks.length; ++i) |
1100 |
|
assertTrue(tasks[i].done); |
728 |
– |
} |
1101 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1102 |
|
} finally { |
1103 |
|
joinPool(p); |
1105 |
|
} |
1106 |
|
|
1107 |
|
/** |
1108 |
< |
* executor using DiscardPolicy drops task if saturated. |
1108 |
> |
* executor using DiscardPolicy drops task if saturated. |
1109 |
|
*/ |
1110 |
|
public void testSaturatedExecute3() { |
1111 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1112 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1112 |
> |
final ThreadPoolExecutor p = |
1113 |
> |
new ThreadPoolExecutor(1, 1, |
1114 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1115 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1116 |
> |
h); |
1117 |
|
try { |
742 |
– |
|
1118 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1119 |
< |
for (int i = 0; i < 5; ++i) { |
1119 |
> |
for (int i = 0; i < tasks.length; ++i) |
1120 |
|
tasks[i] = new TrackedNoOpRunnable(); |
746 |
– |
} |
1121 |
|
p.execute(new TrackedLongRunnable()); |
1122 |
< |
for (int i = 0; i < 5; ++i) { |
1123 |
< |
p.execute(tasks[i]); |
1124 |
< |
} |
1125 |
< |
for (int i = 0; i < 5; ++i) { |
752 |
< |
assertFalse(tasks[i].done); |
753 |
< |
} |
1122 |
> |
for (TrackedNoOpRunnable task : tasks) |
1123 |
> |
p.execute(task); |
1124 |
> |
for (TrackedNoOpRunnable task : tasks) |
1125 |
> |
assertFalse(task.done); |
1126 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1127 |
|
} finally { |
1128 |
|
joinPool(p); |
1130 |
|
} |
1131 |
|
|
1132 |
|
/** |
1133 |
< |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1133 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1134 |
|
*/ |
1135 |
|
public void testSaturatedExecute4() { |
1136 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1137 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1137 |
> |
final ThreadPoolExecutor p = |
1138 |
> |
new ThreadPoolExecutor(1, 1, |
1139 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1140 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1141 |
> |
h); |
1142 |
|
try { |
1143 |
|
p.execute(new TrackedLongRunnable()); |
1144 |
|
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1155 |
|
} |
1156 |
|
|
1157 |
|
/** |
1158 |
< |
* execute throws RejectedExecutionException if shutdown |
1158 |
> |
* execute throws RejectedExecutionException if shutdown |
1159 |
|
*/ |
1160 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1161 |
< |
ThreadPoolExecutor tpe = |
1162 |
< |
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1163 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1164 |
< |
try { |
1165 |
< |
tpe.execute(new NoOpRunnable()); |
1166 |
< |
shouldThrow(); |
1167 |
< |
} catch (RejectedExecutionException success) {} |
1161 |
> |
ThreadPoolExecutor p = |
1162 |
> |
new ThreadPoolExecutor(1, 1, |
1163 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1164 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1165 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1166 |
> |
try { |
1167 |
> |
p.execute(new NoOpRunnable()); |
1168 |
> |
shouldThrow(); |
1169 |
> |
} catch (RejectedExecutionException success) {} |
1170 |
|
|
1171 |
< |
joinPool(tpe); |
1171 |
> |
joinPool(p); |
1172 |
|
} |
1173 |
|
|
1174 |
|
/** |
1175 |
< |
* execute using CallerRunsPolicy drops task on shutdown |
1175 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
1176 |
|
*/ |
1177 |
|
public void testCallerRunsOnShutdown() { |
1178 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1179 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1179 |
> |
final ThreadPoolExecutor p = |
1180 |
> |
new ThreadPoolExecutor(1, 1, |
1181 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1182 |
> |
new ArrayBlockingQueue<Runnable>(1), h); |
1183 |
|
|
1184 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1185 |
< |
try { |
1185 |
> |
try { |
1186 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1187 |
< |
p.execute(r); |
1187 |
> |
p.execute(r); |
1188 |
|
assertFalse(r.done); |
1189 |
|
} finally { |
1190 |
|
joinPool(p); |
1192 |
|
} |
1193 |
|
|
1194 |
|
/** |
1195 |
< |
* execute using DiscardPolicy drops task on shutdown |
1195 |
> |
* execute using DiscardPolicy drops task on shutdown |
1196 |
|
*/ |
1197 |
|
public void testDiscardOnShutdown() { |
1198 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1199 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1199 |
> |
ThreadPoolExecutor p = |
1200 |
> |
new ThreadPoolExecutor(1, 1, |
1201 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1202 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1203 |
> |
h); |
1204 |
|
|
1205 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1206 |
< |
try { |
1206 |
> |
try { |
1207 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1208 |
< |
p.execute(r); |
1208 |
> |
p.execute(r); |
1209 |
|
assertFalse(r.done); |
1210 |
|
} finally { |
1211 |
|
joinPool(p); |
1214 |
|
|
1215 |
|
|
1216 |
|
/** |
1217 |
< |
* execute using DiscardOldestPolicy drops task on shutdown |
1217 |
> |
* execute using DiscardOldestPolicy drops task on shutdown |
1218 |
|
*/ |
1219 |
|
public void testDiscardOldestOnShutdown() { |
1220 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1221 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1221 |
> |
ThreadPoolExecutor p = |
1222 |
> |
new ThreadPoolExecutor(1, 1, |
1223 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1224 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1225 |
> |
h); |
1226 |
|
|
1227 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1228 |
< |
try { |
1228 |
> |
try { |
1229 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1230 |
< |
p.execute(r); |
1230 |
> |
p.execute(r); |
1231 |
|
assertFalse(r.done); |
1232 |
|
} finally { |
1233 |
|
joinPool(p); |
1236 |
|
|
1237 |
|
|
1238 |
|
/** |
1239 |
< |
* execute (null) throws NPE |
1239 |
> |
* execute(null) throws NPE |
1240 |
|
*/ |
1241 |
|
public void testExecuteNull() { |
1242 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1242 |
> |
ThreadPoolExecutor p = |
1243 |
> |
new ThreadPoolExecutor(1, 2, |
1244 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1245 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1246 |
|
try { |
1247 |
< |
tpe.execute(null); |
1247 |
> |
p.execute(null); |
1248 |
|
shouldThrow(); |
1249 |
< |
} catch (NullPointerException success) {} |
1249 |
> |
} catch (NullPointerException success) {} |
1250 |
|
|
1251 |
< |
joinPool(tpe); |
1251 |
> |
joinPool(p); |
1252 |
|
} |
1253 |
|
|
1254 |
|
/** |
1255 |
< |
* setCorePoolSize of negative value throws IllegalArgumentException |
1255 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
1256 |
|
*/ |
1257 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1258 |
< |
ThreadPoolExecutor tpe = |
1258 |
> |
ThreadPoolExecutor p = |
1259 |
|
new ThreadPoolExecutor(1, 2, |
1260 |
|
LONG_DELAY_MS, MILLISECONDS, |
1261 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1262 |
< |
try { |
1263 |
< |
tpe.setCorePoolSize(-1); |
1264 |
< |
shouldThrow(); |
1265 |
< |
} catch (IllegalArgumentException success) { |
1262 |
> |
try { |
1263 |
> |
p.setCorePoolSize(-1); |
1264 |
> |
shouldThrow(); |
1265 |
> |
} catch (IllegalArgumentException success) { |
1266 |
|
} finally { |
1267 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1267 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1268 |
|
} |
1269 |
< |
joinPool(tpe); |
1269 |
> |
joinPool(p); |
1270 |
|
} |
1271 |
|
|
1272 |
|
/** |
1273 |
< |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1274 |
< |
* given a value less the core pool size |
1273 |
> |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1274 |
> |
* given a value less the core pool size |
1275 |
|
*/ |
1276 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1277 |
< |
ThreadPoolExecutor tpe = |
1277 |
> |
ThreadPoolExecutor p = |
1278 |
|
new ThreadPoolExecutor(2, 3, |
1279 |
|
LONG_DELAY_MS, MILLISECONDS, |
1280 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1281 |
|
try { |
1282 |
< |
tpe.setMaximumPoolSize(1); |
1282 |
> |
p.setMaximumPoolSize(1); |
1283 |
|
shouldThrow(); |
1284 |
|
} catch (IllegalArgumentException success) { |
1285 |
|
} finally { |
1286 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1286 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1287 |
|
} |
1288 |
< |
joinPool(tpe); |
1288 |
> |
joinPool(p); |
1289 |
|
} |
1290 |
|
|
1291 |
|
/** |
1292 |
< |
* setMaximumPoolSize throws IllegalArgumentException |
1293 |
< |
* if given a negative value |
1292 |
> |
* setMaximumPoolSize throws IllegalArgumentException |
1293 |
> |
* if given a negative value |
1294 |
|
*/ |
1295 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1296 |
< |
ThreadPoolExecutor tpe = |
1296 |
> |
ThreadPoolExecutor p = |
1297 |
|
new ThreadPoolExecutor(2, 3, |
1298 |
|
LONG_DELAY_MS, MILLISECONDS, |
1299 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1300 |
|
try { |
1301 |
< |
tpe.setMaximumPoolSize(-1); |
1301 |
> |
p.setMaximumPoolSize(-1); |
1302 |
|
shouldThrow(); |
1303 |
|
} catch (IllegalArgumentException success) { |
1304 |
|
} finally { |
1305 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1305 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1306 |
|
} |
1307 |
< |
joinPool(tpe); |
1307 |
> |
joinPool(p); |
1308 |
|
} |
1309 |
|
|
1310 |
|
|
1311 |
|
/** |
1312 |
< |
* setKeepAliveTime throws IllegalArgumentException |
1313 |
< |
* when given a negative value |
1312 |
> |
* setKeepAliveTime throws IllegalArgumentException |
1313 |
> |
* when given a negative value |
1314 |
|
*/ |
1315 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1316 |
< |
ThreadPoolExecutor tpe = |
1316 |
> |
ThreadPoolExecutor p = |
1317 |
|
new ThreadPoolExecutor(2, 3, |
1318 |
|
LONG_DELAY_MS, MILLISECONDS, |
1319 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1320 |
< |
try { |
1321 |
< |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
1320 |
> |
try { |
1321 |
> |
p.setKeepAliveTime(-1,MILLISECONDS); |
1322 |
|
shouldThrow(); |
1323 |
|
} catch (IllegalArgumentException success) { |
1324 |
|
} finally { |
1325 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1325 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1326 |
|
} |
1327 |
< |
joinPool(tpe); |
1327 |
> |
joinPool(p); |
1328 |
|
} |
1329 |
|
|
1330 |
|
/** |
1331 |
|
* terminated() is called on termination |
1332 |
|
*/ |
1333 |
|
public void testTerminated() { |
1334 |
< |
ExtendedTPE tpe = new ExtendedTPE(); |
1335 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1336 |
< |
assertTrue(tpe.terminatedCalled); |
1337 |
< |
joinPool(tpe); |
1334 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1335 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1336 |
> |
assertTrue(p.terminatedCalled); |
1337 |
> |
joinPool(p); |
1338 |
|
} |
1339 |
|
|
1340 |
|
/** |
1341 |
|
* beforeExecute and afterExecute are called when executing task |
1342 |
|
*/ |
1343 |
|
public void testBeforeAfter() throws InterruptedException { |
1344 |
< |
ExtendedTPE tpe = new ExtendedTPE(); |
1344 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1345 |
|
try { |
1346 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1347 |
< |
tpe.execute(r); |
1348 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1347 |
> |
p.execute(r); |
1348 |
> |
delay(SHORT_DELAY_MS); |
1349 |
|
assertTrue(r.done); |
1350 |
< |
assertTrue(tpe.beforeCalled); |
1351 |
< |
assertTrue(tpe.afterCalled); |
1352 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1350 |
> |
assertTrue(p.beforeCalled); |
1351 |
> |
assertTrue(p.afterCalled); |
1352 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1353 |
|
} finally { |
1354 |
< |
joinPool(tpe); |
1354 |
> |
joinPool(p); |
1355 |
|
} |
1356 |
|
} |
1357 |
|
|
1359 |
|
* completed submit of callable returns result |
1360 |
|
*/ |
1361 |
|
public void testSubmitCallable() throws Exception { |
1362 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1362 |
> |
ExecutorService e = |
1363 |
> |
new ThreadPoolExecutor(2, 2, |
1364 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1365 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1366 |
|
try { |
1367 |
|
Future<String> future = e.submit(new StringTask()); |
1368 |
|
String result = future.get(); |
1376 |
|
* completed submit of runnable returns successfully |
1377 |
|
*/ |
1378 |
|
public void testSubmitRunnable() throws Exception { |
1379 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1379 |
> |
ExecutorService e = |
1380 |
> |
new ThreadPoolExecutor(2, 2, |
1381 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1382 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1383 |
|
try { |
1384 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1385 |
|
future.get(); |
1393 |
|
* completed submit of (runnable, result) returns result |
1394 |
|
*/ |
1395 |
|
public void testSubmitRunnable2() throws Exception { |
1396 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1396 |
> |
ExecutorService e = |
1397 |
> |
new ThreadPoolExecutor(2, 2, |
1398 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1399 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1400 |
|
try { |
1401 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1402 |
|
String result = future.get(); |
1411 |
|
* invokeAny(null) throws NPE |
1412 |
|
*/ |
1413 |
|
public void testInvokeAny1() throws Exception { |
1414 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1414 |
> |
ExecutorService e = |
1415 |
> |
new ThreadPoolExecutor(2, 2, |
1416 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1417 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1418 |
|
try { |
1419 |
|
e.invokeAny(null); |
1420 |
|
shouldThrow(); |
1428 |
|
* invokeAny(empty collection) throws IAE |
1429 |
|
*/ |
1430 |
|
public void testInvokeAny2() throws Exception { |
1431 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1431 |
> |
ExecutorService e = |
1432 |
> |
new ThreadPoolExecutor(2, 2, |
1433 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1434 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1435 |
|
try { |
1436 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1437 |
|
shouldThrow(); |
1446 |
|
*/ |
1447 |
|
public void testInvokeAny3() throws Exception { |
1448 |
|
final CountDownLatch latch = new CountDownLatch(1); |
1449 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1449 |
> |
final ExecutorService e = |
1450 |
> |
new ThreadPoolExecutor(2, 2, |
1451 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1452 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1453 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1454 |
> |
l.add(latchAwaitingStringTask(latch)); |
1455 |
> |
l.add(null); |
1456 |
|
try { |
1044 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1045 |
– |
l.add(new Callable<String>() { |
1046 |
– |
public String call() { |
1047 |
– |
try { |
1048 |
– |
latch.await(); |
1049 |
– |
} catch (InterruptedException ok) {} |
1050 |
– |
return TEST_STRING; |
1051 |
– |
}}); |
1052 |
– |
l.add(null); |
1457 |
|
e.invokeAny(l); |
1458 |
|
shouldThrow(); |
1459 |
|
} catch (NullPointerException success) { |
1467 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1468 |
|
*/ |
1469 |
|
public void testInvokeAny4() throws Exception { |
1470 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1470 |
> |
ExecutorService e = |
1471 |
> |
new ThreadPoolExecutor(2, 2, |
1472 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1473 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1474 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1475 |
> |
l.add(new NPETask()); |
1476 |
|
try { |
1068 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1069 |
– |
l.add(new NPETask()); |
1477 |
|
e.invokeAny(l); |
1478 |
|
shouldThrow(); |
1479 |
|
} catch (ExecutionException success) { |
1487 |
|
* invokeAny(c) returns result of some task |
1488 |
|
*/ |
1489 |
|
public void testInvokeAny5() throws Exception { |
1490 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1490 |
> |
ExecutorService e = |
1491 |
> |
new ThreadPoolExecutor(2, 2, |
1492 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1493 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1494 |
|
try { |
1495 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1495 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1496 |
|
l.add(new StringTask()); |
1497 |
|
l.add(new StringTask()); |
1498 |
|
String result = e.invokeAny(l); |
1506 |
|
* invokeAll(null) throws NPE |
1507 |
|
*/ |
1508 |
|
public void testInvokeAll1() throws Exception { |
1509 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1509 |
> |
ExecutorService e = |
1510 |
> |
new ThreadPoolExecutor(2, 2, |
1511 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1512 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1513 |
|
try { |
1514 |
|
e.invokeAll(null); |
1515 |
|
shouldThrow(); |
1523 |
|
* invokeAll(empty collection) returns empty collection |
1524 |
|
*/ |
1525 |
|
public void testInvokeAll2() throws InterruptedException { |
1526 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1526 |
> |
ExecutorService e = |
1527 |
> |
new ThreadPoolExecutor(2, 2, |
1528 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1529 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1530 |
|
try { |
1531 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1532 |
|
assertTrue(r.isEmpty()); |
1539 |
|
* invokeAll(c) throws NPE if c has null elements |
1540 |
|
*/ |
1541 |
|
public void testInvokeAll3() throws Exception { |
1542 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1542 |
> |
ExecutorService e = |
1543 |
> |
new ThreadPoolExecutor(2, 2, |
1544 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1545 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1546 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1547 |
> |
l.add(new StringTask()); |
1548 |
> |
l.add(null); |
1549 |
|
try { |
1128 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1129 |
– |
l.add(new StringTask()); |
1130 |
– |
l.add(null); |
1550 |
|
e.invokeAll(l); |
1551 |
|
shouldThrow(); |
1552 |
|
} catch (NullPointerException success) { |
1559 |
|
* get of element of invokeAll(c) throws exception on failed task |
1560 |
|
*/ |
1561 |
|
public void testInvokeAll4() throws Exception { |
1562 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1562 |
> |
ExecutorService e = |
1563 |
> |
new ThreadPoolExecutor(2, 2, |
1564 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1565 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1566 |
|
try { |
1567 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1567 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1568 |
|
l.add(new NPETask()); |
1569 |
< |
List<Future<String>> result = e.invokeAll(l); |
1570 |
< |
assertEquals(1, result.size()); |
1571 |
< |
for (Future<String> future : result) { |
1572 |
< |
try { |
1573 |
< |
future.get(); |
1574 |
< |
shouldThrow(); |
1575 |
< |
} catch (ExecutionException success) { |
1154 |
< |
Throwable cause = success.getCause(); |
1155 |
< |
assertTrue(cause instanceof NullPointerException); |
1156 |
< |
} |
1569 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1570 |
> |
assertEquals(1, futures.size()); |
1571 |
> |
try { |
1572 |
> |
futures.get(0).get(); |
1573 |
> |
shouldThrow(); |
1574 |
> |
} catch (ExecutionException success) { |
1575 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1576 |
|
} |
1577 |
|
} finally { |
1578 |
|
joinPool(e); |
1583 |
|
* invokeAll(c) returns results of all completed tasks |
1584 |
|
*/ |
1585 |
|
public void testInvokeAll5() throws Exception { |
1586 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1586 |
> |
ExecutorService e = |
1587 |
> |
new ThreadPoolExecutor(2, 2, |
1588 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1589 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1590 |
|
try { |
1591 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1591 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1592 |
|
l.add(new StringTask()); |
1593 |
|
l.add(new StringTask()); |
1594 |
< |
List<Future<String>> result = e.invokeAll(l); |
1595 |
< |
assertEquals(2, result.size()); |
1596 |
< |
for (Future<String> future : result) |
1594 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1595 |
> |
assertEquals(2, futures.size()); |
1596 |
> |
for (Future<String> future : futures) |
1597 |
|
assertSame(TEST_STRING, future.get()); |
1598 |
|
} finally { |
1599 |
|
joinPool(e); |
1606 |
|
* timed invokeAny(null) throws NPE |
1607 |
|
*/ |
1608 |
|
public void testTimedInvokeAny1() throws Exception { |
1609 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1609 |
> |
ExecutorService e = |
1610 |
> |
new ThreadPoolExecutor(2, 2, |
1611 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1612 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1613 |
|
try { |
1614 |
|
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1615 |
|
shouldThrow(); |
1623 |
|
* timed invokeAny(,,null) throws NPE |
1624 |
|
*/ |
1625 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1626 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1626 |
> |
ExecutorService e = |
1627 |
> |
new ThreadPoolExecutor(2, 2, |
1628 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1629 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1630 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1631 |
> |
l.add(new StringTask()); |
1632 |
|
try { |
1203 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1204 |
– |
l.add(new StringTask()); |
1633 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1634 |
|
shouldThrow(); |
1635 |
|
} catch (NullPointerException success) { |
1642 |
|
* timed invokeAny(empty collection) throws IAE |
1643 |
|
*/ |
1644 |
|
public void testTimedInvokeAny2() throws Exception { |
1645 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1645 |
> |
ExecutorService e = |
1646 |
> |
new ThreadPoolExecutor(2, 2, |
1647 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1648 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1649 |
|
try { |
1650 |
|
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1651 |
|
shouldThrow(); |
1659 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1660 |
|
*/ |
1661 |
|
public void testTimedInvokeAny3() throws Exception { |
1662 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1662 |
> |
final CountDownLatch latch = new CountDownLatch(1); |
1663 |
> |
final ExecutorService e = |
1664 |
> |
new ThreadPoolExecutor(2, 2, |
1665 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1666 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1667 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1668 |
> |
l.add(latchAwaitingStringTask(latch)); |
1669 |
> |
l.add(null); |
1670 |
|
try { |
1233 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1234 |
– |
l.add(new StringTask()); |
1235 |
– |
l.add(null); |
1671 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1672 |
|
shouldThrow(); |
1673 |
|
} catch (NullPointerException success) { |
1674 |
|
} finally { |
1675 |
+ |
latch.countDown(); |
1676 |
|
joinPool(e); |
1677 |
|
} |
1678 |
|
} |
1681 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1682 |
|
*/ |
1683 |
|
public void testTimedInvokeAny4() throws Exception { |
1684 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1684 |
> |
ExecutorService e = |
1685 |
> |
new ThreadPoolExecutor(2, 2, |
1686 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1687 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1688 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1689 |
> |
l.add(new NPETask()); |
1690 |
|
try { |
1250 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1251 |
– |
l.add(new NPETask()); |
1691 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1692 |
|
shouldThrow(); |
1693 |
|
} catch (ExecutionException success) { |
1701 |
|
* timed invokeAny(c) returns result of some task |
1702 |
|
*/ |
1703 |
|
public void testTimedInvokeAny5() throws Exception { |
1704 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1704 |
> |
ExecutorService e = |
1705 |
> |
new ThreadPoolExecutor(2, 2, |
1706 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1707 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1708 |
|
try { |
1709 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1709 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1710 |
|
l.add(new StringTask()); |
1711 |
|
l.add(new StringTask()); |
1712 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1720 |
|
* timed invokeAll(null) throws NPE |
1721 |
|
*/ |
1722 |
|
public void testTimedInvokeAll1() throws Exception { |
1723 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1723 |
> |
ExecutorService e = |
1724 |
> |
new ThreadPoolExecutor(2, 2, |
1725 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1726 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1727 |
|
try { |
1728 |
|
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1729 |
|
shouldThrow(); |
1737 |
|
* timed invokeAll(,,null) throws NPE |
1738 |
|
*/ |
1739 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1740 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1740 |
> |
ExecutorService e = |
1741 |
> |
new ThreadPoolExecutor(2, 2, |
1742 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1743 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1744 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1745 |
> |
l.add(new StringTask()); |
1746 |
|
try { |
1297 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1298 |
– |
l.add(new StringTask()); |
1747 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1748 |
|
shouldThrow(); |
1749 |
|
} catch (NullPointerException success) { |
1756 |
|
* timed invokeAll(empty collection) returns empty collection |
1757 |
|
*/ |
1758 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1759 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1759 |
> |
ExecutorService e = |
1760 |
> |
new ThreadPoolExecutor(2, 2, |
1761 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1762 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1763 |
|
try { |
1764 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1765 |
|
assertTrue(r.isEmpty()); |
1772 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1773 |
|
*/ |
1774 |
|
public void testTimedInvokeAll3() throws Exception { |
1775 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1775 |
> |
ExecutorService e = |
1776 |
> |
new ThreadPoolExecutor(2, 2, |
1777 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1778 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1779 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1780 |
> |
l.add(new StringTask()); |
1781 |
> |
l.add(null); |
1782 |
|
try { |
1326 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1327 |
– |
l.add(new StringTask()); |
1328 |
– |
l.add(null); |
1783 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1784 |
|
shouldThrow(); |
1785 |
|
} catch (NullPointerException success) { |
1792 |
|
* get of element of invokeAll(c) throws exception on failed task |
1793 |
|
*/ |
1794 |
|
public void testTimedInvokeAll4() throws Exception { |
1795 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1795 |
> |
ExecutorService e = |
1796 |
> |
new ThreadPoolExecutor(2, 2, |
1797 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1798 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1799 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1800 |
> |
l.add(new NPETask()); |
1801 |
> |
List<Future<String>> futures = |
1802 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1803 |
> |
assertEquals(1, futures.size()); |
1804 |
|
try { |
1805 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1344 |
< |
l.add(new NPETask()); |
1345 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1346 |
< |
assertEquals(1, result.size()); |
1347 |
< |
for (Future<String> future : result) |
1348 |
< |
future.get(); |
1805 |
> |
futures.get(0).get(); |
1806 |
|
shouldThrow(); |
1807 |
|
} catch (ExecutionException success) { |
1808 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1815 |
|
* timed invokeAll(c) returns results of all completed tasks |
1816 |
|
*/ |
1817 |
|
public void testTimedInvokeAll5() throws Exception { |
1818 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1818 |
> |
ExecutorService e = |
1819 |
> |
new ThreadPoolExecutor(2, 2, |
1820 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1821 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1822 |
|
try { |
1823 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1823 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1824 |
|
l.add(new StringTask()); |
1825 |
|
l.add(new StringTask()); |
1826 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1827 |
< |
assertEquals(2, result.size()); |
1828 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1829 |
< |
assertSame(TEST_STRING, it.next().get()); |
1826 |
> |
List<Future<String>> futures = |
1827 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1828 |
> |
assertEquals(2, futures.size()); |
1829 |
> |
for (Future<String> future : futures) |
1830 |
> |
assertSame(TEST_STRING, future.get()); |
1831 |
|
} finally { |
1832 |
|
joinPool(e); |
1833 |
|
} |
1837 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1838 |
|
*/ |
1839 |
|
public void testTimedInvokeAll6() throws Exception { |
1840 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1840 |
> |
ExecutorService e = |
1841 |
> |
new ThreadPoolExecutor(2, 2, |
1842 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1843 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1844 |
|
try { |
1845 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1845 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1846 |
|
l.add(new StringTask()); |
1847 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1848 |
|
l.add(new StringTask()); |
1849 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1850 |
< |
assertEquals(3, result.size()); |
1851 |
< |
Iterator<Future<String>> it = result.iterator(); |
1849 |
> |
List<Future<String>> futures = |
1850 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1851 |
> |
assertEquals(3, futures.size()); |
1852 |
> |
Iterator<Future<String>> it = futures.iterator(); |
1853 |
|
Future<String> f1 = it.next(); |
1854 |
|
Future<String> f2 = it.next(); |
1855 |
|
Future<String> f3 = it.next(); |
1868 |
|
* thread factory fails to create more |
1869 |
|
*/ |
1870 |
|
public void testFailingThreadFactory() throws InterruptedException { |
1871 |
< |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1871 |
> |
final ExecutorService e = |
1872 |
> |
new ThreadPoolExecutor(100, 100, |
1873 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1874 |
> |
new LinkedBlockingQueue<Runnable>(), |
1875 |
> |
new FailingThreadFactory()); |
1876 |
|
try { |
1877 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1878 |
< |
for (int k = 0; k < 100; ++k) { |
1879 |
< |
e.execute(new NoOpRunnable()); |
1880 |
< |
} |
1881 |
< |
Thread.sleep(LONG_DELAY_MS); |
1877 |
> |
final int TASKS = 100; |
1878 |
> |
final CountDownLatch done = new CountDownLatch(TASKS); |
1879 |
> |
for (int k = 0; k < TASKS; ++k) |
1880 |
> |
e.execute(new CheckedRunnable() { |
1881 |
> |
public void realRun() { |
1882 |
> |
done.countDown(); |
1883 |
> |
}}); |
1884 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1885 |
|
} finally { |
1886 |
|
joinPool(e); |
1887 |
|
} |
1891 |
|
* allowsCoreThreadTimeOut is by default false. |
1892 |
|
*/ |
1893 |
|
public void testAllowsCoreThreadTimeOut() { |
1894 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1895 |
< |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1896 |
< |
joinPool(tpe); |
1894 |
> |
final ThreadPoolExecutor p = |
1895 |
> |
new ThreadPoolExecutor(2, 2, |
1896 |
> |
1000, MILLISECONDS, |
1897 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1898 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1899 |
> |
joinPool(p); |
1900 |
|
} |
1901 |
|
|
1902 |
|
/** |
1903 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1904 |
|
*/ |
1905 |
< |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1906 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1907 |
< |
tpe.allowCoreThreadTimeOut(true); |
1908 |
< |
tpe.execute(new NoOpRunnable()); |
1905 |
> |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1906 |
> |
final ThreadPoolExecutor p = |
1907 |
> |
new ThreadPoolExecutor(2, 10, |
1908 |
> |
SHORT_DELAY_MS, MILLISECONDS, |
1909 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1910 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1911 |
|
try { |
1912 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1913 |
< |
assertEquals(0, tpe.getPoolSize()); |
1912 |
> |
p.allowCoreThreadTimeOut(true); |
1913 |
> |
p.execute(new CheckedRunnable() { |
1914 |
> |
public void realRun() throws InterruptedException { |
1915 |
> |
threadStarted.countDown(); |
1916 |
> |
assertEquals(1, p.getPoolSize()); |
1917 |
> |
}}); |
1918 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1919 |
> |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1920 |
> |
if (p.getPoolSize() == 0) |
1921 |
> |
break; |
1922 |
> |
delay(10); |
1923 |
> |
} |
1924 |
> |
assertEquals(0, p.getPoolSize()); |
1925 |
|
} finally { |
1926 |
< |
joinPool(tpe); |
1926 |
> |
joinPool(p); |
1927 |
|
} |
1928 |
|
} |
1929 |
|
|
1930 |
|
/** |
1931 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1932 |
|
*/ |
1933 |
< |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1934 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1935 |
< |
tpe.allowCoreThreadTimeOut(false); |
1936 |
< |
tpe.execute(new NoOpRunnable()); |
1933 |
> |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1934 |
> |
final ThreadPoolExecutor p = |
1935 |
> |
new ThreadPoolExecutor(2, 10, |
1936 |
> |
SHORT_DELAY_MS, MILLISECONDS, |
1937 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1938 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1939 |
|
try { |
1940 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1941 |
< |
assertTrue(tpe.getPoolSize() >= 1); |
1940 |
> |
p.allowCoreThreadTimeOut(false); |
1941 |
> |
p.execute(new CheckedRunnable() { |
1942 |
> |
public void realRun() throws InterruptedException { |
1943 |
> |
threadStarted.countDown(); |
1944 |
> |
assertTrue(p.getPoolSize() >= 1); |
1945 |
> |
}}); |
1946 |
> |
delay(SMALL_DELAY_MS); |
1947 |
> |
assertTrue(p.getPoolSize() >= 1); |
1948 |
|
} finally { |
1949 |
< |
joinPool(tpe); |
1949 |
> |
joinPool(p); |
1950 |
|
} |
1951 |
|
} |
1952 |
|
|
1956 |
|
*/ |
1957 |
|
public void testRejectedRecycledTask() throws InterruptedException { |
1958 |
|
final int nTasks = 1000; |
1959 |
< |
final AtomicInteger nRun = new AtomicInteger(0); |
1959 |
> |
final CountDownLatch done = new CountDownLatch(nTasks); |
1960 |
|
final Runnable recycledTask = new Runnable() { |
1961 |
< |
public void run() { |
1962 |
< |
nRun.getAndIncrement(); |
1963 |
< |
} }; |
1961 |
> |
public void run() { |
1962 |
> |
done.countDown(); |
1963 |
> |
}}; |
1964 |
|
final ThreadPoolExecutor p = |
1965 |
|
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
1966 |
|
new ArrayBlockingQueue(30)); |
1971 |
|
p.execute(recycledTask); |
1972 |
|
break; |
1973 |
|
} |
1974 |
< |
catch (RejectedExecutionException ignore) { |
1479 |
< |
} |
1974 |
> |
catch (RejectedExecutionException ignore) {} |
1975 |
|
} |
1976 |
|
} |
1977 |
< |
Thread.sleep(5000); // enough time to run all tasks |
1978 |
< |
assertEquals(nRun.get(), nTasks); |
1977 |
> |
// enough time to run all tasks |
1978 |
> |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
1979 |
|
} finally { |
1980 |
|
p.shutdown(); |
1981 |
|
} |