1 |
dl |
1.1 |
/* |
2 |
dl |
1.15 |
* 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 |
jsr166 |
1.40 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.24 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.27 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
jsr166 |
1.48 |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
jsr166 |
1.52 |
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
jsr166 |
1.50 |
|
13 |
|
|
import java.util.ArrayList; |
14 |
jsr166 |
1.119 |
import java.util.Collection; |
15 |
|
|
import java.util.Collections; |
16 |
jsr166 |
1.50 |
import java.util.List; |
17 |
|
|
import java.util.concurrent.ArrayBlockingQueue; |
18 |
|
|
import java.util.concurrent.BlockingQueue; |
19 |
jsr166 |
1.58 |
import java.util.concurrent.Callable; |
20 |
jsr166 |
1.56 |
import java.util.concurrent.CancellationException; |
21 |
jsr166 |
1.50 |
import java.util.concurrent.CountDownLatch; |
22 |
|
|
import java.util.concurrent.ExecutionException; |
23 |
|
|
import java.util.concurrent.ExecutorService; |
24 |
|
|
import java.util.concurrent.Future; |
25 |
|
|
import java.util.concurrent.FutureTask; |
26 |
|
|
import java.util.concurrent.LinkedBlockingQueue; |
27 |
|
|
import java.util.concurrent.RejectedExecutionException; |
28 |
|
|
import java.util.concurrent.RejectedExecutionHandler; |
29 |
|
|
import java.util.concurrent.SynchronousQueue; |
30 |
|
|
import java.util.concurrent.ThreadFactory; |
31 |
jsr166 |
1.123 |
import java.util.concurrent.ThreadLocalRandom; |
32 |
jsr166 |
1.50 |
import java.util.concurrent.ThreadPoolExecutor; |
33 |
jsr166 |
1.121 |
import java.util.concurrent.ThreadPoolExecutor.AbortPolicy; |
34 |
|
|
import java.util.concurrent.ThreadPoolExecutor.CallerRunsPolicy; |
35 |
|
|
import java.util.concurrent.ThreadPoolExecutor.DiscardPolicy; |
36 |
|
|
import java.util.concurrent.ThreadPoolExecutor.DiscardOldestPolicy; |
37 |
jsr166 |
1.61 |
import java.util.concurrent.atomic.AtomicInteger; |
38 |
jsr166 |
1.121 |
import java.util.concurrent.atomic.AtomicReference; |
39 |
jsr166 |
1.50 |
|
40 |
|
|
import junit.framework.Test; |
41 |
|
|
import junit.framework.TestSuite; |
42 |
dl |
1.1 |
|
43 |
dl |
1.3 |
public class ThreadPoolExecutorTest extends JSR166TestCase { |
44 |
dl |
1.1 |
public static void main(String[] args) { |
45 |
jsr166 |
1.51 |
main(suite(), args); |
46 |
dl |
1.1 |
} |
47 |
|
|
public static Test suite() { |
48 |
|
|
return new TestSuite(ThreadPoolExecutorTest.class); |
49 |
|
|
} |
50 |
jsr166 |
1.24 |
|
51 |
dl |
1.8 |
static class ExtendedTPE extends ThreadPoolExecutor { |
52 |
jsr166 |
1.45 |
final CountDownLatch beforeCalled = new CountDownLatch(1); |
53 |
|
|
final CountDownLatch afterCalled = new CountDownLatch(1); |
54 |
|
|
final CountDownLatch terminatedCalled = new CountDownLatch(1); |
55 |
|
|
|
56 |
dl |
1.8 |
public ExtendedTPE() { |
57 |
jsr166 |
1.27 |
super(1, 1, LONG_DELAY_MS, MILLISECONDS, new SynchronousQueue<Runnable>()); |
58 |
dl |
1.8 |
} |
59 |
|
|
protected void beforeExecute(Thread t, Runnable r) { |
60 |
jsr166 |
1.45 |
beforeCalled.countDown(); |
61 |
dl |
1.8 |
} |
62 |
|
|
protected void afterExecute(Runnable r, Throwable t) { |
63 |
jsr166 |
1.45 |
afterCalled.countDown(); |
64 |
dl |
1.8 |
} |
65 |
|
|
protected void terminated() { |
66 |
jsr166 |
1.45 |
terminatedCalled.countDown(); |
67 |
|
|
} |
68 |
|
|
|
69 |
|
|
public boolean beforeCalled() { |
70 |
|
|
return beforeCalled.getCount() == 0; |
71 |
|
|
} |
72 |
|
|
public boolean afterCalled() { |
73 |
|
|
return afterCalled.getCount() == 0; |
74 |
|
|
} |
75 |
|
|
public boolean terminatedCalled() { |
76 |
|
|
return terminatedCalled.getCount() == 0; |
77 |
dl |
1.8 |
} |
78 |
|
|
} |
79 |
dl |
1.1 |
|
80 |
jsr166 |
1.26 |
static class FailingThreadFactory implements ThreadFactory { |
81 |
dl |
1.19 |
int calls = 0; |
82 |
jsr166 |
1.26 |
public Thread newThread(Runnable r) { |
83 |
dl |
1.20 |
if (++calls > 1) return null; |
84 |
dl |
1.19 |
return new Thread(r); |
85 |
jsr166 |
1.24 |
} |
86 |
dl |
1.19 |
} |
87 |
jsr166 |
1.24 |
|
88 |
dl |
1.3 |
/** |
89 |
jsr166 |
1.35 |
* execute successfully executes a runnable |
90 |
dl |
1.1 |
*/ |
91 |
jsr166 |
1.27 |
public void testExecute() throws InterruptedException { |
92 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
93 |
|
|
new ThreadPoolExecutor(1, 1, |
94 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
95 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
96 |
jsr166 |
1.80 |
try (PoolCleaner cleaner = cleaner(p)) { |
97 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
98 |
|
|
final Runnable task = new CheckedRunnable() { |
99 |
|
|
public void realRun() { done.countDown(); }}; |
100 |
jsr166 |
1.37 |
p.execute(task); |
101 |
jsr166 |
1.117 |
await(done); |
102 |
jsr166 |
1.24 |
} |
103 |
dl |
1.1 |
} |
104 |
|
|
|
105 |
|
|
/** |
106 |
jsr166 |
1.35 |
* getActiveCount increases but doesn't overestimate, when a |
107 |
|
|
* thread becomes active |
108 |
dl |
1.1 |
*/ |
109 |
jsr166 |
1.27 |
public void testGetActiveCount() throws InterruptedException { |
110 |
jsr166 |
1.109 |
final CountDownLatch done = new CountDownLatch(1); |
111 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
112 |
|
|
new ThreadPoolExecutor(2, 2, |
113 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
114 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
115 |
jsr166 |
1.109 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
116 |
jsr166 |
1.80 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
117 |
jsr166 |
1.37 |
assertEquals(0, p.getActiveCount()); |
118 |
|
|
p.execute(new CheckedRunnable() { |
119 |
|
|
public void realRun() throws InterruptedException { |
120 |
|
|
threadStarted.countDown(); |
121 |
|
|
assertEquals(1, p.getActiveCount()); |
122 |
jsr166 |
1.106 |
await(done); |
123 |
jsr166 |
1.37 |
}}); |
124 |
jsr166 |
1.108 |
await(threadStarted); |
125 |
jsr166 |
1.37 |
assertEquals(1, p.getActiveCount()); |
126 |
|
|
} |
127 |
dl |
1.1 |
} |
128 |
dl |
1.8 |
|
129 |
|
|
/** |
130 |
jsr166 |
1.35 |
* prestartCoreThread starts a thread if under corePoolSize, else doesn't |
131 |
dl |
1.8 |
*/ |
132 |
|
|
public void testPrestartCoreThread() { |
133 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
134 |
jsr166 |
1.66 |
new ThreadPoolExecutor(2, 6, |
135 |
jsr166 |
1.37 |
LONG_DELAY_MS, MILLISECONDS, |
136 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
137 |
jsr166 |
1.66 |
try (PoolCleaner cleaner = cleaner(p)) { |
138 |
|
|
assertEquals(0, p.getPoolSize()); |
139 |
|
|
assertTrue(p.prestartCoreThread()); |
140 |
|
|
assertEquals(1, p.getPoolSize()); |
141 |
|
|
assertTrue(p.prestartCoreThread()); |
142 |
|
|
assertEquals(2, p.getPoolSize()); |
143 |
|
|
assertFalse(p.prestartCoreThread()); |
144 |
|
|
assertEquals(2, p.getPoolSize()); |
145 |
|
|
p.setCorePoolSize(4); |
146 |
|
|
assertTrue(p.prestartCoreThread()); |
147 |
|
|
assertEquals(3, p.getPoolSize()); |
148 |
|
|
assertTrue(p.prestartCoreThread()); |
149 |
|
|
assertEquals(4, p.getPoolSize()); |
150 |
|
|
assertFalse(p.prestartCoreThread()); |
151 |
|
|
assertEquals(4, p.getPoolSize()); |
152 |
|
|
} |
153 |
dl |
1.8 |
} |
154 |
|
|
|
155 |
|
|
/** |
156 |
jsr166 |
1.35 |
* prestartAllCoreThreads starts all corePoolSize threads |
157 |
dl |
1.8 |
*/ |
158 |
|
|
public void testPrestartAllCoreThreads() { |
159 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
160 |
jsr166 |
1.67 |
new ThreadPoolExecutor(2, 6, |
161 |
jsr166 |
1.37 |
LONG_DELAY_MS, MILLISECONDS, |
162 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
163 |
jsr166 |
1.67 |
try (PoolCleaner cleaner = cleaner(p)) { |
164 |
|
|
assertEquals(0, p.getPoolSize()); |
165 |
|
|
p.prestartAllCoreThreads(); |
166 |
|
|
assertEquals(2, p.getPoolSize()); |
167 |
|
|
p.prestartAllCoreThreads(); |
168 |
|
|
assertEquals(2, p.getPoolSize()); |
169 |
|
|
p.setCorePoolSize(4); |
170 |
|
|
p.prestartAllCoreThreads(); |
171 |
|
|
assertEquals(4, p.getPoolSize()); |
172 |
|
|
p.prestartAllCoreThreads(); |
173 |
|
|
assertEquals(4, p.getPoolSize()); |
174 |
|
|
} |
175 |
dl |
1.8 |
} |
176 |
jsr166 |
1.24 |
|
177 |
dl |
1.1 |
/** |
178 |
jsr166 |
1.35 |
* getCompletedTaskCount increases, but doesn't overestimate, |
179 |
|
|
* when tasks complete |
180 |
dl |
1.1 |
*/ |
181 |
jsr166 |
1.27 |
public void testGetCompletedTaskCount() throws InterruptedException { |
182 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
183 |
|
|
new ThreadPoolExecutor(2, 2, |
184 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
185 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
186 |
jsr166 |
1.68 |
try (PoolCleaner cleaner = cleaner(p)) { |
187 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
188 |
|
|
final CountDownLatch threadProceed = new CountDownLatch(1); |
189 |
|
|
final CountDownLatch threadDone = new CountDownLatch(1); |
190 |
jsr166 |
1.37 |
assertEquals(0, p.getCompletedTaskCount()); |
191 |
|
|
p.execute(new CheckedRunnable() { |
192 |
|
|
public void realRun() throws InterruptedException { |
193 |
|
|
threadStarted.countDown(); |
194 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
195 |
jsr166 |
1.117 |
await(threadProceed); |
196 |
jsr166 |
1.37 |
threadDone.countDown(); |
197 |
|
|
}}); |
198 |
jsr166 |
1.45 |
await(threadStarted); |
199 |
jsr166 |
1.37 |
assertEquals(0, p.getCompletedTaskCount()); |
200 |
|
|
threadProceed.countDown(); |
201 |
jsr166 |
1.117 |
await(threadDone); |
202 |
jsr166 |
1.45 |
long startTime = System.nanoTime(); |
203 |
|
|
while (p.getCompletedTaskCount() != 1) { |
204 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
205 |
|
|
fail("timed out"); |
206 |
|
|
Thread.yield(); |
207 |
|
|
} |
208 |
jsr166 |
1.37 |
} |
209 |
dl |
1.1 |
} |
210 |
jsr166 |
1.24 |
|
211 |
dl |
1.1 |
/** |
212 |
jsr166 |
1.35 |
* getCorePoolSize returns size given in constructor if not otherwise set |
213 |
dl |
1.1 |
*/ |
214 |
dl |
1.5 |
public void testGetCorePoolSize() { |
215 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
216 |
|
|
new ThreadPoolExecutor(1, 1, |
217 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
218 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
219 |
jsr166 |
1.69 |
try (PoolCleaner cleaner = cleaner(p)) { |
220 |
|
|
assertEquals(1, p.getCorePoolSize()); |
221 |
|
|
} |
222 |
dl |
1.1 |
} |
223 |
jsr166 |
1.24 |
|
224 |
dl |
1.1 |
/** |
225 |
jsr166 |
1.35 |
* getKeepAliveTime returns value given in constructor if not otherwise set |
226 |
dl |
1.1 |
*/ |
227 |
dl |
1.5 |
public void testGetKeepAliveTime() { |
228 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
229 |
|
|
new ThreadPoolExecutor(2, 2, |
230 |
|
|
1000, MILLISECONDS, |
231 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
232 |
jsr166 |
1.70 |
try (PoolCleaner cleaner = cleaner(p)) { |
233 |
|
|
assertEquals(1, p.getKeepAliveTime(SECONDS)); |
234 |
|
|
} |
235 |
dl |
1.1 |
} |
236 |
dl |
1.8 |
|
237 |
jsr166 |
1.24 |
/** |
238 |
dl |
1.8 |
* getThreadFactory returns factory in constructor if not set |
239 |
|
|
*/ |
240 |
|
|
public void testGetThreadFactory() { |
241 |
jsr166 |
1.71 |
ThreadFactory threadFactory = new SimpleThreadFactory(); |
242 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
243 |
|
|
new ThreadPoolExecutor(1, 2, |
244 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
245 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
246 |
jsr166 |
1.71 |
threadFactory, |
247 |
jsr166 |
1.37 |
new NoOpREHandler()); |
248 |
jsr166 |
1.71 |
try (PoolCleaner cleaner = cleaner(p)) { |
249 |
|
|
assertSame(threadFactory, p.getThreadFactory()); |
250 |
|
|
} |
251 |
dl |
1.8 |
} |
252 |
|
|
|
253 |
jsr166 |
1.24 |
/** |
254 |
dl |
1.8 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
255 |
|
|
*/ |
256 |
|
|
public void testSetThreadFactory() { |
257 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
258 |
|
|
new ThreadPoolExecutor(1, 2, |
259 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
260 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
261 |
jsr166 |
1.71 |
try (PoolCleaner cleaner = cleaner(p)) { |
262 |
|
|
ThreadFactory threadFactory = new SimpleThreadFactory(); |
263 |
|
|
p.setThreadFactory(threadFactory); |
264 |
|
|
assertSame(threadFactory, p.getThreadFactory()); |
265 |
|
|
} |
266 |
dl |
1.8 |
} |
267 |
|
|
|
268 |
jsr166 |
1.24 |
/** |
269 |
dl |
1.8 |
* setThreadFactory(null) throws NPE |
270 |
|
|
*/ |
271 |
|
|
public void testSetThreadFactoryNull() { |
272 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
273 |
|
|
new ThreadPoolExecutor(1, 2, |
274 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
275 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
276 |
jsr166 |
1.72 |
try (PoolCleaner cleaner = cleaner(p)) { |
277 |
|
|
try { |
278 |
|
|
p.setThreadFactory(null); |
279 |
|
|
shouldThrow(); |
280 |
|
|
} catch (NullPointerException success) {} |
281 |
dl |
1.8 |
} |
282 |
|
|
} |
283 |
|
|
|
284 |
jsr166 |
1.24 |
/** |
285 |
jsr166 |
1.116 |
* The default rejected execution handler is AbortPolicy. |
286 |
|
|
*/ |
287 |
|
|
public void testDefaultRejectedExecutionHandler() { |
288 |
|
|
final ThreadPoolExecutor p = |
289 |
|
|
new ThreadPoolExecutor(1, 2, |
290 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
291 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
292 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
293 |
jsr166 |
1.121 |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
294 |
jsr166 |
1.116 |
} |
295 |
|
|
} |
296 |
|
|
|
297 |
|
|
/** |
298 |
dl |
1.9 |
* getRejectedExecutionHandler returns handler in constructor if not set |
299 |
|
|
*/ |
300 |
|
|
public void testGetRejectedExecutionHandler() { |
301 |
jsr166 |
1.73 |
final RejectedExecutionHandler handler = new NoOpREHandler(); |
302 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
303 |
|
|
new ThreadPoolExecutor(1, 2, |
304 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
305 |
|
|
new ArrayBlockingQueue<Runnable>(10), |
306 |
jsr166 |
1.73 |
handler); |
307 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
308 |
|
|
assertSame(handler, p.getRejectedExecutionHandler()); |
309 |
|
|
} |
310 |
dl |
1.9 |
} |
311 |
|
|
|
312 |
jsr166 |
1.24 |
/** |
313 |
dl |
1.9 |
* setRejectedExecutionHandler sets the handler returned by |
314 |
|
|
* getRejectedExecutionHandler |
315 |
|
|
*/ |
316 |
|
|
public void testSetRejectedExecutionHandler() { |
317 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
318 |
|
|
new ThreadPoolExecutor(1, 2, |
319 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
320 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
321 |
jsr166 |
1.74 |
try (PoolCleaner cleaner = cleaner(p)) { |
322 |
|
|
RejectedExecutionHandler handler = new NoOpREHandler(); |
323 |
|
|
p.setRejectedExecutionHandler(handler); |
324 |
|
|
assertSame(handler, p.getRejectedExecutionHandler()); |
325 |
|
|
} |
326 |
dl |
1.9 |
} |
327 |
|
|
|
328 |
jsr166 |
1.24 |
/** |
329 |
dl |
1.9 |
* setRejectedExecutionHandler(null) throws NPE |
330 |
|
|
*/ |
331 |
|
|
public void testSetRejectedExecutionHandlerNull() { |
332 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
333 |
|
|
new ThreadPoolExecutor(1, 2, |
334 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
335 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
336 |
jsr166 |
1.75 |
try (PoolCleaner cleaner = cleaner(p)) { |
337 |
|
|
try { |
338 |
|
|
p.setRejectedExecutionHandler(null); |
339 |
|
|
shouldThrow(); |
340 |
|
|
} catch (NullPointerException success) {} |
341 |
dl |
1.9 |
} |
342 |
|
|
} |
343 |
|
|
|
344 |
dl |
1.1 |
/** |
345 |
jsr166 |
1.35 |
* getLargestPoolSize increases, but doesn't overestimate, when |
346 |
|
|
* multiple threads active |
347 |
dl |
1.1 |
*/ |
348 |
jsr166 |
1.27 |
public void testGetLargestPoolSize() throws InterruptedException { |
349 |
jsr166 |
1.37 |
final int THREADS = 3; |
350 |
jsr166 |
1.103 |
final CountDownLatch done = new CountDownLatch(1); |
351 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
352 |
|
|
new ThreadPoolExecutor(THREADS, THREADS, |
353 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
354 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
355 |
jsr166 |
1.103 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
356 |
|
|
assertEquals(0, p.getLargestPoolSize()); |
357 |
jsr166 |
1.76 |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
358 |
jsr166 |
1.37 |
for (int i = 0; i < THREADS; i++) |
359 |
|
|
p.execute(new CheckedRunnable() { |
360 |
|
|
public void realRun() throws InterruptedException { |
361 |
|
|
threadsStarted.countDown(); |
362 |
jsr166 |
1.106 |
await(done); |
363 |
jsr166 |
1.37 |
assertEquals(THREADS, p.getLargestPoolSize()); |
364 |
|
|
}}); |
365 |
jsr166 |
1.107 |
await(threadsStarted); |
366 |
jsr166 |
1.37 |
assertEquals(THREADS, p.getLargestPoolSize()); |
367 |
|
|
} |
368 |
jsr166 |
1.76 |
assertEquals(THREADS, p.getLargestPoolSize()); |
369 |
dl |
1.1 |
} |
370 |
jsr166 |
1.24 |
|
371 |
dl |
1.1 |
/** |
372 |
jsr166 |
1.35 |
* getMaximumPoolSize returns value given in constructor if not |
373 |
|
|
* otherwise set |
374 |
dl |
1.1 |
*/ |
375 |
dl |
1.5 |
public void testGetMaximumPoolSize() { |
376 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
377 |
|
|
new ThreadPoolExecutor(2, 3, |
378 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
379 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
380 |
jsr166 |
1.77 |
try (PoolCleaner cleaner = cleaner(p)) { |
381 |
|
|
assertEquals(3, p.getMaximumPoolSize()); |
382 |
|
|
p.setMaximumPoolSize(5); |
383 |
|
|
assertEquals(5, p.getMaximumPoolSize()); |
384 |
|
|
p.setMaximumPoolSize(4); |
385 |
|
|
assertEquals(4, p.getMaximumPoolSize()); |
386 |
|
|
} |
387 |
dl |
1.1 |
} |
388 |
jsr166 |
1.24 |
|
389 |
dl |
1.1 |
/** |
390 |
jsr166 |
1.35 |
* getPoolSize increases, but doesn't overestimate, when threads |
391 |
|
|
* become active |
392 |
dl |
1.1 |
*/ |
393 |
jsr166 |
1.37 |
public void testGetPoolSize() throws InterruptedException { |
394 |
jsr166 |
1.103 |
final CountDownLatch done = new CountDownLatch(1); |
395 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
396 |
|
|
new ThreadPoolExecutor(1, 1, |
397 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
398 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
399 |
jsr166 |
1.103 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
400 |
|
|
assertEquals(0, p.getPoolSize()); |
401 |
jsr166 |
1.78 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
402 |
jsr166 |
1.37 |
p.execute(new CheckedRunnable() { |
403 |
|
|
public void realRun() throws InterruptedException { |
404 |
|
|
threadStarted.countDown(); |
405 |
|
|
assertEquals(1, p.getPoolSize()); |
406 |
jsr166 |
1.106 |
await(done); |
407 |
jsr166 |
1.37 |
}}); |
408 |
jsr166 |
1.108 |
await(threadStarted); |
409 |
jsr166 |
1.37 |
assertEquals(1, p.getPoolSize()); |
410 |
|
|
} |
411 |
dl |
1.1 |
} |
412 |
jsr166 |
1.24 |
|
413 |
dl |
1.1 |
/** |
414 |
jsr166 |
1.35 |
* getTaskCount increases, but doesn't overestimate, when tasks submitted |
415 |
dl |
1.1 |
*/ |
416 |
jsr166 |
1.27 |
public void testGetTaskCount() throws InterruptedException { |
417 |
jsr166 |
1.100 |
final int TASKS = 3; |
418 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
419 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
420 |
|
|
new ThreadPoolExecutor(1, 1, |
421 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
422 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
423 |
jsr166 |
1.100 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
424 |
jsr166 |
1.81 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
425 |
jsr166 |
1.37 |
assertEquals(0, p.getTaskCount()); |
426 |
jsr166 |
1.100 |
assertEquals(0, p.getCompletedTaskCount()); |
427 |
jsr166 |
1.37 |
p.execute(new CheckedRunnable() { |
428 |
|
|
public void realRun() throws InterruptedException { |
429 |
|
|
threadStarted.countDown(); |
430 |
jsr166 |
1.106 |
await(done); |
431 |
jsr166 |
1.37 |
}}); |
432 |
jsr166 |
1.108 |
await(threadStarted); |
433 |
jsr166 |
1.37 |
assertEquals(1, p.getTaskCount()); |
434 |
jsr166 |
1.100 |
assertEquals(0, p.getCompletedTaskCount()); |
435 |
|
|
for (int i = 0; i < TASKS; i++) { |
436 |
|
|
assertEquals(1 + i, p.getTaskCount()); |
437 |
|
|
p.execute(new CheckedRunnable() { |
438 |
|
|
public void realRun() throws InterruptedException { |
439 |
|
|
threadStarted.countDown(); |
440 |
|
|
assertEquals(1 + TASKS, p.getTaskCount()); |
441 |
jsr166 |
1.106 |
await(done); |
442 |
jsr166 |
1.100 |
}}); |
443 |
|
|
} |
444 |
|
|
assertEquals(1 + TASKS, p.getTaskCount()); |
445 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
446 |
jsr166 |
1.37 |
} |
447 |
jsr166 |
1.100 |
assertEquals(1 + TASKS, p.getTaskCount()); |
448 |
|
|
assertEquals(1 + TASKS, p.getCompletedTaskCount()); |
449 |
dl |
1.1 |
} |
450 |
jsr166 |
1.24 |
|
451 |
dl |
1.1 |
/** |
452 |
jsr166 |
1.43 |
* isShutdown is false before shutdown, true after |
453 |
dl |
1.1 |
*/ |
454 |
dl |
1.5 |
public void testIsShutdown() { |
455 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
456 |
|
|
new ThreadPoolExecutor(1, 1, |
457 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
458 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
459 |
jsr166 |
1.82 |
try (PoolCleaner cleaner = cleaner(p)) { |
460 |
|
|
assertFalse(p.isShutdown()); |
461 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
462 |
|
|
assertTrue(p.isShutdown()); |
463 |
|
|
} |
464 |
dl |
1.1 |
} |
465 |
|
|
|
466 |
|
|
/** |
467 |
jsr166 |
1.48 |
* awaitTermination on a non-shutdown pool times out |
468 |
|
|
*/ |
469 |
|
|
public void testAwaitTermination_timesOut() throws InterruptedException { |
470 |
|
|
final ThreadPoolExecutor p = |
471 |
|
|
new ThreadPoolExecutor(1, 1, |
472 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
473 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
474 |
jsr166 |
1.84 |
try (PoolCleaner cleaner = cleaner(p)) { |
475 |
|
|
assertFalse(p.isTerminated()); |
476 |
|
|
assertFalse(p.awaitTermination(Long.MIN_VALUE, NANOSECONDS)); |
477 |
|
|
assertFalse(p.awaitTermination(Long.MIN_VALUE, MILLISECONDS)); |
478 |
|
|
assertFalse(p.awaitTermination(-1L, NANOSECONDS)); |
479 |
|
|
assertFalse(p.awaitTermination(-1L, MILLISECONDS)); |
480 |
jsr166 |
1.118 |
assertFalse(p.awaitTermination(randomExpiredTimeout(), |
481 |
|
|
randomTimeUnit())); |
482 |
jsr166 |
1.84 |
long timeoutNanos = 999999L; |
483 |
|
|
long startTime = System.nanoTime(); |
484 |
|
|
assertFalse(p.awaitTermination(timeoutNanos, NANOSECONDS)); |
485 |
|
|
assertTrue(System.nanoTime() - startTime >= timeoutNanos); |
486 |
|
|
assertFalse(p.isTerminated()); |
487 |
|
|
startTime = System.nanoTime(); |
488 |
|
|
long timeoutMillis = timeoutMillis(); |
489 |
|
|
assertFalse(p.awaitTermination(timeoutMillis, MILLISECONDS)); |
490 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis); |
491 |
|
|
assertFalse(p.isTerminated()); |
492 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
493 |
|
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
494 |
|
|
assertTrue(p.isTerminated()); |
495 |
|
|
} |
496 |
jsr166 |
1.48 |
} |
497 |
|
|
|
498 |
|
|
/** |
499 |
jsr166 |
1.35 |
* isTerminated is false before termination, true after |
500 |
dl |
1.1 |
*/ |
501 |
jsr166 |
1.27 |
public void testIsTerminated() throws InterruptedException { |
502 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
503 |
|
|
new ThreadPoolExecutor(1, 1, |
504 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
505 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
506 |
jsr166 |
1.83 |
try (PoolCleaner cleaner = cleaner(p)) { |
507 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
508 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
509 |
|
|
assertFalse(p.isTerminating()); |
510 |
jsr166 |
1.37 |
p.execute(new CheckedRunnable() { |
511 |
|
|
public void realRun() throws InterruptedException { |
512 |
jsr166 |
1.83 |
assertFalse(p.isTerminating()); |
513 |
jsr166 |
1.37 |
threadStarted.countDown(); |
514 |
jsr166 |
1.106 |
await(done); |
515 |
jsr166 |
1.37 |
}}); |
516 |
jsr166 |
1.108 |
await(threadStarted); |
517 |
jsr166 |
1.39 |
assertFalse(p.isTerminating()); |
518 |
jsr166 |
1.37 |
done.countDown(); |
519 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
520 |
jsr166 |
1.83 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
521 |
|
|
assertTrue(p.isTerminated()); |
522 |
|
|
assertFalse(p.isTerminating()); |
523 |
dl |
1.1 |
} |
524 |
dl |
1.5 |
} |
525 |
|
|
|
526 |
|
|
/** |
527 |
jsr166 |
1.35 |
* isTerminating is not true when running or when terminated |
528 |
dl |
1.5 |
*/ |
529 |
jsr166 |
1.27 |
public void testIsTerminating() throws InterruptedException { |
530 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
531 |
|
|
new ThreadPoolExecutor(1, 1, |
532 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
533 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
534 |
jsr166 |
1.85 |
try (PoolCleaner cleaner = cleaner(p)) { |
535 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
536 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
537 |
jsr166 |
1.37 |
assertFalse(p.isTerminating()); |
538 |
|
|
p.execute(new CheckedRunnable() { |
539 |
|
|
public void realRun() throws InterruptedException { |
540 |
jsr166 |
1.38 |
assertFalse(p.isTerminating()); |
541 |
jsr166 |
1.37 |
threadStarted.countDown(); |
542 |
jsr166 |
1.106 |
await(done); |
543 |
jsr166 |
1.37 |
}}); |
544 |
jsr166 |
1.108 |
await(threadStarted); |
545 |
jsr166 |
1.37 |
assertFalse(p.isTerminating()); |
546 |
|
|
done.countDown(); |
547 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
548 |
jsr166 |
1.85 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
549 |
|
|
assertTrue(p.isTerminated()); |
550 |
|
|
assertFalse(p.isTerminating()); |
551 |
jsr166 |
1.37 |
} |
552 |
dl |
1.1 |
} |
553 |
|
|
|
554 |
|
|
/** |
555 |
dl |
1.8 |
* getQueue returns the work queue, which contains queued tasks |
556 |
|
|
*/ |
557 |
jsr166 |
1.27 |
public void testGetQueue() throws InterruptedException { |
558 |
jsr166 |
1.109 |
final CountDownLatch done = new CountDownLatch(1); |
559 |
jsr166 |
1.114 |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
560 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
561 |
|
|
new ThreadPoolExecutor(1, 1, |
562 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
563 |
|
|
q); |
564 |
jsr166 |
1.109 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
565 |
jsr166 |
1.86 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
566 |
jsr166 |
1.37 |
FutureTask[] tasks = new FutureTask[5]; |
567 |
|
|
for (int i = 0; i < tasks.length; i++) { |
568 |
|
|
Callable task = new CheckedCallable<Boolean>() { |
569 |
|
|
public Boolean realCall() throws InterruptedException { |
570 |
|
|
threadStarted.countDown(); |
571 |
|
|
assertSame(q, p.getQueue()); |
572 |
jsr166 |
1.106 |
await(done); |
573 |
jsr166 |
1.37 |
return Boolean.TRUE; |
574 |
|
|
}}; |
575 |
|
|
tasks[i] = new FutureTask(task); |
576 |
|
|
p.execute(tasks[i]); |
577 |
|
|
} |
578 |
jsr166 |
1.108 |
await(threadStarted); |
579 |
jsr166 |
1.37 |
assertSame(q, p.getQueue()); |
580 |
|
|
assertFalse(q.contains(tasks[0])); |
581 |
|
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
582 |
|
|
assertEquals(tasks.length - 1, q.size()); |
583 |
dl |
1.8 |
} |
584 |
|
|
} |
585 |
|
|
|
586 |
|
|
/** |
587 |
|
|
* remove(task) removes queued task, and fails to remove active task |
588 |
|
|
*/ |
589 |
jsr166 |
1.27 |
public void testRemove() throws InterruptedException { |
590 |
jsr166 |
1.109 |
final CountDownLatch done = new CountDownLatch(1); |
591 |
jsr166 |
1.114 |
BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
592 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
593 |
|
|
new ThreadPoolExecutor(1, 1, |
594 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
595 |
|
|
q); |
596 |
jsr166 |
1.109 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
597 |
jsr166 |
1.87 |
Runnable[] tasks = new Runnable[6]; |
598 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
599 |
jsr166 |
1.37 |
for (int i = 0; i < tasks.length; i++) { |
600 |
|
|
tasks[i] = new CheckedRunnable() { |
601 |
|
|
public void realRun() throws InterruptedException { |
602 |
|
|
threadStarted.countDown(); |
603 |
jsr166 |
1.106 |
await(done); |
604 |
jsr166 |
1.37 |
}}; |
605 |
|
|
p.execute(tasks[i]); |
606 |
|
|
} |
607 |
jsr166 |
1.108 |
await(threadStarted); |
608 |
jsr166 |
1.37 |
assertFalse(p.remove(tasks[0])); |
609 |
dl |
1.8 |
assertTrue(q.contains(tasks[4])); |
610 |
|
|
assertTrue(q.contains(tasks[3])); |
611 |
jsr166 |
1.37 |
assertTrue(p.remove(tasks[4])); |
612 |
|
|
assertFalse(p.remove(tasks[4])); |
613 |
dl |
1.8 |
assertFalse(q.contains(tasks[4])); |
614 |
|
|
assertTrue(q.contains(tasks[3])); |
615 |
jsr166 |
1.37 |
assertTrue(p.remove(tasks[3])); |
616 |
dl |
1.8 |
assertFalse(q.contains(tasks[3])); |
617 |
|
|
} |
618 |
|
|
} |
619 |
|
|
|
620 |
|
|
/** |
621 |
jsr166 |
1.35 |
* purge removes cancelled tasks from the queue |
622 |
dl |
1.1 |
*/ |
623 |
jsr166 |
1.37 |
public void testPurge() throws InterruptedException { |
624 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
625 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
626 |
jsr166 |
1.114 |
final BlockingQueue<Runnable> q = new ArrayBlockingQueue<>(10); |
627 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
628 |
|
|
new ThreadPoolExecutor(1, 1, |
629 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
630 |
|
|
q); |
631 |
jsr166 |
1.101 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
632 |
jsr166 |
1.87 |
FutureTask[] tasks = new FutureTask[5]; |
633 |
jsr166 |
1.37 |
for (int i = 0; i < tasks.length; i++) { |
634 |
|
|
Callable task = new CheckedCallable<Boolean>() { |
635 |
|
|
public Boolean realCall() throws InterruptedException { |
636 |
|
|
threadStarted.countDown(); |
637 |
jsr166 |
1.106 |
await(done); |
638 |
jsr166 |
1.37 |
return Boolean.TRUE; |
639 |
|
|
}}; |
640 |
|
|
tasks[i] = new FutureTask(task); |
641 |
|
|
p.execute(tasks[i]); |
642 |
|
|
} |
643 |
jsr166 |
1.108 |
await(threadStarted); |
644 |
jsr166 |
1.37 |
assertEquals(tasks.length, p.getTaskCount()); |
645 |
|
|
assertEquals(tasks.length - 1, q.size()); |
646 |
|
|
assertEquals(1L, p.getActiveCount()); |
647 |
|
|
assertEquals(0L, p.getCompletedTaskCount()); |
648 |
|
|
tasks[4].cancel(true); |
649 |
|
|
tasks[3].cancel(false); |
650 |
|
|
p.purge(); |
651 |
|
|
assertEquals(tasks.length - 3, q.size()); |
652 |
|
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
653 |
|
|
p.purge(); // Nothing to do |
654 |
|
|
assertEquals(tasks.length - 3, q.size()); |
655 |
|
|
assertEquals(tasks.length - 2, p.getTaskCount()); |
656 |
|
|
} |
657 |
dl |
1.1 |
} |
658 |
|
|
|
659 |
|
|
/** |
660 |
jsr166 |
1.60 |
* shutdownNow returns a list containing tasks that were not run, |
661 |
|
|
* and those tasks are drained from the queue |
662 |
dl |
1.1 |
*/ |
663 |
jsr166 |
1.61 |
public void testShutdownNow() throws InterruptedException { |
664 |
|
|
final int poolSize = 2; |
665 |
|
|
final int count = 5; |
666 |
|
|
final AtomicInteger ran = new AtomicInteger(0); |
667 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
668 |
jsr166 |
1.61 |
new ThreadPoolExecutor(poolSize, poolSize, |
669 |
jsr166 |
1.37 |
LONG_DELAY_MS, MILLISECONDS, |
670 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
671 |
jsr166 |
1.99 |
final CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
672 |
jsr166 |
1.63 |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
673 |
jsr166 |
1.61 |
threadsStarted.countDown(); |
674 |
dl |
1.17 |
try { |
675 |
jsr166 |
1.61 |
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
676 |
|
|
} catch (InterruptedException success) {} |
677 |
|
|
ran.getAndIncrement(); |
678 |
|
|
}}; |
679 |
|
|
for (int i = 0; i < count; i++) |
680 |
|
|
p.execute(waiter); |
681 |
jsr166 |
1.107 |
await(threadsStarted); |
682 |
jsr166 |
1.62 |
assertEquals(poolSize, p.getActiveCount()); |
683 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
684 |
jsr166 |
1.61 |
final List<Runnable> queuedTasks; |
685 |
|
|
try { |
686 |
|
|
queuedTasks = p.shutdownNow(); |
687 |
|
|
} catch (SecurityException ok) { |
688 |
|
|
return; // Allowed in case test doesn't have privs |
689 |
dl |
1.1 |
} |
690 |
jsr166 |
1.37 |
assertTrue(p.isShutdown()); |
691 |
jsr166 |
1.60 |
assertTrue(p.getQueue().isEmpty()); |
692 |
jsr166 |
1.61 |
assertEquals(count - poolSize, queuedTasks.size()); |
693 |
|
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
694 |
|
|
assertTrue(p.isTerminated()); |
695 |
|
|
assertEquals(poolSize, ran.get()); |
696 |
jsr166 |
1.62 |
assertEquals(poolSize, p.getCompletedTaskCount()); |
697 |
dl |
1.1 |
} |
698 |
|
|
|
699 |
|
|
// Exception Tests |
700 |
|
|
|
701 |
jsr166 |
1.24 |
/** |
702 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
703 |
dl |
1.6 |
*/ |
704 |
dl |
1.1 |
public void testConstructor1() { |
705 |
dl |
1.5 |
try { |
706 |
jsr166 |
1.52 |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
707 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
708 |
dl |
1.5 |
shouldThrow(); |
709 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
710 |
dl |
1.1 |
} |
711 |
jsr166 |
1.24 |
|
712 |
|
|
/** |
713 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
714 |
dl |
1.6 |
*/ |
715 |
dl |
1.1 |
public void testConstructor2() { |
716 |
dl |
1.5 |
try { |
717 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
718 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
719 |
dl |
1.5 |
shouldThrow(); |
720 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
721 |
dl |
1.1 |
} |
722 |
jsr166 |
1.24 |
|
723 |
|
|
/** |
724 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
725 |
dl |
1.6 |
*/ |
726 |
dl |
1.1 |
public void testConstructor3() { |
727 |
dl |
1.5 |
try { |
728 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
729 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
730 |
dl |
1.5 |
shouldThrow(); |
731 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
732 |
dl |
1.1 |
} |
733 |
|
|
|
734 |
jsr166 |
1.24 |
/** |
735 |
|
|
* Constructor throws if keepAliveTime is less than zero |
736 |
dl |
1.6 |
*/ |
737 |
dl |
1.1 |
public void testConstructor4() { |
738 |
dl |
1.5 |
try { |
739 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
740 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
741 |
dl |
1.5 |
shouldThrow(); |
742 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
743 |
dl |
1.1 |
} |
744 |
|
|
|
745 |
jsr166 |
1.24 |
/** |
746 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
747 |
dl |
1.6 |
*/ |
748 |
dl |
1.1 |
public void testConstructor5() { |
749 |
dl |
1.5 |
try { |
750 |
jsr166 |
1.52 |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
751 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
752 |
dl |
1.5 |
shouldThrow(); |
753 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
754 |
dl |
1.1 |
} |
755 |
jsr166 |
1.24 |
|
756 |
|
|
/** |
757 |
|
|
* Constructor throws if workQueue is set to null |
758 |
dl |
1.6 |
*/ |
759 |
dl |
1.8 |
public void testConstructorNullPointerException() { |
760 |
dl |
1.5 |
try { |
761 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
762 |
jsr166 |
1.37 |
(BlockingQueue) null); |
763 |
dl |
1.5 |
shouldThrow(); |
764 |
jsr166 |
1.28 |
} catch (NullPointerException success) {} |
765 |
dl |
1.1 |
} |
766 |
|
|
|
767 |
jsr166 |
1.24 |
/** |
768 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
769 |
dl |
1.6 |
*/ |
770 |
dl |
1.1 |
public void testConstructor6() { |
771 |
dl |
1.5 |
try { |
772 |
jsr166 |
1.52 |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
773 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
774 |
|
|
new SimpleThreadFactory()); |
775 |
dl |
1.5 |
shouldThrow(); |
776 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
777 |
dl |
1.1 |
} |
778 |
jsr166 |
1.24 |
|
779 |
|
|
/** |
780 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
781 |
dl |
1.6 |
*/ |
782 |
dl |
1.1 |
public void testConstructor7() { |
783 |
dl |
1.5 |
try { |
784 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
785 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
786 |
|
|
new SimpleThreadFactory()); |
787 |
dl |
1.5 |
shouldThrow(); |
788 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
789 |
dl |
1.1 |
} |
790 |
|
|
|
791 |
jsr166 |
1.24 |
/** |
792 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
793 |
dl |
1.6 |
*/ |
794 |
dl |
1.1 |
public void testConstructor8() { |
795 |
dl |
1.5 |
try { |
796 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
797 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
798 |
|
|
new SimpleThreadFactory()); |
799 |
dl |
1.5 |
shouldThrow(); |
800 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
801 |
dl |
1.1 |
} |
802 |
|
|
|
803 |
jsr166 |
1.24 |
/** |
804 |
|
|
* Constructor throws if keepAliveTime is less than zero |
805 |
dl |
1.6 |
*/ |
806 |
dl |
1.1 |
public void testConstructor9() { |
807 |
dl |
1.5 |
try { |
808 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
809 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
810 |
|
|
new SimpleThreadFactory()); |
811 |
dl |
1.5 |
shouldThrow(); |
812 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
813 |
dl |
1.1 |
} |
814 |
|
|
|
815 |
jsr166 |
1.24 |
/** |
816 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
817 |
dl |
1.6 |
*/ |
818 |
dl |
1.1 |
public void testConstructor10() { |
819 |
dl |
1.5 |
try { |
820 |
jsr166 |
1.52 |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
821 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
822 |
|
|
new SimpleThreadFactory()); |
823 |
dl |
1.5 |
shouldThrow(); |
824 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
825 |
dl |
1.1 |
} |
826 |
|
|
|
827 |
jsr166 |
1.24 |
/** |
828 |
|
|
* Constructor throws if workQueue is set to null |
829 |
dl |
1.6 |
*/ |
830 |
dl |
1.8 |
public void testConstructorNullPointerException2() { |
831 |
dl |
1.5 |
try { |
832 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
833 |
jsr166 |
1.37 |
(BlockingQueue) null, |
834 |
|
|
new SimpleThreadFactory()); |
835 |
dl |
1.5 |
shouldThrow(); |
836 |
jsr166 |
1.28 |
} catch (NullPointerException success) {} |
837 |
dl |
1.1 |
} |
838 |
|
|
|
839 |
jsr166 |
1.24 |
/** |
840 |
|
|
* Constructor throws if threadFactory is set to null |
841 |
dl |
1.6 |
*/ |
842 |
dl |
1.8 |
public void testConstructorNullPointerException3() { |
843 |
dl |
1.5 |
try { |
844 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
845 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
846 |
|
|
(ThreadFactory) null); |
847 |
dl |
1.5 |
shouldThrow(); |
848 |
jsr166 |
1.28 |
} catch (NullPointerException success) {} |
849 |
dl |
1.1 |
} |
850 |
jsr166 |
1.24 |
|
851 |
|
|
/** |
852 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
853 |
dl |
1.6 |
*/ |
854 |
dl |
1.1 |
public void testConstructor11() { |
855 |
dl |
1.5 |
try { |
856 |
jsr166 |
1.52 |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
857 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
858 |
|
|
new NoOpREHandler()); |
859 |
dl |
1.5 |
shouldThrow(); |
860 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
861 |
dl |
1.1 |
} |
862 |
|
|
|
863 |
jsr166 |
1.24 |
/** |
864 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
865 |
dl |
1.6 |
*/ |
866 |
dl |
1.1 |
public void testConstructor12() { |
867 |
dl |
1.5 |
try { |
868 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
869 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
870 |
|
|
new NoOpREHandler()); |
871 |
dl |
1.5 |
shouldThrow(); |
872 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
873 |
dl |
1.1 |
} |
874 |
|
|
|
875 |
jsr166 |
1.24 |
/** |
876 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
877 |
dl |
1.6 |
*/ |
878 |
dl |
1.1 |
public void testConstructor13() { |
879 |
dl |
1.5 |
try { |
880 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
881 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
882 |
|
|
new NoOpREHandler()); |
883 |
dl |
1.5 |
shouldThrow(); |
884 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
885 |
dl |
1.1 |
} |
886 |
|
|
|
887 |
jsr166 |
1.24 |
/** |
888 |
|
|
* Constructor throws if keepAliveTime is less than zero |
889 |
dl |
1.6 |
*/ |
890 |
dl |
1.1 |
public void testConstructor14() { |
891 |
dl |
1.5 |
try { |
892 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
893 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
894 |
|
|
new NoOpREHandler()); |
895 |
dl |
1.5 |
shouldThrow(); |
896 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
897 |
dl |
1.1 |
} |
898 |
|
|
|
899 |
jsr166 |
1.24 |
/** |
900 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
901 |
dl |
1.6 |
*/ |
902 |
dl |
1.1 |
public void testConstructor15() { |
903 |
dl |
1.5 |
try { |
904 |
jsr166 |
1.52 |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
905 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
906 |
|
|
new NoOpREHandler()); |
907 |
dl |
1.5 |
shouldThrow(); |
908 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
909 |
dl |
1.1 |
} |
910 |
|
|
|
911 |
jsr166 |
1.24 |
/** |
912 |
|
|
* Constructor throws if workQueue is set to null |
913 |
dl |
1.6 |
*/ |
914 |
dl |
1.8 |
public void testConstructorNullPointerException4() { |
915 |
dl |
1.5 |
try { |
916 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
917 |
jsr166 |
1.37 |
(BlockingQueue) null, |
918 |
|
|
new NoOpREHandler()); |
919 |
dl |
1.5 |
shouldThrow(); |
920 |
jsr166 |
1.28 |
} catch (NullPointerException success) {} |
921 |
dl |
1.1 |
} |
922 |
|
|
|
923 |
jsr166 |
1.24 |
/** |
924 |
|
|
* Constructor throws if handler is set to null |
925 |
dl |
1.6 |
*/ |
926 |
dl |
1.8 |
public void testConstructorNullPointerException5() { |
927 |
dl |
1.5 |
try { |
928 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
929 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
930 |
|
|
(RejectedExecutionHandler) null); |
931 |
dl |
1.5 |
shouldThrow(); |
932 |
jsr166 |
1.28 |
} catch (NullPointerException success) {} |
933 |
dl |
1.1 |
} |
934 |
|
|
|
935 |
jsr166 |
1.24 |
/** |
936 |
|
|
* Constructor throws if corePoolSize argument is less than zero |
937 |
dl |
1.6 |
*/ |
938 |
dl |
1.1 |
public void testConstructor16() { |
939 |
dl |
1.5 |
try { |
940 |
jsr166 |
1.52 |
new ThreadPoolExecutor(-1, 1, 1L, SECONDS, |
941 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
942 |
|
|
new SimpleThreadFactory(), |
943 |
|
|
new NoOpREHandler()); |
944 |
dl |
1.5 |
shouldThrow(); |
945 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
946 |
dl |
1.1 |
} |
947 |
|
|
|
948 |
jsr166 |
1.24 |
/** |
949 |
|
|
* Constructor throws if maximumPoolSize is less than zero |
950 |
dl |
1.6 |
*/ |
951 |
dl |
1.1 |
public void testConstructor17() { |
952 |
dl |
1.5 |
try { |
953 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, -1, 1L, SECONDS, |
954 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
955 |
|
|
new SimpleThreadFactory(), |
956 |
|
|
new NoOpREHandler()); |
957 |
dl |
1.5 |
shouldThrow(); |
958 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
959 |
dl |
1.1 |
} |
960 |
|
|
|
961 |
jsr166 |
1.24 |
/** |
962 |
|
|
* Constructor throws if maximumPoolSize is equal to zero |
963 |
dl |
1.6 |
*/ |
964 |
dl |
1.1 |
public void testConstructor18() { |
965 |
dl |
1.5 |
try { |
966 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 0, 1L, SECONDS, |
967 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
968 |
|
|
new SimpleThreadFactory(), |
969 |
|
|
new NoOpREHandler()); |
970 |
dl |
1.5 |
shouldThrow(); |
971 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
972 |
dl |
1.1 |
} |
973 |
|
|
|
974 |
jsr166 |
1.24 |
/** |
975 |
|
|
* Constructor throws if keepAliveTime is less than zero |
976 |
dl |
1.6 |
*/ |
977 |
dl |
1.1 |
public void testConstructor19() { |
978 |
dl |
1.5 |
try { |
979 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, -1L, SECONDS, |
980 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
981 |
|
|
new SimpleThreadFactory(), |
982 |
|
|
new NoOpREHandler()); |
983 |
dl |
1.5 |
shouldThrow(); |
984 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
985 |
dl |
1.1 |
} |
986 |
|
|
|
987 |
jsr166 |
1.24 |
/** |
988 |
|
|
* Constructor throws if corePoolSize is greater than the maximumPoolSize |
989 |
dl |
1.6 |
*/ |
990 |
dl |
1.1 |
public void testConstructor20() { |
991 |
dl |
1.5 |
try { |
992 |
jsr166 |
1.52 |
new ThreadPoolExecutor(2, 1, 1L, SECONDS, |
993 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
994 |
|
|
new SimpleThreadFactory(), |
995 |
|
|
new NoOpREHandler()); |
996 |
dl |
1.5 |
shouldThrow(); |
997 |
jsr166 |
1.28 |
} catch (IllegalArgumentException success) {} |
998 |
dl |
1.1 |
} |
999 |
|
|
|
1000 |
jsr166 |
1.24 |
/** |
1001 |
jsr166 |
1.36 |
* Constructor throws if workQueue is null |
1002 |
dl |
1.6 |
*/ |
1003 |
dl |
1.8 |
public void testConstructorNullPointerException6() { |
1004 |
dl |
1.5 |
try { |
1005 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1006 |
jsr166 |
1.37 |
(BlockingQueue) null, |
1007 |
|
|
new SimpleThreadFactory(), |
1008 |
|
|
new NoOpREHandler()); |
1009 |
dl |
1.5 |
shouldThrow(); |
1010 |
jsr166 |
1.28 |
} catch (NullPointerException success) {} |
1011 |
dl |
1.1 |
} |
1012 |
|
|
|
1013 |
jsr166 |
1.24 |
/** |
1014 |
jsr166 |
1.36 |
* Constructor throws if handler is null |
1015 |
dl |
1.6 |
*/ |
1016 |
dl |
1.8 |
public void testConstructorNullPointerException7() { |
1017 |
dl |
1.5 |
try { |
1018 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1019 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
1020 |
|
|
new SimpleThreadFactory(), |
1021 |
|
|
(RejectedExecutionHandler) null); |
1022 |
dl |
1.5 |
shouldThrow(); |
1023 |
jsr166 |
1.28 |
} catch (NullPointerException success) {} |
1024 |
dl |
1.1 |
} |
1025 |
|
|
|
1026 |
jsr166 |
1.24 |
/** |
1027 |
jsr166 |
1.36 |
* Constructor throws if ThreadFactory is null |
1028 |
dl |
1.6 |
*/ |
1029 |
dl |
1.8 |
public void testConstructorNullPointerException8() { |
1030 |
dl |
1.5 |
try { |
1031 |
jsr166 |
1.52 |
new ThreadPoolExecutor(1, 2, 1L, SECONDS, |
1032 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10), |
1033 |
|
|
(ThreadFactory) null, |
1034 |
|
|
new NoOpREHandler()); |
1035 |
dl |
1.5 |
shouldThrow(); |
1036 |
jsr166 |
1.28 |
} catch (NullPointerException success) {} |
1037 |
dl |
1.1 |
} |
1038 |
jsr166 |
1.24 |
|
1039 |
jsr166 |
1.37 |
/** |
1040 |
|
|
* get of submitted callable throws InterruptedException if interrupted |
1041 |
|
|
*/ |
1042 |
|
|
public void testInterruptedSubmit() throws InterruptedException { |
1043 |
jsr166 |
1.104 |
final CountDownLatch done = new CountDownLatch(1); |
1044 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
1045 |
|
|
new ThreadPoolExecutor(1, 1, |
1046 |
jsr166 |
1.55 |
60, SECONDS, |
1047 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
1048 |
|
|
|
1049 |
jsr166 |
1.104 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1050 |
jsr166 |
1.88 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
1051 |
jsr166 |
1.37 |
Thread t = newStartedThread(new CheckedInterruptedRunnable() { |
1052 |
|
|
public void realRun() throws Exception { |
1053 |
|
|
Callable task = new CheckedCallable<Boolean>() { |
1054 |
|
|
public Boolean realCall() throws InterruptedException { |
1055 |
|
|
threadStarted.countDown(); |
1056 |
jsr166 |
1.106 |
await(done); |
1057 |
jsr166 |
1.37 |
return Boolean.TRUE; |
1058 |
|
|
}}; |
1059 |
|
|
p.submit(task).get(); |
1060 |
|
|
}}); |
1061 |
|
|
|
1062 |
jsr166 |
1.124 |
await(threadStarted); // ensure quiescence |
1063 |
jsr166 |
1.37 |
t.interrupt(); |
1064 |
jsr166 |
1.104 |
awaitTermination(t); |
1065 |
jsr166 |
1.37 |
} |
1066 |
|
|
} |
1067 |
dl |
1.1 |
|
1068 |
|
|
/** |
1069 |
jsr166 |
1.124 |
* Submitted tasks are rejected when saturated or shutdown |
1070 |
dl |
1.8 |
*/ |
1071 |
jsr166 |
1.124 |
public void testSubmittedTasksRejectedWhenSaturatedOrShutdown() throws InterruptedException { |
1072 |
|
|
final ThreadPoolExecutor p = new ThreadPoolExecutor( |
1073 |
|
|
1, 1, 1, SECONDS, new ArrayBlockingQueue<Runnable>(1)); |
1074 |
|
|
final int saturatedSize = saturatedSize(p); |
1075 |
jsr166 |
1.123 |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
1076 |
jsr166 |
1.124 |
final CountDownLatch threadsStarted = new CountDownLatch(p.getMaximumPoolSize()); |
1077 |
jsr166 |
1.109 |
final CountDownLatch done = new CountDownLatch(1); |
1078 |
jsr166 |
1.124 |
final Runnable r = () -> { |
1079 |
|
|
threadsStarted.countDown(); |
1080 |
|
|
for (;;) { |
1081 |
|
|
try { |
1082 |
|
|
done.await(); |
1083 |
|
|
return; |
1084 |
|
|
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1085 |
|
|
}}; |
1086 |
|
|
final Callable<Boolean> c = () -> { |
1087 |
|
|
threadsStarted.countDown(); |
1088 |
|
|
for (;;) { |
1089 |
|
|
try { |
1090 |
|
|
done.await(); |
1091 |
|
|
return Boolean.TRUE; |
1092 |
|
|
} catch (InterruptedException shutdownNowDeliberatelyIgnored) {} |
1093 |
jsr166 |
1.123 |
}}; |
1094 |
jsr166 |
1.124 |
final boolean shutdownNow = rnd.nextBoolean(); |
1095 |
jsr166 |
1.123 |
|
1096 |
jsr166 |
1.109 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1097 |
jsr166 |
1.123 |
// saturate |
1098 |
jsr166 |
1.124 |
for (int i = saturatedSize; i--> 0; ) { |
1099 |
|
|
switch (rnd.nextInt(4)) { |
1100 |
jsr166 |
1.123 |
case 0: p.execute(r); break; |
1101 |
|
|
case 1: assertFalse(p.submit(r).isDone()); break; |
1102 |
jsr166 |
1.124 |
case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break; |
1103 |
|
|
case 3: assertFalse(p.submit(c).isDone()); break; |
1104 |
jsr166 |
1.123 |
} |
1105 |
|
|
} |
1106 |
|
|
|
1107 |
jsr166 |
1.124 |
await(threadsStarted); |
1108 |
|
|
assertTaskSubmissionsAreRejected(p); |
1109 |
jsr166 |
1.123 |
|
1110 |
jsr166 |
1.124 |
if (shutdownNow) |
1111 |
|
|
p.shutdownNow(); |
1112 |
|
|
else |
1113 |
|
|
p.shutdown(); |
1114 |
|
|
// Pool is shutdown, but not yet terminated |
1115 |
|
|
assertTaskSubmissionsAreRejected(p); |
1116 |
|
|
assertFalse(p.isTerminated()); |
1117 |
|
|
|
1118 |
|
|
done.countDown(); // release blocking tasks |
1119 |
|
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
1120 |
|
|
|
1121 |
|
|
assertTaskSubmissionsAreRejected(p); |
1122 |
|
|
} |
1123 |
|
|
assertEquals(saturatedSize(p) |
1124 |
|
|
- (shutdownNow ? p.getQueue().remainingCapacity() : 0), |
1125 |
|
|
p.getCompletedTaskCount()); |
1126 |
dl |
1.8 |
} |
1127 |
|
|
|
1128 |
|
|
/** |
1129 |
jsr166 |
1.35 |
* executor using DiscardOldestPolicy drops oldest task if saturated. |
1130 |
dl |
1.8 |
*/ |
1131 |
jsr166 |
1.124 |
public void testSaturatedExecute_DiscardOldestPolicy() { |
1132 |
jsr166 |
1.92 |
final CountDownLatch done = new CountDownLatch(1); |
1133 |
|
|
LatchAwaiter r1 = awaiter(done); |
1134 |
|
|
LatchAwaiter r2 = awaiter(done); |
1135 |
|
|
LatchAwaiter r3 = awaiter(done); |
1136 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
1137 |
|
|
new ThreadPoolExecutor(1, 1, |
1138 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1139 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
1140 |
jsr166 |
1.121 |
new DiscardOldestPolicy()); |
1141 |
jsr166 |
1.109 |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1142 |
jsr166 |
1.92 |
assertEquals(LatchAwaiter.NEW, r1.state); |
1143 |
|
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1144 |
|
|
assertEquals(LatchAwaiter.NEW, r3.state); |
1145 |
|
|
p.execute(r1); |
1146 |
dl |
1.8 |
p.execute(r2); |
1147 |
|
|
assertTrue(p.getQueue().contains(r2)); |
1148 |
|
|
p.execute(r3); |
1149 |
|
|
assertFalse(p.getQueue().contains(r2)); |
1150 |
|
|
assertTrue(p.getQueue().contains(r3)); |
1151 |
|
|
} |
1152 |
jsr166 |
1.92 |
assertEquals(LatchAwaiter.DONE, r1.state); |
1153 |
|
|
assertEquals(LatchAwaiter.NEW, r2.state); |
1154 |
|
|
assertEquals(LatchAwaiter.DONE, r3.state); |
1155 |
dl |
1.8 |
} |
1156 |
|
|
|
1157 |
|
|
/** |
1158 |
jsr166 |
1.35 |
* execute using DiscardOldestPolicy drops task on shutdown |
1159 |
dl |
1.6 |
*/ |
1160 |
dl |
1.8 |
public void testDiscardOldestOnShutdown() { |
1161 |
jsr166 |
1.95 |
final ThreadPoolExecutor p = |
1162 |
jsr166 |
1.37 |
new ThreadPoolExecutor(1, 1, |
1163 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1164 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
1165 |
jsr166 |
1.121 |
new DiscardOldestPolicy()); |
1166 |
dl |
1.8 |
|
1167 |
jsr166 |
1.25 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1168 |
jsr166 |
1.95 |
try (PoolCleaner cleaner = cleaner(p)) { |
1169 |
dl |
1.8 |
TrackedNoOpRunnable r = new TrackedNoOpRunnable(); |
1170 |
jsr166 |
1.31 |
p.execute(r); |
1171 |
dl |
1.8 |
assertFalse(r.done); |
1172 |
|
|
} |
1173 |
dl |
1.6 |
} |
1174 |
|
|
|
1175 |
|
|
/** |
1176 |
jsr166 |
1.124 |
* Submitting null tasks throws NullPointerException |
1177 |
dl |
1.6 |
*/ |
1178 |
jsr166 |
1.124 |
public void testNullTaskSubmission() { |
1179 |
jsr166 |
1.95 |
final ThreadPoolExecutor p = |
1180 |
|
|
new ThreadPoolExecutor(1, 2, |
1181 |
|
|
1L, SECONDS, |
1182 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
1183 |
jsr166 |
1.95 |
try (PoolCleaner cleaner = cleaner(p)) { |
1184 |
jsr166 |
1.124 |
assertNullTaskSubmissionThrowsNullPointerException(p); |
1185 |
jsr166 |
1.95 |
} |
1186 |
dl |
1.6 |
} |
1187 |
jsr166 |
1.24 |
|
1188 |
dl |
1.1 |
/** |
1189 |
jsr166 |
1.34 |
* setCorePoolSize of negative value throws IllegalArgumentException |
1190 |
dl |
1.1 |
*/ |
1191 |
dl |
1.5 |
public void testCorePoolSizeIllegalArgumentException() { |
1192 |
jsr166 |
1.96 |
final ThreadPoolExecutor p = |
1193 |
jsr166 |
1.27 |
new ThreadPoolExecutor(1, 2, |
1194 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1195 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1196 |
jsr166 |
1.97 |
try (PoolCleaner cleaner = cleaner(p)) { |
1197 |
|
|
try { |
1198 |
|
|
p.setCorePoolSize(-1); |
1199 |
|
|
shouldThrow(); |
1200 |
|
|
} catch (IllegalArgumentException success) {} |
1201 |
dl |
1.1 |
} |
1202 |
jsr166 |
1.24 |
} |
1203 |
dl |
1.1 |
|
1204 |
|
|
/** |
1205 |
jsr166 |
1.35 |
* setMaximumPoolSize(int) throws IllegalArgumentException if |
1206 |
|
|
* given a value less the core pool size |
1207 |
jsr166 |
1.24 |
*/ |
1208 |
dl |
1.5 |
public void testMaximumPoolSizeIllegalArgumentException() { |
1209 |
jsr166 |
1.96 |
final ThreadPoolExecutor p = |
1210 |
jsr166 |
1.27 |
new ThreadPoolExecutor(2, 3, |
1211 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1212 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1213 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1214 |
|
|
try { |
1215 |
|
|
p.setMaximumPoolSize(1); |
1216 |
|
|
shouldThrow(); |
1217 |
|
|
} catch (IllegalArgumentException success) {} |
1218 |
dl |
1.1 |
} |
1219 |
|
|
} |
1220 |
jsr166 |
1.24 |
|
1221 |
dl |
1.1 |
/** |
1222 |
jsr166 |
1.35 |
* setMaximumPoolSize throws IllegalArgumentException |
1223 |
|
|
* if given a negative value |
1224 |
dl |
1.1 |
*/ |
1225 |
dl |
1.5 |
public void testMaximumPoolSizeIllegalArgumentException2() { |
1226 |
jsr166 |
1.96 |
final ThreadPoolExecutor p = |
1227 |
jsr166 |
1.27 |
new ThreadPoolExecutor(2, 3, |
1228 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1229 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1230 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1231 |
|
|
try { |
1232 |
|
|
p.setMaximumPoolSize(-1); |
1233 |
|
|
shouldThrow(); |
1234 |
|
|
} catch (IllegalArgumentException success) {} |
1235 |
dl |
1.1 |
} |
1236 |
|
|
} |
1237 |
jsr166 |
1.24 |
|
1238 |
dl |
1.1 |
/** |
1239 |
jsr166 |
1.54 |
* Configuration changes that allow core pool size greater than |
1240 |
|
|
* max pool size result in IllegalArgumentException. |
1241 |
|
|
*/ |
1242 |
|
|
public void testPoolSizeInvariants() { |
1243 |
jsr166 |
1.96 |
final ThreadPoolExecutor p = |
1244 |
jsr166 |
1.54 |
new ThreadPoolExecutor(1, 1, |
1245 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1246 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1247 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1248 |
|
|
for (int s = 1; s < 5; s++) { |
1249 |
|
|
p.setMaximumPoolSize(s); |
1250 |
|
|
p.setCorePoolSize(s); |
1251 |
|
|
try { |
1252 |
|
|
p.setMaximumPoolSize(s - 1); |
1253 |
|
|
shouldThrow(); |
1254 |
|
|
} catch (IllegalArgumentException success) {} |
1255 |
|
|
assertEquals(s, p.getCorePoolSize()); |
1256 |
|
|
assertEquals(s, p.getMaximumPoolSize()); |
1257 |
|
|
try { |
1258 |
|
|
p.setCorePoolSize(s + 1); |
1259 |
|
|
shouldThrow(); |
1260 |
|
|
} catch (IllegalArgumentException success) {} |
1261 |
|
|
assertEquals(s, p.getCorePoolSize()); |
1262 |
|
|
assertEquals(s, p.getMaximumPoolSize()); |
1263 |
|
|
} |
1264 |
jsr166 |
1.54 |
} |
1265 |
|
|
} |
1266 |
|
|
|
1267 |
|
|
/** |
1268 |
jsr166 |
1.35 |
* setKeepAliveTime throws IllegalArgumentException |
1269 |
|
|
* when given a negative value |
1270 |
dl |
1.1 |
*/ |
1271 |
dl |
1.5 |
public void testKeepAliveTimeIllegalArgumentException() { |
1272 |
jsr166 |
1.96 |
final ThreadPoolExecutor p = |
1273 |
jsr166 |
1.27 |
new ThreadPoolExecutor(2, 3, |
1274 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1275 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1276 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1277 |
|
|
try { |
1278 |
|
|
p.setKeepAliveTime(-1, MILLISECONDS); |
1279 |
|
|
shouldThrow(); |
1280 |
|
|
} catch (IllegalArgumentException success) {} |
1281 |
dl |
1.1 |
} |
1282 |
|
|
} |
1283 |
dl |
1.8 |
|
1284 |
|
|
/** |
1285 |
|
|
* terminated() is called on termination |
1286 |
|
|
*/ |
1287 |
|
|
public void testTerminated() { |
1288 |
jsr166 |
1.37 |
ExtendedTPE p = new ExtendedTPE(); |
1289 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1290 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
1291 |
|
|
assertTrue(p.terminatedCalled()); |
1292 |
|
|
assertTrue(p.isShutdown()); |
1293 |
|
|
} |
1294 |
dl |
1.8 |
} |
1295 |
|
|
|
1296 |
|
|
/** |
1297 |
|
|
* beforeExecute and afterExecute are called when executing task |
1298 |
|
|
*/ |
1299 |
jsr166 |
1.27 |
public void testBeforeAfter() throws InterruptedException { |
1300 |
jsr166 |
1.37 |
ExtendedTPE p = new ExtendedTPE(); |
1301 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1302 |
jsr166 |
1.45 |
final CountDownLatch done = new CountDownLatch(1); |
1303 |
jsr166 |
1.49 |
p.execute(new CheckedRunnable() { |
1304 |
jsr166 |
1.45 |
public void realRun() { |
1305 |
|
|
done.countDown(); |
1306 |
jsr166 |
1.49 |
}}); |
1307 |
jsr166 |
1.45 |
await(p.afterCalled); |
1308 |
|
|
assertEquals(0, done.getCount()); |
1309 |
|
|
assertTrue(p.afterCalled()); |
1310 |
|
|
assertTrue(p.beforeCalled()); |
1311 |
dl |
1.8 |
} |
1312 |
|
|
} |
1313 |
dl |
1.12 |
|
1314 |
|
|
/** |
1315 |
|
|
* completed submit of callable returns result |
1316 |
|
|
*/ |
1317 |
jsr166 |
1.27 |
public void testSubmitCallable() throws Exception { |
1318 |
jsr166 |
1.98 |
final ExecutorService e = |
1319 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1320 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1321 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1322 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1323 |
dl |
1.12 |
Future<String> future = e.submit(new StringTask()); |
1324 |
|
|
String result = future.get(); |
1325 |
|
|
assertSame(TEST_STRING, result); |
1326 |
|
|
} |
1327 |
|
|
} |
1328 |
|
|
|
1329 |
|
|
/** |
1330 |
|
|
* completed submit of runnable returns successfully |
1331 |
|
|
*/ |
1332 |
jsr166 |
1.27 |
public void testSubmitRunnable() throws Exception { |
1333 |
jsr166 |
1.98 |
final ExecutorService e = |
1334 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1335 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1336 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1337 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1338 |
dl |
1.12 |
Future<?> future = e.submit(new NoOpRunnable()); |
1339 |
|
|
future.get(); |
1340 |
|
|
assertTrue(future.isDone()); |
1341 |
|
|
} |
1342 |
|
|
} |
1343 |
|
|
|
1344 |
|
|
/** |
1345 |
|
|
* completed submit of (runnable, result) returns result |
1346 |
|
|
*/ |
1347 |
jsr166 |
1.27 |
public void testSubmitRunnable2() throws Exception { |
1348 |
jsr166 |
1.98 |
final ExecutorService e = |
1349 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1350 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1351 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1352 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1353 |
dl |
1.12 |
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
1354 |
|
|
String result = future.get(); |
1355 |
|
|
assertSame(TEST_STRING, result); |
1356 |
|
|
} |
1357 |
|
|
} |
1358 |
|
|
|
1359 |
|
|
/** |
1360 |
|
|
* invokeAny(null) throws NPE |
1361 |
|
|
*/ |
1362 |
jsr166 |
1.27 |
public void testInvokeAny1() throws Exception { |
1363 |
jsr166 |
1.98 |
final ExecutorService e = |
1364 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1365 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1366 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1367 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1368 |
|
|
try { |
1369 |
|
|
e.invokeAny(null); |
1370 |
|
|
shouldThrow(); |
1371 |
|
|
} catch (NullPointerException success) {} |
1372 |
dl |
1.12 |
} |
1373 |
|
|
} |
1374 |
|
|
|
1375 |
|
|
/** |
1376 |
jsr166 |
1.119 |
* invokeAny(empty collection) throws IllegalArgumentException |
1377 |
dl |
1.12 |
*/ |
1378 |
jsr166 |
1.27 |
public void testInvokeAny2() throws Exception { |
1379 |
jsr166 |
1.98 |
final ExecutorService e = |
1380 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1381 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1382 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1383 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1384 |
|
|
try { |
1385 |
|
|
e.invokeAny(new ArrayList<Callable<String>>()); |
1386 |
|
|
shouldThrow(); |
1387 |
|
|
} catch (IllegalArgumentException success) {} |
1388 |
dl |
1.12 |
} |
1389 |
|
|
} |
1390 |
|
|
|
1391 |
|
|
/** |
1392 |
|
|
* invokeAny(c) throws NPE if c has null elements |
1393 |
|
|
*/ |
1394 |
jsr166 |
1.27 |
public void testInvokeAny3() throws Exception { |
1395 |
jsr166 |
1.37 |
final CountDownLatch latch = new CountDownLatch(1); |
1396 |
|
|
final ExecutorService e = |
1397 |
|
|
new ThreadPoolExecutor(2, 2, |
1398 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1399 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1400 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1401 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1402 |
jsr166 |
1.98 |
l.add(latchAwaitingStringTask(latch)); |
1403 |
|
|
l.add(null); |
1404 |
|
|
try { |
1405 |
|
|
e.invokeAny(l); |
1406 |
|
|
shouldThrow(); |
1407 |
|
|
} catch (NullPointerException success) {} |
1408 |
jsr166 |
1.27 |
latch.countDown(); |
1409 |
dl |
1.12 |
} |
1410 |
|
|
} |
1411 |
|
|
|
1412 |
|
|
/** |
1413 |
|
|
* invokeAny(c) throws ExecutionException if no task completes |
1414 |
|
|
*/ |
1415 |
jsr166 |
1.27 |
public void testInvokeAny4() throws Exception { |
1416 |
jsr166 |
1.98 |
final ExecutorService e = |
1417 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1418 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1419 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1420 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1421 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1422 |
jsr166 |
1.98 |
l.add(new NPETask()); |
1423 |
|
|
try { |
1424 |
|
|
e.invokeAny(l); |
1425 |
|
|
shouldThrow(); |
1426 |
|
|
} catch (ExecutionException success) { |
1427 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
1428 |
|
|
} |
1429 |
dl |
1.12 |
} |
1430 |
|
|
} |
1431 |
|
|
|
1432 |
|
|
/** |
1433 |
|
|
* invokeAny(c) returns result of some task |
1434 |
|
|
*/ |
1435 |
jsr166 |
1.27 |
public void testInvokeAny5() throws Exception { |
1436 |
jsr166 |
1.98 |
final ExecutorService e = |
1437 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1438 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1439 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1440 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1441 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1442 |
dl |
1.12 |
l.add(new StringTask()); |
1443 |
|
|
l.add(new StringTask()); |
1444 |
|
|
String result = e.invokeAny(l); |
1445 |
|
|
assertSame(TEST_STRING, result); |
1446 |
|
|
} |
1447 |
|
|
} |
1448 |
|
|
|
1449 |
|
|
/** |
1450 |
|
|
* invokeAll(null) throws NPE |
1451 |
|
|
*/ |
1452 |
jsr166 |
1.27 |
public void testInvokeAll1() throws Exception { |
1453 |
jsr166 |
1.98 |
final ExecutorService e = |
1454 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1455 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1456 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1457 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1458 |
|
|
try { |
1459 |
|
|
e.invokeAll(null); |
1460 |
|
|
shouldThrow(); |
1461 |
|
|
} catch (NullPointerException success) {} |
1462 |
dl |
1.12 |
} |
1463 |
|
|
} |
1464 |
|
|
|
1465 |
|
|
/** |
1466 |
jsr166 |
1.119 |
* invokeAll(empty collection) returns empty list |
1467 |
dl |
1.12 |
*/ |
1468 |
jsr166 |
1.27 |
public void testInvokeAll2() throws InterruptedException { |
1469 |
jsr166 |
1.98 |
final ExecutorService e = |
1470 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1471 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1472 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1473 |
jsr166 |
1.119 |
final Collection<Callable<String>> emptyCollection |
1474 |
|
|
= Collections.emptyList(); |
1475 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1476 |
jsr166 |
1.119 |
List<Future<String>> r = e.invokeAll(emptyCollection); |
1477 |
dl |
1.12 |
assertTrue(r.isEmpty()); |
1478 |
|
|
} |
1479 |
|
|
} |
1480 |
|
|
|
1481 |
|
|
/** |
1482 |
|
|
* invokeAll(c) throws NPE if c has null elements |
1483 |
|
|
*/ |
1484 |
jsr166 |
1.27 |
public void testInvokeAll3() throws Exception { |
1485 |
jsr166 |
1.98 |
final ExecutorService e = |
1486 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1487 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1488 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1489 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1490 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1491 |
jsr166 |
1.98 |
l.add(new StringTask()); |
1492 |
|
|
l.add(null); |
1493 |
|
|
try { |
1494 |
|
|
e.invokeAll(l); |
1495 |
|
|
shouldThrow(); |
1496 |
|
|
} catch (NullPointerException success) {} |
1497 |
dl |
1.12 |
} |
1498 |
|
|
} |
1499 |
|
|
|
1500 |
|
|
/** |
1501 |
|
|
* get of element of invokeAll(c) throws exception on failed task |
1502 |
|
|
*/ |
1503 |
jsr166 |
1.27 |
public void testInvokeAll4() throws Exception { |
1504 |
jsr166 |
1.98 |
final ExecutorService e = |
1505 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1506 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1507 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1508 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1509 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1510 |
dl |
1.12 |
l.add(new NPETask()); |
1511 |
jsr166 |
1.33 |
List<Future<String>> futures = e.invokeAll(l); |
1512 |
|
|
assertEquals(1, futures.size()); |
1513 |
|
|
try { |
1514 |
|
|
futures.get(0).get(); |
1515 |
|
|
shouldThrow(); |
1516 |
|
|
} catch (ExecutionException success) { |
1517 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
1518 |
jsr166 |
1.27 |
} |
1519 |
dl |
1.12 |
} |
1520 |
|
|
} |
1521 |
|
|
|
1522 |
|
|
/** |
1523 |
|
|
* invokeAll(c) returns results of all completed tasks |
1524 |
|
|
*/ |
1525 |
jsr166 |
1.27 |
public void testInvokeAll5() throws Exception { |
1526 |
jsr166 |
1.98 |
final ExecutorService e = |
1527 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1528 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1529 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1530 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1531 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1532 |
dl |
1.12 |
l.add(new StringTask()); |
1533 |
|
|
l.add(new StringTask()); |
1534 |
jsr166 |
1.33 |
List<Future<String>> futures = e.invokeAll(l); |
1535 |
|
|
assertEquals(2, futures.size()); |
1536 |
|
|
for (Future<String> future : futures) |
1537 |
jsr166 |
1.27 |
assertSame(TEST_STRING, future.get()); |
1538 |
dl |
1.12 |
} |
1539 |
|
|
} |
1540 |
|
|
|
1541 |
dl |
1.13 |
/** |
1542 |
|
|
* timed invokeAny(null) throws NPE |
1543 |
|
|
*/ |
1544 |
jsr166 |
1.27 |
public void testTimedInvokeAny1() throws Exception { |
1545 |
jsr166 |
1.98 |
final ExecutorService e = |
1546 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1547 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1548 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1549 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1550 |
|
|
try { |
1551 |
jsr166 |
1.119 |
e.invokeAny(null, randomTimeout(), randomTimeUnit()); |
1552 |
jsr166 |
1.98 |
shouldThrow(); |
1553 |
|
|
} catch (NullPointerException success) {} |
1554 |
dl |
1.13 |
} |
1555 |
|
|
} |
1556 |
|
|
|
1557 |
|
|
/** |
1558 |
|
|
* timed invokeAny(,,null) throws NPE |
1559 |
|
|
*/ |
1560 |
jsr166 |
1.27 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1561 |
jsr166 |
1.98 |
final ExecutorService e = |
1562 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1563 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1564 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1565 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1566 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1567 |
jsr166 |
1.98 |
l.add(new StringTask()); |
1568 |
|
|
try { |
1569 |
jsr166 |
1.119 |
e.invokeAny(l, randomTimeout(), null); |
1570 |
jsr166 |
1.98 |
shouldThrow(); |
1571 |
|
|
} catch (NullPointerException success) {} |
1572 |
dl |
1.13 |
} |
1573 |
|
|
} |
1574 |
|
|
|
1575 |
|
|
/** |
1576 |
jsr166 |
1.119 |
* timed invokeAny(empty collection) throws IllegalArgumentException |
1577 |
dl |
1.13 |
*/ |
1578 |
jsr166 |
1.27 |
public void testTimedInvokeAny2() throws Exception { |
1579 |
jsr166 |
1.98 |
final ExecutorService e = |
1580 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1581 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1582 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1583 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1584 |
|
|
try { |
1585 |
|
|
e.invokeAny(new ArrayList<Callable<String>>(), |
1586 |
jsr166 |
1.119 |
randomTimeout(), randomTimeUnit()); |
1587 |
jsr166 |
1.98 |
shouldThrow(); |
1588 |
|
|
} catch (IllegalArgumentException success) {} |
1589 |
dl |
1.13 |
} |
1590 |
|
|
} |
1591 |
|
|
|
1592 |
|
|
/** |
1593 |
jsr166 |
1.119 |
* timed invokeAny(c) throws NullPointerException if c has null elements |
1594 |
dl |
1.13 |
*/ |
1595 |
jsr166 |
1.27 |
public void testTimedInvokeAny3() throws Exception { |
1596 |
jsr166 |
1.37 |
final CountDownLatch latch = new CountDownLatch(1); |
1597 |
|
|
final ExecutorService e = |
1598 |
|
|
new ThreadPoolExecutor(2, 2, |
1599 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1600 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1601 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1602 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1603 |
jsr166 |
1.98 |
l.add(latchAwaitingStringTask(latch)); |
1604 |
|
|
l.add(null); |
1605 |
|
|
try { |
1606 |
jsr166 |
1.119 |
e.invokeAny(l, randomTimeout(), randomTimeUnit()); |
1607 |
jsr166 |
1.98 |
shouldThrow(); |
1608 |
|
|
} catch (NullPointerException success) {} |
1609 |
jsr166 |
1.30 |
latch.countDown(); |
1610 |
dl |
1.13 |
} |
1611 |
|
|
} |
1612 |
|
|
|
1613 |
|
|
/** |
1614 |
|
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1615 |
|
|
*/ |
1616 |
jsr166 |
1.27 |
public void testTimedInvokeAny4() throws Exception { |
1617 |
jsr166 |
1.98 |
final ExecutorService e = |
1618 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1619 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1620 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1621 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1622 |
jsr166 |
1.111 |
long startTime = System.nanoTime(); |
1623 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1624 |
jsr166 |
1.98 |
l.add(new NPETask()); |
1625 |
|
|
try { |
1626 |
jsr166 |
1.111 |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1627 |
jsr166 |
1.98 |
shouldThrow(); |
1628 |
|
|
} catch (ExecutionException success) { |
1629 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
1630 |
|
|
} |
1631 |
jsr166 |
1.111 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1632 |
dl |
1.13 |
} |
1633 |
|
|
} |
1634 |
|
|
|
1635 |
|
|
/** |
1636 |
|
|
* timed invokeAny(c) returns result of some task |
1637 |
|
|
*/ |
1638 |
jsr166 |
1.27 |
public void testTimedInvokeAny5() throws Exception { |
1639 |
jsr166 |
1.98 |
final ExecutorService e = |
1640 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1641 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1642 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1643 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1644 |
jsr166 |
1.112 |
long startTime = System.nanoTime(); |
1645 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1646 |
dl |
1.13 |
l.add(new StringTask()); |
1647 |
|
|
l.add(new StringTask()); |
1648 |
jsr166 |
1.112 |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1649 |
dl |
1.13 |
assertSame(TEST_STRING, result); |
1650 |
jsr166 |
1.112 |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1651 |
dl |
1.13 |
} |
1652 |
|
|
} |
1653 |
|
|
|
1654 |
|
|
/** |
1655 |
|
|
* timed invokeAll(null) throws NPE |
1656 |
|
|
*/ |
1657 |
jsr166 |
1.27 |
public void testTimedInvokeAll1() throws Exception { |
1658 |
jsr166 |
1.98 |
final ExecutorService e = |
1659 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1660 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1661 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1662 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1663 |
|
|
try { |
1664 |
jsr166 |
1.119 |
e.invokeAll(null, randomTimeout(), randomTimeUnit()); |
1665 |
jsr166 |
1.98 |
shouldThrow(); |
1666 |
|
|
} catch (NullPointerException success) {} |
1667 |
dl |
1.13 |
} |
1668 |
|
|
} |
1669 |
|
|
|
1670 |
|
|
/** |
1671 |
|
|
* timed invokeAll(,,null) throws NPE |
1672 |
|
|
*/ |
1673 |
jsr166 |
1.27 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1674 |
jsr166 |
1.98 |
final ExecutorService e = |
1675 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1676 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1677 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1678 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1679 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1680 |
jsr166 |
1.98 |
l.add(new StringTask()); |
1681 |
|
|
try { |
1682 |
jsr166 |
1.119 |
e.invokeAll(l, randomTimeout(), null); |
1683 |
jsr166 |
1.98 |
shouldThrow(); |
1684 |
|
|
} catch (NullPointerException success) {} |
1685 |
dl |
1.13 |
} |
1686 |
|
|
} |
1687 |
|
|
|
1688 |
|
|
/** |
1689 |
jsr166 |
1.119 |
* timed invokeAll(empty collection) returns empty list |
1690 |
dl |
1.13 |
*/ |
1691 |
jsr166 |
1.27 |
public void testTimedInvokeAll2() throws InterruptedException { |
1692 |
jsr166 |
1.98 |
final ExecutorService e = |
1693 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1694 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1695 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1696 |
jsr166 |
1.119 |
final Collection<Callable<String>> emptyCollection |
1697 |
|
|
= Collections.emptyList(); |
1698 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1699 |
jsr166 |
1.119 |
List<Future<String>> r = |
1700 |
|
|
e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); |
1701 |
dl |
1.13 |
assertTrue(r.isEmpty()); |
1702 |
|
|
} |
1703 |
|
|
} |
1704 |
|
|
|
1705 |
|
|
/** |
1706 |
|
|
* timed invokeAll(c) throws NPE if c has null elements |
1707 |
|
|
*/ |
1708 |
jsr166 |
1.27 |
public void testTimedInvokeAll3() throws Exception { |
1709 |
jsr166 |
1.98 |
final ExecutorService e = |
1710 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1711 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1712 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1713 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1714 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1715 |
jsr166 |
1.98 |
l.add(new StringTask()); |
1716 |
|
|
l.add(null); |
1717 |
|
|
try { |
1718 |
jsr166 |
1.119 |
e.invokeAll(l, randomTimeout(), randomTimeUnit()); |
1719 |
jsr166 |
1.98 |
shouldThrow(); |
1720 |
|
|
} catch (NullPointerException success) {} |
1721 |
dl |
1.13 |
} |
1722 |
|
|
} |
1723 |
|
|
|
1724 |
|
|
/** |
1725 |
|
|
* get of element of invokeAll(c) throws exception on failed task |
1726 |
|
|
*/ |
1727 |
jsr166 |
1.27 |
public void testTimedInvokeAll4() throws Exception { |
1728 |
jsr166 |
1.98 |
final ExecutorService e = |
1729 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1730 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1731 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1732 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1733 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1734 |
jsr166 |
1.98 |
l.add(new NPETask()); |
1735 |
|
|
List<Future<String>> futures = |
1736 |
jsr166 |
1.105 |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1737 |
jsr166 |
1.98 |
assertEquals(1, futures.size()); |
1738 |
|
|
try { |
1739 |
|
|
futures.get(0).get(); |
1740 |
|
|
shouldThrow(); |
1741 |
|
|
} catch (ExecutionException success) { |
1742 |
|
|
assertTrue(success.getCause() instanceof NullPointerException); |
1743 |
|
|
} |
1744 |
dl |
1.13 |
} |
1745 |
|
|
} |
1746 |
|
|
|
1747 |
|
|
/** |
1748 |
|
|
* timed invokeAll(c) returns results of all completed tasks |
1749 |
|
|
*/ |
1750 |
jsr166 |
1.27 |
public void testTimedInvokeAll5() throws Exception { |
1751 |
jsr166 |
1.98 |
final ExecutorService e = |
1752 |
jsr166 |
1.37 |
new ThreadPoolExecutor(2, 2, |
1753 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1754 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1755 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1756 |
jsr166 |
1.114 |
List<Callable<String>> l = new ArrayList<>(); |
1757 |
dl |
1.13 |
l.add(new StringTask()); |
1758 |
|
|
l.add(new StringTask()); |
1759 |
jsr166 |
1.33 |
List<Future<String>> futures = |
1760 |
jsr166 |
1.65 |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1761 |
jsr166 |
1.33 |
assertEquals(2, futures.size()); |
1762 |
|
|
for (Future<String> future : futures) |
1763 |
|
|
assertSame(TEST_STRING, future.get()); |
1764 |
dl |
1.13 |
} |
1765 |
|
|
} |
1766 |
|
|
|
1767 |
|
|
/** |
1768 |
|
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1769 |
|
|
*/ |
1770 |
jsr166 |
1.27 |
public void testTimedInvokeAll6() throws Exception { |
1771 |
jsr166 |
1.110 |
for (long timeout = timeoutMillis();;) { |
1772 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
1773 |
|
|
final Callable<String> waiter = new CheckedCallable<String>() { |
1774 |
|
|
public String realCall() { |
1775 |
|
|
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1776 |
|
|
catch (InterruptedException ok) {} |
1777 |
|
|
return "1"; }}; |
1778 |
|
|
final ExecutorService p = |
1779 |
|
|
new ThreadPoolExecutor(2, 2, |
1780 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1781 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1782 |
|
|
try (PoolCleaner cleaner = cleaner(p, done)) { |
1783 |
jsr166 |
1.56 |
List<Callable<String>> tasks = new ArrayList<>(); |
1784 |
jsr166 |
1.59 |
tasks.add(new StringTask("0")); |
1785 |
jsr166 |
1.110 |
tasks.add(waiter); |
1786 |
jsr166 |
1.59 |
tasks.add(new StringTask("2")); |
1787 |
jsr166 |
1.56 |
long startTime = System.nanoTime(); |
1788 |
|
|
List<Future<String>> futures = |
1789 |
jsr166 |
1.110 |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1790 |
jsr166 |
1.56 |
assertEquals(tasks.size(), futures.size()); |
1791 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1792 |
|
|
for (Future future : futures) |
1793 |
|
|
assertTrue(future.isDone()); |
1794 |
|
|
assertTrue(futures.get(1).isCancelled()); |
1795 |
|
|
try { |
1796 |
jsr166 |
1.59 |
assertEquals("0", futures.get(0).get()); |
1797 |
|
|
assertEquals("2", futures.get(2).get()); |
1798 |
jsr166 |
1.56 |
break; |
1799 |
|
|
} catch (CancellationException retryWithLongerTimeout) { |
1800 |
|
|
timeout *= 2; |
1801 |
|
|
if (timeout >= LONG_DELAY_MS / 2) |
1802 |
|
|
fail("expected exactly one task to be cancelled"); |
1803 |
|
|
} |
1804 |
|
|
} |
1805 |
dl |
1.13 |
} |
1806 |
|
|
} |
1807 |
|
|
|
1808 |
dl |
1.19 |
/** |
1809 |
|
|
* Execution continues if there is at least one thread even if |
1810 |
|
|
* thread factory fails to create more |
1811 |
|
|
*/ |
1812 |
jsr166 |
1.27 |
public void testFailingThreadFactory() throws InterruptedException { |
1813 |
jsr166 |
1.37 |
final ExecutorService e = |
1814 |
|
|
new ThreadPoolExecutor(100, 100, |
1815 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1816 |
|
|
new LinkedBlockingQueue<Runnable>(), |
1817 |
|
|
new FailingThreadFactory()); |
1818 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(e)) { |
1819 |
jsr166 |
1.37 |
final int TASKS = 100; |
1820 |
|
|
final CountDownLatch done = new CountDownLatch(TASKS); |
1821 |
|
|
for (int k = 0; k < TASKS; ++k) |
1822 |
|
|
e.execute(new CheckedRunnable() { |
1823 |
|
|
public void realRun() { |
1824 |
|
|
done.countDown(); |
1825 |
|
|
}}); |
1826 |
jsr166 |
1.117 |
await(done); |
1827 |
dl |
1.19 |
} |
1828 |
|
|
} |
1829 |
dl |
1.21 |
|
1830 |
|
|
/** |
1831 |
|
|
* allowsCoreThreadTimeOut is by default false. |
1832 |
|
|
*/ |
1833 |
|
|
public void testAllowsCoreThreadTimeOut() { |
1834 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
1835 |
|
|
new ThreadPoolExecutor(2, 2, |
1836 |
|
|
1000, MILLISECONDS, |
1837 |
|
|
new ArrayBlockingQueue<Runnable>(10)); |
1838 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1839 |
|
|
assertFalse(p.allowsCoreThreadTimeOut()); |
1840 |
|
|
} |
1841 |
dl |
1.21 |
} |
1842 |
|
|
|
1843 |
|
|
/** |
1844 |
|
|
* allowCoreThreadTimeOut(true) causes idle threads to time out |
1845 |
|
|
*/ |
1846 |
jsr166 |
1.37 |
public void testAllowCoreThreadTimeOut_true() throws Exception { |
1847 |
jsr166 |
1.57 |
long keepAliveTime = timeoutMillis(); |
1848 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
1849 |
|
|
new ThreadPoolExecutor(2, 10, |
1850 |
jsr166 |
1.57 |
keepAliveTime, MILLISECONDS, |
1851 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
1852 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1853 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1854 |
jsr166 |
1.37 |
p.allowCoreThreadTimeOut(true); |
1855 |
|
|
p.execute(new CheckedRunnable() { |
1856 |
jsr166 |
1.44 |
public void realRun() { |
1857 |
jsr166 |
1.37 |
threadStarted.countDown(); |
1858 |
|
|
assertEquals(1, p.getPoolSize()); |
1859 |
|
|
}}); |
1860 |
jsr166 |
1.44 |
await(threadStarted); |
1861 |
jsr166 |
1.57 |
delay(keepAliveTime); |
1862 |
jsr166 |
1.44 |
long startTime = System.nanoTime(); |
1863 |
|
|
while (p.getPoolSize() > 0 |
1864 |
|
|
&& millisElapsedSince(startTime) < LONG_DELAY_MS) |
1865 |
|
|
Thread.yield(); |
1866 |
|
|
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1867 |
jsr166 |
1.37 |
assertEquals(0, p.getPoolSize()); |
1868 |
dl |
1.21 |
} |
1869 |
|
|
} |
1870 |
|
|
|
1871 |
|
|
/** |
1872 |
|
|
* allowCoreThreadTimeOut(false) causes idle threads not to time out |
1873 |
|
|
*/ |
1874 |
jsr166 |
1.37 |
public void testAllowCoreThreadTimeOut_false() throws Exception { |
1875 |
jsr166 |
1.57 |
long keepAliveTime = timeoutMillis(); |
1876 |
jsr166 |
1.37 |
final ThreadPoolExecutor p = |
1877 |
|
|
new ThreadPoolExecutor(2, 10, |
1878 |
jsr166 |
1.57 |
keepAliveTime, MILLISECONDS, |
1879 |
jsr166 |
1.37 |
new ArrayBlockingQueue<Runnable>(10)); |
1880 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1881 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
1882 |
jsr166 |
1.37 |
p.allowCoreThreadTimeOut(false); |
1883 |
|
|
p.execute(new CheckedRunnable() { |
1884 |
|
|
public void realRun() throws InterruptedException { |
1885 |
|
|
threadStarted.countDown(); |
1886 |
|
|
assertTrue(p.getPoolSize() >= 1); |
1887 |
|
|
}}); |
1888 |
jsr166 |
1.57 |
delay(2 * keepAliveTime); |
1889 |
jsr166 |
1.37 |
assertTrue(p.getPoolSize() >= 1); |
1890 |
dl |
1.21 |
} |
1891 |
|
|
} |
1892 |
|
|
|
1893 |
dl |
1.23 |
/** |
1894 |
|
|
* execute allows the same task to be submitted multiple times, even |
1895 |
|
|
* if rejected |
1896 |
|
|
*/ |
1897 |
jsr166 |
1.27 |
public void testRejectedRecycledTask() throws InterruptedException { |
1898 |
dl |
1.23 |
final int nTasks = 1000; |
1899 |
jsr166 |
1.37 |
final CountDownLatch done = new CountDownLatch(nTasks); |
1900 |
dl |
1.23 |
final Runnable recycledTask = new Runnable() { |
1901 |
jsr166 |
1.37 |
public void run() { |
1902 |
|
|
done.countDown(); |
1903 |
|
|
}}; |
1904 |
jsr166 |
1.24 |
final ThreadPoolExecutor p = |
1905 |
jsr166 |
1.55 |
new ThreadPoolExecutor(1, 30, |
1906 |
|
|
60, SECONDS, |
1907 |
dl |
1.23 |
new ArrayBlockingQueue(30)); |
1908 |
jsr166 |
1.98 |
try (PoolCleaner cleaner = cleaner(p)) { |
1909 |
dl |
1.23 |
for (int i = 0; i < nTasks; ++i) { |
1910 |
|
|
for (;;) { |
1911 |
|
|
try { |
1912 |
|
|
p.execute(recycledTask); |
1913 |
|
|
break; |
1914 |
|
|
} |
1915 |
jsr166 |
1.37 |
catch (RejectedExecutionException ignore) {} |
1916 |
dl |
1.23 |
} |
1917 |
|
|
} |
1918 |
jsr166 |
1.37 |
// enough time to run all tasks |
1919 |
jsr166 |
1.117 |
await(done, nTasks * SHORT_DELAY_MS); |
1920 |
dl |
1.23 |
} |
1921 |
|
|
} |
1922 |
jsr166 |
1.24 |
|
1923 |
jsr166 |
1.64 |
/** |
1924 |
|
|
* get(cancelled task) throws CancellationException |
1925 |
|
|
*/ |
1926 |
|
|
public void testGet_cancelled() throws Exception { |
1927 |
jsr166 |
1.109 |
final CountDownLatch done = new CountDownLatch(1); |
1928 |
jsr166 |
1.64 |
final ExecutorService e = |
1929 |
|
|
new ThreadPoolExecutor(1, 1, |
1930 |
|
|
LONG_DELAY_MS, MILLISECONDS, |
1931 |
|
|
new LinkedBlockingQueue<Runnable>()); |
1932 |
jsr166 |
1.109 |
try (PoolCleaner cleaner = cleaner(e, done)) { |
1933 |
jsr166 |
1.64 |
final CountDownLatch blockerStarted = new CountDownLatch(1); |
1934 |
|
|
final List<Future<?>> futures = new ArrayList<>(); |
1935 |
|
|
for (int i = 0; i < 2; i++) { |
1936 |
|
|
Runnable r = new CheckedRunnable() { public void realRun() |
1937 |
|
|
throws Throwable { |
1938 |
|
|
blockerStarted.countDown(); |
1939 |
|
|
assertTrue(done.await(2 * LONG_DELAY_MS, MILLISECONDS)); |
1940 |
|
|
}}; |
1941 |
|
|
futures.add(e.submit(r)); |
1942 |
|
|
} |
1943 |
jsr166 |
1.108 |
await(blockerStarted); |
1944 |
jsr166 |
1.64 |
for (Future<?> future : futures) future.cancel(false); |
1945 |
|
|
for (Future<?> future : futures) { |
1946 |
|
|
try { |
1947 |
|
|
future.get(); |
1948 |
|
|
shouldThrow(); |
1949 |
|
|
} catch (CancellationException success) {} |
1950 |
|
|
try { |
1951 |
|
|
future.get(LONG_DELAY_MS, MILLISECONDS); |
1952 |
|
|
shouldThrow(); |
1953 |
|
|
} catch (CancellationException success) {} |
1954 |
|
|
assertTrue(future.isCancelled()); |
1955 |
|
|
assertTrue(future.isDone()); |
1956 |
|
|
} |
1957 |
|
|
} |
1958 |
|
|
} |
1959 |
|
|
|
1960 |
jsr166 |
1.122 |
/** Directly test simple ThreadPoolExecutor RejectedExecutionHandlers. */ |
1961 |
|
|
public void testStandardRejectedExecutionHandlers() { |
1962 |
jsr166 |
1.120 |
final ThreadPoolExecutor p = |
1963 |
jsr166 |
1.122 |
new ThreadPoolExecutor(1, 1, 1, SECONDS, |
1964 |
|
|
new ArrayBlockingQueue<Runnable>(1)); |
1965 |
|
|
final AtomicReference<Thread> thread = new AtomicReference<>(); |
1966 |
|
|
final Runnable r = new Runnable() { public void run() { |
1967 |
|
|
thread.set(Thread.currentThread()); }}; |
1968 |
|
|
|
1969 |
jsr166 |
1.120 |
try { |
1970 |
jsr166 |
1.122 |
new AbortPolicy().rejectedExecution(r, p); |
1971 |
jsr166 |
1.120 |
shouldThrow(); |
1972 |
|
|
} catch (RejectedExecutionException success) {} |
1973 |
jsr166 |
1.122 |
assertNull(thread.get()); |
1974 |
|
|
|
1975 |
|
|
new DiscardPolicy().rejectedExecution(r, p); |
1976 |
|
|
assertNull(thread.get()); |
1977 |
jsr166 |
1.120 |
|
1978 |
jsr166 |
1.122 |
new CallerRunsPolicy().rejectedExecution(r, p); |
1979 |
jsr166 |
1.121 |
assertSame(Thread.currentThread(), thread.get()); |
1980 |
jsr166 |
1.122 |
|
1981 |
|
|
// check that pool was not perturbed by handlers |
1982 |
jsr166 |
1.121 |
assertTrue(p.getRejectedExecutionHandler() instanceof AbortPolicy); |
1983 |
|
|
assertEquals(0, p.getTaskCount()); |
1984 |
|
|
assertTrue(p.getQueue().isEmpty()); |
1985 |
|
|
} |
1986 |
|
|
|
1987 |
jsr166 |
1.125 |
public void testThreadFactoryReturnsTerminatedThread_shouldThrow() { |
1988 |
|
|
if (!testImplementationDetails) |
1989 |
|
|
return; |
1990 |
|
|
|
1991 |
|
|
ThreadFactory returnsTerminatedThread = runnableIgnored -> { |
1992 |
|
|
Thread thread = new Thread(() -> {}); |
1993 |
|
|
thread.start(); |
1994 |
|
|
try { thread.join(); } |
1995 |
|
|
catch (InterruptedException ex) { throw new Error(ex); } |
1996 |
|
|
return thread; |
1997 |
|
|
}; |
1998 |
|
|
ThreadPoolExecutor p = |
1999 |
|
|
new ThreadPoolExecutor(1, 1, 1, SECONDS, |
2000 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
2001 |
|
|
returnsTerminatedThread); |
2002 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
2003 |
|
|
assertThrows(IllegalThreadStateException.class, |
2004 |
|
|
() -> p.execute(() -> {})); |
2005 |
|
|
} |
2006 |
|
|
} |
2007 |
|
|
|
2008 |
|
|
public void testThreadFactoryReturnsStartedThread_shouldThrow() { |
2009 |
|
|
if (!testImplementationDetails) |
2010 |
|
|
return; |
2011 |
|
|
|
2012 |
|
|
CountDownLatch latch = new CountDownLatch(1); |
2013 |
|
|
Runnable awaitLatch = () -> { |
2014 |
|
|
try { latch.await(); } |
2015 |
|
|
catch (InterruptedException ex) { throw new Error(ex); }}; |
2016 |
|
|
ThreadFactory returnsStartedThread = runnable -> { |
2017 |
|
|
Thread thread = new Thread(awaitLatch); |
2018 |
|
|
thread.start(); |
2019 |
|
|
return thread; |
2020 |
|
|
}; |
2021 |
|
|
ThreadPoolExecutor p = |
2022 |
|
|
new ThreadPoolExecutor(1, 1, 1, SECONDS, |
2023 |
|
|
new ArrayBlockingQueue<Runnable>(1), |
2024 |
|
|
returnsStartedThread); |
2025 |
|
|
try (PoolCleaner cleaner = cleaner(p)) { |
2026 |
|
|
assertThrows(IllegalThreadStateException.class, |
2027 |
|
|
() -> p.execute(() -> {})); |
2028 |
|
|
latch.countDown(); |
2029 |
|
|
} |
2030 |
|
|
} |
2031 |
|
|
|
2032 |
dl |
1.1 |
} |