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 |
> |
Thread.sleep(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 |
> |
threadStarted.countDown(); |
410 |
> |
assertFalse(p.isTerminated()); |
411 |
> |
done.await(); |
412 |
> |
}}); |
413 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
414 |
> |
done.countDown(); |
415 |
|
} finally { |
416 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
416 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
417 |
|
} |
418 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
419 |
< |
assertTrue(p1.isTerminated()); |
418 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
419 |
> |
assertTrue(p.isTerminated()); |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* isTerminating is not true when running or when terminated |
423 |
> |
* isTerminating is not true when running or when terminated |
424 |
|
*/ |
425 |
|
public void testIsTerminating() throws InterruptedException { |
426 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
427 |
< |
assertFalse(p1.isTerminating()); |
426 |
> |
final ThreadPoolExecutor p = |
427 |
> |
new ThreadPoolExecutor(1, 1, |
428 |
> |
LONG_DELAY_MS, MILLISECONDS, |
429 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
430 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
431 |
> |
final CountDownLatch done = new CountDownLatch(1); |
432 |
|
try { |
433 |
< |
p1.execute(new SmallRunnable()); |
434 |
< |
assertFalse(p1.isTerminating()); |
435 |
< |
} finally { |
436 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
437 |
< |
} |
438 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
439 |
< |
assertTrue(p1.isTerminated()); |
440 |
< |
assertFalse(p1.isTerminating()); |
433 |
> |
assertFalse(p.isTerminating()); |
434 |
> |
p.execute(new CheckedRunnable() { |
435 |
> |
public void realRun() throws InterruptedException { |
436 |
> |
assertFalse(p.isTerminating()); |
437 |
> |
threadStarted.countDown(); |
438 |
> |
done.await(); |
439 |
> |
}}); |
440 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
441 |
> |
assertFalse(p.isTerminating()); |
442 |
> |
done.countDown(); |
443 |
> |
} finally { |
444 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
445 |
> |
} |
446 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
447 |
> |
assertTrue(p.isTerminated()); |
448 |
> |
assertFalse(p.isTerminating()); |
449 |
|
} |
450 |
|
|
451 |
|
/** |
452 |
|
* getQueue returns the work queue, which contains queued tasks |
453 |
|
*/ |
454 |
|
public void testGetQueue() throws InterruptedException { |
455 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
456 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
457 |
< |
FutureTask[] tasks = new FutureTask[5]; |
458 |
< |
for (int i = 0; i < 5; i++) { |
459 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
460 |
< |
p1.execute(tasks[i]); |
461 |
< |
} |
462 |
< |
try { |
463 |
< |
Thread.sleep(SHORT_DELAY_MS); |
464 |
< |
BlockingQueue<Runnable> wq = p1.getQueue(); |
465 |
< |
assertSame(q, wq); |
466 |
< |
assertFalse(wq.contains(tasks[0])); |
467 |
< |
assertTrue(wq.contains(tasks[4])); |
468 |
< |
for (int i = 1; i < 5; ++i) |
469 |
< |
tasks[i].cancel(true); |
470 |
< |
p1.shutdownNow(); |
455 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
456 |
> |
final ThreadPoolExecutor p = |
457 |
> |
new ThreadPoolExecutor(1, 1, |
458 |
> |
LONG_DELAY_MS, MILLISECONDS, |
459 |
> |
q); |
460 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
461 |
> |
final CountDownLatch done = new CountDownLatch(1); |
462 |
> |
try { |
463 |
> |
FutureTask[] tasks = new FutureTask[5]; |
464 |
> |
for (int i = 0; i < tasks.length; i++) { |
465 |
> |
Callable task = new CheckedCallable<Boolean>() { |
466 |
> |
public Boolean realCall() throws InterruptedException { |
467 |
> |
threadStarted.countDown(); |
468 |
> |
assertSame(q, p.getQueue()); |
469 |
> |
done.await(); |
470 |
> |
return Boolean.TRUE; |
471 |
> |
}}; |
472 |
> |
tasks[i] = new FutureTask(task); |
473 |
> |
p.execute(tasks[i]); |
474 |
> |
} |
475 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
476 |
> |
assertSame(q, p.getQueue()); |
477 |
> |
assertFalse(q.contains(tasks[0])); |
478 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
479 |
> |
assertEquals(tasks.length - 1, q.size()); |
480 |
|
} finally { |
481 |
< |
joinPool(p1); |
481 |
> |
done.countDown(); |
482 |
> |
joinPool(p); |
483 |
|
} |
484 |
|
} |
485 |
|
|
488 |
|
*/ |
489 |
|
public void testRemove() throws InterruptedException { |
490 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
491 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
492 |
< |
FutureTask[] tasks = new FutureTask[5]; |
493 |
< |
for (int i = 0; i < 5; i++) { |
494 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
495 |
< |
p1.execute(tasks[i]); |
496 |
< |
} |
497 |
< |
try { |
498 |
< |
Thread.sleep(SHORT_DELAY_MS); |
499 |
< |
assertFalse(p1.remove(tasks[0])); |
491 |
> |
final ThreadPoolExecutor p = |
492 |
> |
new ThreadPoolExecutor(1, 1, |
493 |
> |
LONG_DELAY_MS, MILLISECONDS, |
494 |
> |
q); |
495 |
> |
Runnable[] tasks = new Runnable[5]; |
496 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
497 |
> |
final CountDownLatch done = new CountDownLatch(1); |
498 |
> |
try { |
499 |
> |
for (int i = 0; i < tasks.length; i++) { |
500 |
> |
tasks[i] = new CheckedRunnable() { |
501 |
> |
public void realRun() throws InterruptedException { |
502 |
> |
threadStarted.countDown(); |
503 |
> |
done.await(); |
504 |
> |
}}; |
505 |
> |
p.execute(tasks[i]); |
506 |
> |
} |
507 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
508 |
> |
assertFalse(p.remove(tasks[0])); |
509 |
|
assertTrue(q.contains(tasks[4])); |
510 |
|
assertTrue(q.contains(tasks[3])); |
511 |
< |
assertTrue(p1.remove(tasks[4])); |
512 |
< |
assertFalse(p1.remove(tasks[4])); |
511 |
> |
assertTrue(p.remove(tasks[4])); |
512 |
> |
assertFalse(p.remove(tasks[4])); |
513 |
|
assertFalse(q.contains(tasks[4])); |
514 |
|
assertTrue(q.contains(tasks[3])); |
515 |
< |
assertTrue(p1.remove(tasks[3])); |
515 |
> |
assertTrue(p.remove(tasks[3])); |
516 |
|
assertFalse(q.contains(tasks[3])); |
517 |
|
} finally { |
518 |
< |
joinPool(p1); |
518 |
> |
done.countDown(); |
519 |
> |
joinPool(p); |
520 |
|
} |
521 |
|
} |
522 |
|
|
523 |
|
/** |
524 |
< |
* purge removes cancelled tasks from the queue |
524 |
> |
* purge removes cancelled tasks from the queue |
525 |
|
*/ |
526 |
< |
public void testPurge() { |
527 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
526 |
> |
public void testPurge() throws InterruptedException { |
527 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
528 |
> |
final CountDownLatch done = new CountDownLatch(1); |
529 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
530 |
> |
final ThreadPoolExecutor p = |
531 |
> |
new ThreadPoolExecutor(1, 1, |
532 |
> |
LONG_DELAY_MS, MILLISECONDS, |
533 |
> |
q); |
534 |
|
FutureTask[] tasks = new FutureTask[5]; |
535 |
< |
for (int i = 0; i < 5; i++) { |
536 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
537 |
< |
p1.execute(tasks[i]); |
535 |
> |
try { |
536 |
> |
for (int i = 0; i < tasks.length; i++) { |
537 |
> |
Callable task = new CheckedCallable<Boolean>() { |
538 |
> |
public Boolean realCall() throws InterruptedException { |
539 |
> |
threadStarted.countDown(); |
540 |
> |
done.await(); |
541 |
> |
return Boolean.TRUE; |
542 |
> |
}}; |
543 |
> |
tasks[i] = new FutureTask(task); |
544 |
> |
p.execute(tasks[i]); |
545 |
> |
} |
546 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
547 |
> |
assertEquals(tasks.length, p.getTaskCount()); |
548 |
> |
assertEquals(tasks.length - 1, q.size()); |
549 |
> |
assertEquals(1L, p.getActiveCount()); |
550 |
> |
assertEquals(0L, p.getCompletedTaskCount()); |
551 |
> |
tasks[4].cancel(true); |
552 |
> |
tasks[3].cancel(false); |
553 |
> |
p.purge(); |
554 |
> |
assertEquals(tasks.length - 3, q.size()); |
555 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
556 |
> |
p.purge(); // Nothing to do |
557 |
> |
assertEquals(tasks.length - 3, q.size()); |
558 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
559 |
> |
} finally { |
560 |
> |
done.countDown(); |
561 |
> |
joinPool(p); |
562 |
|
} |
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); |
563 |
|
} |
564 |
|
|
565 |
|
/** |
566 |
< |
* shutDownNow returns a list containing tasks that were not run |
566 |
> |
* shutDownNow returns a list containing tasks that were not run |
567 |
|
*/ |
568 |
|
public void testShutDownNow() { |
569 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
569 |
> |
final ThreadPoolExecutor p = |
570 |
> |
new ThreadPoolExecutor(1, 1, |
571 |
> |
LONG_DELAY_MS, MILLISECONDS, |
572 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
573 |
|
List l; |
574 |
|
try { |
575 |
|
for (int i = 0; i < 5; i++) |
576 |
< |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
576 |
> |
p.execute(new MediumPossiblyInterruptedRunnable()); |
577 |
|
} |
578 |
|
finally { |
579 |
|
try { |
580 |
< |
l = p1.shutdownNow(); |
580 |
> |
l = p.shutdownNow(); |
581 |
|
} catch (SecurityException ok) { return; } |
388 |
– |
|
582 |
|
} |
583 |
< |
assertTrue(p1.isShutdown()); |
584 |
< |
assertTrue(l.size() <= 4); |
583 |
> |
assertTrue(p.isShutdown()); |
584 |
> |
assertTrue(l.size() <= 4); |
585 |
|
} |
586 |
|
|
587 |
|
// Exception Tests |
592 |
|
*/ |
593 |
|
public void testConstructor1() { |
594 |
|
try { |
595 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
595 |
> |
new ThreadPoolExecutor(-1, 1, |
596 |
> |
LONG_DELAY_MS, MILLISECONDS, |
597 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
598 |
|
shouldThrow(); |
599 |
|
} catch (IllegalArgumentException success) {} |
600 |
|
} |
604 |
|
*/ |
605 |
|
public void testConstructor2() { |
606 |
|
try { |
607 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
607 |
> |
new ThreadPoolExecutor(1, -1, |
608 |
> |
LONG_DELAY_MS, MILLISECONDS, |
609 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
610 |
|
shouldThrow(); |
611 |
|
} catch (IllegalArgumentException success) {} |
612 |
|
} |
616 |
|
*/ |
617 |
|
public void testConstructor3() { |
618 |
|
try { |
619 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
619 |
> |
new ThreadPoolExecutor(1, 0, |
620 |
> |
LONG_DELAY_MS, MILLISECONDS, |
621 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
622 |
|
shouldThrow(); |
623 |
|
} catch (IllegalArgumentException success) {} |
624 |
|
} |
628 |
|
*/ |
629 |
|
public void testConstructor4() { |
630 |
|
try { |
631 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
631 |
> |
new ThreadPoolExecutor(1, 2, |
632 |
> |
-1L, MILLISECONDS, |
633 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
634 |
|
shouldThrow(); |
635 |
|
} catch (IllegalArgumentException success) {} |
636 |
|
} |
640 |
|
*/ |
641 |
|
public void testConstructor5() { |
642 |
|
try { |
643 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
643 |
> |
new ThreadPoolExecutor(2, 1, |
644 |
> |
LONG_DELAY_MS, MILLISECONDS, |
645 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
646 |
|
shouldThrow(); |
647 |
|
} catch (IllegalArgumentException success) {} |
648 |
|
} |
652 |
|
*/ |
653 |
|
public void testConstructorNullPointerException() { |
654 |
|
try { |
655 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
655 |
> |
new ThreadPoolExecutor(1, 2, |
656 |
> |
LONG_DELAY_MS, MILLISECONDS, |
657 |
> |
(BlockingQueue) null); |
658 |
|
shouldThrow(); |
659 |
|
} catch (NullPointerException success) {} |
660 |
|
} |
666 |
|
*/ |
667 |
|
public void testConstructor6() { |
668 |
|
try { |
669 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
669 |
> |
new ThreadPoolExecutor(-1, 1, |
670 |
> |
LONG_DELAY_MS, MILLISECONDS, |
671 |
> |
new ArrayBlockingQueue<Runnable>(10), |
672 |
> |
new SimpleThreadFactory()); |
673 |
|
shouldThrow(); |
674 |
|
} catch (IllegalArgumentException success) {} |
675 |
|
} |
679 |
|
*/ |
680 |
|
public void testConstructor7() { |
681 |
|
try { |
682 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
682 |
> |
new ThreadPoolExecutor(1, -1, |
683 |
> |
LONG_DELAY_MS, MILLISECONDS, |
684 |
> |
new ArrayBlockingQueue<Runnable>(10), |
685 |
> |
new SimpleThreadFactory()); |
686 |
|
shouldThrow(); |
687 |
|
} catch (IllegalArgumentException success) {} |
688 |
|
} |
692 |
|
*/ |
693 |
|
public void testConstructor8() { |
694 |
|
try { |
695 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
695 |
> |
new ThreadPoolExecutor(1, 0, |
696 |
> |
LONG_DELAY_MS, MILLISECONDS, |
697 |
> |
new ArrayBlockingQueue<Runnable>(10), |
698 |
> |
new SimpleThreadFactory()); |
699 |
|
shouldThrow(); |
700 |
|
} catch (IllegalArgumentException success) {} |
701 |
|
} |
705 |
|
*/ |
706 |
|
public void testConstructor9() { |
707 |
|
try { |
708 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
708 |
> |
new ThreadPoolExecutor(1, 2, |
709 |
> |
-1L, MILLISECONDS, |
710 |
> |
new ArrayBlockingQueue<Runnable>(10), |
711 |
> |
new SimpleThreadFactory()); |
712 |
|
shouldThrow(); |
713 |
|
} catch (IllegalArgumentException success) {} |
714 |
|
} |
718 |
|
*/ |
719 |
|
public void testConstructor10() { |
720 |
|
try { |
721 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
721 |
> |
new ThreadPoolExecutor(2, 1, |
722 |
> |
LONG_DELAY_MS, MILLISECONDS, |
723 |
> |
new ArrayBlockingQueue<Runnable>(10), |
724 |
> |
new SimpleThreadFactory()); |
725 |
|
shouldThrow(); |
726 |
|
} catch (IllegalArgumentException success) {} |
727 |
|
} |
731 |
|
*/ |
732 |
|
public void testConstructorNullPointerException2() { |
733 |
|
try { |
734 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
734 |
> |
new ThreadPoolExecutor(1, 2, |
735 |
> |
LONG_DELAY_MS, MILLISECONDS, |
736 |
> |
(BlockingQueue) null, |
737 |
> |
new SimpleThreadFactory()); |
738 |
|
shouldThrow(); |
739 |
|
} catch (NullPointerException success) {} |
740 |
|
} |
744 |
|
*/ |
745 |
|
public void testConstructorNullPointerException3() { |
746 |
|
try { |
747 |
< |
ThreadFactory f = null; |
748 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
747 |
> |
new ThreadPoolExecutor(1, 2, |
748 |
> |
LONG_DELAY_MS, MILLISECONDS, |
749 |
> |
new ArrayBlockingQueue<Runnable>(10), |
750 |
> |
(ThreadFactory) null); |
751 |
|
shouldThrow(); |
752 |
|
} catch (NullPointerException success) {} |
753 |
|
} |
758 |
|
*/ |
759 |
|
public void testConstructor11() { |
760 |
|
try { |
761 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
761 |
> |
new ThreadPoolExecutor(-1, 1, |
762 |
> |
LONG_DELAY_MS, MILLISECONDS, |
763 |
> |
new ArrayBlockingQueue<Runnable>(10), |
764 |
> |
new NoOpREHandler()); |
765 |
|
shouldThrow(); |
766 |
|
} catch (IllegalArgumentException success) {} |
767 |
|
} |
771 |
|
*/ |
772 |
|
public void testConstructor12() { |
773 |
|
try { |
774 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
774 |
> |
new ThreadPoolExecutor(1, -1, |
775 |
> |
LONG_DELAY_MS, MILLISECONDS, |
776 |
> |
new ArrayBlockingQueue<Runnable>(10), |
777 |
> |
new NoOpREHandler()); |
778 |
|
shouldThrow(); |
779 |
|
} catch (IllegalArgumentException success) {} |
780 |
|
} |
784 |
|
*/ |
785 |
|
public void testConstructor13() { |
786 |
|
try { |
787 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
787 |
> |
new ThreadPoolExecutor(1, 0, |
788 |
> |
LONG_DELAY_MS, MILLISECONDS, |
789 |
> |
new ArrayBlockingQueue<Runnable>(10), |
790 |
> |
new NoOpREHandler()); |
791 |
|
shouldThrow(); |
792 |
|
} catch (IllegalArgumentException success) {} |
793 |
|
} |
797 |
|
*/ |
798 |
|
public void testConstructor14() { |
799 |
|
try { |
800 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
800 |
> |
new ThreadPoolExecutor(1, 2, |
801 |
> |
-1L, MILLISECONDS, |
802 |
> |
new ArrayBlockingQueue<Runnable>(10), |
803 |
> |
new NoOpREHandler()); |
804 |
|
shouldThrow(); |
805 |
|
} catch (IllegalArgumentException success) {} |
806 |
|
} |
810 |
|
*/ |
811 |
|
public void testConstructor15() { |
812 |
|
try { |
813 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
813 |
> |
new ThreadPoolExecutor(2, 1, |
814 |
> |
LONG_DELAY_MS, MILLISECONDS, |
815 |
> |
new ArrayBlockingQueue<Runnable>(10), |
816 |
> |
new NoOpREHandler()); |
817 |
|
shouldThrow(); |
818 |
|
} catch (IllegalArgumentException success) {} |
819 |
|
} |
823 |
|
*/ |
824 |
|
public void testConstructorNullPointerException4() { |
825 |
|
try { |
826 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
826 |
> |
new ThreadPoolExecutor(1, 2, |
827 |
> |
LONG_DELAY_MS, MILLISECONDS, |
828 |
> |
(BlockingQueue) null, |
829 |
> |
new NoOpREHandler()); |
830 |
|
shouldThrow(); |
831 |
|
} catch (NullPointerException success) {} |
832 |
|
} |
836 |
|
*/ |
837 |
|
public void testConstructorNullPointerException5() { |
838 |
|
try { |
839 |
< |
RejectedExecutionHandler r = null; |
840 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
839 |
> |
new ThreadPoolExecutor(1, 2, |
840 |
> |
LONG_DELAY_MS, MILLISECONDS, |
841 |
> |
new ArrayBlockingQueue<Runnable>(10), |
842 |
> |
(RejectedExecutionHandler) null); |
843 |
|
shouldThrow(); |
844 |
|
} catch (NullPointerException success) {} |
845 |
|
} |
850 |
|
*/ |
851 |
|
public void testConstructor16() { |
852 |
|
try { |
853 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
853 |
> |
new ThreadPoolExecutor(-1, 1, |
854 |
> |
LONG_DELAY_MS, MILLISECONDS, |
855 |
> |
new ArrayBlockingQueue<Runnable>(10), |
856 |
> |
new SimpleThreadFactory(), |
857 |
> |
new NoOpREHandler()); |
858 |
|
shouldThrow(); |
859 |
|
} catch (IllegalArgumentException success) {} |
860 |
|
} |
864 |
|
*/ |
865 |
|
public void testConstructor17() { |
866 |
|
try { |
867 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
867 |
> |
new ThreadPoolExecutor(1, -1, |
868 |
> |
LONG_DELAY_MS, MILLISECONDS, |
869 |
> |
new ArrayBlockingQueue<Runnable>(10), |
870 |
> |
new SimpleThreadFactory(), |
871 |
> |
new NoOpREHandler()); |
872 |
|
shouldThrow(); |
873 |
|
} catch (IllegalArgumentException success) {} |
874 |
|
} |
878 |
|
*/ |
879 |
|
public void testConstructor18() { |
880 |
|
try { |
881 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
881 |
> |
new ThreadPoolExecutor(1, 0, |
882 |
> |
LONG_DELAY_MS, MILLISECONDS, |
883 |
> |
new ArrayBlockingQueue<Runnable>(10), |
884 |
> |
new SimpleThreadFactory(), |
885 |
> |
new NoOpREHandler()); |
886 |
|
shouldThrow(); |
887 |
|
} catch (IllegalArgumentException success) {} |
888 |
|
} |
892 |
|
*/ |
893 |
|
public void testConstructor19() { |
894 |
|
try { |
895 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
895 |
> |
new ThreadPoolExecutor(1, 2, |
896 |
> |
-1L, MILLISECONDS, |
897 |
> |
new ArrayBlockingQueue<Runnable>(10), |
898 |
> |
new SimpleThreadFactory(), |
899 |
> |
new NoOpREHandler()); |
900 |
|
shouldThrow(); |
901 |
|
} catch (IllegalArgumentException success) {} |
902 |
|
} |
906 |
|
*/ |
907 |
|
public void testConstructor20() { |
908 |
|
try { |
909 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
909 |
> |
new ThreadPoolExecutor(2, 1, |
910 |
> |
LONG_DELAY_MS, MILLISECONDS, |
911 |
> |
new ArrayBlockingQueue<Runnable>(10), |
912 |
> |
new SimpleThreadFactory(), |
913 |
> |
new NoOpREHandler()); |
914 |
|
shouldThrow(); |
915 |
|
} catch (IllegalArgumentException success) {} |
916 |
|
} |
917 |
|
|
918 |
|
/** |
919 |
< |
* Constructor throws if workQueue is set to null |
919 |
> |
* Constructor throws if workQueue is null |
920 |
|
*/ |
921 |
|
public void testConstructorNullPointerException6() { |
922 |
|
try { |
923 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
923 |
> |
new ThreadPoolExecutor(1, 2, |
924 |
> |
LONG_DELAY_MS, MILLISECONDS, |
925 |
> |
(BlockingQueue) null, |
926 |
> |
new SimpleThreadFactory(), |
927 |
> |
new NoOpREHandler()); |
928 |
|
shouldThrow(); |
929 |
|
} catch (NullPointerException success) {} |
930 |
|
} |
931 |
|
|
932 |
|
/** |
933 |
< |
* Constructor throws if handler is set to null |
933 |
> |
* Constructor throws if handler is null |
934 |
|
*/ |
935 |
|
public void testConstructorNullPointerException7() { |
936 |
|
try { |
937 |
< |
RejectedExecutionHandler r = null; |
938 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
937 |
> |
new ThreadPoolExecutor(1, 2, |
938 |
> |
LONG_DELAY_MS, MILLISECONDS, |
939 |
> |
new ArrayBlockingQueue<Runnable>(10), |
940 |
> |
new SimpleThreadFactory(), |
941 |
> |
(RejectedExecutionHandler) null); |
942 |
|
shouldThrow(); |
943 |
|
} catch (NullPointerException success) {} |
944 |
|
} |
945 |
|
|
946 |
|
/** |
947 |
< |
* Constructor throws if ThreadFactory is set top null |
947 |
> |
* Constructor throws if ThreadFactory is null |
948 |
|
*/ |
949 |
|
public void testConstructorNullPointerException8() { |
950 |
|
try { |
951 |
< |
ThreadFactory f = null; |
952 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
951 |
> |
new ThreadPoolExecutor(1, 2, |
952 |
> |
LONG_DELAY_MS, MILLISECONDS, |
953 |
> |
new ArrayBlockingQueue<Runnable>(10), |
954 |
> |
(ThreadFactory) null, |
955 |
> |
new NoOpREHandler()); |
956 |
|
shouldThrow(); |
957 |
|
} catch (NullPointerException success) {} |
958 |
|
} |
959 |
|
|
960 |
+ |
/** |
961 |
+ |
* get of submitted callable throws InterruptedException if interrupted |
962 |
+ |
*/ |
963 |
+ |
public void testInterruptedSubmit() throws InterruptedException { |
964 |
+ |
final ThreadPoolExecutor p = |
965 |
+ |
new ThreadPoolExecutor(1, 1, |
966 |
+ |
60, TimeUnit.SECONDS, |
967 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
968 |
+ |
|
969 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
970 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
971 |
+ |
try { |
972 |
+ |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
973 |
+ |
public void realRun() throws Exception { |
974 |
+ |
Callable task = new CheckedCallable<Boolean>() { |
975 |
+ |
public Boolean realCall() throws InterruptedException { |
976 |
+ |
threadStarted.countDown(); |
977 |
+ |
done.await(); |
978 |
+ |
return Boolean.TRUE; |
979 |
+ |
}}; |
980 |
+ |
p.submit(task).get(); |
981 |
+ |
}}); |
982 |
+ |
|
983 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
984 |
+ |
t.interrupt(); |
985 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
986 |
+ |
} finally { |
987 |
+ |
done.countDown(); |
988 |
+ |
joinPool(p); |
989 |
+ |
} |
990 |
+ |
} |
991 |
|
|
992 |
|
/** |
993 |
< |
* execute throws RejectedExecutionException |
688 |
< |
* if saturated. |
993 |
> |
* execute throws RejectedExecutionException if saturated. |
994 |
|
*/ |
995 |
|
public void testSaturatedExecute() { |
996 |
|
ThreadPoolExecutor p = |
997 |
|
new ThreadPoolExecutor(1, 1, |
998 |
|
LONG_DELAY_MS, MILLISECONDS, |
999 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1000 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1001 |
+ |
try { |
1002 |
+ |
Runnable task = new CheckedRunnable() { |
1003 |
+ |
public void realRun() throws InterruptedException { |
1004 |
+ |
done.await(); |
1005 |
+ |
}}; |
1006 |
+ |
for (int i = 0; i < 2; ++i) |
1007 |
+ |
p.execute(task); |
1008 |
+ |
for (int i = 0; i < 2; ++i) { |
1009 |
+ |
try { |
1010 |
+ |
p.execute(task); |
1011 |
+ |
shouldThrow(); |
1012 |
+ |
} catch (RejectedExecutionException success) {} |
1013 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1014 |
+ |
} |
1015 |
+ |
} finally { |
1016 |
+ |
done.countDown(); |
1017 |
+ |
joinPool(p); |
1018 |
+ |
} |
1019 |
+ |
} |
1020 |
+ |
|
1021 |
+ |
/** |
1022 |
+ |
* submit(runnable) throws RejectedExecutionException if saturated. |
1023 |
+ |
*/ |
1024 |
+ |
public void testSaturatedSubmitRunnable() { |
1025 |
+ |
ThreadPoolExecutor p = |
1026 |
+ |
new ThreadPoolExecutor(1, 1, |
1027 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1028 |
+ |
new ArrayBlockingQueue<Runnable>(1)); |
1029 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1030 |
|
try { |
1031 |
+ |
Runnable task = new CheckedRunnable() { |
1032 |
+ |
public void realRun() throws InterruptedException { |
1033 |
+ |
done.await(); |
1034 |
+ |
}}; |
1035 |
|
for (int i = 0; i < 2; ++i) |
1036 |
< |
p.execute(new MediumRunnable()); |
1036 |
> |
p.submit(task); |
1037 |
|
for (int i = 0; i < 2; ++i) { |
1038 |
|
try { |
1039 |
< |
p.execute(new MediumRunnable()); |
1039 |
> |
p.execute(task); |
1040 |
|
shouldThrow(); |
1041 |
|
} catch (RejectedExecutionException success) {} |
1042 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1043 |
|
} |
1044 |
|
} finally { |
1045 |
+ |
done.countDown(); |
1046 |
|
joinPool(p); |
1047 |
|
} |
1048 |
|
} |
1049 |
|
|
1050 |
|
/** |
1051 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1051 |
> |
* submit(callable) throws RejectedExecutionException if saturated. |
1052 |
> |
*/ |
1053 |
> |
public void testSaturatedSubmitCallable() { |
1054 |
> |
ThreadPoolExecutor p = |
1055 |
> |
new ThreadPoolExecutor(1, 1, |
1056 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1057 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1058 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1059 |
> |
try { |
1060 |
> |
Runnable task = new CheckedRunnable() { |
1061 |
> |
public void realRun() throws InterruptedException { |
1062 |
> |
done.await(); |
1063 |
> |
}}; |
1064 |
> |
for (int i = 0; i < 2; ++i) |
1065 |
> |
p.submit(Executors.callable(task)); |
1066 |
> |
for (int i = 0; i < 2; ++i) { |
1067 |
> |
try { |
1068 |
> |
p.execute(task); |
1069 |
> |
shouldThrow(); |
1070 |
> |
} catch (RejectedExecutionException success) {} |
1071 |
> |
assertTrue(p.getTaskCount() <= 2); |
1072 |
> |
} |
1073 |
> |
} finally { |
1074 |
> |
done.countDown(); |
1075 |
> |
joinPool(p); |
1076 |
> |
} |
1077 |
> |
} |
1078 |
> |
|
1079 |
> |
/** |
1080 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
1081 |
|
*/ |
1082 |
|
public void testSaturatedExecute2() { |
1083 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1084 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1084 |
> |
final ThreadPoolExecutor p = |
1085 |
> |
new ThreadPoolExecutor(1, 1, |
1086 |
> |
LONG_DELAY_MS, |
1087 |
> |
MILLISECONDS, |
1088 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1089 |
> |
h); |
1090 |
|
try { |
716 |
– |
|
1091 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1092 |
< |
for (int i = 0; i < 5; ++i) { |
1092 |
> |
for (int i = 0; i < tasks.length; ++i) |
1093 |
|
tasks[i] = new TrackedNoOpRunnable(); |
720 |
– |
} |
1094 |
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1095 |
|
p.execute(mr); |
1096 |
< |
for (int i = 0; i < 5; ++i) { |
1096 |
> |
for (int i = 0; i < tasks.length; ++i) |
1097 |
|
p.execute(tasks[i]); |
1098 |
< |
} |
726 |
< |
for (int i = 1; i < 5; ++i) { |
1098 |
> |
for (int i = 1; i < tasks.length; ++i) |
1099 |
|
assertTrue(tasks[i].done); |
728 |
– |
} |
1100 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1101 |
|
} finally { |
1102 |
|
joinPool(p); |
1104 |
|
} |
1105 |
|
|
1106 |
|
/** |
1107 |
< |
* executor using DiscardPolicy drops task if saturated. |
1107 |
> |
* executor using DiscardPolicy drops task if saturated. |
1108 |
|
*/ |
1109 |
|
public void testSaturatedExecute3() { |
1110 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1111 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1111 |
> |
final ThreadPoolExecutor p = |
1112 |
> |
new ThreadPoolExecutor(1, 1, |
1113 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1114 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1115 |
> |
h); |
1116 |
|
try { |
742 |
– |
|
1117 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1118 |
< |
for (int i = 0; i < 5; ++i) { |
1118 |
> |
for (int i = 0; i < tasks.length; ++i) |
1119 |
|
tasks[i] = new TrackedNoOpRunnable(); |
746 |
– |
} |
1120 |
|
p.execute(new TrackedLongRunnable()); |
1121 |
< |
for (int i = 0; i < 5; ++i) { |
1122 |
< |
p.execute(tasks[i]); |
1123 |
< |
} |
1124 |
< |
for (int i = 0; i < 5; ++i) { |
752 |
< |
assertFalse(tasks[i].done); |
753 |
< |
} |
1121 |
> |
for (TrackedNoOpRunnable task : tasks) |
1122 |
> |
p.execute(task); |
1123 |
> |
for (TrackedNoOpRunnable task : tasks) |
1124 |
> |
assertFalse(task.done); |
1125 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1126 |
|
} finally { |
1127 |
|
joinPool(p); |
1129 |
|
} |
1130 |
|
|
1131 |
|
/** |
1132 |
< |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1132 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1133 |
|
*/ |
1134 |
|
public void testSaturatedExecute4() { |
1135 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1136 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1136 |
> |
final ThreadPoolExecutor p = |
1137 |
> |
new ThreadPoolExecutor(1, 1, |
1138 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1139 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1140 |
> |
h); |
1141 |
|
try { |
1142 |
|
p.execute(new TrackedLongRunnable()); |
1143 |
|
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1154 |
|
} |
1155 |
|
|
1156 |
|
/** |
1157 |
< |
* execute throws RejectedExecutionException if shutdown |
1157 |
> |
* execute throws RejectedExecutionException if shutdown |
1158 |
|
*/ |
1159 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1160 |
< |
ThreadPoolExecutor tpe = |
1161 |
< |
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1162 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1163 |
< |
try { |
1164 |
< |
tpe.execute(new NoOpRunnable()); |
1165 |
< |
shouldThrow(); |
1166 |
< |
} catch (RejectedExecutionException success) {} |
1160 |
> |
ThreadPoolExecutor p = |
1161 |
> |
new ThreadPoolExecutor(1, 1, |
1162 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1163 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1164 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1165 |
> |
try { |
1166 |
> |
p.execute(new NoOpRunnable()); |
1167 |
> |
shouldThrow(); |
1168 |
> |
} catch (RejectedExecutionException success) {} |
1169 |
|
|
1170 |
< |
joinPool(tpe); |
1170 |
> |
joinPool(p); |
1171 |
|
} |
1172 |
|
|
1173 |
|
/** |
1174 |
< |
* execute using CallerRunsPolicy drops task on shutdown |
1174 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
1175 |
|
*/ |
1176 |
|
public void testCallerRunsOnShutdown() { |
1177 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1178 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1178 |
> |
final ThreadPoolExecutor p = |
1179 |
> |
new ThreadPoolExecutor(1, 1, |
1180 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1181 |
> |
new ArrayBlockingQueue<Runnable>(1), h); |
1182 |
|
|
1183 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1184 |
< |
try { |
1184 |
> |
try { |
1185 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1186 |
< |
p.execute(r); |
1186 |
> |
p.execute(r); |
1187 |
|
assertFalse(r.done); |
1188 |
|
} finally { |
1189 |
|
joinPool(p); |
1191 |
|
} |
1192 |
|
|
1193 |
|
/** |
1194 |
< |
* execute using DiscardPolicy drops task on shutdown |
1194 |
> |
* execute using DiscardPolicy drops task on shutdown |
1195 |
|
*/ |
1196 |
|
public void testDiscardOnShutdown() { |
1197 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1198 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1198 |
> |
ThreadPoolExecutor p = |
1199 |
> |
new ThreadPoolExecutor(1, 1, |
1200 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1201 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1202 |
> |
h); |
1203 |
|
|
1204 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1205 |
< |
try { |
1205 |
> |
try { |
1206 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1207 |
< |
p.execute(r); |
1207 |
> |
p.execute(r); |
1208 |
|
assertFalse(r.done); |
1209 |
|
} finally { |
1210 |
|
joinPool(p); |
1213 |
|
|
1214 |
|
|
1215 |
|
/** |
1216 |
< |
* execute using DiscardOldestPolicy drops task on shutdown |
1216 |
> |
* execute using DiscardOldestPolicy drops task on shutdown |
1217 |
|
*/ |
1218 |
|
public void testDiscardOldestOnShutdown() { |
1219 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1220 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1220 |
> |
ThreadPoolExecutor p = |
1221 |
> |
new ThreadPoolExecutor(1, 1, |
1222 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1223 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1224 |
> |
h); |
1225 |
|
|
1226 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1227 |
< |
try { |
1227 |
> |
try { |
1228 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1229 |
< |
p.execute(r); |
1229 |
> |
p.execute(r); |
1230 |
|
assertFalse(r.done); |
1231 |
|
} finally { |
1232 |
|
joinPool(p); |
1235 |
|
|
1236 |
|
|
1237 |
|
/** |
1238 |
< |
* execute (null) throws NPE |
1238 |
> |
* execute(null) throws NPE |
1239 |
|
*/ |
1240 |
|
public void testExecuteNull() { |
1241 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1241 |
> |
ThreadPoolExecutor p = |
1242 |
> |
new ThreadPoolExecutor(1, 2, |
1243 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1244 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1245 |
|
try { |
1246 |
< |
tpe.execute(null); |
1246 |
> |
p.execute(null); |
1247 |
|
shouldThrow(); |
1248 |
< |
} catch (NullPointerException success) {} |
1248 |
> |
} catch (NullPointerException success) {} |
1249 |
|
|
1250 |
< |
joinPool(tpe); |
1250 |
> |
joinPool(p); |
1251 |
|
} |
1252 |
|
|
1253 |
|
/** |
1254 |
< |
* setCorePoolSize of negative value throws IllegalArgumentException |
1254 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
1255 |
|
*/ |
1256 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1257 |
< |
ThreadPoolExecutor tpe = |
1257 |
> |
ThreadPoolExecutor p = |
1258 |
|
new ThreadPoolExecutor(1, 2, |
1259 |
|
LONG_DELAY_MS, MILLISECONDS, |
1260 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1261 |
< |
try { |
1262 |
< |
tpe.setCorePoolSize(-1); |
1263 |
< |
shouldThrow(); |
1264 |
< |
} catch (IllegalArgumentException success) { |
1261 |
> |
try { |
1262 |
> |
p.setCorePoolSize(-1); |
1263 |
> |
shouldThrow(); |
1264 |
> |
} catch (IllegalArgumentException success) { |
1265 |
|
} finally { |
1266 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1266 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1267 |
|
} |
1268 |
< |
joinPool(tpe); |
1268 |
> |
joinPool(p); |
1269 |
|
} |
1270 |
|
|
1271 |
|
/** |
1272 |
< |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1273 |
< |
* given a value less the core pool size |
1272 |
> |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1273 |
> |
* given a value less the core pool size |
1274 |
|
*/ |
1275 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1276 |
< |
ThreadPoolExecutor tpe = |
1276 |
> |
ThreadPoolExecutor p = |
1277 |
|
new ThreadPoolExecutor(2, 3, |
1278 |
|
LONG_DELAY_MS, MILLISECONDS, |
1279 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1280 |
|
try { |
1281 |
< |
tpe.setMaximumPoolSize(1); |
1281 |
> |
p.setMaximumPoolSize(1); |
1282 |
|
shouldThrow(); |
1283 |
|
} catch (IllegalArgumentException success) { |
1284 |
|
} finally { |
1285 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1285 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1286 |
|
} |
1287 |
< |
joinPool(tpe); |
1287 |
> |
joinPool(p); |
1288 |
|
} |
1289 |
|
|
1290 |
|
/** |
1291 |
< |
* setMaximumPoolSize throws IllegalArgumentException |
1292 |
< |
* if given a negative value |
1291 |
> |
* setMaximumPoolSize throws IllegalArgumentException |
1292 |
> |
* if given a negative value |
1293 |
|
*/ |
1294 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1295 |
< |
ThreadPoolExecutor tpe = |
1295 |
> |
ThreadPoolExecutor p = |
1296 |
|
new ThreadPoolExecutor(2, 3, |
1297 |
|
LONG_DELAY_MS, MILLISECONDS, |
1298 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1299 |
|
try { |
1300 |
< |
tpe.setMaximumPoolSize(-1); |
1300 |
> |
p.setMaximumPoolSize(-1); |
1301 |
|
shouldThrow(); |
1302 |
|
} catch (IllegalArgumentException success) { |
1303 |
|
} finally { |
1304 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1304 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1305 |
|
} |
1306 |
< |
joinPool(tpe); |
1306 |
> |
joinPool(p); |
1307 |
|
} |
1308 |
|
|
1309 |
|
|
1310 |
|
/** |
1311 |
< |
* setKeepAliveTime throws IllegalArgumentException |
1312 |
< |
* when given a negative value |
1311 |
> |
* setKeepAliveTime throws IllegalArgumentException |
1312 |
> |
* when given a negative value |
1313 |
|
*/ |
1314 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1315 |
< |
ThreadPoolExecutor tpe = |
1315 |
> |
ThreadPoolExecutor p = |
1316 |
|
new ThreadPoolExecutor(2, 3, |
1317 |
|
LONG_DELAY_MS, MILLISECONDS, |
1318 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1319 |
< |
try { |
1320 |
< |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
1319 |
> |
try { |
1320 |
> |
p.setKeepAliveTime(-1,MILLISECONDS); |
1321 |
|
shouldThrow(); |
1322 |
|
} catch (IllegalArgumentException success) { |
1323 |
|
} finally { |
1324 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1324 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1325 |
|
} |
1326 |
< |
joinPool(tpe); |
1326 |
> |
joinPool(p); |
1327 |
|
} |
1328 |
|
|
1329 |
|
/** |
1330 |
|
* terminated() is called on termination |
1331 |
|
*/ |
1332 |
|
public void testTerminated() { |
1333 |
< |
ExtendedTPE tpe = new ExtendedTPE(); |
1334 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1335 |
< |
assertTrue(tpe.terminatedCalled); |
1336 |
< |
joinPool(tpe); |
1333 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1334 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1335 |
> |
assertTrue(p.terminatedCalled); |
1336 |
> |
joinPool(p); |
1337 |
|
} |
1338 |
|
|
1339 |
|
/** |
1340 |
|
* beforeExecute and afterExecute are called when executing task |
1341 |
|
*/ |
1342 |
|
public void testBeforeAfter() throws InterruptedException { |
1343 |
< |
ExtendedTPE tpe = new ExtendedTPE(); |
1343 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1344 |
|
try { |
1345 |
|
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1346 |
< |
tpe.execute(r); |
1346 |
> |
p.execute(r); |
1347 |
|
Thread.sleep(SHORT_DELAY_MS); |
1348 |
|
assertTrue(r.done); |
1349 |
< |
assertTrue(tpe.beforeCalled); |
1350 |
< |
assertTrue(tpe.afterCalled); |
1351 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1349 |
> |
assertTrue(p.beforeCalled); |
1350 |
> |
assertTrue(p.afterCalled); |
1351 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1352 |
|
} finally { |
1353 |
< |
joinPool(tpe); |
1353 |
> |
joinPool(p); |
1354 |
|
} |
1355 |
|
} |
1356 |
|
|
1358 |
|
* completed submit of callable returns result |
1359 |
|
*/ |
1360 |
|
public void testSubmitCallable() throws Exception { |
1361 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1361 |
> |
ExecutorService e = |
1362 |
> |
new ThreadPoolExecutor(2, 2, |
1363 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1364 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1365 |
|
try { |
1366 |
|
Future<String> future = e.submit(new StringTask()); |
1367 |
|
String result = future.get(); |
1375 |
|
* completed submit of runnable returns successfully |
1376 |
|
*/ |
1377 |
|
public void testSubmitRunnable() throws Exception { |
1378 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1378 |
> |
ExecutorService e = |
1379 |
> |
new ThreadPoolExecutor(2, 2, |
1380 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1381 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1382 |
|
try { |
1383 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1384 |
|
future.get(); |
1392 |
|
* completed submit of (runnable, result) returns result |
1393 |
|
*/ |
1394 |
|
public void testSubmitRunnable2() throws Exception { |
1395 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1395 |
> |
ExecutorService e = |
1396 |
> |
new ThreadPoolExecutor(2, 2, |
1397 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1398 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1399 |
|
try { |
1400 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1401 |
|
String result = future.get(); |
1410 |
|
* invokeAny(null) throws NPE |
1411 |
|
*/ |
1412 |
|
public void testInvokeAny1() throws Exception { |
1413 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1413 |
> |
ExecutorService e = |
1414 |
> |
new ThreadPoolExecutor(2, 2, |
1415 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1416 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1417 |
|
try { |
1418 |
|
e.invokeAny(null); |
1419 |
|
shouldThrow(); |
1427 |
|
* invokeAny(empty collection) throws IAE |
1428 |
|
*/ |
1429 |
|
public void testInvokeAny2() throws Exception { |
1430 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1430 |
> |
ExecutorService e = |
1431 |
> |
new ThreadPoolExecutor(2, 2, |
1432 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1433 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1434 |
|
try { |
1435 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1436 |
|
shouldThrow(); |
1445 |
|
*/ |
1446 |
|
public void testInvokeAny3() throws Exception { |
1447 |
|
final CountDownLatch latch = new CountDownLatch(1); |
1448 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1448 |
> |
final ExecutorService e = |
1449 |
> |
new ThreadPoolExecutor(2, 2, |
1450 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1451 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1452 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1453 |
> |
l.add(latchAwaitingStringTask(latch)); |
1454 |
> |
l.add(null); |
1455 |
|
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); |
1456 |
|
e.invokeAny(l); |
1457 |
|
shouldThrow(); |
1458 |
|
} catch (NullPointerException success) { |
1466 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1467 |
|
*/ |
1468 |
|
public void testInvokeAny4() throws Exception { |
1469 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1469 |
> |
ExecutorService e = |
1470 |
> |
new ThreadPoolExecutor(2, 2, |
1471 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1472 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1473 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1474 |
> |
l.add(new NPETask()); |
1475 |
|
try { |
1068 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1069 |
– |
l.add(new NPETask()); |
1476 |
|
e.invokeAny(l); |
1477 |
|
shouldThrow(); |
1478 |
|
} catch (ExecutionException success) { |
1486 |
|
* invokeAny(c) returns result of some task |
1487 |
|
*/ |
1488 |
|
public void testInvokeAny5() throws Exception { |
1489 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1489 |
> |
ExecutorService e = |
1490 |
> |
new ThreadPoolExecutor(2, 2, |
1491 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1492 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1493 |
|
try { |
1494 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1494 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1495 |
|
l.add(new StringTask()); |
1496 |
|
l.add(new StringTask()); |
1497 |
|
String result = e.invokeAny(l); |
1505 |
|
* invokeAll(null) throws NPE |
1506 |
|
*/ |
1507 |
|
public void testInvokeAll1() throws Exception { |
1508 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1508 |
> |
ExecutorService e = |
1509 |
> |
new ThreadPoolExecutor(2, 2, |
1510 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1511 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1512 |
|
try { |
1513 |
|
e.invokeAll(null); |
1514 |
|
shouldThrow(); |
1522 |
|
* invokeAll(empty collection) returns empty collection |
1523 |
|
*/ |
1524 |
|
public void testInvokeAll2() throws InterruptedException { |
1525 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1525 |
> |
ExecutorService e = |
1526 |
> |
new ThreadPoolExecutor(2, 2, |
1527 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1528 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1529 |
|
try { |
1530 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1531 |
|
assertTrue(r.isEmpty()); |
1538 |
|
* invokeAll(c) throws NPE if c has null elements |
1539 |
|
*/ |
1540 |
|
public void testInvokeAll3() throws Exception { |
1541 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1541 |
> |
ExecutorService e = |
1542 |
> |
new ThreadPoolExecutor(2, 2, |
1543 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1544 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1545 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1546 |
> |
l.add(new StringTask()); |
1547 |
> |
l.add(null); |
1548 |
|
try { |
1128 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1129 |
– |
l.add(new StringTask()); |
1130 |
– |
l.add(null); |
1549 |
|
e.invokeAll(l); |
1550 |
|
shouldThrow(); |
1551 |
|
} catch (NullPointerException success) { |
1558 |
|
* get of element of invokeAll(c) throws exception on failed task |
1559 |
|
*/ |
1560 |
|
public void testInvokeAll4() throws Exception { |
1561 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1561 |
> |
ExecutorService e = |
1562 |
> |
new ThreadPoolExecutor(2, 2, |
1563 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1564 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1565 |
|
try { |
1566 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1566 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1567 |
|
l.add(new NPETask()); |
1568 |
< |
List<Future<String>> result = e.invokeAll(l); |
1569 |
< |
assertEquals(1, result.size()); |
1570 |
< |
for (Future<String> future : result) { |
1571 |
< |
try { |
1572 |
< |
future.get(); |
1573 |
< |
shouldThrow(); |
1574 |
< |
} catch (ExecutionException success) { |
1154 |
< |
Throwable cause = success.getCause(); |
1155 |
< |
assertTrue(cause instanceof NullPointerException); |
1156 |
< |
} |
1568 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1569 |
> |
assertEquals(1, futures.size()); |
1570 |
> |
try { |
1571 |
> |
futures.get(0).get(); |
1572 |
> |
shouldThrow(); |
1573 |
> |
} catch (ExecutionException success) { |
1574 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1575 |
|
} |
1576 |
|
} finally { |
1577 |
|
joinPool(e); |
1582 |
|
* invokeAll(c) returns results of all completed tasks |
1583 |
|
*/ |
1584 |
|
public void testInvokeAll5() throws Exception { |
1585 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1585 |
> |
ExecutorService e = |
1586 |
> |
new ThreadPoolExecutor(2, 2, |
1587 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1588 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1589 |
|
try { |
1590 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1590 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1591 |
|
l.add(new StringTask()); |
1592 |
|
l.add(new StringTask()); |
1593 |
< |
List<Future<String>> result = e.invokeAll(l); |
1594 |
< |
assertEquals(2, result.size()); |
1595 |
< |
for (Future<String> future : result) |
1593 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1594 |
> |
assertEquals(2, futures.size()); |
1595 |
> |
for (Future<String> future : futures) |
1596 |
|
assertSame(TEST_STRING, future.get()); |
1597 |
|
} finally { |
1598 |
|
joinPool(e); |
1605 |
|
* timed invokeAny(null) throws NPE |
1606 |
|
*/ |
1607 |
|
public void testTimedInvokeAny1() throws Exception { |
1608 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1608 |
> |
ExecutorService e = |
1609 |
> |
new ThreadPoolExecutor(2, 2, |
1610 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1611 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1612 |
|
try { |
1613 |
|
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1614 |
|
shouldThrow(); |
1622 |
|
* timed invokeAny(,,null) throws NPE |
1623 |
|
*/ |
1624 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1625 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1625 |
> |
ExecutorService e = |
1626 |
> |
new ThreadPoolExecutor(2, 2, |
1627 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1628 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1629 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1630 |
> |
l.add(new StringTask()); |
1631 |
|
try { |
1203 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1204 |
– |
l.add(new StringTask()); |
1632 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1633 |
|
shouldThrow(); |
1634 |
|
} catch (NullPointerException success) { |
1641 |
|
* timed invokeAny(empty collection) throws IAE |
1642 |
|
*/ |
1643 |
|
public void testTimedInvokeAny2() throws Exception { |
1644 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1644 |
> |
ExecutorService e = |
1645 |
> |
new ThreadPoolExecutor(2, 2, |
1646 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1647 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1648 |
|
try { |
1649 |
|
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1650 |
|
shouldThrow(); |
1659 |
|
*/ |
1660 |
|
public void testTimedInvokeAny3() throws Exception { |
1661 |
|
final CountDownLatch latch = new CountDownLatch(1); |
1662 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1662 |
> |
final ExecutorService e = |
1663 |
> |
new ThreadPoolExecutor(2, 2, |
1664 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1665 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1666 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1667 |
> |
l.add(latchAwaitingStringTask(latch)); |
1668 |
> |
l.add(null); |
1669 |
|
try { |
1234 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1235 |
– |
l.add(new Callable<String>() { |
1236 |
– |
public String call() { |
1237 |
– |
try { |
1238 |
– |
latch.await(); |
1239 |
– |
} catch (InterruptedException ok) {} |
1240 |
– |
return TEST_STRING; |
1241 |
– |
}}); |
1242 |
– |
l.add(null); |
1670 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1671 |
|
shouldThrow(); |
1672 |
|
} catch (NullPointerException success) { |
1680 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1681 |
|
*/ |
1682 |
|
public void testTimedInvokeAny4() throws Exception { |
1683 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1683 |
> |
ExecutorService e = |
1684 |
> |
new ThreadPoolExecutor(2, 2, |
1685 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1686 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1687 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1688 |
> |
l.add(new NPETask()); |
1689 |
|
try { |
1258 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1259 |
– |
l.add(new NPETask()); |
1690 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1691 |
|
shouldThrow(); |
1692 |
|
} catch (ExecutionException success) { |
1700 |
|
* timed invokeAny(c) returns result of some task |
1701 |
|
*/ |
1702 |
|
public void testTimedInvokeAny5() throws Exception { |
1703 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1703 |
> |
ExecutorService e = |
1704 |
> |
new ThreadPoolExecutor(2, 2, |
1705 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1706 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1707 |
|
try { |
1708 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1708 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1709 |
|
l.add(new StringTask()); |
1710 |
|
l.add(new StringTask()); |
1711 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1719 |
|
* timed invokeAll(null) throws NPE |
1720 |
|
*/ |
1721 |
|
public void testTimedInvokeAll1() throws Exception { |
1722 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1722 |
> |
ExecutorService e = |
1723 |
> |
new ThreadPoolExecutor(2, 2, |
1724 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1725 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1726 |
|
try { |
1727 |
|
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1728 |
|
shouldThrow(); |
1736 |
|
* timed invokeAll(,,null) throws NPE |
1737 |
|
*/ |
1738 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1739 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1739 |
> |
ExecutorService e = |
1740 |
> |
new ThreadPoolExecutor(2, 2, |
1741 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1742 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1743 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1744 |
> |
l.add(new StringTask()); |
1745 |
|
try { |
1305 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1306 |
– |
l.add(new StringTask()); |
1746 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1747 |
|
shouldThrow(); |
1748 |
|
} catch (NullPointerException success) { |
1755 |
|
* timed invokeAll(empty collection) returns empty collection |
1756 |
|
*/ |
1757 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1758 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1758 |
> |
ExecutorService e = |
1759 |
> |
new ThreadPoolExecutor(2, 2, |
1760 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1761 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1762 |
|
try { |
1763 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1764 |
|
assertTrue(r.isEmpty()); |
1771 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1772 |
|
*/ |
1773 |
|
public void testTimedInvokeAll3() throws Exception { |
1774 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1774 |
> |
ExecutorService e = |
1775 |
> |
new ThreadPoolExecutor(2, 2, |
1776 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1777 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1778 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1779 |
> |
l.add(new StringTask()); |
1780 |
> |
l.add(null); |
1781 |
|
try { |
1334 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1335 |
– |
l.add(new StringTask()); |
1336 |
– |
l.add(null); |
1782 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1783 |
|
shouldThrow(); |
1784 |
|
} catch (NullPointerException success) { |
1791 |
|
* get of element of invokeAll(c) throws exception on failed task |
1792 |
|
*/ |
1793 |
|
public void testTimedInvokeAll4() throws Exception { |
1794 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1794 |
> |
ExecutorService e = |
1795 |
> |
new ThreadPoolExecutor(2, 2, |
1796 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1797 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1798 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1799 |
> |
l.add(new NPETask()); |
1800 |
> |
List<Future<String>> futures = |
1801 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1802 |
> |
assertEquals(1, futures.size()); |
1803 |
|
try { |
1804 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1352 |
< |
l.add(new NPETask()); |
1353 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1354 |
< |
assertEquals(1, result.size()); |
1355 |
< |
for (Future<String> future : result) |
1356 |
< |
future.get(); |
1804 |
> |
futures.get(0).get(); |
1805 |
|
shouldThrow(); |
1806 |
|
} catch (ExecutionException success) { |
1807 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1814 |
|
* timed invokeAll(c) returns results of all completed tasks |
1815 |
|
*/ |
1816 |
|
public void testTimedInvokeAll5() throws Exception { |
1817 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1817 |
> |
ExecutorService e = |
1818 |
> |
new ThreadPoolExecutor(2, 2, |
1819 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1820 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1821 |
|
try { |
1822 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1822 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1823 |
|
l.add(new StringTask()); |
1824 |
|
l.add(new StringTask()); |
1825 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1826 |
< |
assertEquals(2, result.size()); |
1827 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1828 |
< |
assertSame(TEST_STRING, it.next().get()); |
1825 |
> |
List<Future<String>> futures = |
1826 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1827 |
> |
assertEquals(2, futures.size()); |
1828 |
> |
for (Future<String> future : futures) |
1829 |
> |
assertSame(TEST_STRING, future.get()); |
1830 |
|
} finally { |
1831 |
|
joinPool(e); |
1832 |
|
} |
1836 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1837 |
|
*/ |
1838 |
|
public void testTimedInvokeAll6() throws Exception { |
1839 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1839 |
> |
ExecutorService e = |
1840 |
> |
new ThreadPoolExecutor(2, 2, |
1841 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1842 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1843 |
|
try { |
1844 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1844 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1845 |
|
l.add(new StringTask()); |
1846 |
|
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1847 |
|
l.add(new StringTask()); |
1848 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1849 |
< |
assertEquals(3, result.size()); |
1850 |
< |
Iterator<Future<String>> it = result.iterator(); |
1848 |
> |
List<Future<String>> futures = |
1849 |
> |
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1850 |
> |
assertEquals(3, futures.size()); |
1851 |
> |
Iterator<Future<String>> it = futures.iterator(); |
1852 |
|
Future<String> f1 = it.next(); |
1853 |
|
Future<String> f2 = it.next(); |
1854 |
|
Future<String> f3 = it.next(); |
1867 |
|
* thread factory fails to create more |
1868 |
|
*/ |
1869 |
|
public void testFailingThreadFactory() throws InterruptedException { |
1870 |
< |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1870 |
> |
final ExecutorService e = |
1871 |
> |
new ThreadPoolExecutor(100, 100, |
1872 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1873 |
> |
new LinkedBlockingQueue<Runnable>(), |
1874 |
> |
new FailingThreadFactory()); |
1875 |
|
try { |
1876 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1877 |
< |
for (int k = 0; k < 100; ++k) { |
1878 |
< |
e.execute(new NoOpRunnable()); |
1879 |
< |
} |
1880 |
< |
Thread.sleep(LONG_DELAY_MS); |
1876 |
> |
final int TASKS = 100; |
1877 |
> |
final CountDownLatch done = new CountDownLatch(TASKS); |
1878 |
> |
for (int k = 0; k < TASKS; ++k) |
1879 |
> |
e.execute(new CheckedRunnable() { |
1880 |
> |
public void realRun() { |
1881 |
> |
done.countDown(); |
1882 |
> |
}}); |
1883 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1884 |
|
} finally { |
1885 |
|
joinPool(e); |
1886 |
|
} |
1890 |
|
* allowsCoreThreadTimeOut is by default false. |
1891 |
|
*/ |
1892 |
|
public void testAllowsCoreThreadTimeOut() { |
1893 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1894 |
< |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1895 |
< |
joinPool(tpe); |
1893 |
> |
final ThreadPoolExecutor p = |
1894 |
> |
new ThreadPoolExecutor(2, 2, |
1895 |
> |
1000, MILLISECONDS, |
1896 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1897 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1898 |
> |
joinPool(p); |
1899 |
|
} |
1900 |
|
|
1901 |
|
/** |
1902 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1903 |
|
*/ |
1904 |
< |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1905 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1906 |
< |
tpe.allowCoreThreadTimeOut(true); |
1907 |
< |
tpe.execute(new NoOpRunnable()); |
1904 |
> |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1905 |
> |
final ThreadPoolExecutor p = |
1906 |
> |
new ThreadPoolExecutor(2, 10, |
1907 |
> |
SHORT_DELAY_MS, MILLISECONDS, |
1908 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1909 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1910 |
|
try { |
1911 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1912 |
< |
assertEquals(0, tpe.getPoolSize()); |
1911 |
> |
p.allowCoreThreadTimeOut(true); |
1912 |
> |
p.execute(new CheckedRunnable() { |
1913 |
> |
public void realRun() throws InterruptedException { |
1914 |
> |
threadStarted.countDown(); |
1915 |
> |
assertEquals(1, p.getPoolSize()); |
1916 |
> |
}}); |
1917 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1918 |
> |
for (int i = 0; i < (MEDIUM_DELAY_MS/10); i++) { |
1919 |
> |
if (p.getPoolSize() == 0) |
1920 |
> |
break; |
1921 |
> |
Thread.sleep(10); |
1922 |
> |
} |
1923 |
> |
assertEquals(0, p.getPoolSize()); |
1924 |
|
} finally { |
1925 |
< |
joinPool(tpe); |
1925 |
> |
joinPool(p); |
1926 |
|
} |
1927 |
|
} |
1928 |
|
|
1929 |
|
/** |
1930 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1931 |
|
*/ |
1932 |
< |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1933 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1934 |
< |
tpe.allowCoreThreadTimeOut(false); |
1935 |
< |
tpe.execute(new NoOpRunnable()); |
1932 |
> |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1933 |
> |
final ThreadPoolExecutor p = |
1934 |
> |
new ThreadPoolExecutor(2, 10, |
1935 |
> |
SHORT_DELAY_MS, MILLISECONDS, |
1936 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1937 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1938 |
|
try { |
1939 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1940 |
< |
assertTrue(tpe.getPoolSize() >= 1); |
1939 |
> |
p.allowCoreThreadTimeOut(false); |
1940 |
> |
p.execute(new CheckedRunnable() { |
1941 |
> |
public void realRun() throws InterruptedException { |
1942 |
> |
threadStarted.countDown(); |
1943 |
> |
assertTrue(p.getPoolSize() >= 1); |
1944 |
> |
}}); |
1945 |
> |
Thread.sleep(SMALL_DELAY_MS); |
1946 |
> |
assertTrue(p.getPoolSize() >= 1); |
1947 |
|
} finally { |
1948 |
< |
joinPool(tpe); |
1948 |
> |
joinPool(p); |
1949 |
|
} |
1950 |
|
} |
1951 |
|
|
1955 |
|
*/ |
1956 |
|
public void testRejectedRecycledTask() throws InterruptedException { |
1957 |
|
final int nTasks = 1000; |
1958 |
< |
final AtomicInteger nRun = new AtomicInteger(0); |
1958 |
> |
final CountDownLatch done = new CountDownLatch(nTasks); |
1959 |
|
final Runnable recycledTask = new Runnable() { |
1960 |
< |
public void run() { |
1961 |
< |
nRun.getAndIncrement(); |
1962 |
< |
} }; |
1960 |
> |
public void run() { |
1961 |
> |
done.countDown(); |
1962 |
> |
}}; |
1963 |
|
final ThreadPoolExecutor p = |
1964 |
|
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
1965 |
|
new ArrayBlockingQueue(30)); |
1970 |
|
p.execute(recycledTask); |
1971 |
|
break; |
1972 |
|
} |
1973 |
< |
catch (RejectedExecutionException ignore) { |
1487 |
< |
} |
1973 |
> |
catch (RejectedExecutionException ignore) {} |
1974 |
|
} |
1975 |
|
} |
1976 |
< |
Thread.sleep(5000); // enough time to run all tasks |
1977 |
< |
assertEquals(nRun.get(), nTasks); |
1976 |
> |
// enough time to run all tasks |
1977 |
> |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
1978 |
|
} finally { |
1979 |
|
p.shutdown(); |
1980 |
|
} |