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 |
|
*/ |
8 |
|
|
9 |
– |
import java.util.concurrent.*; |
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
< |
import java.util.concurrent.atomic.*; |
11 |
< |
import junit.framework.*; |
12 |
< |
import java.util.*; |
10 |
> |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
> |
|
12 |
> |
import java.util.ArrayList; |
13 |
> |
import java.util.List; |
14 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
15 |
> |
import java.util.concurrent.BlockingQueue; |
16 |
> |
import java.util.concurrent.Callable; |
17 |
> |
import java.util.concurrent.CountDownLatch; |
18 |
> |
import java.util.concurrent.ExecutionException; |
19 |
> |
import java.util.concurrent.Executors; |
20 |
> |
import java.util.concurrent.ExecutorService; |
21 |
> |
import java.util.concurrent.Future; |
22 |
> |
import java.util.concurrent.FutureTask; |
23 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
24 |
> |
import java.util.concurrent.RejectedExecutionException; |
25 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
26 |
> |
import java.util.concurrent.SynchronousQueue; |
27 |
> |
import java.util.concurrent.ThreadFactory; |
28 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
29 |
> |
import java.util.concurrent.TimeUnit; |
30 |
> |
|
31 |
> |
import junit.framework.Test; |
32 |
> |
import junit.framework.TestSuite; |
33 |
|
|
34 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
35 |
|
public static void main(String[] args) { |
36 |
< |
junit.textui.TestRunner.run(suite()); |
36 |
> |
main(suite(), args); |
37 |
|
} |
38 |
|
public static Test suite() { |
39 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
40 |
|
} |
41 |
|
|
42 |
|
static class ExtendedTPE extends ThreadPoolExecutor { |
43 |
< |
volatile boolean beforeCalled = false; |
44 |
< |
volatile boolean afterCalled = false; |
45 |
< |
volatile boolean terminatedCalled = false; |
43 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
44 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
45 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
46 |
> |
|
47 |
|
public ExtendedTPE() { |
48 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
49 |
|
} |
50 |
|
protected void beforeExecute(Thread t, Runnable r) { |
51 |
< |
beforeCalled = true; |
51 |
> |
beforeCalled.countDown(); |
52 |
|
} |
53 |
|
protected void afterExecute(Runnable r, Throwable t) { |
54 |
< |
afterCalled = true; |
54 |
> |
afterCalled.countDown(); |
55 |
|
} |
56 |
|
protected void terminated() { |
57 |
< |
terminatedCalled = true; |
57 |
> |
terminatedCalled.countDown(); |
58 |
> |
} |
59 |
> |
|
60 |
> |
public boolean beforeCalled() { |
61 |
> |
return beforeCalled.getCount() == 0; |
62 |
> |
} |
63 |
> |
public boolean afterCalled() { |
64 |
> |
return afterCalled.getCount() == 0; |
65 |
> |
} |
66 |
> |
public boolean terminatedCalled() { |
67 |
> |
return terminatedCalled.getCount() == 0; |
68 |
|
} |
69 |
|
} |
70 |
|
|
76 |
|
} |
77 |
|
} |
78 |
|
|
49 |
– |
|
79 |
|
/** |
80 |
|
* execute successfully executes a runnable |
81 |
|
*/ |
82 |
|
public void testExecute() throws InterruptedException { |
83 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
83 |
> |
final ThreadPoolExecutor p = |
84 |
> |
new ThreadPoolExecutor(1, 1, |
85 |
> |
LONG_DELAY_MS, MILLISECONDS, |
86 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
87 |
> |
final CountDownLatch done = new CountDownLatch(1); |
88 |
> |
final Runnable task = new CheckedRunnable() { |
89 |
> |
public void realRun() { |
90 |
> |
done.countDown(); |
91 |
> |
}}; |
92 |
|
try { |
93 |
< |
p1.execute(new ShortRunnable()); |
94 |
< |
Thread.sleep(SMALL_DELAY_MS); |
93 |
> |
p.execute(task); |
94 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
95 |
|
} finally { |
96 |
< |
joinPool(p1); |
96 |
> |
joinPool(p); |
97 |
|
} |
98 |
|
} |
99 |
|
|
102 |
|
* thread becomes active |
103 |
|
*/ |
104 |
|
public void testGetActiveCount() throws InterruptedException { |
105 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
106 |
< |
assertEquals(0, p2.getActiveCount()); |
107 |
< |
p2.execute(new MediumRunnable()); |
108 |
< |
Thread.sleep(SHORT_DELAY_MS); |
109 |
< |
assertEquals(1, p2.getActiveCount()); |
110 |
< |
joinPool(p2); |
105 |
> |
final ThreadPoolExecutor p = |
106 |
> |
new ThreadPoolExecutor(2, 2, |
107 |
> |
LONG_DELAY_MS, MILLISECONDS, |
108 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
109 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
110 |
> |
final CountDownLatch done = new CountDownLatch(1); |
111 |
> |
try { |
112 |
> |
assertEquals(0, p.getActiveCount()); |
113 |
> |
p.execute(new CheckedRunnable() { |
114 |
> |
public void realRun() throws InterruptedException { |
115 |
> |
threadStarted.countDown(); |
116 |
> |
assertEquals(1, p.getActiveCount()); |
117 |
> |
done.await(); |
118 |
> |
}}); |
119 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
120 |
> |
assertEquals(1, p.getActiveCount()); |
121 |
> |
} finally { |
122 |
> |
done.countDown(); |
123 |
> |
joinPool(p); |
124 |
> |
} |
125 |
|
} |
126 |
|
|
127 |
|
/** |
128 |
|
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
129 |
|
*/ |
130 |
|
public void testPrestartCoreThread() { |
131 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
132 |
< |
assertEquals(0, p2.getPoolSize()); |
133 |
< |
assertTrue(p2.prestartCoreThread()); |
134 |
< |
assertEquals(1, p2.getPoolSize()); |
135 |
< |
assertTrue(p2.prestartCoreThread()); |
136 |
< |
assertEquals(2, p2.getPoolSize()); |
137 |
< |
assertFalse(p2.prestartCoreThread()); |
138 |
< |
assertEquals(2, p2.getPoolSize()); |
139 |
< |
joinPool(p2); |
131 |
> |
final ThreadPoolExecutor p = |
132 |
> |
new ThreadPoolExecutor(2, 2, |
133 |
> |
LONG_DELAY_MS, MILLISECONDS, |
134 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
135 |
> |
assertEquals(0, p.getPoolSize()); |
136 |
> |
assertTrue(p.prestartCoreThread()); |
137 |
> |
assertEquals(1, p.getPoolSize()); |
138 |
> |
assertTrue(p.prestartCoreThread()); |
139 |
> |
assertEquals(2, p.getPoolSize()); |
140 |
> |
assertFalse(p.prestartCoreThread()); |
141 |
> |
assertEquals(2, p.getPoolSize()); |
142 |
> |
joinPool(p); |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
|
* prestartAllCoreThreads starts all corePoolSize threads |
147 |
|
*/ |
148 |
|
public void testPrestartAllCoreThreads() { |
149 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
150 |
< |
assertEquals(0, p2.getPoolSize()); |
151 |
< |
p2.prestartAllCoreThreads(); |
152 |
< |
assertEquals(2, p2.getPoolSize()); |
153 |
< |
p2.prestartAllCoreThreads(); |
154 |
< |
assertEquals(2, p2.getPoolSize()); |
155 |
< |
joinPool(p2); |
149 |
> |
final ThreadPoolExecutor p = |
150 |
> |
new ThreadPoolExecutor(2, 2, |
151 |
> |
LONG_DELAY_MS, MILLISECONDS, |
152 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
153 |
> |
assertEquals(0, p.getPoolSize()); |
154 |
> |
p.prestartAllCoreThreads(); |
155 |
> |
assertEquals(2, p.getPoolSize()); |
156 |
> |
p.prestartAllCoreThreads(); |
157 |
> |
assertEquals(2, p.getPoolSize()); |
158 |
> |
joinPool(p); |
159 |
|
} |
160 |
|
|
161 |
|
/** |
163 |
|
* when tasks complete |
164 |
|
*/ |
165 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
166 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
167 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
168 |
< |
p2.execute(new ShortRunnable()); |
169 |
< |
Thread.sleep(SMALL_DELAY_MS); |
170 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
171 |
< |
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
172 |
< |
joinPool(p2); |
166 |
> |
final ThreadPoolExecutor p = |
167 |
> |
new ThreadPoolExecutor(2, 2, |
168 |
> |
LONG_DELAY_MS, MILLISECONDS, |
169 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
170 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
171 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
172 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
173 |
> |
try { |
174 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
175 |
> |
p.execute(new CheckedRunnable() { |
176 |
> |
public void realRun() throws InterruptedException { |
177 |
> |
threadStarted.countDown(); |
178 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
179 |
> |
threadProceed.await(); |
180 |
> |
threadDone.countDown(); |
181 |
> |
}}); |
182 |
> |
await(threadStarted); |
183 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
184 |
> |
threadProceed.countDown(); |
185 |
> |
threadDone.await(); |
186 |
> |
long startTime = System.nanoTime(); |
187 |
> |
while (p.getCompletedTaskCount() != 1) { |
188 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
189 |
> |
fail("timed out"); |
190 |
> |
Thread.yield(); |
191 |
> |
} |
192 |
> |
} finally { |
193 |
> |
joinPool(p); |
194 |
> |
} |
195 |
|
} |
196 |
|
|
197 |
|
/** |
198 |
|
* getCorePoolSize returns size given in constructor if not otherwise set |
199 |
|
*/ |
200 |
|
public void testGetCorePoolSize() { |
201 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
202 |
< |
assertEquals(1, p1.getCorePoolSize()); |
203 |
< |
joinPool(p1); |
201 |
> |
final ThreadPoolExecutor p = |
202 |
> |
new ThreadPoolExecutor(1, 1, |
203 |
> |
LONG_DELAY_MS, MILLISECONDS, |
204 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
205 |
> |
assertEquals(1, p.getCorePoolSize()); |
206 |
> |
joinPool(p); |
207 |
|
} |
208 |
|
|
209 |
|
/** |
210 |
|
* getKeepAliveTime returns value given in constructor if not otherwise set |
211 |
|
*/ |
212 |
|
public void testGetKeepAliveTime() { |
213 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
214 |
< |
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
215 |
< |
joinPool(p2); |
213 |
> |
final ThreadPoolExecutor p = |
214 |
> |
new ThreadPoolExecutor(2, 2, |
215 |
> |
1000, MILLISECONDS, |
216 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
217 |
> |
assertEquals(1, p.getKeepAliveTime(TimeUnit.SECONDS)); |
218 |
> |
joinPool(p); |
219 |
|
} |
220 |
|
|
136 |
– |
|
221 |
|
/** |
222 |
|
* getThreadFactory returns factory in constructor if not set |
223 |
|
*/ |
224 |
|
public void testGetThreadFactory() { |
225 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
226 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
226 |
> |
final ThreadPoolExecutor p = |
227 |
> |
new ThreadPoolExecutor(1, 2, |
228 |
> |
LONG_DELAY_MS, MILLISECONDS, |
229 |
> |
new ArrayBlockingQueue<Runnable>(10), |
230 |
> |
tf, |
231 |
> |
new NoOpREHandler()); |
232 |
|
assertSame(tf, p.getThreadFactory()); |
233 |
|
joinPool(p); |
234 |
|
} |
237 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
238 |
|
*/ |
239 |
|
public void testSetThreadFactory() { |
240 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
240 |
> |
final ThreadPoolExecutor p = |
241 |
> |
new ThreadPoolExecutor(1, 2, |
242 |
> |
LONG_DELAY_MS, MILLISECONDS, |
243 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
244 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
245 |
|
p.setThreadFactory(tf); |
246 |
|
assertSame(tf, p.getThreadFactory()); |
247 |
|
joinPool(p); |
248 |
|
} |
249 |
|
|
158 |
– |
|
250 |
|
/** |
251 |
|
* setThreadFactory(null) throws NPE |
252 |
|
*/ |
253 |
|
public void testSetThreadFactoryNull() { |
254 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
254 |
> |
final ThreadPoolExecutor p = |
255 |
> |
new ThreadPoolExecutor(1, 2, |
256 |
> |
LONG_DELAY_MS, MILLISECONDS, |
257 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
258 |
|
try { |
259 |
|
p.setThreadFactory(null); |
260 |
|
shouldThrow(); |
268 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
269 |
|
*/ |
270 |
|
public void testGetRejectedExecutionHandler() { |
271 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
272 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
271 |
> |
final RejectedExecutionHandler h = new NoOpREHandler(); |
272 |
> |
final ThreadPoolExecutor p = |
273 |
> |
new ThreadPoolExecutor(1, 2, |
274 |
> |
LONG_DELAY_MS, MILLISECONDS, |
275 |
> |
new ArrayBlockingQueue<Runnable>(10), |
276 |
> |
h); |
277 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
278 |
|
joinPool(p); |
279 |
|
} |
283 |
|
* getRejectedExecutionHandler |
284 |
|
*/ |
285 |
|
public void testSetRejectedExecutionHandler() { |
286 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
286 |
> |
final ThreadPoolExecutor p = |
287 |
> |
new ThreadPoolExecutor(1, 2, |
288 |
> |
LONG_DELAY_MS, MILLISECONDS, |
289 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
290 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
291 |
|
p.setRejectedExecutionHandler(h); |
292 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
293 |
|
joinPool(p); |
294 |
|
} |
295 |
|
|
195 |
– |
|
296 |
|
/** |
297 |
|
* setRejectedExecutionHandler(null) throws NPE |
298 |
|
*/ |
299 |
|
public void testSetRejectedExecutionHandlerNull() { |
300 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
300 |
> |
final ThreadPoolExecutor p = |
301 |
> |
new ThreadPoolExecutor(1, 2, |
302 |
> |
LONG_DELAY_MS, MILLISECONDS, |
303 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
304 |
|
try { |
305 |
|
p.setRejectedExecutionHandler(null); |
306 |
|
shouldThrow(); |
310 |
|
} |
311 |
|
} |
312 |
|
|
210 |
– |
|
313 |
|
/** |
314 |
|
* getLargestPoolSize increases, but doesn't overestimate, when |
315 |
|
* multiple threads active |
316 |
|
*/ |
317 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
318 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
319 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
320 |
< |
p2.execute(new MediumRunnable()); |
321 |
< |
p2.execute(new MediumRunnable()); |
322 |
< |
Thread.sleep(SHORT_DELAY_MS); |
323 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
324 |
< |
joinPool(p2); |
318 |
> |
final int THREADS = 3; |
319 |
> |
final ThreadPoolExecutor p = |
320 |
> |
new ThreadPoolExecutor(THREADS, THREADS, |
321 |
> |
LONG_DELAY_MS, MILLISECONDS, |
322 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
323 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
324 |
> |
final CountDownLatch done = new CountDownLatch(1); |
325 |
> |
try { |
326 |
> |
assertEquals(0, p.getLargestPoolSize()); |
327 |
> |
for (int i = 0; i < THREADS; i++) |
328 |
> |
p.execute(new CheckedRunnable() { |
329 |
> |
public void realRun() throws InterruptedException { |
330 |
> |
threadsStarted.countDown(); |
331 |
> |
done.await(); |
332 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
333 |
> |
}}); |
334 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
335 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
336 |
> |
} finally { |
337 |
> |
done.countDown(); |
338 |
> |
joinPool(p); |
339 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
340 |
> |
} |
341 |
|
} |
342 |
|
|
343 |
|
/** |
345 |
|
* otherwise set |
346 |
|
*/ |
347 |
|
public void testGetMaximumPoolSize() { |
348 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
349 |
< |
assertEquals(2, p2.getMaximumPoolSize()); |
350 |
< |
joinPool(p2); |
348 |
> |
final ThreadPoolExecutor p = |
349 |
> |
new ThreadPoolExecutor(2, 3, |
350 |
> |
LONG_DELAY_MS, MILLISECONDS, |
351 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
352 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
353 |
> |
joinPool(p); |
354 |
|
} |
355 |
|
|
356 |
|
/** |
357 |
|
* getPoolSize increases, but doesn't overestimate, when threads |
358 |
|
* become active |
359 |
|
*/ |
360 |
< |
public void testGetPoolSize() { |
361 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
362 |
< |
assertEquals(0, p1.getPoolSize()); |
363 |
< |
p1.execute(new MediumRunnable()); |
364 |
< |
assertEquals(1, p1.getPoolSize()); |
365 |
< |
joinPool(p1); |
360 |
> |
public void testGetPoolSize() throws InterruptedException { |
361 |
> |
final ThreadPoolExecutor p = |
362 |
> |
new ThreadPoolExecutor(1, 1, |
363 |
> |
LONG_DELAY_MS, MILLISECONDS, |
364 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
365 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
366 |
> |
final CountDownLatch done = new CountDownLatch(1); |
367 |
> |
try { |
368 |
> |
assertEquals(0, p.getPoolSize()); |
369 |
> |
p.execute(new CheckedRunnable() { |
370 |
> |
public void realRun() throws InterruptedException { |
371 |
> |
threadStarted.countDown(); |
372 |
> |
assertEquals(1, p.getPoolSize()); |
373 |
> |
done.await(); |
374 |
> |
}}); |
375 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
376 |
> |
assertEquals(1, p.getPoolSize()); |
377 |
> |
} finally { |
378 |
> |
done.countDown(); |
379 |
> |
joinPool(p); |
380 |
> |
} |
381 |
|
} |
382 |
|
|
383 |
|
/** |
384 |
|
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
385 |
|
*/ |
386 |
|
public void testGetTaskCount() throws InterruptedException { |
387 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
388 |
< |
assertEquals(0, p1.getTaskCount()); |
389 |
< |
p1.execute(new MediumRunnable()); |
390 |
< |
Thread.sleep(SHORT_DELAY_MS); |
391 |
< |
assertEquals(1, p1.getTaskCount()); |
392 |
< |
joinPool(p1); |
387 |
> |
final ThreadPoolExecutor p = |
388 |
> |
new ThreadPoolExecutor(1, 1, |
389 |
> |
LONG_DELAY_MS, MILLISECONDS, |
390 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
391 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
392 |
> |
final CountDownLatch done = new CountDownLatch(1); |
393 |
> |
try { |
394 |
> |
assertEquals(0, p.getTaskCount()); |
395 |
> |
p.execute(new CheckedRunnable() { |
396 |
> |
public void realRun() throws InterruptedException { |
397 |
> |
threadStarted.countDown(); |
398 |
> |
assertEquals(1, p.getTaskCount()); |
399 |
> |
done.await(); |
400 |
> |
}}); |
401 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
402 |
> |
assertEquals(1, p.getTaskCount()); |
403 |
> |
} finally { |
404 |
> |
done.countDown(); |
405 |
> |
joinPool(p); |
406 |
> |
} |
407 |
|
} |
408 |
|
|
409 |
|
/** |
410 |
< |
* isShutDown is false before shutdown, true after |
410 |
> |
* isShutdown is false before shutdown, true after |
411 |
|
*/ |
412 |
|
public void testIsShutdown() { |
413 |
< |
|
414 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
415 |
< |
assertFalse(p1.isShutdown()); |
416 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
417 |
< |
assertTrue(p1.isShutdown()); |
418 |
< |
joinPool(p1); |
413 |
> |
final ThreadPoolExecutor p = |
414 |
> |
new ThreadPoolExecutor(1, 1, |
415 |
> |
LONG_DELAY_MS, MILLISECONDS, |
416 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
417 |
> |
assertFalse(p.isShutdown()); |
418 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
419 |
> |
assertTrue(p.isShutdown()); |
420 |
> |
joinPool(p); |
421 |
|
} |
422 |
|
|
423 |
+ |
/** |
424 |
+ |
* awaitTermination on a non-shutdown pool times out |
425 |
+ |
*/ |
426 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
427 |
+ |
final ThreadPoolExecutor p = |
428 |
+ |
new ThreadPoolExecutor(1, 1, |
429 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
430 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
431 |
+ |
assertFalse(p.isTerminated()); |
432 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
433 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
434 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
435 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
436 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
437 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
438 |
+ |
long timeoutNanos = 999999L; |
439 |
+ |
long startTime = System.nanoTime(); |
440 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
441 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
442 |
+ |
assertFalse(p.isTerminated()); |
443 |
+ |
startTime = System.nanoTime(); |
444 |
+ |
long timeoutMillis = timeoutMillis(); |
445 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
446 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
447 |
+ |
assertFalse(p.isTerminated()); |
448 |
+ |
p.shutdown(); |
449 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
450 |
+ |
assertTrue(p.isTerminated()); |
451 |
+ |
} |
452 |
|
|
453 |
|
/** |
454 |
|
* isTerminated is false before termination, true after |
455 |
|
*/ |
456 |
|
public void testIsTerminated() throws InterruptedException { |
457 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
458 |
< |
assertFalse(p1.isTerminated()); |
459 |
< |
try { |
460 |
< |
p1.execute(new MediumRunnable()); |
457 |
> |
final ThreadPoolExecutor p = |
458 |
> |
new ThreadPoolExecutor(1, 1, |
459 |
> |
LONG_DELAY_MS, MILLISECONDS, |
460 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
461 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
462 |
> |
final CountDownLatch done = new CountDownLatch(1); |
463 |
> |
assertFalse(p.isTerminated()); |
464 |
> |
try { |
465 |
> |
p.execute(new CheckedRunnable() { |
466 |
> |
public void realRun() throws InterruptedException { |
467 |
> |
assertFalse(p.isTerminated()); |
468 |
> |
threadStarted.countDown(); |
469 |
> |
done.await(); |
470 |
> |
}}); |
471 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
472 |
> |
assertFalse(p.isTerminating()); |
473 |
> |
done.countDown(); |
474 |
|
} finally { |
475 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
475 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
476 |
|
} |
477 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
478 |
< |
assertTrue(p1.isTerminated()); |
477 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
478 |
> |
assertTrue(p.isTerminated()); |
479 |
|
} |
480 |
|
|
481 |
|
/** |
482 |
|
* isTerminating is not true when running or when terminated |
483 |
|
*/ |
484 |
|
public void testIsTerminating() throws InterruptedException { |
485 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
486 |
< |
assertFalse(p1.isTerminating()); |
485 |
> |
final ThreadPoolExecutor p = |
486 |
> |
new ThreadPoolExecutor(1, 1, |
487 |
> |
LONG_DELAY_MS, MILLISECONDS, |
488 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
489 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
490 |
> |
final CountDownLatch done = new CountDownLatch(1); |
491 |
|
try { |
492 |
< |
p1.execute(new SmallRunnable()); |
493 |
< |
assertFalse(p1.isTerminating()); |
494 |
< |
} finally { |
495 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
496 |
< |
} |
497 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
498 |
< |
assertTrue(p1.isTerminated()); |
499 |
< |
assertFalse(p1.isTerminating()); |
492 |
> |
assertFalse(p.isTerminating()); |
493 |
> |
p.execute(new CheckedRunnable() { |
494 |
> |
public void realRun() throws InterruptedException { |
495 |
> |
assertFalse(p.isTerminating()); |
496 |
> |
threadStarted.countDown(); |
497 |
> |
done.await(); |
498 |
> |
}}); |
499 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
500 |
> |
assertFalse(p.isTerminating()); |
501 |
> |
done.countDown(); |
502 |
> |
} finally { |
503 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
504 |
> |
} |
505 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
506 |
> |
assertTrue(p.isTerminated()); |
507 |
> |
assertFalse(p.isTerminating()); |
508 |
|
} |
509 |
|
|
510 |
|
/** |
511 |
|
* getQueue returns the work queue, which contains queued tasks |
512 |
|
*/ |
513 |
|
public void testGetQueue() throws InterruptedException { |
514 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
515 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
516 |
< |
FutureTask[] tasks = new FutureTask[5]; |
517 |
< |
for (int i = 0; i < 5; i++) { |
518 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
519 |
< |
p1.execute(tasks[i]); |
520 |
< |
} |
521 |
< |
try { |
522 |
< |
Thread.sleep(SHORT_DELAY_MS); |
523 |
< |
BlockingQueue<Runnable> wq = p1.getQueue(); |
524 |
< |
assertSame(q, wq); |
525 |
< |
assertFalse(wq.contains(tasks[0])); |
526 |
< |
assertTrue(wq.contains(tasks[4])); |
527 |
< |
for (int i = 1; i < 5; ++i) |
528 |
< |
tasks[i].cancel(true); |
529 |
< |
p1.shutdownNow(); |
514 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
515 |
> |
final ThreadPoolExecutor p = |
516 |
> |
new ThreadPoolExecutor(1, 1, |
517 |
> |
LONG_DELAY_MS, MILLISECONDS, |
518 |
> |
q); |
519 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
520 |
> |
final CountDownLatch done = new CountDownLatch(1); |
521 |
> |
try { |
522 |
> |
FutureTask[] tasks = new FutureTask[5]; |
523 |
> |
for (int i = 0; i < tasks.length; i++) { |
524 |
> |
Callable task = new CheckedCallable<Boolean>() { |
525 |
> |
public Boolean realCall() throws InterruptedException { |
526 |
> |
threadStarted.countDown(); |
527 |
> |
assertSame(q, p.getQueue()); |
528 |
> |
done.await(); |
529 |
> |
return Boolean.TRUE; |
530 |
> |
}}; |
531 |
> |
tasks[i] = new FutureTask(task); |
532 |
> |
p.execute(tasks[i]); |
533 |
> |
} |
534 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
535 |
> |
assertSame(q, p.getQueue()); |
536 |
> |
assertFalse(q.contains(tasks[0])); |
537 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
538 |
> |
assertEquals(tasks.length - 1, q.size()); |
539 |
|
} finally { |
540 |
< |
joinPool(p1); |
540 |
> |
done.countDown(); |
541 |
> |
joinPool(p); |
542 |
|
} |
543 |
|
} |
544 |
|
|
547 |
|
*/ |
548 |
|
public void testRemove() throws InterruptedException { |
549 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
550 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
551 |
< |
FutureTask[] tasks = new FutureTask[5]; |
552 |
< |
for (int i = 0; i < 5; i++) { |
553 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
554 |
< |
p1.execute(tasks[i]); |
555 |
< |
} |
556 |
< |
try { |
557 |
< |
Thread.sleep(SHORT_DELAY_MS); |
558 |
< |
assertFalse(p1.remove(tasks[0])); |
550 |
> |
final ThreadPoolExecutor p = |
551 |
> |
new ThreadPoolExecutor(1, 1, |
552 |
> |
LONG_DELAY_MS, MILLISECONDS, |
553 |
> |
q); |
554 |
> |
Runnable[] tasks = new Runnable[5]; |
555 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
556 |
> |
final CountDownLatch done = new CountDownLatch(1); |
557 |
> |
try { |
558 |
> |
for (int i = 0; i < tasks.length; i++) { |
559 |
> |
tasks[i] = new CheckedRunnable() { |
560 |
> |
public void realRun() throws InterruptedException { |
561 |
> |
threadStarted.countDown(); |
562 |
> |
done.await(); |
563 |
> |
}}; |
564 |
> |
p.execute(tasks[i]); |
565 |
> |
} |
566 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
567 |
> |
assertFalse(p.remove(tasks[0])); |
568 |
|
assertTrue(q.contains(tasks[4])); |
569 |
|
assertTrue(q.contains(tasks[3])); |
570 |
< |
assertTrue(p1.remove(tasks[4])); |
571 |
< |
assertFalse(p1.remove(tasks[4])); |
570 |
> |
assertTrue(p.remove(tasks[4])); |
571 |
> |
assertFalse(p.remove(tasks[4])); |
572 |
|
assertFalse(q.contains(tasks[4])); |
573 |
|
assertTrue(q.contains(tasks[3])); |
574 |
< |
assertTrue(p1.remove(tasks[3])); |
574 |
> |
assertTrue(p.remove(tasks[3])); |
575 |
|
assertFalse(q.contains(tasks[3])); |
576 |
|
} finally { |
577 |
< |
joinPool(p1); |
577 |
> |
done.countDown(); |
578 |
> |
joinPool(p); |
579 |
|
} |
580 |
|
} |
581 |
|
|
582 |
|
/** |
583 |
|
* purge removes cancelled tasks from the queue |
584 |
|
*/ |
585 |
< |
public void testPurge() { |
586 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
585 |
> |
public void testPurge() throws InterruptedException { |
586 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
587 |
> |
final CountDownLatch done = new CountDownLatch(1); |
588 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
589 |
> |
final ThreadPoolExecutor p = |
590 |
> |
new ThreadPoolExecutor(1, 1, |
591 |
> |
LONG_DELAY_MS, MILLISECONDS, |
592 |
> |
q); |
593 |
|
FutureTask[] tasks = new FutureTask[5]; |
594 |
< |
for (int i = 0; i < 5; i++) { |
595 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
596 |
< |
p1.execute(tasks[i]); |
594 |
> |
try { |
595 |
> |
for (int i = 0; i < tasks.length; i++) { |
596 |
> |
Callable task = new CheckedCallable<Boolean>() { |
597 |
> |
public Boolean realCall() throws InterruptedException { |
598 |
> |
threadStarted.countDown(); |
599 |
> |
done.await(); |
600 |
> |
return Boolean.TRUE; |
601 |
> |
}}; |
602 |
> |
tasks[i] = new FutureTask(task); |
603 |
> |
p.execute(tasks[i]); |
604 |
> |
} |
605 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
606 |
> |
assertEquals(tasks.length, p.getTaskCount()); |
607 |
> |
assertEquals(tasks.length - 1, q.size()); |
608 |
> |
assertEquals(1L, p.getActiveCount()); |
609 |
> |
assertEquals(0L, p.getCompletedTaskCount()); |
610 |
> |
tasks[4].cancel(true); |
611 |
> |
tasks[3].cancel(false); |
612 |
> |
p.purge(); |
613 |
> |
assertEquals(tasks.length - 3, q.size()); |
614 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
615 |
> |
p.purge(); // Nothing to do |
616 |
> |
assertEquals(tasks.length - 3, q.size()); |
617 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
618 |
> |
} finally { |
619 |
> |
done.countDown(); |
620 |
> |
joinPool(p); |
621 |
|
} |
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); |
622 |
|
} |
623 |
|
|
624 |
|
/** |
625 |
< |
* shutDownNow returns a list containing tasks that were not run |
625 |
> |
* shutdownNow returns a list containing tasks that were not run |
626 |
|
*/ |
627 |
< |
public void testShutDownNow() { |
628 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
627 |
> |
public void testShutdownNow() { |
628 |
> |
final ThreadPoolExecutor p = |
629 |
> |
new ThreadPoolExecutor(1, 1, |
630 |
> |
LONG_DELAY_MS, MILLISECONDS, |
631 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
632 |
|
List l; |
633 |
|
try { |
634 |
|
for (int i = 0; i < 5; i++) |
635 |
< |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
635 |
> |
p.execute(new MediumPossiblyInterruptedRunnable()); |
636 |
|
} |
637 |
|
finally { |
638 |
|
try { |
639 |
< |
l = p1.shutdownNow(); |
639 |
> |
l = p.shutdownNow(); |
640 |
|
} catch (SecurityException ok) { return; } |
641 |
|
} |
642 |
< |
assertTrue(p1.isShutdown()); |
642 |
> |
assertTrue(p.isShutdown()); |
643 |
|
assertTrue(l.size() <= 4); |
644 |
|
} |
645 |
|
|
646 |
|
// Exception Tests |
647 |
|
|
395 |
– |
|
648 |
|
/** |
649 |
|
* Constructor throws if corePoolSize argument is less than zero |
650 |
|
*/ |
651 |
|
public void testConstructor1() { |
652 |
|
try { |
653 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
653 |
> |
new ThreadPoolExecutor(-1, 1, |
654 |
> |
LONG_DELAY_MS, MILLISECONDS, |
655 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
656 |
|
shouldThrow(); |
657 |
|
} catch (IllegalArgumentException success) {} |
658 |
|
} |
662 |
|
*/ |
663 |
|
public void testConstructor2() { |
664 |
|
try { |
665 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
665 |
> |
new ThreadPoolExecutor(1, -1, |
666 |
> |
LONG_DELAY_MS, MILLISECONDS, |
667 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
668 |
|
shouldThrow(); |
669 |
|
} catch (IllegalArgumentException success) {} |
670 |
|
} |
674 |
|
*/ |
675 |
|
public void testConstructor3() { |
676 |
|
try { |
677 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
677 |
> |
new ThreadPoolExecutor(1, 0, |
678 |
> |
LONG_DELAY_MS, MILLISECONDS, |
679 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
680 |
|
shouldThrow(); |
681 |
|
} catch (IllegalArgumentException success) {} |
682 |
|
} |
686 |
|
*/ |
687 |
|
public void testConstructor4() { |
688 |
|
try { |
689 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
689 |
> |
new ThreadPoolExecutor(1, 2, |
690 |
> |
-1L, MILLISECONDS, |
691 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
692 |
|
shouldThrow(); |
693 |
|
} catch (IllegalArgumentException success) {} |
694 |
|
} |
698 |
|
*/ |
699 |
|
public void testConstructor5() { |
700 |
|
try { |
701 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
701 |
> |
new ThreadPoolExecutor(2, 1, |
702 |
> |
LONG_DELAY_MS, MILLISECONDS, |
703 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
704 |
|
shouldThrow(); |
705 |
|
} catch (IllegalArgumentException success) {} |
706 |
|
} |
710 |
|
*/ |
711 |
|
public void testConstructorNullPointerException() { |
712 |
|
try { |
713 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
713 |
> |
new ThreadPoolExecutor(1, 2, |
714 |
> |
LONG_DELAY_MS, MILLISECONDS, |
715 |
> |
(BlockingQueue) null); |
716 |
|
shouldThrow(); |
717 |
|
} catch (NullPointerException success) {} |
718 |
|
} |
719 |
|
|
456 |
– |
|
457 |
– |
|
720 |
|
/** |
721 |
|
* Constructor throws if corePoolSize argument is less than zero |
722 |
|
*/ |
723 |
|
public void testConstructor6() { |
724 |
|
try { |
725 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
725 |
> |
new ThreadPoolExecutor(-1, 1, |
726 |
> |
LONG_DELAY_MS, MILLISECONDS, |
727 |
> |
new ArrayBlockingQueue<Runnable>(10), |
728 |
> |
new SimpleThreadFactory()); |
729 |
|
shouldThrow(); |
730 |
|
} catch (IllegalArgumentException success) {} |
731 |
|
} |
735 |
|
*/ |
736 |
|
public void testConstructor7() { |
737 |
|
try { |
738 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
738 |
> |
new ThreadPoolExecutor(1, -1, |
739 |
> |
LONG_DELAY_MS, MILLISECONDS, |
740 |
> |
new ArrayBlockingQueue<Runnable>(10), |
741 |
> |
new SimpleThreadFactory()); |
742 |
|
shouldThrow(); |
743 |
|
} catch (IllegalArgumentException success) {} |
744 |
|
} |
748 |
|
*/ |
749 |
|
public void testConstructor8() { |
750 |
|
try { |
751 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
751 |
> |
new ThreadPoolExecutor(1, 0, |
752 |
> |
LONG_DELAY_MS, MILLISECONDS, |
753 |
> |
new ArrayBlockingQueue<Runnable>(10), |
754 |
> |
new SimpleThreadFactory()); |
755 |
|
shouldThrow(); |
756 |
|
} catch (IllegalArgumentException success) {} |
757 |
|
} |
761 |
|
*/ |
762 |
|
public void testConstructor9() { |
763 |
|
try { |
764 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
764 |
> |
new ThreadPoolExecutor(1, 2, |
765 |
> |
-1L, MILLISECONDS, |
766 |
> |
new ArrayBlockingQueue<Runnable>(10), |
767 |
> |
new SimpleThreadFactory()); |
768 |
|
shouldThrow(); |
769 |
|
} catch (IllegalArgumentException success) {} |
770 |
|
} |
774 |
|
*/ |
775 |
|
public void testConstructor10() { |
776 |
|
try { |
777 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
777 |
> |
new ThreadPoolExecutor(2, 1, |
778 |
> |
LONG_DELAY_MS, MILLISECONDS, |
779 |
> |
new ArrayBlockingQueue<Runnable>(10), |
780 |
> |
new SimpleThreadFactory()); |
781 |
|
shouldThrow(); |
782 |
|
} catch (IllegalArgumentException success) {} |
783 |
|
} |
787 |
|
*/ |
788 |
|
public void testConstructorNullPointerException2() { |
789 |
|
try { |
790 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
790 |
> |
new ThreadPoolExecutor(1, 2, |
791 |
> |
LONG_DELAY_MS, MILLISECONDS, |
792 |
> |
(BlockingQueue) null, |
793 |
> |
new SimpleThreadFactory()); |
794 |
|
shouldThrow(); |
795 |
|
} catch (NullPointerException success) {} |
796 |
|
} |
800 |
|
*/ |
801 |
|
public void testConstructorNullPointerException3() { |
802 |
|
try { |
803 |
< |
ThreadFactory f = null; |
804 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
803 |
> |
new ThreadPoolExecutor(1, 2, |
804 |
> |
LONG_DELAY_MS, MILLISECONDS, |
805 |
> |
new ArrayBlockingQueue<Runnable>(10), |
806 |
> |
(ThreadFactory) null); |
807 |
|
shouldThrow(); |
808 |
|
} catch (NullPointerException success) {} |
809 |
|
} |
810 |
|
|
529 |
– |
|
811 |
|
/** |
812 |
|
* Constructor throws if corePoolSize argument is less than zero |
813 |
|
*/ |
814 |
|
public void testConstructor11() { |
815 |
|
try { |
816 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
816 |
> |
new ThreadPoolExecutor(-1, 1, |
817 |
> |
LONG_DELAY_MS, MILLISECONDS, |
818 |
> |
new ArrayBlockingQueue<Runnable>(10), |
819 |
> |
new NoOpREHandler()); |
820 |
|
shouldThrow(); |
821 |
|
} catch (IllegalArgumentException success) {} |
822 |
|
} |
826 |
|
*/ |
827 |
|
public void testConstructor12() { |
828 |
|
try { |
829 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
829 |
> |
new ThreadPoolExecutor(1, -1, |
830 |
> |
LONG_DELAY_MS, MILLISECONDS, |
831 |
> |
new ArrayBlockingQueue<Runnable>(10), |
832 |
> |
new NoOpREHandler()); |
833 |
|
shouldThrow(); |
834 |
|
} catch (IllegalArgumentException success) {} |
835 |
|
} |
839 |
|
*/ |
840 |
|
public void testConstructor13() { |
841 |
|
try { |
842 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
842 |
> |
new ThreadPoolExecutor(1, 0, |
843 |
> |
LONG_DELAY_MS, MILLISECONDS, |
844 |
> |
new ArrayBlockingQueue<Runnable>(10), |
845 |
> |
new NoOpREHandler()); |
846 |
|
shouldThrow(); |
847 |
|
} catch (IllegalArgumentException success) {} |
848 |
|
} |
852 |
|
*/ |
853 |
|
public void testConstructor14() { |
854 |
|
try { |
855 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
855 |
> |
new ThreadPoolExecutor(1, 2, |
856 |
> |
-1L, MILLISECONDS, |
857 |
> |
new ArrayBlockingQueue<Runnable>(10), |
858 |
> |
new NoOpREHandler()); |
859 |
|
shouldThrow(); |
860 |
|
} catch (IllegalArgumentException success) {} |
861 |
|
} |
865 |
|
*/ |
866 |
|
public void testConstructor15() { |
867 |
|
try { |
868 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
868 |
> |
new ThreadPoolExecutor(2, 1, |
869 |
> |
LONG_DELAY_MS, MILLISECONDS, |
870 |
> |
new ArrayBlockingQueue<Runnable>(10), |
871 |
> |
new NoOpREHandler()); |
872 |
|
shouldThrow(); |
873 |
|
} catch (IllegalArgumentException success) {} |
874 |
|
} |
878 |
|
*/ |
879 |
|
public void testConstructorNullPointerException4() { |
880 |
|
try { |
881 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
881 |
> |
new ThreadPoolExecutor(1, 2, |
882 |
> |
LONG_DELAY_MS, MILLISECONDS, |
883 |
> |
(BlockingQueue) null, |
884 |
> |
new NoOpREHandler()); |
885 |
|
shouldThrow(); |
886 |
|
} catch (NullPointerException success) {} |
887 |
|
} |
891 |
|
*/ |
892 |
|
public void testConstructorNullPointerException5() { |
893 |
|
try { |
894 |
< |
RejectedExecutionHandler r = null; |
895 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
894 |
> |
new ThreadPoolExecutor(1, 2, |
895 |
> |
LONG_DELAY_MS, MILLISECONDS, |
896 |
> |
new ArrayBlockingQueue<Runnable>(10), |
897 |
> |
(RejectedExecutionHandler) null); |
898 |
|
shouldThrow(); |
899 |
|
} catch (NullPointerException success) {} |
900 |
|
} |
901 |
|
|
601 |
– |
|
902 |
|
/** |
903 |
|
* Constructor throws if corePoolSize argument is less than zero |
904 |
|
*/ |
905 |
|
public void testConstructor16() { |
906 |
|
try { |
907 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
907 |
> |
new ThreadPoolExecutor(-1, 1, |
908 |
> |
LONG_DELAY_MS, MILLISECONDS, |
909 |
> |
new ArrayBlockingQueue<Runnable>(10), |
910 |
> |
new SimpleThreadFactory(), |
911 |
> |
new NoOpREHandler()); |
912 |
|
shouldThrow(); |
913 |
|
} catch (IllegalArgumentException success) {} |
914 |
|
} |
918 |
|
*/ |
919 |
|
public void testConstructor17() { |
920 |
|
try { |
921 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
921 |
> |
new ThreadPoolExecutor(1, -1, |
922 |
> |
LONG_DELAY_MS, MILLISECONDS, |
923 |
> |
new ArrayBlockingQueue<Runnable>(10), |
924 |
> |
new SimpleThreadFactory(), |
925 |
> |
new NoOpREHandler()); |
926 |
|
shouldThrow(); |
927 |
|
} catch (IllegalArgumentException success) {} |
928 |
|
} |
932 |
|
*/ |
933 |
|
public void testConstructor18() { |
934 |
|
try { |
935 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
935 |
> |
new ThreadPoolExecutor(1, 0, |
936 |
> |
LONG_DELAY_MS, MILLISECONDS, |
937 |
> |
new ArrayBlockingQueue<Runnable>(10), |
938 |
> |
new SimpleThreadFactory(), |
939 |
> |
new NoOpREHandler()); |
940 |
|
shouldThrow(); |
941 |
|
} catch (IllegalArgumentException success) {} |
942 |
|
} |
946 |
|
*/ |
947 |
|
public void testConstructor19() { |
948 |
|
try { |
949 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
949 |
> |
new ThreadPoolExecutor(1, 2, |
950 |
> |
-1L, MILLISECONDS, |
951 |
> |
new ArrayBlockingQueue<Runnable>(10), |
952 |
> |
new SimpleThreadFactory(), |
953 |
> |
new NoOpREHandler()); |
954 |
|
shouldThrow(); |
955 |
|
} catch (IllegalArgumentException success) {} |
956 |
|
} |
960 |
|
*/ |
961 |
|
public void testConstructor20() { |
962 |
|
try { |
963 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
963 |
> |
new ThreadPoolExecutor(2, 1, |
964 |
> |
LONG_DELAY_MS, MILLISECONDS, |
965 |
> |
new ArrayBlockingQueue<Runnable>(10), |
966 |
> |
new SimpleThreadFactory(), |
967 |
> |
new NoOpREHandler()); |
968 |
|
shouldThrow(); |
969 |
|
} catch (IllegalArgumentException success) {} |
970 |
|
} |
971 |
|
|
972 |
|
/** |
973 |
< |
* Constructor throws if workQueue is set to null |
973 |
> |
* Constructor throws if workQueue is null |
974 |
|
*/ |
975 |
|
public void testConstructorNullPointerException6() { |
976 |
|
try { |
977 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
977 |
> |
new ThreadPoolExecutor(1, 2, |
978 |
> |
LONG_DELAY_MS, MILLISECONDS, |
979 |
> |
(BlockingQueue) null, |
980 |
> |
new SimpleThreadFactory(), |
981 |
> |
new NoOpREHandler()); |
982 |
|
shouldThrow(); |
983 |
|
} catch (NullPointerException success) {} |
984 |
|
} |
985 |
|
|
986 |
|
/** |
987 |
< |
* Constructor throws if handler is set to null |
987 |
> |
* Constructor throws if handler is null |
988 |
|
*/ |
989 |
|
public void testConstructorNullPointerException7() { |
990 |
|
try { |
991 |
< |
RejectedExecutionHandler r = null; |
992 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
991 |
> |
new ThreadPoolExecutor(1, 2, |
992 |
> |
LONG_DELAY_MS, MILLISECONDS, |
993 |
> |
new ArrayBlockingQueue<Runnable>(10), |
994 |
> |
new SimpleThreadFactory(), |
995 |
> |
(RejectedExecutionHandler) null); |
996 |
|
shouldThrow(); |
997 |
|
} catch (NullPointerException success) {} |
998 |
|
} |
999 |
|
|
1000 |
|
/** |
1001 |
< |
* Constructor throws if ThreadFactory is set top null |
1001 |
> |
* Constructor throws if ThreadFactory is null |
1002 |
|
*/ |
1003 |
|
public void testConstructorNullPointerException8() { |
1004 |
|
try { |
1005 |
< |
ThreadFactory f = null; |
1006 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
1005 |
> |
new ThreadPoolExecutor(1, 2, |
1006 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1007 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1008 |
> |
(ThreadFactory) null, |
1009 |
> |
new NoOpREHandler()); |
1010 |
|
shouldThrow(); |
1011 |
|
} catch (NullPointerException success) {} |
1012 |
|
} |
1013 |
|
|
1014 |
+ |
/** |
1015 |
+ |
* get of submitted callable throws InterruptedException if interrupted |
1016 |
+ |
*/ |
1017 |
+ |
public void testInterruptedSubmit() throws InterruptedException { |
1018 |
+ |
final ThreadPoolExecutor p = |
1019 |
+ |
new ThreadPoolExecutor(1, 1, |
1020 |
+ |
60, TimeUnit.SECONDS, |
1021 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1022 |
+ |
|
1023 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1024 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1025 |
+ |
try { |
1026 |
+ |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1027 |
+ |
public void realRun() throws Exception { |
1028 |
+ |
Callable task = new CheckedCallable<Boolean>() { |
1029 |
+ |
public Boolean realCall() throws InterruptedException { |
1030 |
+ |
threadStarted.countDown(); |
1031 |
+ |
done.await(); |
1032 |
+ |
return Boolean.TRUE; |
1033 |
+ |
}}; |
1034 |
+ |
p.submit(task).get(); |
1035 |
+ |
}}); |
1036 |
+ |
|
1037 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1038 |
+ |
t.interrupt(); |
1039 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
1040 |
+ |
} finally { |
1041 |
+ |
done.countDown(); |
1042 |
+ |
joinPool(p); |
1043 |
+ |
} |
1044 |
+ |
} |
1045 |
|
|
1046 |
|
/** |
1047 |
|
* execute throws RejectedExecutionException if saturated. |
1051 |
|
new ThreadPoolExecutor(1, 1, |
1052 |
|
LONG_DELAY_MS, MILLISECONDS, |
1053 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1054 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1055 |
|
try { |
1056 |
+ |
Runnable task = new CheckedRunnable() { |
1057 |
+ |
public void realRun() throws InterruptedException { |
1058 |
+ |
done.await(); |
1059 |
+ |
}}; |
1060 |
|
for (int i = 0; i < 2; ++i) |
1061 |
< |
p.execute(new MediumRunnable()); |
1061 |
> |
p.execute(task); |
1062 |
|
for (int i = 0; i < 2; ++i) { |
1063 |
|
try { |
1064 |
< |
p.execute(new MediumRunnable()); |
1064 |
> |
p.execute(task); |
1065 |
|
shouldThrow(); |
1066 |
|
} catch (RejectedExecutionException success) {} |
1067 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1068 |
|
} |
1069 |
|
} finally { |
1070 |
+ |
done.countDown(); |
1071 |
+ |
joinPool(p); |
1072 |
+ |
} |
1073 |
+ |
} |
1074 |
+ |
|
1075 |
+ |
/** |
1076 |
+ |
* submit(runnable) throws RejectedExecutionException if saturated. |
1077 |
+ |
*/ |
1078 |
+ |
public void testSaturatedSubmitRunnable() { |
1079 |
+ |
ThreadPoolExecutor p = |
1080 |
+ |
new ThreadPoolExecutor(1, 1, |
1081 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1082 |
+ |
new ArrayBlockingQueue<Runnable>(1)); |
1083 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1084 |
+ |
try { |
1085 |
+ |
Runnable task = new CheckedRunnable() { |
1086 |
+ |
public void realRun() throws InterruptedException { |
1087 |
+ |
done.await(); |
1088 |
+ |
}}; |
1089 |
+ |
for (int i = 0; i < 2; ++i) |
1090 |
+ |
p.submit(task); |
1091 |
+ |
for (int i = 0; i < 2; ++i) { |
1092 |
+ |
try { |
1093 |
+ |
p.execute(task); |
1094 |
+ |
shouldThrow(); |
1095 |
+ |
} catch (RejectedExecutionException success) {} |
1096 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1097 |
+ |
} |
1098 |
+ |
} finally { |
1099 |
+ |
done.countDown(); |
1100 |
+ |
joinPool(p); |
1101 |
+ |
} |
1102 |
+ |
} |
1103 |
+ |
|
1104 |
+ |
/** |
1105 |
+ |
* submit(callable) throws RejectedExecutionException if saturated. |
1106 |
+ |
*/ |
1107 |
+ |
public void testSaturatedSubmitCallable() { |
1108 |
+ |
ThreadPoolExecutor p = |
1109 |
+ |
new ThreadPoolExecutor(1, 1, |
1110 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1111 |
+ |
new ArrayBlockingQueue<Runnable>(1)); |
1112 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1113 |
+ |
try { |
1114 |
+ |
Runnable task = new CheckedRunnable() { |
1115 |
+ |
public void realRun() throws InterruptedException { |
1116 |
+ |
done.await(); |
1117 |
+ |
}}; |
1118 |
+ |
for (int i = 0; i < 2; ++i) |
1119 |
+ |
p.submit(Executors.callable(task)); |
1120 |
+ |
for (int i = 0; i < 2; ++i) { |
1121 |
+ |
try { |
1122 |
+ |
p.execute(task); |
1123 |
+ |
shouldThrow(); |
1124 |
+ |
} catch (RejectedExecutionException success) {} |
1125 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1126 |
+ |
} |
1127 |
+ |
} finally { |
1128 |
+ |
done.countDown(); |
1129 |
|
joinPool(p); |
1130 |
|
} |
1131 |
|
} |
1135 |
|
*/ |
1136 |
|
public void testSaturatedExecute2() { |
1137 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1138 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1138 |
> |
final ThreadPoolExecutor p = |
1139 |
> |
new ThreadPoolExecutor(1, 1, |
1140 |
> |
LONG_DELAY_MS, |
1141 |
> |
MILLISECONDS, |
1142 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1143 |
> |
h); |
1144 |
|
try { |
714 |
– |
|
1145 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1146 |
< |
for (int i = 0; i < 5; ++i) { |
1146 |
> |
for (int i = 0; i < tasks.length; ++i) |
1147 |
|
tasks[i] = new TrackedNoOpRunnable(); |
718 |
– |
} |
1148 |
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1149 |
|
p.execute(mr); |
1150 |
< |
for (int i = 0; i < 5; ++i) { |
1150 |
> |
for (int i = 0; i < tasks.length; ++i) |
1151 |
|
p.execute(tasks[i]); |
1152 |
< |
} |
724 |
< |
for (int i = 1; i < 5; ++i) { |
1152 |
> |
for (int i = 1; i < tasks.length; ++i) |
1153 |
|
assertTrue(tasks[i].done); |
726 |
– |
} |
1154 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1155 |
|
} finally { |
1156 |
|
joinPool(p); |
1162 |
|
*/ |
1163 |
|
public void testSaturatedExecute3() { |
1164 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1165 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1165 |
> |
final ThreadPoolExecutor p = |
1166 |
> |
new ThreadPoolExecutor(1, 1, |
1167 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1168 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1169 |
> |
h); |
1170 |
|
try { |
740 |
– |
|
1171 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1172 |
< |
for (int i = 0; i < 5; ++i) { |
1172 |
> |
for (int i = 0; i < tasks.length; ++i) |
1173 |
|
tasks[i] = new TrackedNoOpRunnable(); |
744 |
– |
} |
1174 |
|
p.execute(new TrackedLongRunnable()); |
1175 |
< |
for (int i = 0; i < 5; ++i) { |
1176 |
< |
p.execute(tasks[i]); |
1177 |
< |
} |
1178 |
< |
for (int i = 0; i < 5; ++i) { |
750 |
< |
assertFalse(tasks[i].done); |
751 |
< |
} |
1175 |
> |
for (TrackedNoOpRunnable task : tasks) |
1176 |
> |
p.execute(task); |
1177 |
> |
for (TrackedNoOpRunnable task : tasks) |
1178 |
> |
assertFalse(task.done); |
1179 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1180 |
|
} finally { |
1181 |
|
joinPool(p); |
1187 |
|
*/ |
1188 |
|
public void testSaturatedExecute4() { |
1189 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1190 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1190 |
> |
final ThreadPoolExecutor p = |
1191 |
> |
new ThreadPoolExecutor(1, 1, |
1192 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1193 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1194 |
> |
h); |
1195 |
|
try { |
1196 |
|
p.execute(new TrackedLongRunnable()); |
1197 |
|
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1211 |
|
* execute throws RejectedExecutionException if shutdown |
1212 |
|
*/ |
1213 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1214 |
< |
ThreadPoolExecutor tpe = |
1215 |
< |
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1216 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1214 |
> |
ThreadPoolExecutor p = |
1215 |
> |
new ThreadPoolExecutor(1, 1, |
1216 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1217 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1218 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1219 |
|
try { |
1220 |
< |
tpe.execute(new NoOpRunnable()); |
1220 |
> |
p.execute(new NoOpRunnable()); |
1221 |
|
shouldThrow(); |
1222 |
|
} catch (RejectedExecutionException success) {} |
1223 |
|
|
1224 |
< |
joinPool(tpe); |
1224 |
> |
joinPool(p); |
1225 |
|
} |
1226 |
|
|
1227 |
|
/** |
1229 |
|
*/ |
1230 |
|
public void testCallerRunsOnShutdown() { |
1231 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1232 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1232 |
> |
final ThreadPoolExecutor p = |
1233 |
> |
new ThreadPoolExecutor(1, 1, |
1234 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1235 |
> |
new ArrayBlockingQueue<Runnable>(1), h); |
1236 |
|
|
1237 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1238 |
|
try { |
1249 |
|
*/ |
1250 |
|
public void testDiscardOnShutdown() { |
1251 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1252 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1252 |
> |
ThreadPoolExecutor p = |
1253 |
> |
new ThreadPoolExecutor(1, 1, |
1254 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1255 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1256 |
> |
h); |
1257 |
|
|
1258 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1259 |
|
try { |
1265 |
|
} |
1266 |
|
} |
1267 |
|
|
828 |
– |
|
1268 |
|
/** |
1269 |
|
* execute using DiscardOldestPolicy drops task on shutdown |
1270 |
|
*/ |
1271 |
|
public void testDiscardOldestOnShutdown() { |
1272 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1273 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1273 |
> |
ThreadPoolExecutor p = |
1274 |
> |
new ThreadPoolExecutor(1, 1, |
1275 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1276 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1277 |
> |
h); |
1278 |
|
|
1279 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1280 |
|
try { |
1286 |
|
} |
1287 |
|
} |
1288 |
|
|
846 |
– |
|
1289 |
|
/** |
1290 |
|
* execute(null) throws NPE |
1291 |
|
*/ |
1292 |
|
public void testExecuteNull() { |
1293 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1293 |
> |
ThreadPoolExecutor p = |
1294 |
> |
new ThreadPoolExecutor(1, 2, |
1295 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1296 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1297 |
|
try { |
1298 |
< |
tpe.execute(null); |
1298 |
> |
p.execute(null); |
1299 |
|
shouldThrow(); |
1300 |
|
} catch (NullPointerException success) {} |
1301 |
|
|
1302 |
< |
joinPool(tpe); |
1302 |
> |
joinPool(p); |
1303 |
|
} |
1304 |
|
|
1305 |
|
/** |
1306 |
|
* setCorePoolSize of negative value throws IllegalArgumentException |
1307 |
|
*/ |
1308 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1309 |
< |
ThreadPoolExecutor tpe = |
1309 |
> |
ThreadPoolExecutor p = |
1310 |
|
new ThreadPoolExecutor(1, 2, |
1311 |
|
LONG_DELAY_MS, MILLISECONDS, |
1312 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1313 |
|
try { |
1314 |
< |
tpe.setCorePoolSize(-1); |
1314 |
> |
p.setCorePoolSize(-1); |
1315 |
|
shouldThrow(); |
1316 |
|
} catch (IllegalArgumentException success) { |
1317 |
|
} finally { |
1318 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1318 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1319 |
|
} |
1320 |
< |
joinPool(tpe); |
1320 |
> |
joinPool(p); |
1321 |
|
} |
1322 |
|
|
1323 |
|
/** |
1325 |
|
* given a value less the core pool size |
1326 |
|
*/ |
1327 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1328 |
< |
ThreadPoolExecutor tpe = |
1328 |
> |
ThreadPoolExecutor p = |
1329 |
|
new ThreadPoolExecutor(2, 3, |
1330 |
|
LONG_DELAY_MS, MILLISECONDS, |
1331 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1332 |
|
try { |
1333 |
< |
tpe.setMaximumPoolSize(1); |
1333 |
> |
p.setMaximumPoolSize(1); |
1334 |
|
shouldThrow(); |
1335 |
|
} catch (IllegalArgumentException success) { |
1336 |
|
} finally { |
1337 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1337 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1338 |
|
} |
1339 |
< |
joinPool(tpe); |
1339 |
> |
joinPool(p); |
1340 |
|
} |
1341 |
|
|
1342 |
|
/** |
1344 |
|
* if given a negative value |
1345 |
|
*/ |
1346 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1347 |
< |
ThreadPoolExecutor tpe = |
1347 |
> |
ThreadPoolExecutor p = |
1348 |
|
new ThreadPoolExecutor(2, 3, |
1349 |
|
LONG_DELAY_MS, MILLISECONDS, |
1350 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1351 |
|
try { |
1352 |
< |
tpe.setMaximumPoolSize(-1); |
1352 |
> |
p.setMaximumPoolSize(-1); |
1353 |
|
shouldThrow(); |
1354 |
|
} catch (IllegalArgumentException success) { |
1355 |
|
} finally { |
1356 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1356 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1357 |
|
} |
1358 |
< |
joinPool(tpe); |
1358 |
> |
joinPool(p); |
1359 |
|
} |
1360 |
|
|
916 |
– |
|
1361 |
|
/** |
1362 |
|
* setKeepAliveTime throws IllegalArgumentException |
1363 |
|
* when given a negative value |
1364 |
|
*/ |
1365 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1366 |
< |
ThreadPoolExecutor tpe = |
1366 |
> |
ThreadPoolExecutor p = |
1367 |
|
new ThreadPoolExecutor(2, 3, |
1368 |
|
LONG_DELAY_MS, MILLISECONDS, |
1369 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1370 |
|
try { |
1371 |
< |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
1371 |
> |
p.setKeepAliveTime(-1,MILLISECONDS); |
1372 |
|
shouldThrow(); |
1373 |
|
} catch (IllegalArgumentException success) { |
1374 |
|
} finally { |
1375 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1375 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1376 |
|
} |
1377 |
< |
joinPool(tpe); |
1377 |
> |
joinPool(p); |
1378 |
|
} |
1379 |
|
|
1380 |
|
/** |
1381 |
|
* terminated() is called on termination |
1382 |
|
*/ |
1383 |
|
public void testTerminated() { |
1384 |
< |
ExtendedTPE tpe = new ExtendedTPE(); |
1385 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1386 |
< |
assertTrue(tpe.terminatedCalled); |
1387 |
< |
joinPool(tpe); |
1384 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1385 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1386 |
> |
assertTrue(p.terminatedCalled()); |
1387 |
> |
joinPool(p); |
1388 |
|
} |
1389 |
|
|
1390 |
|
/** |
1391 |
|
* beforeExecute and afterExecute are called when executing task |
1392 |
|
*/ |
1393 |
|
public void testBeforeAfter() throws InterruptedException { |
1394 |
< |
ExtendedTPE tpe = new ExtendedTPE(); |
1394 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1395 |
|
try { |
1396 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1397 |
< |
tpe.execute(r); |
1398 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1399 |
< |
assertTrue(r.done); |
1400 |
< |
assertTrue(tpe.beforeCalled); |
1401 |
< |
assertTrue(tpe.afterCalled); |
1402 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1396 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1397 |
> |
p.execute(new CheckedRunnable() { |
1398 |
> |
public void realRun() { |
1399 |
> |
done.countDown(); |
1400 |
> |
}}); |
1401 |
> |
await(p.afterCalled); |
1402 |
> |
assertEquals(0, done.getCount()); |
1403 |
> |
assertTrue(p.afterCalled()); |
1404 |
> |
assertTrue(p.beforeCalled()); |
1405 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1406 |
|
} finally { |
1407 |
< |
joinPool(tpe); |
1407 |
> |
joinPool(p); |
1408 |
|
} |
1409 |
|
} |
1410 |
|
|
1412 |
|
* completed submit of callable returns result |
1413 |
|
*/ |
1414 |
|
public void testSubmitCallable() throws Exception { |
1415 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1415 |
> |
ExecutorService e = |
1416 |
> |
new ThreadPoolExecutor(2, 2, |
1417 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1418 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1419 |
|
try { |
1420 |
|
Future<String> future = e.submit(new StringTask()); |
1421 |
|
String result = future.get(); |
1429 |
|
* completed submit of runnable returns successfully |
1430 |
|
*/ |
1431 |
|
public void testSubmitRunnable() throws Exception { |
1432 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1432 |
> |
ExecutorService e = |
1433 |
> |
new ThreadPoolExecutor(2, 2, |
1434 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1435 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1436 |
|
try { |
1437 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1438 |
|
future.get(); |
1446 |
|
* completed submit of (runnable, result) returns result |
1447 |
|
*/ |
1448 |
|
public void testSubmitRunnable2() throws Exception { |
1449 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1449 |
> |
ExecutorService e = |
1450 |
> |
new ThreadPoolExecutor(2, 2, |
1451 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1452 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1453 |
|
try { |
1454 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1455 |
|
String result = future.get(); |
1459 |
|
} |
1460 |
|
} |
1461 |
|
|
1006 |
– |
|
1462 |
|
/** |
1463 |
|
* invokeAny(null) throws NPE |
1464 |
|
*/ |
1465 |
|
public void testInvokeAny1() throws Exception { |
1466 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1466 |
> |
ExecutorService e = |
1467 |
> |
new ThreadPoolExecutor(2, 2, |
1468 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1469 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1470 |
|
try { |
1471 |
|
e.invokeAny(null); |
1472 |
|
shouldThrow(); |
1480 |
|
* invokeAny(empty collection) throws IAE |
1481 |
|
*/ |
1482 |
|
public void testInvokeAny2() throws Exception { |
1483 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1483 |
> |
ExecutorService e = |
1484 |
> |
new ThreadPoolExecutor(2, 2, |
1485 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1486 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1487 |
|
try { |
1488 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1489 |
|
shouldThrow(); |
1497 |
|
* invokeAny(c) throws NPE if c has null elements |
1498 |
|
*/ |
1499 |
|
public void testInvokeAny3() throws Exception { |
1500 |
< |
CountDownLatch latch = new CountDownLatch(1); |
1501 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1500 |
> |
final CountDownLatch latch = new CountDownLatch(1); |
1501 |
> |
final ExecutorService e = |
1502 |
> |
new ThreadPoolExecutor(2, 2, |
1503 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1504 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1505 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1506 |
|
l.add(latchAwaitingStringTask(latch)); |
1507 |
|
l.add(null); |
1519 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1520 |
|
*/ |
1521 |
|
public void testInvokeAny4() throws Exception { |
1522 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1522 |
> |
ExecutorService e = |
1523 |
> |
new ThreadPoolExecutor(2, 2, |
1524 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1525 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1526 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1527 |
|
l.add(new NPETask()); |
1528 |
|
try { |
1539 |
|
* invokeAny(c) returns result of some task |
1540 |
|
*/ |
1541 |
|
public void testInvokeAny5() 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 |
|
try { |
1547 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1548 |
|
l.add(new StringTask()); |
1558 |
|
* invokeAll(null) throws NPE |
1559 |
|
*/ |
1560 |
|
public void testInvokeAll1() 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 |
|
e.invokeAll(null); |
1567 |
|
shouldThrow(); |
1575 |
|
* invokeAll(empty collection) returns empty collection |
1576 |
|
*/ |
1577 |
|
public void testInvokeAll2() throws InterruptedException { |
1578 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1578 |
> |
ExecutorService e = |
1579 |
> |
new ThreadPoolExecutor(2, 2, |
1580 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1581 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1582 |
|
try { |
1583 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1584 |
|
assertTrue(r.isEmpty()); |
1591 |
|
* invokeAll(c) throws NPE if c has null elements |
1592 |
|
*/ |
1593 |
|
public void testInvokeAll3() throws Exception { |
1594 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1594 |
> |
ExecutorService e = |
1595 |
> |
new ThreadPoolExecutor(2, 2, |
1596 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1597 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1598 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1599 |
|
l.add(new StringTask()); |
1600 |
|
l.add(null); |
1611 |
|
* get of element of invokeAll(c) throws exception on failed task |
1612 |
|
*/ |
1613 |
|
public void testInvokeAll4() throws Exception { |
1614 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1614 |
> |
ExecutorService e = |
1615 |
> |
new ThreadPoolExecutor(2, 2, |
1616 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1617 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1618 |
|
try { |
1619 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1620 |
|
l.add(new NPETask()); |
1635 |
|
* invokeAll(c) returns results of all completed tasks |
1636 |
|
*/ |
1637 |
|
public void testInvokeAll5() throws Exception { |
1638 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1638 |
> |
ExecutorService e = |
1639 |
> |
new ThreadPoolExecutor(2, 2, |
1640 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1641 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1642 |
|
try { |
1643 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1644 |
|
l.add(new StringTask()); |
1652 |
|
} |
1653 |
|
} |
1654 |
|
|
1170 |
– |
|
1171 |
– |
|
1655 |
|
/** |
1656 |
|
* timed invokeAny(null) throws NPE |
1657 |
|
*/ |
1658 |
|
public void testTimedInvokeAny1() throws Exception { |
1659 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1659 |
> |
ExecutorService e = |
1660 |
> |
new ThreadPoolExecutor(2, 2, |
1661 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1662 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1663 |
|
try { |
1664 |
|
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1665 |
|
shouldThrow(); |
1673 |
|
* timed invokeAny(,,null) throws NPE |
1674 |
|
*/ |
1675 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1676 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1676 |
> |
ExecutorService e = |
1677 |
> |
new ThreadPoolExecutor(2, 2, |
1678 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1679 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1680 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1681 |
|
l.add(new StringTask()); |
1682 |
|
try { |
1692 |
|
* timed invokeAny(empty collection) throws IAE |
1693 |
|
*/ |
1694 |
|
public void testTimedInvokeAny2() throws Exception { |
1695 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1695 |
> |
ExecutorService e = |
1696 |
> |
new ThreadPoolExecutor(2, 2, |
1697 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1698 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1699 |
|
try { |
1700 |
|
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1701 |
|
shouldThrow(); |
1709 |
|
* timed invokeAny(c) throws NPE if c has null elements |
1710 |
|
*/ |
1711 |
|
public void testTimedInvokeAny3() throws Exception { |
1712 |
< |
CountDownLatch latch = new CountDownLatch(1); |
1713 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1712 |
> |
final CountDownLatch latch = new CountDownLatch(1); |
1713 |
> |
final ExecutorService e = |
1714 |
> |
new ThreadPoolExecutor(2, 2, |
1715 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1716 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1717 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1718 |
|
l.add(latchAwaitingStringTask(latch)); |
1719 |
|
l.add(null); |
1731 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1732 |
|
*/ |
1733 |
|
public void testTimedInvokeAny4() throws Exception { |
1734 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1734 |
> |
ExecutorService e = |
1735 |
> |
new ThreadPoolExecutor(2, 2, |
1736 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1737 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1738 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1739 |
|
l.add(new NPETask()); |
1740 |
|
try { |
1751 |
|
* timed invokeAny(c) returns result of some task |
1752 |
|
*/ |
1753 |
|
public void testTimedInvokeAny5() throws Exception { |
1754 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1754 |
> |
ExecutorService e = |
1755 |
> |
new ThreadPoolExecutor(2, 2, |
1756 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1757 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1758 |
|
try { |
1759 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1760 |
|
l.add(new StringTask()); |
1770 |
|
* timed invokeAll(null) throws NPE |
1771 |
|
*/ |
1772 |
|
public void testTimedInvokeAll1() throws Exception { |
1773 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1773 |
> |
ExecutorService e = |
1774 |
> |
new ThreadPoolExecutor(2, 2, |
1775 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1776 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1777 |
|
try { |
1778 |
|
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1779 |
|
shouldThrow(); |
1787 |
|
* timed invokeAll(,,null) throws NPE |
1788 |
|
*/ |
1789 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1790 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1790 |
> |
ExecutorService e = |
1791 |
> |
new ThreadPoolExecutor(2, 2, |
1792 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1793 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1794 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1795 |
|
l.add(new StringTask()); |
1796 |
|
try { |
1806 |
|
* timed invokeAll(empty collection) returns empty collection |
1807 |
|
*/ |
1808 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1809 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1809 |
> |
ExecutorService e = |
1810 |
> |
new ThreadPoolExecutor(2, 2, |
1811 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1812 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1813 |
|
try { |
1814 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1815 |
|
assertTrue(r.isEmpty()); |
1822 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1823 |
|
*/ |
1824 |
|
public void testTimedInvokeAll3() throws Exception { |
1825 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1825 |
> |
ExecutorService e = |
1826 |
> |
new ThreadPoolExecutor(2, 2, |
1827 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1828 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1829 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1830 |
|
l.add(new StringTask()); |
1831 |
|
l.add(null); |
1842 |
|
* get of element of invokeAll(c) throws exception on failed task |
1843 |
|
*/ |
1844 |
|
public void testTimedInvokeAll4() throws Exception { |
1845 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1845 |
> |
ExecutorService e = |
1846 |
> |
new ThreadPoolExecutor(2, 2, |
1847 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1848 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1849 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1850 |
|
l.add(new NPETask()); |
1851 |
|
List<Future<String>> futures = |
1865 |
|
* timed invokeAll(c) returns results of all completed tasks |
1866 |
|
*/ |
1867 |
|
public void testTimedInvokeAll5() throws Exception { |
1868 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1868 |
> |
ExecutorService e = |
1869 |
> |
new ThreadPoolExecutor(2, 2, |
1870 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1871 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1872 |
|
try { |
1873 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1874 |
|
l.add(new StringTask()); |
1887 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1888 |
|
*/ |
1889 |
|
public void testTimedInvokeAll6() throws Exception { |
1890 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1890 |
> |
ExecutorService e = |
1891 |
> |
new ThreadPoolExecutor(2, 2, |
1892 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1893 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1894 |
|
try { |
1895 |
|
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1896 |
|
l.add(new StringTask()); |
1898 |
|
l.add(new StringTask()); |
1899 |
|
List<Future<String>> futures = |
1900 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1901 |
< |
assertEquals(3, futures.size()); |
1902 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1903 |
< |
Future<String> f1 = it.next(); |
1904 |
< |
Future<String> f2 = it.next(); |
1905 |
< |
Future<String> f3 = it.next(); |
1384 |
< |
assertTrue(f1.isDone()); |
1385 |
< |
assertTrue(f2.isDone()); |
1386 |
< |
assertTrue(f3.isDone()); |
1387 |
< |
assertFalse(f1.isCancelled()); |
1388 |
< |
assertTrue(f2.isCancelled()); |
1901 |
> |
assertEquals(l.size(), futures.size()); |
1902 |
> |
for (Future future : futures) |
1903 |
> |
assertTrue(future.isDone()); |
1904 |
> |
assertFalse(futures.get(0).isCancelled()); |
1905 |
> |
assertTrue(futures.get(1).isCancelled()); |
1906 |
|
} finally { |
1907 |
|
joinPool(e); |
1908 |
|
} |
1913 |
|
* thread factory fails to create more |
1914 |
|
*/ |
1915 |
|
public void testFailingThreadFactory() throws InterruptedException { |
1916 |
< |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1916 |
> |
final ExecutorService e = |
1917 |
> |
new ThreadPoolExecutor(100, 100, |
1918 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1919 |
> |
new LinkedBlockingQueue<Runnable>(), |
1920 |
> |
new FailingThreadFactory()); |
1921 |
|
try { |
1922 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1923 |
< |
for (int k = 0; k < 100; ++k) { |
1924 |
< |
e.execute(new NoOpRunnable()); |
1925 |
< |
} |
1926 |
< |
Thread.sleep(LONG_DELAY_MS); |
1922 |
> |
final int TASKS = 100; |
1923 |
> |
final CountDownLatch done = new CountDownLatch(TASKS); |
1924 |
> |
for (int k = 0; k < TASKS; ++k) |
1925 |
> |
e.execute(new CheckedRunnable() { |
1926 |
> |
public void realRun() { |
1927 |
> |
done.countDown(); |
1928 |
> |
}}); |
1929 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1930 |
|
} finally { |
1931 |
|
joinPool(e); |
1932 |
|
} |
1936 |
|
* allowsCoreThreadTimeOut is by default false. |
1937 |
|
*/ |
1938 |
|
public void testAllowsCoreThreadTimeOut() { |
1939 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1940 |
< |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1941 |
< |
joinPool(tpe); |
1939 |
> |
final ThreadPoolExecutor p = |
1940 |
> |
new ThreadPoolExecutor(2, 2, |
1941 |
> |
1000, MILLISECONDS, |
1942 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1943 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1944 |
> |
joinPool(p); |
1945 |
|
} |
1946 |
|
|
1947 |
|
/** |
1948 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1949 |
|
*/ |
1950 |
< |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1951 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1952 |
< |
tpe.allowCoreThreadTimeOut(true); |
1953 |
< |
tpe.execute(new NoOpRunnable()); |
1950 |
> |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1951 |
> |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1952 |
> |
final ThreadPoolExecutor p = |
1953 |
> |
new ThreadPoolExecutor(2, 10, |
1954 |
> |
coreThreadTimeOut, MILLISECONDS, |
1955 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1956 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1957 |
|
try { |
1958 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1959 |
< |
assertEquals(0, tpe.getPoolSize()); |
1958 |
> |
p.allowCoreThreadTimeOut(true); |
1959 |
> |
p.execute(new CheckedRunnable() { |
1960 |
> |
public void realRun() { |
1961 |
> |
threadStarted.countDown(); |
1962 |
> |
assertEquals(1, p.getPoolSize()); |
1963 |
> |
}}); |
1964 |
> |
await(threadStarted); |
1965 |
> |
delay(coreThreadTimeOut); |
1966 |
> |
long startTime = System.nanoTime(); |
1967 |
> |
while (p.getPoolSize() > 0 |
1968 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1969 |
> |
Thread.yield(); |
1970 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1971 |
> |
assertEquals(0, p.getPoolSize()); |
1972 |
|
} finally { |
1973 |
< |
joinPool(tpe); |
1973 |
> |
joinPool(p); |
1974 |
|
} |
1975 |
|
} |
1976 |
|
|
1977 |
|
/** |
1978 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1979 |
|
*/ |
1980 |
< |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
1981 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1982 |
< |
tpe.allowCoreThreadTimeOut(false); |
1983 |
< |
tpe.execute(new NoOpRunnable()); |
1980 |
> |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1981 |
> |
long coreThreadTimeOut = SHORT_DELAY_MS; |
1982 |
> |
final ThreadPoolExecutor p = |
1983 |
> |
new ThreadPoolExecutor(2, 10, |
1984 |
> |
coreThreadTimeOut, MILLISECONDS, |
1985 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1986 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1987 |
|
try { |
1988 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
1989 |
< |
assertTrue(tpe.getPoolSize() >= 1); |
1988 |
> |
p.allowCoreThreadTimeOut(false); |
1989 |
> |
p.execute(new CheckedRunnable() { |
1990 |
> |
public void realRun() throws InterruptedException { |
1991 |
> |
threadStarted.countDown(); |
1992 |
> |
assertTrue(p.getPoolSize() >= 1); |
1993 |
> |
}}); |
1994 |
> |
delay(2 * coreThreadTimeOut); |
1995 |
> |
assertTrue(p.getPoolSize() >= 1); |
1996 |
|
} finally { |
1997 |
< |
joinPool(tpe); |
1997 |
> |
joinPool(p); |
1998 |
|
} |
1999 |
|
} |
2000 |
|
|
2004 |
|
*/ |
2005 |
|
public void testRejectedRecycledTask() throws InterruptedException { |
2006 |
|
final int nTasks = 1000; |
2007 |
< |
final AtomicInteger nRun = new AtomicInteger(0); |
2007 |
> |
final CountDownLatch done = new CountDownLatch(nTasks); |
2008 |
|
final Runnable recycledTask = new Runnable() { |
2009 |
< |
public void run() { |
2010 |
< |
nRun.getAndIncrement(); |
2011 |
< |
} }; |
2009 |
> |
public void run() { |
2010 |
> |
done.countDown(); |
2011 |
> |
}}; |
2012 |
|
final ThreadPoolExecutor p = |
2013 |
|
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2014 |
|
new ArrayBlockingQueue(30)); |
2019 |
|
p.execute(recycledTask); |
2020 |
|
break; |
2021 |
|
} |
2022 |
< |
catch (RejectedExecutionException ignore) { |
1472 |
< |
} |
2022 |
> |
catch (RejectedExecutionException ignore) {} |
2023 |
|
} |
2024 |
|
} |
2025 |
< |
Thread.sleep(5000); // enough time to run all tasks |
2026 |
< |
assertEquals(nRun.get(), nTasks); |
2025 |
> |
// enough time to run all tasks |
2026 |
> |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2027 |
|
} finally { |
2028 |
< |
p.shutdown(); |
2028 |
> |
joinPool(p); |
2029 |
|
} |
2030 |
|
} |
2031 |
|
|