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 |
> |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
> |
|
13 |
> |
import java.util.ArrayList; |
14 |
> |
import java.util.List; |
15 |
> |
import java.util.concurrent.ArrayBlockingQueue; |
16 |
> |
import java.util.concurrent.BlockingQueue; |
17 |
> |
import java.util.concurrent.Callable; |
18 |
> |
import java.util.concurrent.CancellationException; |
19 |
> |
import java.util.concurrent.CountDownLatch; |
20 |
> |
import java.util.concurrent.ExecutionException; |
21 |
> |
import java.util.concurrent.Executors; |
22 |
> |
import java.util.concurrent.ExecutorService; |
23 |
> |
import java.util.concurrent.Future; |
24 |
> |
import java.util.concurrent.FutureTask; |
25 |
> |
import java.util.concurrent.LinkedBlockingQueue; |
26 |
> |
import java.util.concurrent.RejectedExecutionException; |
27 |
> |
import java.util.concurrent.RejectedExecutionHandler; |
28 |
> |
import java.util.concurrent.SynchronousQueue; |
29 |
> |
import java.util.concurrent.ThreadFactory; |
30 |
> |
import java.util.concurrent.ThreadPoolExecutor; |
31 |
> |
import java.util.concurrent.TimeUnit; |
32 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
33 |
> |
|
34 |
> |
import junit.framework.Test; |
35 |
> |
import junit.framework.TestSuite; |
36 |
|
|
37 |
|
public class ThreadPoolExecutorTest extends JSR166TestCase { |
38 |
|
public static void main(String[] args) { |
39 |
< |
junit.textui.TestRunner.run (suite()); |
39 |
> |
main(suite(), args); |
40 |
|
} |
41 |
|
public static Test suite() { |
42 |
|
return new TestSuite(ThreadPoolExecutorTest.class); |
43 |
|
} |
44 |
|
|
45 |
|
static class ExtendedTPE extends ThreadPoolExecutor { |
46 |
< |
volatile boolean beforeCalled = false; |
47 |
< |
volatile boolean afterCalled = false; |
48 |
< |
volatile boolean terminatedCalled = false; |
46 |
> |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
47 |
> |
final CountDownLatch afterCalled = new CountDownLatch(1); |
48 |
> |
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
49 |
> |
|
50 |
|
public ExtendedTPE() { |
51 |
|
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
52 |
|
} |
53 |
|
protected void beforeExecute(Thread t, Runnable r) { |
54 |
< |
beforeCalled = true; |
54 |
> |
beforeCalled.countDown(); |
55 |
|
} |
56 |
|
protected void afterExecute(Runnable r, Throwable t) { |
57 |
< |
afterCalled = true; |
57 |
> |
afterCalled.countDown(); |
58 |
|
} |
59 |
|
protected void terminated() { |
60 |
< |
terminatedCalled = true; |
60 |
> |
terminatedCalled.countDown(); |
61 |
> |
} |
62 |
> |
|
63 |
> |
public boolean beforeCalled() { |
64 |
> |
return beforeCalled.getCount() == 0; |
65 |
> |
} |
66 |
> |
public boolean afterCalled() { |
67 |
> |
return afterCalled.getCount() == 0; |
68 |
> |
} |
69 |
> |
public boolean terminatedCalled() { |
70 |
> |
return terminatedCalled.getCount() == 0; |
71 |
|
} |
72 |
|
} |
73 |
|
|
79 |
|
} |
80 |
|
} |
81 |
|
|
49 |
– |
|
82 |
|
/** |
83 |
< |
* execute successfully executes a runnable |
83 |
> |
* execute successfully executes a runnable |
84 |
|
*/ |
85 |
|
public void testExecute() throws InterruptedException { |
86 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
86 |
> |
final ThreadPoolExecutor p = |
87 |
> |
new ThreadPoolExecutor(1, 1, |
88 |
> |
LONG_DELAY_MS, MILLISECONDS, |
89 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
90 |
> |
final CountDownLatch done = new CountDownLatch(1); |
91 |
> |
final Runnable task = new CheckedRunnable() { |
92 |
> |
public void realRun() { |
93 |
> |
done.countDown(); |
94 |
> |
}}; |
95 |
|
try { |
96 |
< |
p1.execute(new ShortRunnable()); |
97 |
< |
Thread.sleep(SMALL_DELAY_MS); |
96 |
> |
p.execute(task); |
97 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
98 |
|
} finally { |
99 |
< |
joinPool(p1); |
99 |
> |
joinPool(p); |
100 |
|
} |
101 |
|
} |
102 |
|
|
103 |
|
/** |
104 |
< |
* getActiveCount increases but doesn't overestimate, when a |
105 |
< |
* thread becomes active |
104 |
> |
* getActiveCount increases but doesn't overestimate, when a |
105 |
> |
* thread becomes active |
106 |
|
*/ |
107 |
|
public void testGetActiveCount() throws InterruptedException { |
108 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
109 |
< |
assertEquals(0, p2.getActiveCount()); |
110 |
< |
p2.execute(new MediumRunnable()); |
111 |
< |
Thread.sleep(SHORT_DELAY_MS); |
112 |
< |
assertEquals(1, p2.getActiveCount()); |
113 |
< |
joinPool(p2); |
108 |
> |
final ThreadPoolExecutor p = |
109 |
> |
new ThreadPoolExecutor(2, 2, |
110 |
> |
LONG_DELAY_MS, MILLISECONDS, |
111 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
112 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
113 |
> |
final CountDownLatch done = new CountDownLatch(1); |
114 |
> |
try { |
115 |
> |
assertEquals(0, p.getActiveCount()); |
116 |
> |
p.execute(new CheckedRunnable() { |
117 |
> |
public void realRun() throws InterruptedException { |
118 |
> |
threadStarted.countDown(); |
119 |
> |
assertEquals(1, p.getActiveCount()); |
120 |
> |
done.await(); |
121 |
> |
}}); |
122 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
123 |
> |
assertEquals(1, p.getActiveCount()); |
124 |
> |
} finally { |
125 |
> |
done.countDown(); |
126 |
> |
joinPool(p); |
127 |
> |
} |
128 |
|
} |
129 |
|
|
130 |
|
/** |
131 |
< |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
131 |
> |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
132 |
|
*/ |
133 |
|
public void testPrestartCoreThread() { |
134 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
135 |
< |
assertEquals(0, p2.getPoolSize()); |
136 |
< |
assertTrue(p2.prestartCoreThread()); |
137 |
< |
assertEquals(1, p2.getPoolSize()); |
138 |
< |
assertTrue(p2.prestartCoreThread()); |
139 |
< |
assertEquals(2, p2.getPoolSize()); |
140 |
< |
assertFalse(p2.prestartCoreThread()); |
141 |
< |
assertEquals(2, p2.getPoolSize()); |
142 |
< |
joinPool(p2); |
134 |
> |
final ThreadPoolExecutor p = |
135 |
> |
new ThreadPoolExecutor(2, 6, |
136 |
> |
LONG_DELAY_MS, MILLISECONDS, |
137 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
138 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
139 |
> |
assertEquals(0, p.getPoolSize()); |
140 |
> |
assertTrue(p.prestartCoreThread()); |
141 |
> |
assertEquals(1, p.getPoolSize()); |
142 |
> |
assertTrue(p.prestartCoreThread()); |
143 |
> |
assertEquals(2, p.getPoolSize()); |
144 |
> |
assertFalse(p.prestartCoreThread()); |
145 |
> |
assertEquals(2, p.getPoolSize()); |
146 |
> |
p.setCorePoolSize(4); |
147 |
> |
assertTrue(p.prestartCoreThread()); |
148 |
> |
assertEquals(3, p.getPoolSize()); |
149 |
> |
assertTrue(p.prestartCoreThread()); |
150 |
> |
assertEquals(4, p.getPoolSize()); |
151 |
> |
assertFalse(p.prestartCoreThread()); |
152 |
> |
assertEquals(4, p.getPoolSize()); |
153 |
> |
} |
154 |
|
} |
155 |
|
|
156 |
|
/** |
157 |
< |
* prestartAllCoreThreads starts all corePoolSize threads |
157 |
> |
* prestartAllCoreThreads starts all corePoolSize threads |
158 |
|
*/ |
159 |
|
public void testPrestartAllCoreThreads() { |
160 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
161 |
< |
assertEquals(0, p2.getPoolSize()); |
162 |
< |
p2.prestartAllCoreThreads(); |
163 |
< |
assertEquals(2, p2.getPoolSize()); |
164 |
< |
p2.prestartAllCoreThreads(); |
165 |
< |
assertEquals(2, p2.getPoolSize()); |
166 |
< |
joinPool(p2); |
160 |
> |
final ThreadPoolExecutor p = |
161 |
> |
new ThreadPoolExecutor(2, 6, |
162 |
> |
LONG_DELAY_MS, MILLISECONDS, |
163 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
164 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
165 |
> |
assertEquals(0, p.getPoolSize()); |
166 |
> |
p.prestartAllCoreThreads(); |
167 |
> |
assertEquals(2, p.getPoolSize()); |
168 |
> |
p.prestartAllCoreThreads(); |
169 |
> |
assertEquals(2, p.getPoolSize()); |
170 |
> |
p.setCorePoolSize(4); |
171 |
> |
p.prestartAllCoreThreads(); |
172 |
> |
assertEquals(4, p.getPoolSize()); |
173 |
> |
p.prestartAllCoreThreads(); |
174 |
> |
assertEquals(4, p.getPoolSize()); |
175 |
> |
} |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
180 |
< |
* when tasks complete |
179 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
180 |
> |
* when tasks complete |
181 |
|
*/ |
182 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
183 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
184 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
185 |
< |
p2.execute(new ShortRunnable()); |
186 |
< |
Thread.sleep(SMALL_DELAY_MS); |
187 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
188 |
< |
try { p2.shutdown(); } catch (SecurityException ok) { return; } |
189 |
< |
joinPool(p2); |
183 |
> |
final ThreadPoolExecutor p = |
184 |
> |
new ThreadPoolExecutor(2, 2, |
185 |
> |
LONG_DELAY_MS, MILLISECONDS, |
186 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
187 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
188 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
189 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
190 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
191 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
192 |
> |
p.execute(new CheckedRunnable() { |
193 |
> |
public void realRun() throws InterruptedException { |
194 |
> |
threadStarted.countDown(); |
195 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
196 |
> |
threadProceed.await(); |
197 |
> |
threadDone.countDown(); |
198 |
> |
}}); |
199 |
> |
await(threadStarted); |
200 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
201 |
> |
threadProceed.countDown(); |
202 |
> |
threadDone.await(); |
203 |
> |
long startTime = System.nanoTime(); |
204 |
> |
while (p.getCompletedTaskCount() != 1) { |
205 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
206 |
> |
fail("timed out"); |
207 |
> |
Thread.yield(); |
208 |
> |
} |
209 |
> |
} |
210 |
|
} |
211 |
|
|
212 |
|
/** |
213 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
213 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
214 |
|
*/ |
215 |
|
public void testGetCorePoolSize() { |
216 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
217 |
< |
assertEquals(1, p1.getCorePoolSize()); |
218 |
< |
joinPool(p1); |
216 |
> |
final ThreadPoolExecutor p = |
217 |
> |
new ThreadPoolExecutor(1, 1, |
218 |
> |
LONG_DELAY_MS, MILLISECONDS, |
219 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
220 |
> |
assertEquals(1, p.getCorePoolSize()); |
221 |
> |
joinPool(p); |
222 |
|
} |
223 |
|
|
224 |
|
/** |
225 |
< |
* getKeepAliveTime returns value given in constructor if not otherwise set |
225 |
> |
* getKeepAliveTime returns value given in constructor if not otherwise set |
226 |
|
*/ |
227 |
|
public void testGetKeepAliveTime() { |
228 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
229 |
< |
assertEquals(1, p2.getKeepAliveTime(TimeUnit.SECONDS)); |
230 |
< |
joinPool(p2); |
228 |
> |
final ThreadPoolExecutor p = |
229 |
> |
new ThreadPoolExecutor(2, 2, |
230 |
> |
1000, MILLISECONDS, |
231 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
232 |
> |
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
233 |
> |
joinPool(p); |
234 |
|
} |
235 |
|
|
136 |
– |
|
236 |
|
/** |
237 |
|
* getThreadFactory returns factory in constructor if not set |
238 |
|
*/ |
239 |
|
public void testGetThreadFactory() { |
240 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
241 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), tf, new NoOpREHandler()); |
241 |
> |
final ThreadPoolExecutor p = |
242 |
> |
new ThreadPoolExecutor(1, 2, |
243 |
> |
LONG_DELAY_MS, MILLISECONDS, |
244 |
> |
new ArrayBlockingQueue<Runnable>(10), |
245 |
> |
tf, |
246 |
> |
new NoOpREHandler()); |
247 |
|
assertSame(tf, p.getThreadFactory()); |
248 |
|
joinPool(p); |
249 |
|
} |
252 |
|
* setThreadFactory sets the thread factory returned by getThreadFactory |
253 |
|
*/ |
254 |
|
public void testSetThreadFactory() { |
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 |
|
ThreadFactory tf = new SimpleThreadFactory(); |
260 |
|
p.setThreadFactory(tf); |
261 |
|
assertSame(tf, p.getThreadFactory()); |
262 |
|
joinPool(p); |
263 |
|
} |
264 |
|
|
158 |
– |
|
265 |
|
/** |
266 |
|
* setThreadFactory(null) throws NPE |
267 |
|
*/ |
268 |
|
public void testSetThreadFactoryNull() { |
269 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
269 |
> |
final ThreadPoolExecutor p = |
270 |
> |
new ThreadPoolExecutor(1, 2, |
271 |
> |
LONG_DELAY_MS, MILLISECONDS, |
272 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
273 |
|
try { |
274 |
|
p.setThreadFactory(null); |
275 |
|
shouldThrow(); |
283 |
|
* getRejectedExecutionHandler returns handler in constructor if not set |
284 |
|
*/ |
285 |
|
public void testGetRejectedExecutionHandler() { |
286 |
< |
RejectedExecutionHandler h = new NoOpREHandler(); |
287 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10), h); |
286 |
> |
final RejectedExecutionHandler h = new NoOpREHandler(); |
287 |
> |
final ThreadPoolExecutor p = |
288 |
> |
new ThreadPoolExecutor(1, 2, |
289 |
> |
LONG_DELAY_MS, MILLISECONDS, |
290 |
> |
new ArrayBlockingQueue<Runnable>(10), |
291 |
> |
h); |
292 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
293 |
|
joinPool(p); |
294 |
|
} |
298 |
|
* getRejectedExecutionHandler |
299 |
|
*/ |
300 |
|
public void testSetRejectedExecutionHandler() { |
301 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
301 |
> |
final ThreadPoolExecutor p = |
302 |
> |
new ThreadPoolExecutor(1, 2, |
303 |
> |
LONG_DELAY_MS, MILLISECONDS, |
304 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
305 |
|
RejectedExecutionHandler h = new NoOpREHandler(); |
306 |
|
p.setRejectedExecutionHandler(h); |
307 |
|
assertSame(h, p.getRejectedExecutionHandler()); |
308 |
|
joinPool(p); |
309 |
|
} |
310 |
|
|
195 |
– |
|
311 |
|
/** |
312 |
|
* setRejectedExecutionHandler(null) throws NPE |
313 |
|
*/ |
314 |
|
public void testSetRejectedExecutionHandlerNull() { |
315 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
315 |
> |
final ThreadPoolExecutor p = |
316 |
> |
new ThreadPoolExecutor(1, 2, |
317 |
> |
LONG_DELAY_MS, MILLISECONDS, |
318 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
319 |
|
try { |
320 |
|
p.setRejectedExecutionHandler(null); |
321 |
|
shouldThrow(); |
325 |
|
} |
326 |
|
} |
327 |
|
|
210 |
– |
|
328 |
|
/** |
329 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
330 |
< |
* multiple threads active |
329 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
330 |
> |
* multiple threads active |
331 |
|
*/ |
332 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
333 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
334 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
335 |
< |
p2.execute(new MediumRunnable()); |
336 |
< |
p2.execute(new MediumRunnable()); |
337 |
< |
Thread.sleep(SHORT_DELAY_MS); |
338 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
339 |
< |
joinPool(p2); |
333 |
> |
final int THREADS = 3; |
334 |
> |
final ThreadPoolExecutor p = |
335 |
> |
new ThreadPoolExecutor(THREADS, THREADS, |
336 |
> |
LONG_DELAY_MS, MILLISECONDS, |
337 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
338 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
339 |
> |
final CountDownLatch done = new CountDownLatch(1); |
340 |
> |
try { |
341 |
> |
assertEquals(0, p.getLargestPoolSize()); |
342 |
> |
for (int i = 0; i < THREADS; i++) |
343 |
> |
p.execute(new CheckedRunnable() { |
344 |
> |
public void realRun() throws InterruptedException { |
345 |
> |
threadsStarted.countDown(); |
346 |
> |
done.await(); |
347 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
348 |
> |
}}); |
349 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
350 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
351 |
> |
} finally { |
352 |
> |
done.countDown(); |
353 |
> |
joinPool(p); |
354 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
355 |
> |
} |
356 |
|
} |
357 |
|
|
358 |
|
/** |
359 |
< |
* getMaximumPoolSize returns value given in constructor if not |
360 |
< |
* otherwise set |
359 |
> |
* getMaximumPoolSize returns value given in constructor if not |
360 |
> |
* otherwise set |
361 |
|
*/ |
362 |
|
public void testGetMaximumPoolSize() { |
363 |
< |
ThreadPoolExecutor p2 = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
364 |
< |
assertEquals(2, p2.getMaximumPoolSize()); |
365 |
< |
joinPool(p2); |
363 |
> |
final ThreadPoolExecutor p = |
364 |
> |
new ThreadPoolExecutor(2, 3, |
365 |
> |
LONG_DELAY_MS, MILLISECONDS, |
366 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
367 |
> |
assertEquals(3, p.getMaximumPoolSize()); |
368 |
> |
joinPool(p); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
373 |
< |
* become active |
372 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
373 |
> |
* become active |
374 |
|
*/ |
375 |
< |
public void testGetPoolSize() { |
376 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
377 |
< |
assertEquals(0, p1.getPoolSize()); |
378 |
< |
p1.execute(new MediumRunnable()); |
379 |
< |
assertEquals(1, p1.getPoolSize()); |
380 |
< |
joinPool(p1); |
375 |
> |
public void testGetPoolSize() throws InterruptedException { |
376 |
> |
final ThreadPoolExecutor p = |
377 |
> |
new ThreadPoolExecutor(1, 1, |
378 |
> |
LONG_DELAY_MS, MILLISECONDS, |
379 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
380 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
381 |
> |
final CountDownLatch done = new CountDownLatch(1); |
382 |
> |
try { |
383 |
> |
assertEquals(0, p.getPoolSize()); |
384 |
> |
p.execute(new CheckedRunnable() { |
385 |
> |
public void realRun() throws InterruptedException { |
386 |
> |
threadStarted.countDown(); |
387 |
> |
assertEquals(1, p.getPoolSize()); |
388 |
> |
done.await(); |
389 |
> |
}}); |
390 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
391 |
> |
assertEquals(1, p.getPoolSize()); |
392 |
> |
} finally { |
393 |
> |
done.countDown(); |
394 |
> |
joinPool(p); |
395 |
> |
} |
396 |
|
} |
397 |
|
|
398 |
|
/** |
399 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
399 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
400 |
|
*/ |
401 |
|
public void testGetTaskCount() throws InterruptedException { |
402 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
403 |
< |
assertEquals(0, p1.getTaskCount()); |
404 |
< |
p1.execute(new MediumRunnable()); |
405 |
< |
Thread.sleep(SHORT_DELAY_MS); |
406 |
< |
assertEquals(1, p1.getTaskCount()); |
407 |
< |
joinPool(p1); |
402 |
> |
final ThreadPoolExecutor p = |
403 |
> |
new ThreadPoolExecutor(1, 1, |
404 |
> |
LONG_DELAY_MS, MILLISECONDS, |
405 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
406 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
407 |
> |
final CountDownLatch done = new CountDownLatch(1); |
408 |
> |
try { |
409 |
> |
assertEquals(0, p.getTaskCount()); |
410 |
> |
p.execute(new CheckedRunnable() { |
411 |
> |
public void realRun() throws InterruptedException { |
412 |
> |
threadStarted.countDown(); |
413 |
> |
assertEquals(1, p.getTaskCount()); |
414 |
> |
done.await(); |
415 |
> |
}}); |
416 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
417 |
> |
assertEquals(1, p.getTaskCount()); |
418 |
> |
} finally { |
419 |
> |
done.countDown(); |
420 |
> |
joinPool(p); |
421 |
> |
} |
422 |
|
} |
423 |
|
|
424 |
|
/** |
425 |
< |
* isShutDown is false before shutdown, true after |
425 |
> |
* isShutdown is false before shutdown, true after |
426 |
|
*/ |
427 |
|
public void testIsShutdown() { |
428 |
< |
|
429 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
430 |
< |
assertFalse(p1.isShutdown()); |
431 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
432 |
< |
assertTrue(p1.isShutdown()); |
433 |
< |
joinPool(p1); |
428 |
> |
final ThreadPoolExecutor p = |
429 |
> |
new ThreadPoolExecutor(1, 1, |
430 |
> |
LONG_DELAY_MS, MILLISECONDS, |
431 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
432 |
> |
assertFalse(p.isShutdown()); |
433 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
434 |
> |
assertTrue(p.isShutdown()); |
435 |
> |
joinPool(p); |
436 |
|
} |
437 |
|
|
438 |
+ |
/** |
439 |
+ |
* awaitTermination on a non-shutdown pool times out |
440 |
+ |
*/ |
441 |
+ |
public void testAwaitTermination_timesOut() throws InterruptedException { |
442 |
+ |
final ThreadPoolExecutor p = |
443 |
+ |
new ThreadPoolExecutor(1, 1, |
444 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
445 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
446 |
+ |
assertFalse(p.isTerminated()); |
447 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
448 |
+ |
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
449 |
+ |
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
450 |
+ |
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
451 |
+ |
assertFalse(p.awaitTermination(0L, NANOSECONDS)); |
452 |
+ |
assertFalse(p.awaitTermination(0L, MILLISECONDS)); |
453 |
+ |
long timeoutNanos = 999999L; |
454 |
+ |
long startTime = System.nanoTime(); |
455 |
+ |
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
456 |
+ |
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
457 |
+ |
assertFalse(p.isTerminated()); |
458 |
+ |
startTime = System.nanoTime(); |
459 |
+ |
long timeoutMillis = timeoutMillis(); |
460 |
+ |
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
461 |
+ |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
462 |
+ |
assertFalse(p.isTerminated()); |
463 |
+ |
p.shutdown(); |
464 |
+ |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
465 |
+ |
assertTrue(p.isTerminated()); |
466 |
+ |
} |
467 |
|
|
468 |
|
/** |
469 |
< |
* isTerminated is false before termination, true after |
469 |
> |
* isTerminated is false before termination, true after |
470 |
|
*/ |
471 |
|
public void testIsTerminated() throws InterruptedException { |
472 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
473 |
< |
assertFalse(p1.isTerminated()); |
472 |
> |
final ThreadPoolExecutor p = |
473 |
> |
new ThreadPoolExecutor(1, 1, |
474 |
> |
LONG_DELAY_MS, MILLISECONDS, |
475 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
476 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
477 |
> |
final CountDownLatch done = new CountDownLatch(1); |
478 |
> |
assertFalse(p.isTerminated()); |
479 |
|
try { |
480 |
< |
p1.execute(new MediumRunnable()); |
480 |
> |
p.execute(new CheckedRunnable() { |
481 |
> |
public void realRun() throws InterruptedException { |
482 |
> |
assertFalse(p.isTerminated()); |
483 |
> |
threadStarted.countDown(); |
484 |
> |
done.await(); |
485 |
> |
}}); |
486 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
487 |
> |
assertFalse(p.isTerminating()); |
488 |
> |
done.countDown(); |
489 |
|
} finally { |
490 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
490 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
491 |
|
} |
492 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
493 |
< |
assertTrue(p1.isTerminated()); |
492 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
493 |
> |
assertTrue(p.isTerminated()); |
494 |
|
} |
495 |
|
|
496 |
|
/** |
497 |
< |
* isTerminating is not true when running or when terminated |
497 |
> |
* isTerminating is not true when running or when terminated |
498 |
|
*/ |
499 |
|
public void testIsTerminating() throws InterruptedException { |
500 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
501 |
< |
assertFalse(p1.isTerminating()); |
500 |
> |
final ThreadPoolExecutor p = |
501 |
> |
new ThreadPoolExecutor(1, 1, |
502 |
> |
LONG_DELAY_MS, MILLISECONDS, |
503 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
504 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
505 |
> |
final CountDownLatch done = new CountDownLatch(1); |
506 |
|
try { |
507 |
< |
p1.execute(new SmallRunnable()); |
508 |
< |
assertFalse(p1.isTerminating()); |
509 |
< |
} finally { |
510 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
511 |
< |
} |
512 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
513 |
< |
assertTrue(p1.isTerminated()); |
514 |
< |
assertFalse(p1.isTerminating()); |
507 |
> |
assertFalse(p.isTerminating()); |
508 |
> |
p.execute(new CheckedRunnable() { |
509 |
> |
public void realRun() throws InterruptedException { |
510 |
> |
assertFalse(p.isTerminating()); |
511 |
> |
threadStarted.countDown(); |
512 |
> |
done.await(); |
513 |
> |
}}); |
514 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
515 |
> |
assertFalse(p.isTerminating()); |
516 |
> |
done.countDown(); |
517 |
> |
} finally { |
518 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
519 |
> |
} |
520 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
521 |
> |
assertTrue(p.isTerminated()); |
522 |
> |
assertFalse(p.isTerminating()); |
523 |
|
} |
524 |
|
|
525 |
|
/** |
526 |
|
* getQueue returns the work queue, which contains queued tasks |
527 |
|
*/ |
528 |
|
public void testGetQueue() throws InterruptedException { |
529 |
< |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
530 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
531 |
< |
FutureTask[] tasks = new FutureTask[5]; |
532 |
< |
for (int i = 0; i < 5; i++) { |
533 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
534 |
< |
p1.execute(tasks[i]); |
535 |
< |
} |
536 |
< |
try { |
537 |
< |
Thread.sleep(SHORT_DELAY_MS); |
538 |
< |
BlockingQueue<Runnable> wq = p1.getQueue(); |
539 |
< |
assertSame(q, wq); |
540 |
< |
assertFalse(wq.contains(tasks[0])); |
541 |
< |
assertTrue(wq.contains(tasks[4])); |
542 |
< |
for (int i = 1; i < 5; ++i) |
543 |
< |
tasks[i].cancel(true); |
544 |
< |
p1.shutdownNow(); |
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 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
535 |
> |
final CountDownLatch done = new CountDownLatch(1); |
536 |
> |
try { |
537 |
> |
FutureTask[] tasks = new FutureTask[5]; |
538 |
> |
for (int i = 0; i < tasks.length; i++) { |
539 |
> |
Callable task = new CheckedCallable<Boolean>() { |
540 |
> |
public Boolean realCall() throws InterruptedException { |
541 |
> |
threadStarted.countDown(); |
542 |
> |
assertSame(q, p.getQueue()); |
543 |
> |
done.await(); |
544 |
> |
return Boolean.TRUE; |
545 |
> |
}}; |
546 |
> |
tasks[i] = new FutureTask(task); |
547 |
> |
p.execute(tasks[i]); |
548 |
> |
} |
549 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
550 |
> |
assertSame(q, p.getQueue()); |
551 |
> |
assertFalse(q.contains(tasks[0])); |
552 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
553 |
> |
assertEquals(tasks.length - 1, q.size()); |
554 |
|
} finally { |
555 |
< |
joinPool(p1); |
555 |
> |
done.countDown(); |
556 |
> |
joinPool(p); |
557 |
|
} |
558 |
|
} |
559 |
|
|
562 |
|
*/ |
563 |
|
public void testRemove() throws InterruptedException { |
564 |
|
BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
565 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, q); |
566 |
< |
FutureTask[] tasks = new FutureTask[5]; |
567 |
< |
for (int i = 0; i < 5; i++) { |
568 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
569 |
< |
p1.execute(tasks[i]); |
570 |
< |
} |
571 |
< |
try { |
572 |
< |
Thread.sleep(SHORT_DELAY_MS); |
573 |
< |
assertFalse(p1.remove(tasks[0])); |
565 |
> |
final ThreadPoolExecutor p = |
566 |
> |
new ThreadPoolExecutor(1, 1, |
567 |
> |
LONG_DELAY_MS, MILLISECONDS, |
568 |
> |
q); |
569 |
> |
Runnable[] tasks = new Runnable[5]; |
570 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
571 |
> |
final CountDownLatch done = new CountDownLatch(1); |
572 |
> |
try { |
573 |
> |
for (int i = 0; i < tasks.length; i++) { |
574 |
> |
tasks[i] = new CheckedRunnable() { |
575 |
> |
public void realRun() throws InterruptedException { |
576 |
> |
threadStarted.countDown(); |
577 |
> |
done.await(); |
578 |
> |
}}; |
579 |
> |
p.execute(tasks[i]); |
580 |
> |
} |
581 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
582 |
> |
assertFalse(p.remove(tasks[0])); |
583 |
|
assertTrue(q.contains(tasks[4])); |
584 |
|
assertTrue(q.contains(tasks[3])); |
585 |
< |
assertTrue(p1.remove(tasks[4])); |
586 |
< |
assertFalse(p1.remove(tasks[4])); |
585 |
> |
assertTrue(p.remove(tasks[4])); |
586 |
> |
assertFalse(p.remove(tasks[4])); |
587 |
|
assertFalse(q.contains(tasks[4])); |
588 |
|
assertTrue(q.contains(tasks[3])); |
589 |
< |
assertTrue(p1.remove(tasks[3])); |
589 |
> |
assertTrue(p.remove(tasks[3])); |
590 |
|
assertFalse(q.contains(tasks[3])); |
591 |
|
} finally { |
592 |
< |
joinPool(p1); |
592 |
> |
done.countDown(); |
593 |
> |
joinPool(p); |
594 |
|
} |
595 |
|
} |
596 |
|
|
597 |
|
/** |
598 |
< |
* purge removes cancelled tasks from the queue |
598 |
> |
* purge removes cancelled tasks from the queue |
599 |
|
*/ |
600 |
< |
public void testPurge() { |
601 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
600 |
> |
public void testPurge() throws InterruptedException { |
601 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
602 |
> |
final CountDownLatch done = new CountDownLatch(1); |
603 |
> |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<Runnable>(10); |
604 |
> |
final ThreadPoolExecutor p = |
605 |
> |
new ThreadPoolExecutor(1, 1, |
606 |
> |
LONG_DELAY_MS, MILLISECONDS, |
607 |
> |
q); |
608 |
|
FutureTask[] tasks = new FutureTask[5]; |
609 |
< |
for (int i = 0; i < 5; i++) { |
610 |
< |
tasks[i] = new FutureTask(new MediumPossiblyInterruptedRunnable(), Boolean.TRUE); |
611 |
< |
p1.execute(tasks[i]); |
609 |
> |
try { |
610 |
> |
for (int i = 0; i < tasks.length; i++) { |
611 |
> |
Callable task = new CheckedCallable<Boolean>() { |
612 |
> |
public Boolean realCall() throws InterruptedException { |
613 |
> |
threadStarted.countDown(); |
614 |
> |
done.await(); |
615 |
> |
return Boolean.TRUE; |
616 |
> |
}}; |
617 |
> |
tasks[i] = new FutureTask(task); |
618 |
> |
p.execute(tasks[i]); |
619 |
> |
} |
620 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
621 |
> |
assertEquals(tasks.length, p.getTaskCount()); |
622 |
> |
assertEquals(tasks.length - 1, q.size()); |
623 |
> |
assertEquals(1L, p.getActiveCount()); |
624 |
> |
assertEquals(0L, p.getCompletedTaskCount()); |
625 |
> |
tasks[4].cancel(true); |
626 |
> |
tasks[3].cancel(false); |
627 |
> |
p.purge(); |
628 |
> |
assertEquals(tasks.length - 3, q.size()); |
629 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
630 |
> |
p.purge(); // Nothing to do |
631 |
> |
assertEquals(tasks.length - 3, q.size()); |
632 |
> |
assertEquals(tasks.length - 2, p.getTaskCount()); |
633 |
> |
} finally { |
634 |
> |
done.countDown(); |
635 |
> |
joinPool(p); |
636 |
|
} |
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); |
637 |
|
} |
638 |
|
|
639 |
|
/** |
640 |
< |
* shutDownNow returns a list containing tasks that were not run |
640 |
> |
* shutdownNow returns a list containing tasks that were not run, |
641 |
> |
* and those tasks are drained from the queue |
642 |
|
*/ |
643 |
< |
public void testShutDownNow() { |
644 |
< |
ThreadPoolExecutor p1 = new ThreadPoolExecutor(1, 1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
645 |
< |
List l; |
646 |
< |
try { |
647 |
< |
for (int i = 0; i < 5; i++) |
648 |
< |
p1.execute(new MediumPossiblyInterruptedRunnable()); |
649 |
< |
} |
650 |
< |
finally { |
643 |
> |
public void testShutdownNow() throws InterruptedException { |
644 |
> |
final int poolSize = 2; |
645 |
> |
final int count = 5; |
646 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
647 |
> |
final ThreadPoolExecutor p = |
648 |
> |
new ThreadPoolExecutor(poolSize, poolSize, |
649 |
> |
LONG_DELAY_MS, MILLISECONDS, |
650 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
651 |
> |
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
652 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
653 |
> |
threadsStarted.countDown(); |
654 |
|
try { |
655 |
< |
l = p1.shutdownNow(); |
656 |
< |
} catch (SecurityException ok) { return; } |
657 |
< |
} |
658 |
< |
assertTrue(p1.isShutdown()); |
659 |
< |
assertTrue(l.size() <= 4); |
655 |
> |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
656 |
> |
} catch (InterruptedException success) {} |
657 |
> |
ran.getAndIncrement(); |
658 |
> |
}}; |
659 |
> |
for (int i = 0; i < count; i++) |
660 |
> |
p.execute(waiter); |
661 |
> |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
662 |
> |
assertEquals(poolSize, p.getActiveCount()); |
663 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
664 |
> |
final List<Runnable> queuedTasks; |
665 |
> |
try { |
666 |
> |
queuedTasks = p.shutdownNow(); |
667 |
> |
} catch (SecurityException ok) { |
668 |
> |
return; // Allowed in case test doesn't have privs |
669 |
> |
} |
670 |
> |
assertTrue(p.isShutdown()); |
671 |
> |
assertTrue(p.getQueue().isEmpty()); |
672 |
> |
assertEquals(count - poolSize, queuedTasks.size()); |
673 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
674 |
> |
assertTrue(p.isTerminated()); |
675 |
> |
assertEquals(poolSize, ran.get()); |
676 |
> |
assertEquals(poolSize, p.getCompletedTaskCount()); |
677 |
|
} |
678 |
|
|
679 |
|
// Exception Tests |
680 |
|
|
395 |
– |
|
681 |
|
/** |
682 |
|
* Constructor throws if corePoolSize argument is less than zero |
683 |
|
*/ |
684 |
|
public void testConstructor1() { |
685 |
|
try { |
686 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
686 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
687 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
688 |
|
shouldThrow(); |
689 |
|
} catch (IllegalArgumentException success) {} |
690 |
|
} |
694 |
|
*/ |
695 |
|
public void testConstructor2() { |
696 |
|
try { |
697 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
697 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
698 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
699 |
|
shouldThrow(); |
700 |
|
} catch (IllegalArgumentException success) {} |
701 |
|
} |
705 |
|
*/ |
706 |
|
public void testConstructor3() { |
707 |
|
try { |
708 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
708 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
709 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
710 |
|
shouldThrow(); |
711 |
|
} catch (IllegalArgumentException success) {} |
712 |
|
} |
716 |
|
*/ |
717 |
|
public void testConstructor4() { |
718 |
|
try { |
719 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
719 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
720 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
721 |
|
shouldThrow(); |
722 |
|
} catch (IllegalArgumentException success) {} |
723 |
|
} |
727 |
|
*/ |
728 |
|
public void testConstructor5() { |
729 |
|
try { |
730 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
730 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
731 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
732 |
|
shouldThrow(); |
733 |
|
} catch (IllegalArgumentException success) {} |
734 |
|
} |
738 |
|
*/ |
739 |
|
public void testConstructorNullPointerException() { |
740 |
|
try { |
741 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null); |
741 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
742 |
> |
(BlockingQueue) null); |
743 |
|
shouldThrow(); |
744 |
|
} catch (NullPointerException success) {} |
745 |
|
} |
746 |
|
|
456 |
– |
|
457 |
– |
|
747 |
|
/** |
748 |
|
* Constructor throws if corePoolSize argument is less than zero |
749 |
|
*/ |
750 |
|
public void testConstructor6() { |
751 |
|
try { |
752 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
752 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
753 |
> |
new ArrayBlockingQueue<Runnable>(10), |
754 |
> |
new SimpleThreadFactory()); |
755 |
|
shouldThrow(); |
756 |
|
} catch (IllegalArgumentException success) {} |
757 |
|
} |
761 |
|
*/ |
762 |
|
public void testConstructor7() { |
763 |
|
try { |
764 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
764 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
765 |
> |
new ArrayBlockingQueue<Runnable>(10), |
766 |
> |
new SimpleThreadFactory()); |
767 |
|
shouldThrow(); |
768 |
|
} catch (IllegalArgumentException success) {} |
769 |
|
} |
773 |
|
*/ |
774 |
|
public void testConstructor8() { |
775 |
|
try { |
776 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
776 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
777 |
> |
new ArrayBlockingQueue<Runnable>(10), |
778 |
> |
new SimpleThreadFactory()); |
779 |
|
shouldThrow(); |
780 |
|
} catch (IllegalArgumentException success) {} |
781 |
|
} |
785 |
|
*/ |
786 |
|
public void testConstructor9() { |
787 |
|
try { |
788 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
788 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
789 |
> |
new ArrayBlockingQueue<Runnable>(10), |
790 |
> |
new SimpleThreadFactory()); |
791 |
|
shouldThrow(); |
792 |
|
} catch (IllegalArgumentException success) {} |
793 |
|
} |
797 |
|
*/ |
798 |
|
public void testConstructor10() { |
799 |
|
try { |
800 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory()); |
800 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
801 |
> |
new ArrayBlockingQueue<Runnable>(10), |
802 |
> |
new SimpleThreadFactory()); |
803 |
|
shouldThrow(); |
804 |
|
} catch (IllegalArgumentException success) {} |
805 |
|
} |
809 |
|
*/ |
810 |
|
public void testConstructorNullPointerException2() { |
811 |
|
try { |
812 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory()); |
812 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
813 |
> |
(BlockingQueue) null, |
814 |
> |
new SimpleThreadFactory()); |
815 |
|
shouldThrow(); |
816 |
|
} catch (NullPointerException success) {} |
817 |
|
} |
821 |
|
*/ |
822 |
|
public void testConstructorNullPointerException3() { |
823 |
|
try { |
824 |
< |
ThreadFactory f = null; |
825 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f); |
824 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
825 |
> |
new ArrayBlockingQueue<Runnable>(10), |
826 |
> |
(ThreadFactory) null); |
827 |
|
shouldThrow(); |
828 |
|
} catch (NullPointerException success) {} |
829 |
|
} |
830 |
|
|
529 |
– |
|
831 |
|
/** |
832 |
|
* Constructor throws if corePoolSize argument is less than zero |
833 |
|
*/ |
834 |
|
public void testConstructor11() { |
835 |
|
try { |
836 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
836 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
837 |
> |
new ArrayBlockingQueue<Runnable>(10), |
838 |
> |
new NoOpREHandler()); |
839 |
|
shouldThrow(); |
840 |
|
} catch (IllegalArgumentException success) {} |
841 |
|
} |
845 |
|
*/ |
846 |
|
public void testConstructor12() { |
847 |
|
try { |
848 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
848 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
849 |
> |
new ArrayBlockingQueue<Runnable>(10), |
850 |
> |
new NoOpREHandler()); |
851 |
|
shouldThrow(); |
852 |
|
} catch (IllegalArgumentException success) {} |
853 |
|
} |
857 |
|
*/ |
858 |
|
public void testConstructor13() { |
859 |
|
try { |
860 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
860 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
861 |
> |
new ArrayBlockingQueue<Runnable>(10), |
862 |
> |
new NoOpREHandler()); |
863 |
|
shouldThrow(); |
864 |
|
} catch (IllegalArgumentException success) {} |
865 |
|
} |
869 |
|
*/ |
870 |
|
public void testConstructor14() { |
871 |
|
try { |
872 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
872 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
873 |
> |
new ArrayBlockingQueue<Runnable>(10), |
874 |
> |
new NoOpREHandler()); |
875 |
|
shouldThrow(); |
876 |
|
} catch (IllegalArgumentException success) {} |
877 |
|
} |
881 |
|
*/ |
882 |
|
public void testConstructor15() { |
883 |
|
try { |
884 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new NoOpREHandler()); |
884 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
885 |
> |
new ArrayBlockingQueue<Runnable>(10), |
886 |
> |
new NoOpREHandler()); |
887 |
|
shouldThrow(); |
888 |
|
} catch (IllegalArgumentException success) {} |
889 |
|
} |
893 |
|
*/ |
894 |
|
public void testConstructorNullPointerException4() { |
895 |
|
try { |
896 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new NoOpREHandler()); |
896 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
897 |
> |
(BlockingQueue) null, |
898 |
> |
new NoOpREHandler()); |
899 |
|
shouldThrow(); |
900 |
|
} catch (NullPointerException success) {} |
901 |
|
} |
905 |
|
*/ |
906 |
|
public void testConstructorNullPointerException5() { |
907 |
|
try { |
908 |
< |
RejectedExecutionHandler r = null; |
909 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),r); |
908 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
909 |
> |
new ArrayBlockingQueue<Runnable>(10), |
910 |
> |
(RejectedExecutionHandler) null); |
911 |
|
shouldThrow(); |
912 |
|
} catch (NullPointerException success) {} |
913 |
|
} |
914 |
|
|
601 |
– |
|
915 |
|
/** |
916 |
|
* Constructor throws if corePoolSize argument is less than zero |
917 |
|
*/ |
918 |
|
public void testConstructor16() { |
919 |
|
try { |
920 |
< |
new ThreadPoolExecutor(-1,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
920 |
> |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
921 |
> |
new ArrayBlockingQueue<Runnable>(10), |
922 |
> |
new SimpleThreadFactory(), |
923 |
> |
new NoOpREHandler()); |
924 |
|
shouldThrow(); |
925 |
|
} catch (IllegalArgumentException success) {} |
926 |
|
} |
930 |
|
*/ |
931 |
|
public void testConstructor17() { |
932 |
|
try { |
933 |
< |
new ThreadPoolExecutor(1,-1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
933 |
> |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
934 |
> |
new ArrayBlockingQueue<Runnable>(10), |
935 |
> |
new SimpleThreadFactory(), |
936 |
> |
new NoOpREHandler()); |
937 |
|
shouldThrow(); |
938 |
|
} catch (IllegalArgumentException success) {} |
939 |
|
} |
943 |
|
*/ |
944 |
|
public void testConstructor18() { |
945 |
|
try { |
946 |
< |
new ThreadPoolExecutor(1,0,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
946 |
> |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
947 |
> |
new ArrayBlockingQueue<Runnable>(10), |
948 |
> |
new SimpleThreadFactory(), |
949 |
> |
new NoOpREHandler()); |
950 |
|
shouldThrow(); |
951 |
|
} catch (IllegalArgumentException success) {} |
952 |
|
} |
956 |
|
*/ |
957 |
|
public void testConstructor19() { |
958 |
|
try { |
959 |
< |
new ThreadPoolExecutor(1,2,-1L,MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
959 |
> |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
960 |
> |
new ArrayBlockingQueue<Runnable>(10), |
961 |
> |
new SimpleThreadFactory(), |
962 |
> |
new NoOpREHandler()); |
963 |
|
shouldThrow(); |
964 |
|
} catch (IllegalArgumentException success) {} |
965 |
|
} |
969 |
|
*/ |
970 |
|
public void testConstructor20() { |
971 |
|
try { |
972 |
< |
new ThreadPoolExecutor(2,1,LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),new NoOpREHandler()); |
972 |
> |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
973 |
> |
new ArrayBlockingQueue<Runnable>(10), |
974 |
> |
new SimpleThreadFactory(), |
975 |
> |
new NoOpREHandler()); |
976 |
|
shouldThrow(); |
977 |
|
} catch (IllegalArgumentException success) {} |
978 |
|
} |
979 |
|
|
980 |
|
/** |
981 |
< |
* Constructor throws if workQueue is set to null |
981 |
> |
* Constructor throws if workQueue is null |
982 |
|
*/ |
983 |
|
public void testConstructorNullPointerException6() { |
984 |
|
try { |
985 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,null,new SimpleThreadFactory(),new NoOpREHandler()); |
985 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
986 |
> |
(BlockingQueue) null, |
987 |
> |
new SimpleThreadFactory(), |
988 |
> |
new NoOpREHandler()); |
989 |
|
shouldThrow(); |
990 |
|
} catch (NullPointerException success) {} |
991 |
|
} |
992 |
|
|
993 |
|
/** |
994 |
< |
* Constructor throws if handler is set to null |
994 |
> |
* Constructor throws if handler is null |
995 |
|
*/ |
996 |
|
public void testConstructorNullPointerException7() { |
997 |
|
try { |
998 |
< |
RejectedExecutionHandler r = null; |
999 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),new SimpleThreadFactory(),r); |
998 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
999 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1000 |
> |
new SimpleThreadFactory(), |
1001 |
> |
(RejectedExecutionHandler) null); |
1002 |
|
shouldThrow(); |
1003 |
|
} catch (NullPointerException success) {} |
1004 |
|
} |
1005 |
|
|
1006 |
|
/** |
1007 |
< |
* Constructor throws if ThreadFactory is set top null |
1007 |
> |
* Constructor throws if ThreadFactory is null |
1008 |
|
*/ |
1009 |
|
public void testConstructorNullPointerException8() { |
1010 |
|
try { |
1011 |
< |
ThreadFactory f = null; |
1012 |
< |
new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10),f,new NoOpREHandler()); |
1011 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1012 |
> |
new ArrayBlockingQueue<Runnable>(10), |
1013 |
> |
(ThreadFactory) null, |
1014 |
> |
new NoOpREHandler()); |
1015 |
|
shouldThrow(); |
1016 |
|
} catch (NullPointerException success) {} |
1017 |
|
} |
1018 |
|
|
1019 |
+ |
/** |
1020 |
+ |
* get of submitted callable throws InterruptedException if interrupted |
1021 |
+ |
*/ |
1022 |
+ |
public void testInterruptedSubmit() throws InterruptedException { |
1023 |
+ |
final ThreadPoolExecutor p = |
1024 |
+ |
new ThreadPoolExecutor(1, 1, |
1025 |
+ |
60, SECONDS, |
1026 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1027 |
+ |
|
1028 |
+ |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1029 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1030 |
+ |
try { |
1031 |
+ |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1032 |
+ |
public void realRun() throws Exception { |
1033 |
+ |
Callable task = new CheckedCallable<Boolean>() { |
1034 |
+ |
public Boolean realCall() throws InterruptedException { |
1035 |
+ |
threadStarted.countDown(); |
1036 |
+ |
done.await(); |
1037 |
+ |
return Boolean.TRUE; |
1038 |
+ |
}}; |
1039 |
+ |
p.submit(task).get(); |
1040 |
+ |
}}); |
1041 |
+ |
|
1042 |
+ |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
1043 |
+ |
t.interrupt(); |
1044 |
+ |
awaitTermination(t, MEDIUM_DELAY_MS); |
1045 |
+ |
} finally { |
1046 |
+ |
done.countDown(); |
1047 |
+ |
joinPool(p); |
1048 |
+ |
} |
1049 |
+ |
} |
1050 |
|
|
1051 |
|
/** |
1052 |
< |
* execute throws RejectedExecutionException |
687 |
< |
* if saturated. |
1052 |
> |
* execute throws RejectedExecutionException if saturated. |
1053 |
|
*/ |
1054 |
|
public void testSaturatedExecute() { |
1055 |
|
ThreadPoolExecutor p = |
1056 |
|
new ThreadPoolExecutor(1, 1, |
1057 |
|
LONG_DELAY_MS, MILLISECONDS, |
1058 |
|
new ArrayBlockingQueue<Runnable>(1)); |
1059 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1060 |
+ |
try { |
1061 |
+ |
Runnable task = new CheckedRunnable() { |
1062 |
+ |
public void realRun() throws InterruptedException { |
1063 |
+ |
done.await(); |
1064 |
+ |
}}; |
1065 |
+ |
for (int i = 0; i < 2; ++i) |
1066 |
+ |
p.execute(task); |
1067 |
+ |
for (int i = 0; i < 2; ++i) { |
1068 |
+ |
try { |
1069 |
+ |
p.execute(task); |
1070 |
+ |
shouldThrow(); |
1071 |
+ |
} catch (RejectedExecutionException success) {} |
1072 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1073 |
+ |
} |
1074 |
+ |
} finally { |
1075 |
+ |
done.countDown(); |
1076 |
+ |
joinPool(p); |
1077 |
+ |
} |
1078 |
+ |
} |
1079 |
+ |
|
1080 |
+ |
/** |
1081 |
+ |
* submit(runnable) throws RejectedExecutionException if saturated. |
1082 |
+ |
*/ |
1083 |
+ |
public void testSaturatedSubmitRunnable() { |
1084 |
+ |
ThreadPoolExecutor p = |
1085 |
+ |
new ThreadPoolExecutor(1, 1, |
1086 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1087 |
+ |
new ArrayBlockingQueue<Runnable>(1)); |
1088 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1089 |
+ |
try { |
1090 |
+ |
Runnable task = new CheckedRunnable() { |
1091 |
+ |
public void realRun() throws InterruptedException { |
1092 |
+ |
done.await(); |
1093 |
+ |
}}; |
1094 |
+ |
for (int i = 0; i < 2; ++i) |
1095 |
+ |
p.submit(task); |
1096 |
+ |
for (int i = 0; i < 2; ++i) { |
1097 |
+ |
try { |
1098 |
+ |
p.execute(task); |
1099 |
+ |
shouldThrow(); |
1100 |
+ |
} catch (RejectedExecutionException success) {} |
1101 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1102 |
+ |
} |
1103 |
+ |
} finally { |
1104 |
+ |
done.countDown(); |
1105 |
+ |
joinPool(p); |
1106 |
+ |
} |
1107 |
+ |
} |
1108 |
+ |
|
1109 |
+ |
/** |
1110 |
+ |
* submit(callable) throws RejectedExecutionException if saturated. |
1111 |
+ |
*/ |
1112 |
+ |
public void testSaturatedSubmitCallable() { |
1113 |
+ |
ThreadPoolExecutor p = |
1114 |
+ |
new ThreadPoolExecutor(1, 1, |
1115 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1116 |
+ |
new ArrayBlockingQueue<Runnable>(1)); |
1117 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
1118 |
|
try { |
1119 |
+ |
Runnable task = new CheckedRunnable() { |
1120 |
+ |
public void realRun() throws InterruptedException { |
1121 |
+ |
done.await(); |
1122 |
+ |
}}; |
1123 |
|
for (int i = 0; i < 2; ++i) |
1124 |
< |
p.execute(new MediumRunnable()); |
1124 |
> |
p.submit(Executors.callable(task)); |
1125 |
|
for (int i = 0; i < 2; ++i) { |
1126 |
|
try { |
1127 |
< |
p.execute(new MediumRunnable()); |
1127 |
> |
p.execute(task); |
1128 |
|
shouldThrow(); |
1129 |
|
} catch (RejectedExecutionException success) {} |
1130 |
+ |
assertTrue(p.getTaskCount() <= 2); |
1131 |
|
} |
1132 |
|
} finally { |
1133 |
+ |
done.countDown(); |
1134 |
|
joinPool(p); |
1135 |
|
} |
1136 |
|
} |
1137 |
|
|
1138 |
|
/** |
1139 |
< |
* executor using CallerRunsPolicy runs task if saturated. |
1139 |
> |
* executor using CallerRunsPolicy runs task if saturated. |
1140 |
|
*/ |
1141 |
|
public void testSaturatedExecute2() { |
1142 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1143 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1143 |
> |
final ThreadPoolExecutor p = |
1144 |
> |
new ThreadPoolExecutor(1, 1, |
1145 |
> |
LONG_DELAY_MS, |
1146 |
> |
MILLISECONDS, |
1147 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1148 |
> |
h); |
1149 |
|
try { |
715 |
– |
|
1150 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1151 |
< |
for (int i = 0; i < 5; ++i) { |
1151 |
> |
for (int i = 0; i < tasks.length; ++i) |
1152 |
|
tasks[i] = new TrackedNoOpRunnable(); |
719 |
– |
} |
1153 |
|
TrackedLongRunnable mr = new TrackedLongRunnable(); |
1154 |
|
p.execute(mr); |
1155 |
< |
for (int i = 0; i < 5; ++i) { |
1155 |
> |
for (int i = 0; i < tasks.length; ++i) |
1156 |
|
p.execute(tasks[i]); |
1157 |
< |
} |
725 |
< |
for (int i = 1; i < 5; ++i) { |
1157 |
> |
for (int i = 1; i < tasks.length; ++i) |
1158 |
|
assertTrue(tasks[i].done); |
727 |
– |
} |
1159 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1160 |
|
} finally { |
1161 |
|
joinPool(p); |
1163 |
|
} |
1164 |
|
|
1165 |
|
/** |
1166 |
< |
* executor using DiscardPolicy drops task if saturated. |
1166 |
> |
* executor using DiscardPolicy drops task if saturated. |
1167 |
|
*/ |
1168 |
|
public void testSaturatedExecute3() { |
1169 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1170 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1170 |
> |
final ThreadPoolExecutor p = |
1171 |
> |
new ThreadPoolExecutor(1, 1, |
1172 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1173 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1174 |
> |
h); |
1175 |
|
try { |
741 |
– |
|
1176 |
|
TrackedNoOpRunnable[] tasks = new TrackedNoOpRunnable[5]; |
1177 |
< |
for (int i = 0; i < 5; ++i) { |
1177 |
> |
for (int i = 0; i < tasks.length; ++i) |
1178 |
|
tasks[i] = new TrackedNoOpRunnable(); |
745 |
– |
} |
1179 |
|
p.execute(new TrackedLongRunnable()); |
1180 |
< |
for (int i = 0; i < 5; ++i) { |
1181 |
< |
p.execute(tasks[i]); |
1182 |
< |
} |
1183 |
< |
for (int i = 0; i < 5; ++i) { |
751 |
< |
assertFalse(tasks[i].done); |
752 |
< |
} |
1180 |
> |
for (TrackedNoOpRunnable task : tasks) |
1181 |
> |
p.execute(task); |
1182 |
> |
for (TrackedNoOpRunnable task : tasks) |
1183 |
> |
assertFalse(task.done); |
1184 |
|
try { p.shutdownNow(); } catch (SecurityException ok) { return; } |
1185 |
|
} finally { |
1186 |
|
joinPool(p); |
1188 |
|
} |
1189 |
|
|
1190 |
|
/** |
1191 |
< |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1191 |
> |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1192 |
|
*/ |
1193 |
|
public void testSaturatedExecute4() { |
1194 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1195 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1195 |
> |
final ThreadPoolExecutor p = |
1196 |
> |
new ThreadPoolExecutor(1, 1, |
1197 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1198 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1199 |
> |
h); |
1200 |
|
try { |
1201 |
|
p.execute(new TrackedLongRunnable()); |
1202 |
|
TrackedLongRunnable r2 = new TrackedLongRunnable(); |
1213 |
|
} |
1214 |
|
|
1215 |
|
/** |
1216 |
< |
* execute throws RejectedExecutionException if shutdown |
1216 |
> |
* execute throws RejectedExecutionException if shutdown |
1217 |
|
*/ |
1218 |
|
public void testRejectedExecutionExceptionOnShutdown() { |
1219 |
< |
ThreadPoolExecutor tpe = |
1220 |
< |
new ThreadPoolExecutor(1,1,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(1)); |
1221 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1219 |
> |
ThreadPoolExecutor p = |
1220 |
> |
new ThreadPoolExecutor(1, 1, |
1221 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1222 |
> |
new ArrayBlockingQueue<Runnable>(1)); |
1223 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1224 |
|
try { |
1225 |
< |
tpe.execute(new NoOpRunnable()); |
1225 |
> |
p.execute(new NoOpRunnable()); |
1226 |
|
shouldThrow(); |
1227 |
|
} catch (RejectedExecutionException success) {} |
1228 |
|
|
1229 |
< |
joinPool(tpe); |
1229 |
> |
joinPool(p); |
1230 |
|
} |
1231 |
|
|
1232 |
|
/** |
1233 |
< |
* execute using CallerRunsPolicy drops task on shutdown |
1233 |
> |
* execute using CallerRunsPolicy drops task on shutdown |
1234 |
|
*/ |
1235 |
|
public void testCallerRunsOnShutdown() { |
1236 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.CallerRunsPolicy(); |
1237 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1237 |
> |
final ThreadPoolExecutor p = |
1238 |
> |
new ThreadPoolExecutor(1, 1, |
1239 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1240 |
> |
new ArrayBlockingQueue<Runnable>(1), h); |
1241 |
|
|
1242 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1243 |
|
try { |
1250 |
|
} |
1251 |
|
|
1252 |
|
/** |
1253 |
< |
* execute using DiscardPolicy drops task on shutdown |
1253 |
> |
* execute using DiscardPolicy drops task on shutdown |
1254 |
|
*/ |
1255 |
|
public void testDiscardOnShutdown() { |
1256 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardPolicy(); |
1257 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1257 |
> |
ThreadPoolExecutor p = |
1258 |
> |
new ThreadPoolExecutor(1, 1, |
1259 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1260 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1261 |
> |
h); |
1262 |
|
|
1263 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1264 |
|
try { |
1270 |
|
} |
1271 |
|
} |
1272 |
|
|
829 |
– |
|
1273 |
|
/** |
1274 |
< |
* execute using DiscardOldestPolicy drops task on shutdown |
1274 |
> |
* execute using DiscardOldestPolicy drops task on shutdown |
1275 |
|
*/ |
1276 |
|
public void testDiscardOldestOnShutdown() { |
1277 |
|
RejectedExecutionHandler h = new ThreadPoolExecutor.DiscardOldestPolicy(); |
1278 |
< |
ThreadPoolExecutor p = new ThreadPoolExecutor(1,1, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(1), h); |
1278 |
> |
ThreadPoolExecutor p = |
1279 |
> |
new ThreadPoolExecutor(1, 1, |
1280 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1281 |
> |
new ArrayBlockingQueue<Runnable>(1), |
1282 |
> |
h); |
1283 |
|
|
1284 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1285 |
|
try { |
1291 |
|
} |
1292 |
|
} |
1293 |
|
|
847 |
– |
|
1294 |
|
/** |
1295 |
< |
* execute (null) throws NPE |
1295 |
> |
* execute(null) throws NPE |
1296 |
|
*/ |
1297 |
|
public void testExecuteNull() { |
1298 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(1,2,LONG_DELAY_MS, MILLISECONDS,new ArrayBlockingQueue<Runnable>(10)); |
1298 |
> |
ThreadPoolExecutor p = |
1299 |
> |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1300 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1301 |
|
try { |
1302 |
< |
tpe.execute(null); |
1302 |
> |
p.execute(null); |
1303 |
|
shouldThrow(); |
1304 |
|
} catch (NullPointerException success) {} |
1305 |
|
|
1306 |
< |
joinPool(tpe); |
1306 |
> |
joinPool(p); |
1307 |
|
} |
1308 |
|
|
1309 |
|
/** |
1310 |
< |
* setCorePoolSize of negative value throws IllegalArgumentException |
1310 |
> |
* setCorePoolSize of negative value throws IllegalArgumentException |
1311 |
|
*/ |
1312 |
|
public void testCorePoolSizeIllegalArgumentException() { |
1313 |
< |
ThreadPoolExecutor tpe = |
1313 |
> |
ThreadPoolExecutor p = |
1314 |
|
new ThreadPoolExecutor(1, 2, |
1315 |
|
LONG_DELAY_MS, MILLISECONDS, |
1316 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1317 |
|
try { |
1318 |
< |
tpe.setCorePoolSize(-1); |
1318 |
> |
p.setCorePoolSize(-1); |
1319 |
|
shouldThrow(); |
1320 |
|
} catch (IllegalArgumentException success) { |
1321 |
|
} finally { |
1322 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1322 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1323 |
|
} |
1324 |
< |
joinPool(tpe); |
1324 |
> |
joinPool(p); |
1325 |
|
} |
1326 |
|
|
1327 |
|
/** |
1328 |
< |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1329 |
< |
* given a value less the core pool size |
1328 |
> |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1329 |
> |
* given a value less the core pool size |
1330 |
|
*/ |
1331 |
|
public void testMaximumPoolSizeIllegalArgumentException() { |
1332 |
< |
ThreadPoolExecutor tpe = |
1332 |
> |
ThreadPoolExecutor p = |
1333 |
|
new ThreadPoolExecutor(2, 3, |
1334 |
|
LONG_DELAY_MS, MILLISECONDS, |
1335 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1336 |
|
try { |
1337 |
< |
tpe.setMaximumPoolSize(1); |
1337 |
> |
p.setMaximumPoolSize(1); |
1338 |
|
shouldThrow(); |
1339 |
|
} catch (IllegalArgumentException success) { |
1340 |
|
} finally { |
1341 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1341 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1342 |
|
} |
1343 |
< |
joinPool(tpe); |
1343 |
> |
joinPool(p); |
1344 |
|
} |
1345 |
|
|
1346 |
|
/** |
1347 |
< |
* setMaximumPoolSize throws IllegalArgumentException |
1348 |
< |
* if given a negative value |
1347 |
> |
* setMaximumPoolSize throws IllegalArgumentException |
1348 |
> |
* if given a negative value |
1349 |
|
*/ |
1350 |
|
public void testMaximumPoolSizeIllegalArgumentException2() { |
1351 |
< |
ThreadPoolExecutor tpe = |
1351 |
> |
ThreadPoolExecutor p = |
1352 |
|
new ThreadPoolExecutor(2, 3, |
1353 |
|
LONG_DELAY_MS, MILLISECONDS, |
1354 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1355 |
|
try { |
1356 |
< |
tpe.setMaximumPoolSize(-1); |
1356 |
> |
p.setMaximumPoolSize(-1); |
1357 |
|
shouldThrow(); |
1358 |
|
} catch (IllegalArgumentException success) { |
1359 |
|
} finally { |
1360 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1360 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1361 |
|
} |
1362 |
< |
joinPool(tpe); |
1362 |
> |
joinPool(p); |
1363 |
|
} |
1364 |
|
|
1365 |
+ |
/** |
1366 |
+ |
* Configuration changes that allow core pool size greater than |
1367 |
+ |
* max pool size result in IllegalArgumentException. |
1368 |
+ |
*/ |
1369 |
+ |
public void testPoolSizeInvariants() { |
1370 |
+ |
ThreadPoolExecutor p = |
1371 |
+ |
new ThreadPoolExecutor(1, 1, |
1372 |
+ |
LONG_DELAY_MS, MILLISECONDS, |
1373 |
+ |
new ArrayBlockingQueue<Runnable>(10)); |
1374 |
+ |
for (int s = 1; s < 5; s++) { |
1375 |
+ |
p.setMaximumPoolSize(s); |
1376 |
+ |
p.setCorePoolSize(s); |
1377 |
+ |
try { |
1378 |
+ |
p.setMaximumPoolSize(s - 1); |
1379 |
+ |
shouldThrow(); |
1380 |
+ |
} catch (IllegalArgumentException success) {} |
1381 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1382 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1383 |
+ |
try { |
1384 |
+ |
p.setCorePoolSize(s + 1); |
1385 |
+ |
shouldThrow(); |
1386 |
+ |
} catch (IllegalArgumentException success) {} |
1387 |
+ |
assertEquals(s, p.getCorePoolSize()); |
1388 |
+ |
assertEquals(s, p.getMaximumPoolSize()); |
1389 |
+ |
} |
1390 |
+ |
joinPool(p); |
1391 |
+ |
} |
1392 |
|
|
1393 |
|
/** |
1394 |
< |
* setKeepAliveTime throws IllegalArgumentException |
1395 |
< |
* when given a negative value |
1394 |
> |
* setKeepAliveTime throws IllegalArgumentException |
1395 |
> |
* when given a negative value |
1396 |
|
*/ |
1397 |
|
public void testKeepAliveTimeIllegalArgumentException() { |
1398 |
< |
ThreadPoolExecutor tpe = |
1398 |
> |
ThreadPoolExecutor p = |
1399 |
|
new ThreadPoolExecutor(2, 3, |
1400 |
|
LONG_DELAY_MS, MILLISECONDS, |
1401 |
|
new ArrayBlockingQueue<Runnable>(10)); |
1402 |
|
try { |
1403 |
< |
tpe.setKeepAliveTime(-1,MILLISECONDS); |
1403 |
> |
p.setKeepAliveTime(-1,MILLISECONDS); |
1404 |
|
shouldThrow(); |
1405 |
|
} catch (IllegalArgumentException success) { |
1406 |
|
} finally { |
1407 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1407 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1408 |
|
} |
1409 |
< |
joinPool(tpe); |
1409 |
> |
joinPool(p); |
1410 |
|
} |
1411 |
|
|
1412 |
|
/** |
1413 |
|
* terminated() is called on termination |
1414 |
|
*/ |
1415 |
|
public void testTerminated() { |
1416 |
< |
ExtendedTPE tpe = new ExtendedTPE(); |
1417 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1418 |
< |
assertTrue(tpe.terminatedCalled); |
1419 |
< |
joinPool(tpe); |
1416 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1417 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1418 |
> |
assertTrue(p.terminatedCalled()); |
1419 |
> |
joinPool(p); |
1420 |
|
} |
1421 |
|
|
1422 |
|
/** |
1423 |
|
* beforeExecute and afterExecute are called when executing task |
1424 |
|
*/ |
1425 |
|
public void testBeforeAfter() throws InterruptedException { |
1426 |
< |
ExtendedTPE tpe = new ExtendedTPE(); |
1426 |
> |
ExtendedTPE p = new ExtendedTPE(); |
1427 |
|
try { |
1428 |
< |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1429 |
< |
tpe.execute(r); |
1430 |
< |
Thread.sleep(SHORT_DELAY_MS); |
1431 |
< |
assertTrue(r.done); |
1432 |
< |
assertTrue(tpe.beforeCalled); |
1433 |
< |
assertTrue(tpe.afterCalled); |
1434 |
< |
try { tpe.shutdown(); } catch (SecurityException ok) { return; } |
1428 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1429 |
> |
p.execute(new CheckedRunnable() { |
1430 |
> |
public void realRun() { |
1431 |
> |
done.countDown(); |
1432 |
> |
}}); |
1433 |
> |
await(p.afterCalled); |
1434 |
> |
assertEquals(0, done.getCount()); |
1435 |
> |
assertTrue(p.afterCalled()); |
1436 |
> |
assertTrue(p.beforeCalled()); |
1437 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1438 |
|
} finally { |
1439 |
< |
joinPool(tpe); |
1439 |
> |
joinPool(p); |
1440 |
|
} |
1441 |
|
} |
1442 |
|
|
1444 |
|
* completed submit of callable returns result |
1445 |
|
*/ |
1446 |
|
public void testSubmitCallable() throws Exception { |
1447 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1447 |
> |
ExecutorService e = |
1448 |
> |
new ThreadPoolExecutor(2, 2, |
1449 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1450 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1451 |
|
try { |
1452 |
|
Future<String> future = e.submit(new StringTask()); |
1453 |
|
String result = future.get(); |
1461 |
|
* completed submit of runnable returns successfully |
1462 |
|
*/ |
1463 |
|
public void testSubmitRunnable() throws Exception { |
1464 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1464 |
> |
ExecutorService e = |
1465 |
> |
new ThreadPoolExecutor(2, 2, |
1466 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1467 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1468 |
|
try { |
1469 |
|
Future<?> future = e.submit(new NoOpRunnable()); |
1470 |
|
future.get(); |
1478 |
|
* completed submit of (runnable, result) returns result |
1479 |
|
*/ |
1480 |
|
public void testSubmitRunnable2() throws Exception { |
1481 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1481 |
> |
ExecutorService e = |
1482 |
> |
new ThreadPoolExecutor(2, 2, |
1483 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1484 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1485 |
|
try { |
1486 |
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1487 |
|
String result = future.get(); |
1491 |
|
} |
1492 |
|
} |
1493 |
|
|
1007 |
– |
|
1494 |
|
/** |
1495 |
|
* invokeAny(null) throws NPE |
1496 |
|
*/ |
1497 |
|
public void testInvokeAny1() throws Exception { |
1498 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1498 |
> |
ExecutorService e = |
1499 |
> |
new ThreadPoolExecutor(2, 2, |
1500 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1501 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1502 |
|
try { |
1503 |
|
e.invokeAny(null); |
1504 |
|
shouldThrow(); |
1512 |
|
* invokeAny(empty collection) throws IAE |
1513 |
|
*/ |
1514 |
|
public void testInvokeAny2() throws Exception { |
1515 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1515 |
> |
ExecutorService e = |
1516 |
> |
new ThreadPoolExecutor(2, 2, |
1517 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1518 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1519 |
|
try { |
1520 |
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1521 |
|
shouldThrow(); |
1530 |
|
*/ |
1531 |
|
public void testInvokeAny3() throws Exception { |
1532 |
|
final CountDownLatch latch = new CountDownLatch(1); |
1533 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1533 |
> |
final ExecutorService e = |
1534 |
> |
new ThreadPoolExecutor(2, 2, |
1535 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1536 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1537 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1538 |
> |
l.add(latchAwaitingStringTask(latch)); |
1539 |
> |
l.add(null); |
1540 |
|
try { |
1043 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1044 |
– |
l.add(new Callable<String>() { |
1045 |
– |
public String call() { |
1046 |
– |
try { |
1047 |
– |
latch.await(); |
1048 |
– |
} catch (InterruptedException ok) {} |
1049 |
– |
return TEST_STRING; |
1050 |
– |
}}); |
1051 |
– |
l.add(null); |
1541 |
|
e.invokeAny(l); |
1542 |
|
shouldThrow(); |
1543 |
|
} catch (NullPointerException success) { |
1551 |
|
* invokeAny(c) throws ExecutionException if no task completes |
1552 |
|
*/ |
1553 |
|
public void testInvokeAny4() throws Exception { |
1554 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1554 |
> |
ExecutorService e = |
1555 |
> |
new ThreadPoolExecutor(2, 2, |
1556 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1557 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1558 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1559 |
> |
l.add(new NPETask()); |
1560 |
|
try { |
1067 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1068 |
– |
l.add(new NPETask()); |
1561 |
|
e.invokeAny(l); |
1562 |
|
shouldThrow(); |
1563 |
|
} catch (ExecutionException success) { |
1571 |
|
* invokeAny(c) returns result of some task |
1572 |
|
*/ |
1573 |
|
public void testInvokeAny5() throws Exception { |
1574 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1574 |
> |
ExecutorService e = |
1575 |
> |
new ThreadPoolExecutor(2, 2, |
1576 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1577 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1578 |
|
try { |
1579 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1579 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1580 |
|
l.add(new StringTask()); |
1581 |
|
l.add(new StringTask()); |
1582 |
|
String result = e.invokeAny(l); |
1590 |
|
* invokeAll(null) throws NPE |
1591 |
|
*/ |
1592 |
|
public void testInvokeAll1() throws Exception { |
1593 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1593 |
> |
ExecutorService e = |
1594 |
> |
new ThreadPoolExecutor(2, 2, |
1595 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1596 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1597 |
|
try { |
1598 |
|
e.invokeAll(null); |
1599 |
|
shouldThrow(); |
1607 |
|
* invokeAll(empty collection) returns empty collection |
1608 |
|
*/ |
1609 |
|
public void testInvokeAll2() throws InterruptedException { |
1610 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1610 |
> |
ExecutorService e = |
1611 |
> |
new ThreadPoolExecutor(2, 2, |
1612 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1613 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1614 |
|
try { |
1615 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
1616 |
|
assertTrue(r.isEmpty()); |
1623 |
|
* invokeAll(c) throws NPE if c has null elements |
1624 |
|
*/ |
1625 |
|
public void testInvokeAll3() throws Exception { |
1626 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1626 |
> |
ExecutorService e = |
1627 |
> |
new ThreadPoolExecutor(2, 2, |
1628 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1629 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1630 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1631 |
> |
l.add(new StringTask()); |
1632 |
> |
l.add(null); |
1633 |
|
try { |
1127 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1128 |
– |
l.add(new StringTask()); |
1129 |
– |
l.add(null); |
1634 |
|
e.invokeAll(l); |
1635 |
|
shouldThrow(); |
1636 |
|
} catch (NullPointerException success) { |
1643 |
|
* get of element of invokeAll(c) throws exception on failed task |
1644 |
|
*/ |
1645 |
|
public void testInvokeAll4() throws Exception { |
1646 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1646 |
> |
ExecutorService e = |
1647 |
> |
new ThreadPoolExecutor(2, 2, |
1648 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1649 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1650 |
|
try { |
1651 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1651 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1652 |
|
l.add(new NPETask()); |
1653 |
< |
List<Future<String>> result = e.invokeAll(l); |
1654 |
< |
assertEquals(1, result.size()); |
1655 |
< |
for (Future<String> future : result) { |
1656 |
< |
try { |
1657 |
< |
future.get(); |
1658 |
< |
shouldThrow(); |
1659 |
< |
} catch (ExecutionException success) { |
1153 |
< |
Throwable cause = success.getCause(); |
1154 |
< |
assertTrue(cause instanceof NullPointerException); |
1155 |
< |
} |
1653 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1654 |
> |
assertEquals(1, futures.size()); |
1655 |
> |
try { |
1656 |
> |
futures.get(0).get(); |
1657 |
> |
shouldThrow(); |
1658 |
> |
} catch (ExecutionException success) { |
1659 |
> |
assertTrue(success.getCause() instanceof NullPointerException); |
1660 |
|
} |
1661 |
|
} finally { |
1662 |
|
joinPool(e); |
1667 |
|
* invokeAll(c) returns results of all completed tasks |
1668 |
|
*/ |
1669 |
|
public void testInvokeAll5() throws Exception { |
1670 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1670 |
> |
ExecutorService e = |
1671 |
> |
new ThreadPoolExecutor(2, 2, |
1672 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1673 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1674 |
|
try { |
1675 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1675 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1676 |
|
l.add(new StringTask()); |
1677 |
|
l.add(new StringTask()); |
1678 |
< |
List<Future<String>> result = e.invokeAll(l); |
1679 |
< |
assertEquals(2, result.size()); |
1680 |
< |
for (Future<String> future : result) |
1678 |
> |
List<Future<String>> futures = e.invokeAll(l); |
1679 |
> |
assertEquals(2, futures.size()); |
1680 |
> |
for (Future<String> future : futures) |
1681 |
|
assertSame(TEST_STRING, future.get()); |
1682 |
|
} finally { |
1683 |
|
joinPool(e); |
1684 |
|
} |
1685 |
|
} |
1686 |
|
|
1180 |
– |
|
1181 |
– |
|
1687 |
|
/** |
1688 |
|
* timed invokeAny(null) throws NPE |
1689 |
|
*/ |
1690 |
|
public void testTimedInvokeAny1() throws Exception { |
1691 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1691 |
> |
ExecutorService e = |
1692 |
> |
new ThreadPoolExecutor(2, 2, |
1693 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1694 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1695 |
|
try { |
1696 |
|
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1697 |
|
shouldThrow(); |
1705 |
|
* timed invokeAny(,,null) throws NPE |
1706 |
|
*/ |
1707 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1708 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1708 |
> |
ExecutorService e = |
1709 |
> |
new ThreadPoolExecutor(2, 2, |
1710 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1711 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1712 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1713 |
> |
l.add(new StringTask()); |
1714 |
|
try { |
1202 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1203 |
– |
l.add(new StringTask()); |
1715 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1716 |
|
shouldThrow(); |
1717 |
|
} catch (NullPointerException success) { |
1724 |
|
* timed invokeAny(empty collection) throws IAE |
1725 |
|
*/ |
1726 |
|
public void testTimedInvokeAny2() throws Exception { |
1727 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1727 |
> |
ExecutorService e = |
1728 |
> |
new ThreadPoolExecutor(2, 2, |
1729 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1730 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1731 |
|
try { |
1732 |
|
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1733 |
|
shouldThrow(); |
1742 |
|
*/ |
1743 |
|
public void testTimedInvokeAny3() throws Exception { |
1744 |
|
final CountDownLatch latch = new CountDownLatch(1); |
1745 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1745 |
> |
final ExecutorService e = |
1746 |
> |
new ThreadPoolExecutor(2, 2, |
1747 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1748 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1749 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1750 |
> |
l.add(latchAwaitingStringTask(latch)); |
1751 |
> |
l.add(null); |
1752 |
|
try { |
1233 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1234 |
– |
l.add(new Callable<String>() { |
1235 |
– |
public String call() { |
1236 |
– |
try { |
1237 |
– |
latch.await(); |
1238 |
– |
} catch (InterruptedException ok) {} |
1239 |
– |
return TEST_STRING; |
1240 |
– |
}}); |
1241 |
– |
l.add(null); |
1753 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1754 |
|
shouldThrow(); |
1755 |
|
} catch (NullPointerException success) { |
1763 |
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1764 |
|
*/ |
1765 |
|
public void testTimedInvokeAny4() throws Exception { |
1766 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1766 |
> |
ExecutorService e = |
1767 |
> |
new ThreadPoolExecutor(2, 2, |
1768 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1769 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1770 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1771 |
> |
l.add(new NPETask()); |
1772 |
|
try { |
1257 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1258 |
– |
l.add(new NPETask()); |
1773 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1774 |
|
shouldThrow(); |
1775 |
|
} catch (ExecutionException success) { |
1783 |
|
* timed invokeAny(c) returns result of some task |
1784 |
|
*/ |
1785 |
|
public void testTimedInvokeAny5() throws Exception { |
1786 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1786 |
> |
ExecutorService e = |
1787 |
> |
new ThreadPoolExecutor(2, 2, |
1788 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1789 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1790 |
|
try { |
1791 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1791 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1792 |
|
l.add(new StringTask()); |
1793 |
|
l.add(new StringTask()); |
1794 |
|
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1802 |
|
* timed invokeAll(null) throws NPE |
1803 |
|
*/ |
1804 |
|
public void testTimedInvokeAll1() throws Exception { |
1805 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1805 |
> |
ExecutorService e = |
1806 |
> |
new ThreadPoolExecutor(2, 2, |
1807 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1808 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1809 |
|
try { |
1810 |
|
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1811 |
|
shouldThrow(); |
1819 |
|
* timed invokeAll(,,null) throws NPE |
1820 |
|
*/ |
1821 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1822 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1822 |
> |
ExecutorService e = |
1823 |
> |
new ThreadPoolExecutor(2, 2, |
1824 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1825 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1826 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1827 |
> |
l.add(new StringTask()); |
1828 |
|
try { |
1304 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1305 |
– |
l.add(new StringTask()); |
1829 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1830 |
|
shouldThrow(); |
1831 |
|
} catch (NullPointerException success) { |
1838 |
|
* timed invokeAll(empty collection) returns empty collection |
1839 |
|
*/ |
1840 |
|
public void testTimedInvokeAll2() throws InterruptedException { |
1841 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1841 |
> |
ExecutorService e = |
1842 |
> |
new ThreadPoolExecutor(2, 2, |
1843 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1844 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1845 |
|
try { |
1846 |
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1847 |
|
assertTrue(r.isEmpty()); |
1854 |
|
* timed invokeAll(c) throws NPE if c has null elements |
1855 |
|
*/ |
1856 |
|
public void testTimedInvokeAll3() throws Exception { |
1857 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1857 |
> |
ExecutorService e = |
1858 |
> |
new ThreadPoolExecutor(2, 2, |
1859 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1860 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1861 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1862 |
> |
l.add(new StringTask()); |
1863 |
> |
l.add(null); |
1864 |
|
try { |
1333 |
– |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1334 |
– |
l.add(new StringTask()); |
1335 |
– |
l.add(null); |
1865 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1866 |
|
shouldThrow(); |
1867 |
|
} catch (NullPointerException success) { |
1874 |
|
* get of element of invokeAll(c) throws exception on failed task |
1875 |
|
*/ |
1876 |
|
public void testTimedInvokeAll4() throws Exception { |
1877 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1877 |
> |
ExecutorService e = |
1878 |
> |
new ThreadPoolExecutor(2, 2, |
1879 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1880 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1881 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1882 |
> |
l.add(new NPETask()); |
1883 |
> |
List<Future<String>> futures = |
1884 |
> |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1885 |
> |
assertEquals(1, futures.size()); |
1886 |
|
try { |
1887 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1351 |
< |
l.add(new NPETask()); |
1352 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1353 |
< |
assertEquals(1, result.size()); |
1354 |
< |
for (Future<String> future : result) |
1355 |
< |
future.get(); |
1887 |
> |
futures.get(0).get(); |
1888 |
|
shouldThrow(); |
1889 |
|
} catch (ExecutionException success) { |
1890 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1897 |
|
* timed invokeAll(c) returns results of all completed tasks |
1898 |
|
*/ |
1899 |
|
public void testTimedInvokeAll5() throws Exception { |
1900 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1900 |
> |
ExecutorService e = |
1901 |
> |
new ThreadPoolExecutor(2, 2, |
1902 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1903 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1904 |
|
try { |
1905 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1905 |
> |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1906 |
|
l.add(new StringTask()); |
1907 |
|
l.add(new StringTask()); |
1908 |
< |
List<Future<String>> result = e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1909 |
< |
assertEquals(2, result.size()); |
1910 |
< |
for (Iterator<Future<String>> it = result.iterator(); it.hasNext();) |
1911 |
< |
assertSame(TEST_STRING, it.next().get()); |
1908 |
> |
List<Future<String>> futures = |
1909 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1910 |
> |
assertEquals(2, futures.size()); |
1911 |
> |
for (Future<String> future : futures) |
1912 |
> |
assertSame(TEST_STRING, future.get()); |
1913 |
|
} finally { |
1914 |
|
joinPool(e); |
1915 |
|
} |
1919 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1920 |
|
*/ |
1921 |
|
public void testTimedInvokeAll6() throws Exception { |
1922 |
< |
ExecutorService e = new ThreadPoolExecutor(2, 2, LONG_DELAY_MS, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1922 |
> |
ExecutorService e = |
1923 |
> |
new ThreadPoolExecutor(2, 2, |
1924 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1925 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1926 |
|
try { |
1927 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1928 |
< |
l.add(new StringTask()); |
1929 |
< |
l.add(Executors.callable(new MediumPossiblyInterruptedRunnable(), TEST_STRING)); |
1930 |
< |
l.add(new StringTask()); |
1931 |
< |
List<Future<String>> result = e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1932 |
< |
assertEquals(3, result.size()); |
1933 |
< |
Iterator<Future<String>> it = result.iterator(); |
1934 |
< |
Future<String> f1 = it.next(); |
1935 |
< |
Future<String> f2 = it.next(); |
1936 |
< |
Future<String> f3 = it.next(); |
1937 |
< |
assertTrue(f1.isDone()); |
1938 |
< |
assertTrue(f2.isDone()); |
1939 |
< |
assertTrue(f3.isDone()); |
1940 |
< |
assertFalse(f1.isCancelled()); |
1941 |
< |
assertTrue(f2.isCancelled()); |
1927 |
> |
for (long timeout = timeoutMillis();;) { |
1928 |
> |
List<Callable<String>> tasks = new ArrayList<>(); |
1929 |
> |
tasks.add(new StringTask("0")); |
1930 |
> |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1931 |
> |
tasks.add(new StringTask("2")); |
1932 |
> |
long startTime = System.nanoTime(); |
1933 |
> |
List<Future<String>> futures = |
1934 |
> |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1935 |
> |
assertEquals(tasks.size(), futures.size()); |
1936 |
> |
assertTrue(millisElapsedSince(startTime) >= timeout); |
1937 |
> |
for (Future future : futures) |
1938 |
> |
assertTrue(future.isDone()); |
1939 |
> |
assertTrue(futures.get(1).isCancelled()); |
1940 |
> |
try { |
1941 |
> |
assertEquals("0", futures.get(0).get()); |
1942 |
> |
assertEquals("2", futures.get(2).get()); |
1943 |
> |
break; |
1944 |
> |
} catch (CancellationException retryWithLongerTimeout) { |
1945 |
> |
timeout *= 2; |
1946 |
> |
if (timeout >= LONG_DELAY_MS / 2) |
1947 |
> |
fail("expected exactly one task to be cancelled"); |
1948 |
> |
} |
1949 |
> |
} |
1950 |
|
} finally { |
1951 |
|
joinPool(e); |
1952 |
|
} |
1957 |
|
* thread factory fails to create more |
1958 |
|
*/ |
1959 |
|
public void testFailingThreadFactory() throws InterruptedException { |
1960 |
< |
ExecutorService e = new ThreadPoolExecutor(100, 100, LONG_DELAY_MS, MILLISECONDS, new LinkedBlockingQueue<Runnable>(), new FailingThreadFactory()); |
1960 |
> |
final ExecutorService e = |
1961 |
> |
new ThreadPoolExecutor(100, 100, |
1962 |
> |
LONG_DELAY_MS, MILLISECONDS, |
1963 |
> |
new LinkedBlockingQueue<Runnable>(), |
1964 |
> |
new FailingThreadFactory()); |
1965 |
|
try { |
1966 |
< |
ArrayList<Callable<String>> l = new ArrayList<Callable<String>>(); |
1967 |
< |
for (int k = 0; k < 100; ++k) { |
1968 |
< |
e.execute(new NoOpRunnable()); |
1969 |
< |
} |
1970 |
< |
Thread.sleep(LONG_DELAY_MS); |
1966 |
> |
final int TASKS = 100; |
1967 |
> |
final CountDownLatch done = new CountDownLatch(TASKS); |
1968 |
> |
for (int k = 0; k < TASKS; ++k) |
1969 |
> |
e.execute(new CheckedRunnable() { |
1970 |
> |
public void realRun() { |
1971 |
> |
done.countDown(); |
1972 |
> |
}}); |
1973 |
> |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
1974 |
|
} finally { |
1975 |
|
joinPool(e); |
1976 |
|
} |
1980 |
|
* allowsCoreThreadTimeOut is by default false. |
1981 |
|
*/ |
1982 |
|
public void testAllowsCoreThreadTimeOut() { |
1983 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 2, 1000, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1984 |
< |
assertFalse(tpe.allowsCoreThreadTimeOut()); |
1985 |
< |
joinPool(tpe); |
1983 |
> |
final ThreadPoolExecutor p = |
1984 |
> |
new ThreadPoolExecutor(2, 2, |
1985 |
> |
1000, MILLISECONDS, |
1986 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
1987 |
> |
assertFalse(p.allowsCoreThreadTimeOut()); |
1988 |
> |
joinPool(p); |
1989 |
|
} |
1990 |
|
|
1991 |
|
/** |
1992 |
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1993 |
|
*/ |
1994 |
< |
public void testAllowCoreThreadTimeOut_true() throws InterruptedException { |
1995 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
1996 |
< |
tpe.allowCoreThreadTimeOut(true); |
1997 |
< |
tpe.execute(new NoOpRunnable()); |
1994 |
> |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1995 |
> |
long keepAliveTime = timeoutMillis(); |
1996 |
> |
final ThreadPoolExecutor p = |
1997 |
> |
new ThreadPoolExecutor(2, 10, |
1998 |
> |
keepAliveTime, MILLISECONDS, |
1999 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
2000 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
2001 |
|
try { |
2002 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
2003 |
< |
assertEquals(0, tpe.getPoolSize()); |
2002 |
> |
p.allowCoreThreadTimeOut(true); |
2003 |
> |
p.execute(new CheckedRunnable() { |
2004 |
> |
public void realRun() { |
2005 |
> |
threadStarted.countDown(); |
2006 |
> |
assertEquals(1, p.getPoolSize()); |
2007 |
> |
}}); |
2008 |
> |
await(threadStarted); |
2009 |
> |
delay(keepAliveTime); |
2010 |
> |
long startTime = System.nanoTime(); |
2011 |
> |
while (p.getPoolSize() > 0 |
2012 |
> |
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
2013 |
> |
Thread.yield(); |
2014 |
> |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
2015 |
> |
assertEquals(0, p.getPoolSize()); |
2016 |
|
} finally { |
2017 |
< |
joinPool(tpe); |
2017 |
> |
joinPool(p); |
2018 |
|
} |
2019 |
|
} |
2020 |
|
|
2021 |
|
/** |
2022 |
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
2023 |
|
*/ |
2024 |
< |
public void testAllowCoreThreadTimeOut_false() throws InterruptedException { |
2025 |
< |
ThreadPoolExecutor tpe = new ThreadPoolExecutor(2, 10, 10, MILLISECONDS, new ArrayBlockingQueue<Runnable>(10)); |
2026 |
< |
tpe.allowCoreThreadTimeOut(false); |
2027 |
< |
tpe.execute(new NoOpRunnable()); |
2024 |
> |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
2025 |
> |
long keepAliveTime = timeoutMillis(); |
2026 |
> |
final ThreadPoolExecutor p = |
2027 |
> |
new ThreadPoolExecutor(2, 10, |
2028 |
> |
keepAliveTime, MILLISECONDS, |
2029 |
> |
new ArrayBlockingQueue<Runnable>(10)); |
2030 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
2031 |
|
try { |
2032 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
2033 |
< |
assertTrue(tpe.getPoolSize() >= 1); |
2032 |
> |
p.allowCoreThreadTimeOut(false); |
2033 |
> |
p.execute(new CheckedRunnable() { |
2034 |
> |
public void realRun() throws InterruptedException { |
2035 |
> |
threadStarted.countDown(); |
2036 |
> |
assertTrue(p.getPoolSize() >= 1); |
2037 |
> |
}}); |
2038 |
> |
delay(2 * keepAliveTime); |
2039 |
> |
assertTrue(p.getPoolSize() >= 1); |
2040 |
|
} finally { |
2041 |
< |
joinPool(tpe); |
2041 |
> |
joinPool(p); |
2042 |
|
} |
2043 |
|
} |
2044 |
|
|
2048 |
|
*/ |
2049 |
|
public void testRejectedRecycledTask() throws InterruptedException { |
2050 |
|
final int nTasks = 1000; |
2051 |
< |
final AtomicInteger nRun = new AtomicInteger(0); |
2051 |
> |
final CountDownLatch done = new CountDownLatch(nTasks); |
2052 |
|
final Runnable recycledTask = new Runnable() { |
2053 |
< |
public void run() { |
2054 |
< |
nRun.getAndIncrement(); |
2055 |
< |
} }; |
2053 |
> |
public void run() { |
2054 |
> |
done.countDown(); |
2055 |
> |
}}; |
2056 |
|
final ThreadPoolExecutor p = |
2057 |
< |
new ThreadPoolExecutor(1, 30, 60, TimeUnit.SECONDS, |
2057 |
> |
new ThreadPoolExecutor(1, 30, |
2058 |
> |
60, SECONDS, |
2059 |
|
new ArrayBlockingQueue(30)); |
2060 |
|
try { |
2061 |
|
for (int i = 0; i < nTasks; ++i) { |
2064 |
|
p.execute(recycledTask); |
2065 |
|
break; |
2066 |
|
} |
2067 |
< |
catch (RejectedExecutionException ignore) { |
1486 |
< |
} |
2067 |
> |
catch (RejectedExecutionException ignore) {} |
2068 |
|
} |
2069 |
|
} |
2070 |
< |
Thread.sleep(5000); // enough time to run all tasks |
2071 |
< |
assertEquals(nRun.get(), nTasks); |
2070 |
> |
// enough time to run all tasks |
2071 |
> |
assertTrue(done.await(nTasks * SHORT_DELAY_MS, MILLISECONDS)); |
2072 |
|
} finally { |
2073 |
< |
p.shutdown(); |
2073 |
> |
joinPool(p); |
2074 |
> |
} |
2075 |
> |
} |
2076 |
> |
|
2077 |
> |
/** |
2078 |
> |
* get(cancelled task) throws CancellationException |
2079 |
> |
*/ |
2080 |
> |
public void testGet_cancelled() throws Exception { |
2081 |
> |
final ExecutorService e = |
2082 |
> |
new ThreadPoolExecutor(1, 1, |
2083 |
> |
LONG_DELAY_MS, MILLISECONDS, |
2084 |
> |
new LinkedBlockingQueue<Runnable>()); |
2085 |
> |
try { |
2086 |
> |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
2087 |
> |
final CountDownLatch done = new CountDownLatch(1); |
2088 |
> |
final List<Future<?>> futures = new ArrayList<>(); |
2089 |
> |
for (int i = 0; i < 2; i++) { |
2090 |
> |
Runnable r = new CheckedRunnable() { public void realRun() |
2091 |
> |
throws Throwable { |
2092 |
> |
blockerStarted.countDown(); |
2093 |
> |
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
2094 |
> |
}}; |
2095 |
> |
futures.add(e.submit(r)); |
2096 |
> |
} |
2097 |
> |
assertTrue(blockerStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
2098 |
> |
for (Future<?> future : futures) future.cancel(false); |
2099 |
> |
for (Future<?> future : futures) { |
2100 |
> |
try { |
2101 |
> |
future.get(); |
2102 |
> |
shouldThrow(); |
2103 |
> |
} catch (CancellationException success) {} |
2104 |
> |
try { |
2105 |
> |
future.get(LONG_DELAY_MS, MILLISECONDS); |
2106 |
> |
shouldThrow(); |
2107 |
> |
} catch (CancellationException success) {} |
2108 |
> |
assertTrue(future.isCancelled()); |
2109 |
> |
assertTrue(future.isDone()); |
2110 |
> |
} |
2111 |
> |
done.countDown(); |
2112 |
> |
} finally { |
2113 |
> |
joinPool(e); |
2114 |
|
} |
2115 |
|
} |
2116 |
|
|