1 |
dl |
1.1 |
/* |
2 |
dl |
1.13 |
* 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.37 |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
jsr166 |
1.22 |
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
|
* Pat Fisher, Mike Judd. |
7 |
dl |
1.1 |
*/ |
8 |
|
|
|
9 |
jsr166 |
1.25 |
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
jsr166 |
1.50 |
|
11 |
|
|
import java.util.ArrayList; |
12 |
|
|
import java.util.List; |
13 |
|
|
import java.util.concurrent.BlockingQueue; |
14 |
|
|
import java.util.concurrent.Callable; |
15 |
jsr166 |
1.52 |
import java.util.concurrent.CancellationException; |
16 |
jsr166 |
1.50 |
import java.util.concurrent.CountDownLatch; |
17 |
|
|
import java.util.concurrent.ExecutionException; |
18 |
|
|
import java.util.concurrent.Executors; |
19 |
|
|
import java.util.concurrent.ExecutorService; |
20 |
|
|
import java.util.concurrent.Future; |
21 |
|
|
import java.util.concurrent.RejectedExecutionException; |
22 |
|
|
import java.util.concurrent.ScheduledFuture; |
23 |
|
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
24 |
|
|
import java.util.concurrent.ThreadFactory; |
25 |
|
|
import java.util.concurrent.ThreadPoolExecutor; |
26 |
jsr166 |
1.46 |
import java.util.concurrent.atomic.AtomicInteger; |
27 |
dl |
1.1 |
|
28 |
jsr166 |
1.50 |
import junit.framework.Test; |
29 |
|
|
import junit.framework.TestSuite; |
30 |
|
|
|
31 |
dl |
1.4 |
public class ScheduledExecutorTest extends JSR166TestCase { |
32 |
dl |
1.1 |
public static void main(String[] args) { |
33 |
jsr166 |
1.51 |
main(suite(), args); |
34 |
dl |
1.1 |
} |
35 |
|
|
public static Test suite() { |
36 |
jsr166 |
1.26 |
return new TestSuite(ScheduledExecutorTest.class); |
37 |
dl |
1.1 |
} |
38 |
|
|
|
39 |
dl |
1.5 |
/** |
40 |
dl |
1.6 |
* execute successfully executes a runnable |
41 |
dl |
1.5 |
*/ |
42 |
jsr166 |
1.25 |
public void testExecute() throws InterruptedException { |
43 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
44 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
45 |
|
|
final Runnable task = new CheckedRunnable() { |
46 |
|
|
public void realRun() { |
47 |
|
|
done.countDown(); |
48 |
|
|
}}; |
49 |
|
|
try { |
50 |
|
|
p.execute(task); |
51 |
|
|
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
52 |
|
|
} finally { |
53 |
|
|
joinPool(p); |
54 |
|
|
} |
55 |
dl |
1.1 |
} |
56 |
|
|
|
57 |
dl |
1.5 |
/** |
58 |
dl |
1.6 |
* delayed schedule of callable successfully executes after delay |
59 |
dl |
1.5 |
*/ |
60 |
jsr166 |
1.25 |
public void testSchedule1() throws Exception { |
61 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
62 |
jsr166 |
1.44 |
final long startTime = System.nanoTime(); |
63 |
jsr166 |
1.33 |
final CountDownLatch done = new CountDownLatch(1); |
64 |
|
|
try { |
65 |
|
|
Callable task = new CheckedCallable<Boolean>() { |
66 |
|
|
public Boolean realCall() { |
67 |
|
|
done.countDown(); |
68 |
jsr166 |
1.44 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
69 |
jsr166 |
1.33 |
return Boolean.TRUE; |
70 |
|
|
}}; |
71 |
jsr166 |
1.44 |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
72 |
|
|
assertSame(Boolean.TRUE, f.get()); |
73 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
74 |
jsr166 |
1.33 |
assertTrue(done.await(0L, MILLISECONDS)); |
75 |
|
|
} finally { |
76 |
|
|
joinPool(p); |
77 |
|
|
} |
78 |
dl |
1.1 |
} |
79 |
|
|
|
80 |
|
|
/** |
81 |
jsr166 |
1.32 |
* delayed schedule of runnable successfully executes after delay |
82 |
dl |
1.1 |
*/ |
83 |
jsr166 |
1.33 |
public void testSchedule3() throws Exception { |
84 |
|
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
85 |
jsr166 |
1.44 |
final long startTime = System.nanoTime(); |
86 |
jsr166 |
1.33 |
final CountDownLatch done = new CountDownLatch(1); |
87 |
|
|
try { |
88 |
|
|
Runnable task = new CheckedRunnable() { |
89 |
|
|
public void realRun() { |
90 |
|
|
done.countDown(); |
91 |
jsr166 |
1.44 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
92 |
jsr166 |
1.33 |
}}; |
93 |
jsr166 |
1.44 |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
94 |
|
|
await(done); |
95 |
|
|
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
96 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
97 |
jsr166 |
1.33 |
} finally { |
98 |
|
|
joinPool(p); |
99 |
|
|
} |
100 |
dl |
1.1 |
} |
101 |
jsr166 |
1.22 |
|
102 |
dl |
1.1 |
/** |
103 |
dl |
1.6 |
* scheduleAtFixedRate executes runnable after given initial delay |
104 |
dl |
1.1 |
*/ |
105 |
jsr166 |
1.33 |
public void testSchedule4() throws Exception { |
106 |
|
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
107 |
jsr166 |
1.44 |
final long startTime = System.nanoTime(); |
108 |
jsr166 |
1.33 |
final CountDownLatch done = new CountDownLatch(1); |
109 |
|
|
try { |
110 |
|
|
Runnable task = new CheckedRunnable() { |
111 |
|
|
public void realRun() { |
112 |
|
|
done.countDown(); |
113 |
jsr166 |
1.44 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
114 |
jsr166 |
1.33 |
}}; |
115 |
|
|
ScheduledFuture f = |
116 |
jsr166 |
1.44 |
p.scheduleAtFixedRate(task, timeoutMillis(), |
117 |
|
|
LONG_DELAY_MS, MILLISECONDS); |
118 |
|
|
await(done); |
119 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
120 |
jsr166 |
1.33 |
f.cancel(true); |
121 |
|
|
} finally { |
122 |
|
|
joinPool(p); |
123 |
|
|
} |
124 |
dl |
1.18 |
} |
125 |
|
|
|
126 |
dl |
1.6 |
/** |
127 |
|
|
* scheduleWithFixedDelay executes runnable after given initial delay |
128 |
|
|
*/ |
129 |
jsr166 |
1.44 |
public void testSchedule5() throws Exception { |
130 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
131 |
jsr166 |
1.44 |
final long startTime = System.nanoTime(); |
132 |
jsr166 |
1.33 |
final CountDownLatch done = new CountDownLatch(1); |
133 |
|
|
try { |
134 |
|
|
Runnable task = new CheckedRunnable() { |
135 |
|
|
public void realRun() { |
136 |
|
|
done.countDown(); |
137 |
jsr166 |
1.44 |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
138 |
jsr166 |
1.33 |
}}; |
139 |
|
|
ScheduledFuture f = |
140 |
jsr166 |
1.44 |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
141 |
|
|
LONG_DELAY_MS, MILLISECONDS); |
142 |
|
|
await(done); |
143 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
144 |
jsr166 |
1.33 |
f.cancel(true); |
145 |
|
|
} finally { |
146 |
|
|
joinPool(p); |
147 |
|
|
} |
148 |
|
|
} |
149 |
|
|
|
150 |
|
|
static class RunnableCounter implements Runnable { |
151 |
|
|
AtomicInteger count = new AtomicInteger(0); |
152 |
|
|
public void run() { count.getAndIncrement(); } |
153 |
dl |
1.1 |
} |
154 |
jsr166 |
1.22 |
|
155 |
dl |
1.6 |
/** |
156 |
dl |
1.18 |
* scheduleAtFixedRate executes series of tasks at given rate |
157 |
|
|
*/ |
158 |
jsr166 |
1.25 |
public void testFixedRateSequence() throws InterruptedException { |
159 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
160 |
jsr166 |
1.47 |
try { |
161 |
|
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
162 |
|
|
long startTime = System.nanoTime(); |
163 |
|
|
int cycles = 10; |
164 |
|
|
final CountDownLatch done = new CountDownLatch(cycles); |
165 |
jsr166 |
1.49 |
Runnable task = new CheckedRunnable() { |
166 |
jsr166 |
1.47 |
public void realRun() { done.countDown(); }}; |
167 |
|
|
ScheduledFuture h = |
168 |
|
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
169 |
|
|
done.await(); |
170 |
|
|
h.cancel(true); |
171 |
|
|
double normalizedTime = |
172 |
|
|
(double) millisElapsedSince(startTime) / delay; |
173 |
|
|
if (normalizedTime >= cycles - 1 && |
174 |
|
|
normalizedTime <= cycles) |
175 |
|
|
return; |
176 |
|
|
} |
177 |
|
|
throw new AssertionError("unexpected execution rate"); |
178 |
|
|
} finally { |
179 |
|
|
joinPool(p); |
180 |
|
|
} |
181 |
dl |
1.18 |
} |
182 |
|
|
|
183 |
|
|
/** |
184 |
|
|
* scheduleWithFixedDelay executes series of tasks with given period |
185 |
|
|
*/ |
186 |
jsr166 |
1.25 |
public void testFixedDelaySequence() throws InterruptedException { |
187 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
188 |
jsr166 |
1.47 |
try { |
189 |
|
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
190 |
|
|
long startTime = System.nanoTime(); |
191 |
|
|
int cycles = 10; |
192 |
|
|
final CountDownLatch done = new CountDownLatch(cycles); |
193 |
jsr166 |
1.49 |
Runnable task = new CheckedRunnable() { |
194 |
jsr166 |
1.47 |
public void realRun() { done.countDown(); }}; |
195 |
|
|
ScheduledFuture h = |
196 |
|
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
197 |
|
|
done.await(); |
198 |
|
|
h.cancel(true); |
199 |
|
|
double normalizedTime = |
200 |
|
|
(double) millisElapsedSince(startTime) / delay; |
201 |
|
|
if (normalizedTime >= cycles - 1 && |
202 |
|
|
normalizedTime <= cycles) |
203 |
|
|
return; |
204 |
|
|
} |
205 |
|
|
throw new AssertionError("unexpected execution rate"); |
206 |
|
|
} finally { |
207 |
|
|
joinPool(p); |
208 |
|
|
} |
209 |
dl |
1.18 |
} |
210 |
|
|
|
211 |
|
|
/** |
212 |
jsr166 |
1.32 |
* execute(null) throws NPE |
213 |
dl |
1.6 |
*/ |
214 |
jsr166 |
1.25 |
public void testExecuteNull() throws InterruptedException { |
215 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = null; |
216 |
dl |
1.6 |
try { |
217 |
jsr166 |
1.26 |
se = new ScheduledThreadPoolExecutor(1); |
218 |
|
|
se.execute(null); |
219 |
dl |
1.6 |
shouldThrow(); |
220 |
jsr166 |
1.26 |
} catch (NullPointerException success) {} |
221 |
jsr166 |
1.22 |
|
222 |
jsr166 |
1.26 |
joinPool(se); |
223 |
dl |
1.6 |
} |
224 |
|
|
|
225 |
|
|
/** |
226 |
jsr166 |
1.30 |
* schedule(null) throws NPE |
227 |
dl |
1.6 |
*/ |
228 |
jsr166 |
1.25 |
public void testScheduleNull() throws InterruptedException { |
229 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
230 |
jsr166 |
1.26 |
try { |
231 |
dl |
1.7 |
TrackedCallable callable = null; |
232 |
jsr166 |
1.26 |
Future f = se.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
233 |
dl |
1.6 |
shouldThrow(); |
234 |
jsr166 |
1.26 |
} catch (NullPointerException success) {} |
235 |
|
|
joinPool(se); |
236 |
dl |
1.6 |
} |
237 |
jsr166 |
1.22 |
|
238 |
dl |
1.1 |
/** |
239 |
dl |
1.6 |
* execute throws RejectedExecutionException if shutdown |
240 |
dl |
1.1 |
*/ |
241 |
jsr166 |
1.25 |
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
242 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
243 |
dl |
1.5 |
try { |
244 |
dl |
1.1 |
se.shutdown(); |
245 |
dl |
1.4 |
se.schedule(new NoOpRunnable(), |
246 |
jsr166 |
1.25 |
MEDIUM_DELAY_MS, MILLISECONDS); |
247 |
dl |
1.5 |
shouldThrow(); |
248 |
jsr166 |
1.24 |
} catch (RejectedExecutionException success) { |
249 |
dl |
1.17 |
} catch (SecurityException ok) { |
250 |
dl |
1.4 |
} |
251 |
jsr166 |
1.22 |
|
252 |
dl |
1.4 |
joinPool(se); |
253 |
dl |
1.1 |
} |
254 |
|
|
|
255 |
|
|
/** |
256 |
dl |
1.6 |
* schedule throws RejectedExecutionException if shutdown |
257 |
dl |
1.1 |
*/ |
258 |
jsr166 |
1.25 |
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
259 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
260 |
dl |
1.5 |
try { |
261 |
dl |
1.1 |
se.shutdown(); |
262 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
263 |
jsr166 |
1.25 |
MEDIUM_DELAY_MS, MILLISECONDS); |
264 |
dl |
1.5 |
shouldThrow(); |
265 |
jsr166 |
1.24 |
} catch (RejectedExecutionException success) { |
266 |
dl |
1.17 |
} catch (SecurityException ok) { |
267 |
dl |
1.4 |
} |
268 |
|
|
joinPool(se); |
269 |
dl |
1.1 |
} |
270 |
|
|
|
271 |
|
|
/** |
272 |
dl |
1.6 |
* schedule callable throws RejectedExecutionException if shutdown |
273 |
dl |
1.1 |
*/ |
274 |
jsr166 |
1.38 |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
275 |
|
|
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
276 |
|
|
try { |
277 |
dl |
1.1 |
se.shutdown(); |
278 |
dl |
1.4 |
se.schedule(new NoOpCallable(), |
279 |
jsr166 |
1.25 |
MEDIUM_DELAY_MS, MILLISECONDS); |
280 |
dl |
1.5 |
shouldThrow(); |
281 |
jsr166 |
1.24 |
} catch (RejectedExecutionException success) { |
282 |
dl |
1.17 |
} catch (SecurityException ok) { |
283 |
|
|
} |
284 |
jsr166 |
1.38 |
joinPool(se); |
285 |
dl |
1.1 |
} |
286 |
|
|
|
287 |
|
|
/** |
288 |
jsr166 |
1.32 |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
289 |
dl |
1.1 |
*/ |
290 |
jsr166 |
1.25 |
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
291 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
292 |
dl |
1.5 |
try { |
293 |
dl |
1.1 |
se.shutdown(); |
294 |
dl |
1.4 |
se.scheduleAtFixedRate(new NoOpRunnable(), |
295 |
jsr166 |
1.25 |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
296 |
dl |
1.5 |
shouldThrow(); |
297 |
jsr166 |
1.24 |
} catch (RejectedExecutionException success) { |
298 |
dl |
1.17 |
} catch (SecurityException ok) { |
299 |
jsr166 |
1.22 |
} |
300 |
dl |
1.4 |
joinPool(se); |
301 |
dl |
1.1 |
} |
302 |
jsr166 |
1.22 |
|
303 |
dl |
1.1 |
/** |
304 |
dl |
1.6 |
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
305 |
dl |
1.1 |
*/ |
306 |
jsr166 |
1.25 |
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
307 |
dl |
1.9 |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
308 |
dl |
1.5 |
try { |
309 |
dl |
1.1 |
se.shutdown(); |
310 |
dl |
1.4 |
se.scheduleWithFixedDelay(new NoOpRunnable(), |
311 |
jsr166 |
1.25 |
MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); |
312 |
dl |
1.5 |
shouldThrow(); |
313 |
jsr166 |
1.24 |
} catch (RejectedExecutionException success) { |
314 |
dl |
1.17 |
} catch (SecurityException ok) { |
315 |
jsr166 |
1.22 |
} |
316 |
dl |
1.4 |
joinPool(se); |
317 |
dl |
1.1 |
} |
318 |
|
|
|
319 |
|
|
/** |
320 |
jsr166 |
1.32 |
* getActiveCount increases but doesn't overestimate, when a |
321 |
|
|
* thread becomes active |
322 |
dl |
1.2 |
*/ |
323 |
jsr166 |
1.25 |
public void testGetActiveCount() throws InterruptedException { |
324 |
jsr166 |
1.33 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
325 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
326 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
327 |
|
|
try { |
328 |
|
|
assertEquals(0, p.getActiveCount()); |
329 |
|
|
p.execute(new CheckedRunnable() { |
330 |
|
|
public void realRun() throws InterruptedException { |
331 |
|
|
threadStarted.countDown(); |
332 |
|
|
assertEquals(1, p.getActiveCount()); |
333 |
|
|
done.await(); |
334 |
|
|
}}); |
335 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
336 |
|
|
assertEquals(1, p.getActiveCount()); |
337 |
|
|
} finally { |
338 |
|
|
done.countDown(); |
339 |
|
|
joinPool(p); |
340 |
|
|
} |
341 |
dl |
1.2 |
} |
342 |
jsr166 |
1.22 |
|
343 |
dl |
1.2 |
/** |
344 |
jsr166 |
1.32 |
* getCompletedTaskCount increases, but doesn't overestimate, |
345 |
|
|
* when tasks complete |
346 |
dl |
1.2 |
*/ |
347 |
jsr166 |
1.25 |
public void testGetCompletedTaskCount() throws InterruptedException { |
348 |
jsr166 |
1.33 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
349 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
350 |
|
|
final CountDownLatch threadProceed = new CountDownLatch(1); |
351 |
|
|
final CountDownLatch threadDone = new CountDownLatch(1); |
352 |
|
|
try { |
353 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
354 |
|
|
p.execute(new CheckedRunnable() { |
355 |
|
|
public void realRun() throws InterruptedException { |
356 |
|
|
threadStarted.countDown(); |
357 |
|
|
assertEquals(0, p.getCompletedTaskCount()); |
358 |
|
|
threadProceed.await(); |
359 |
|
|
threadDone.countDown(); |
360 |
|
|
}}); |
361 |
jsr166 |
1.45 |
await(threadStarted); |
362 |
jsr166 |
1.33 |
assertEquals(0, p.getCompletedTaskCount()); |
363 |
|
|
threadProceed.countDown(); |
364 |
|
|
threadDone.await(); |
365 |
jsr166 |
1.44 |
long startTime = System.nanoTime(); |
366 |
|
|
while (p.getCompletedTaskCount() != 1) { |
367 |
|
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
368 |
|
|
fail("timed out"); |
369 |
|
|
Thread.yield(); |
370 |
|
|
} |
371 |
jsr166 |
1.33 |
} finally { |
372 |
|
|
joinPool(p); |
373 |
|
|
} |
374 |
dl |
1.2 |
} |
375 |
jsr166 |
1.22 |
|
376 |
dl |
1.2 |
/** |
377 |
jsr166 |
1.32 |
* getCorePoolSize returns size given in constructor if not otherwise set |
378 |
dl |
1.2 |
*/ |
379 |
jsr166 |
1.25 |
public void testGetCorePoolSize() throws InterruptedException { |
380 |
jsr166 |
1.33 |
ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
381 |
|
|
assertEquals(1, p.getCorePoolSize()); |
382 |
|
|
joinPool(p); |
383 |
dl |
1.2 |
} |
384 |
jsr166 |
1.22 |
|
385 |
dl |
1.2 |
/** |
386 |
jsr166 |
1.32 |
* getLargestPoolSize increases, but doesn't overestimate, when |
387 |
|
|
* multiple threads active |
388 |
dl |
1.2 |
*/ |
389 |
jsr166 |
1.25 |
public void testGetLargestPoolSize() throws InterruptedException { |
390 |
jsr166 |
1.33 |
final int THREADS = 3; |
391 |
|
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
392 |
|
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
393 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
394 |
|
|
try { |
395 |
|
|
assertEquals(0, p.getLargestPoolSize()); |
396 |
|
|
for (int i = 0; i < THREADS; i++) |
397 |
|
|
p.execute(new CheckedRunnable() { |
398 |
|
|
public void realRun() throws InterruptedException { |
399 |
|
|
threadsStarted.countDown(); |
400 |
|
|
done.await(); |
401 |
|
|
assertEquals(THREADS, p.getLargestPoolSize()); |
402 |
|
|
}}); |
403 |
|
|
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
404 |
|
|
assertEquals(THREADS, p.getLargestPoolSize()); |
405 |
|
|
} finally { |
406 |
|
|
done.countDown(); |
407 |
|
|
joinPool(p); |
408 |
|
|
assertEquals(THREADS, p.getLargestPoolSize()); |
409 |
|
|
} |
410 |
dl |
1.2 |
} |
411 |
jsr166 |
1.22 |
|
412 |
dl |
1.2 |
/** |
413 |
jsr166 |
1.32 |
* getPoolSize increases, but doesn't overestimate, when threads |
414 |
|
|
* become active |
415 |
dl |
1.2 |
*/ |
416 |
jsr166 |
1.25 |
public void testGetPoolSize() throws InterruptedException { |
417 |
jsr166 |
1.33 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
418 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
419 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
420 |
|
|
try { |
421 |
|
|
assertEquals(0, p.getPoolSize()); |
422 |
|
|
p.execute(new CheckedRunnable() { |
423 |
|
|
public void realRun() throws InterruptedException { |
424 |
|
|
threadStarted.countDown(); |
425 |
|
|
assertEquals(1, p.getPoolSize()); |
426 |
|
|
done.await(); |
427 |
|
|
}}); |
428 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
429 |
|
|
assertEquals(1, p.getPoolSize()); |
430 |
|
|
} finally { |
431 |
|
|
done.countDown(); |
432 |
|
|
joinPool(p); |
433 |
|
|
} |
434 |
dl |
1.2 |
} |
435 |
jsr166 |
1.22 |
|
436 |
dl |
1.2 |
/** |
437 |
jsr166 |
1.32 |
* getTaskCount increases, but doesn't overestimate, when tasks |
438 |
|
|
* submitted |
439 |
dl |
1.2 |
*/ |
440 |
jsr166 |
1.25 |
public void testGetTaskCount() throws InterruptedException { |
441 |
jsr166 |
1.33 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
442 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
443 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
444 |
|
|
final int TASKS = 5; |
445 |
|
|
try { |
446 |
|
|
assertEquals(0, p.getTaskCount()); |
447 |
|
|
for (int i = 0; i < TASKS; i++) |
448 |
|
|
p.execute(new CheckedRunnable() { |
449 |
|
|
public void realRun() throws InterruptedException { |
450 |
|
|
threadStarted.countDown(); |
451 |
|
|
done.await(); |
452 |
|
|
}}); |
453 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
454 |
|
|
assertEquals(TASKS, p.getTaskCount()); |
455 |
|
|
} finally { |
456 |
|
|
done.countDown(); |
457 |
|
|
joinPool(p); |
458 |
|
|
} |
459 |
dl |
1.2 |
} |
460 |
dl |
1.8 |
|
461 |
jsr166 |
1.22 |
/** |
462 |
dl |
1.8 |
* getThreadFactory returns factory in constructor if not set |
463 |
|
|
*/ |
464 |
jsr166 |
1.25 |
public void testGetThreadFactory() throws InterruptedException { |
465 |
dl |
1.8 |
ThreadFactory tf = new SimpleThreadFactory(); |
466 |
jsr166 |
1.26 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1, tf); |
467 |
dl |
1.8 |
assertSame(tf, p.getThreadFactory()); |
468 |
|
|
joinPool(p); |
469 |
|
|
} |
470 |
|
|
|
471 |
jsr166 |
1.22 |
/** |
472 |
dl |
1.8 |
* setThreadFactory sets the thread factory returned by getThreadFactory |
473 |
|
|
*/ |
474 |
jsr166 |
1.25 |
public void testSetThreadFactory() throws InterruptedException { |
475 |
dl |
1.8 |
ThreadFactory tf = new SimpleThreadFactory(); |
476 |
jsr166 |
1.26 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
477 |
dl |
1.8 |
p.setThreadFactory(tf); |
478 |
|
|
assertSame(tf, p.getThreadFactory()); |
479 |
|
|
joinPool(p); |
480 |
|
|
} |
481 |
|
|
|
482 |
jsr166 |
1.22 |
/** |
483 |
dl |
1.8 |
* setThreadFactory(null) throws NPE |
484 |
|
|
*/ |
485 |
jsr166 |
1.25 |
public void testSetThreadFactoryNull() throws InterruptedException { |
486 |
jsr166 |
1.26 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
487 |
dl |
1.8 |
try { |
488 |
|
|
p.setThreadFactory(null); |
489 |
|
|
shouldThrow(); |
490 |
|
|
} catch (NullPointerException success) { |
491 |
|
|
} finally { |
492 |
|
|
joinPool(p); |
493 |
|
|
} |
494 |
|
|
} |
495 |
jsr166 |
1.22 |
|
496 |
dl |
1.2 |
/** |
497 |
jsr166 |
1.41 |
* isShutdown is false before shutdown, true after |
498 |
dl |
1.2 |
*/ |
499 |
dl |
1.5 |
public void testIsShutdown() { |
500 |
jsr166 |
1.22 |
|
501 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
502 |
dl |
1.2 |
try { |
503 |
jsr166 |
1.33 |
assertFalse(p.isShutdown()); |
504 |
dl |
1.2 |
} |
505 |
|
|
finally { |
506 |
jsr166 |
1.33 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
507 |
dl |
1.2 |
} |
508 |
jsr166 |
1.33 |
assertTrue(p.isShutdown()); |
509 |
dl |
1.2 |
} |
510 |
|
|
|
511 |
|
|
/** |
512 |
jsr166 |
1.32 |
* isTerminated is false before termination, true after |
513 |
dl |
1.2 |
*/ |
514 |
jsr166 |
1.25 |
public void testIsTerminated() throws InterruptedException { |
515 |
jsr166 |
1.33 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
516 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
517 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
518 |
|
|
assertFalse(p.isTerminated()); |
519 |
|
|
try { |
520 |
|
|
p.execute(new CheckedRunnable() { |
521 |
|
|
public void realRun() throws InterruptedException { |
522 |
jsr166 |
1.36 |
assertFalse(p.isTerminated()); |
523 |
jsr166 |
1.33 |
threadStarted.countDown(); |
524 |
|
|
done.await(); |
525 |
|
|
}}); |
526 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
527 |
jsr166 |
1.36 |
assertFalse(p.isTerminating()); |
528 |
jsr166 |
1.33 |
done.countDown(); |
529 |
dl |
1.2 |
} finally { |
530 |
jsr166 |
1.33 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
531 |
dl |
1.2 |
} |
532 |
jsr166 |
1.33 |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
533 |
|
|
assertTrue(p.isTerminated()); |
534 |
dl |
1.5 |
} |
535 |
|
|
|
536 |
|
|
/** |
537 |
jsr166 |
1.32 |
* isTerminating is not true when running or when terminated |
538 |
dl |
1.5 |
*/ |
539 |
jsr166 |
1.25 |
public void testIsTerminating() throws InterruptedException { |
540 |
jsr166 |
1.33 |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
541 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
542 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
543 |
|
|
try { |
544 |
|
|
assertFalse(p.isTerminating()); |
545 |
|
|
p.execute(new CheckedRunnable() { |
546 |
|
|
public void realRun() throws InterruptedException { |
547 |
jsr166 |
1.35 |
assertFalse(p.isTerminating()); |
548 |
jsr166 |
1.33 |
threadStarted.countDown(); |
549 |
|
|
done.await(); |
550 |
|
|
}}); |
551 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
552 |
|
|
assertFalse(p.isTerminating()); |
553 |
|
|
done.countDown(); |
554 |
|
|
} finally { |
555 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
556 |
|
|
} |
557 |
|
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
558 |
|
|
assertTrue(p.isTerminated()); |
559 |
|
|
assertFalse(p.isTerminating()); |
560 |
dl |
1.2 |
} |
561 |
|
|
|
562 |
|
|
/** |
563 |
dl |
1.8 |
* getQueue returns the work queue, which contains queued tasks |
564 |
|
|
*/ |
565 |
jsr166 |
1.25 |
public void testGetQueue() throws InterruptedException { |
566 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
567 |
|
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
568 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
569 |
dl |
1.8 |
try { |
570 |
jsr166 |
1.33 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
571 |
|
|
for (int i = 0; i < tasks.length; i++) { |
572 |
|
|
Runnable r = new CheckedRunnable() { |
573 |
|
|
public void realRun() throws InterruptedException { |
574 |
|
|
threadStarted.countDown(); |
575 |
|
|
done.await(); |
576 |
|
|
}}; |
577 |
|
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
578 |
|
|
} |
579 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
580 |
|
|
BlockingQueue<Runnable> q = p.getQueue(); |
581 |
|
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
582 |
dl |
1.8 |
assertFalse(q.contains(tasks[0])); |
583 |
|
|
} finally { |
584 |
jsr166 |
1.33 |
done.countDown(); |
585 |
|
|
joinPool(p); |
586 |
dl |
1.8 |
} |
587 |
|
|
} |
588 |
|
|
|
589 |
|
|
/** |
590 |
|
|
* remove(task) removes queued task, and fails to remove active task |
591 |
|
|
*/ |
592 |
jsr166 |
1.25 |
public void testRemove() throws InterruptedException { |
593 |
jsr166 |
1.33 |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
594 |
dl |
1.9 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
595 |
jsr166 |
1.33 |
final CountDownLatch threadStarted = new CountDownLatch(1); |
596 |
|
|
final CountDownLatch done = new CountDownLatch(1); |
597 |
dl |
1.8 |
try { |
598 |
jsr166 |
1.33 |
for (int i = 0; i < tasks.length; i++) { |
599 |
|
|
Runnable r = new CheckedRunnable() { |
600 |
|
|
public void realRun() throws InterruptedException { |
601 |
|
|
threadStarted.countDown(); |
602 |
|
|
done.await(); |
603 |
|
|
}}; |
604 |
|
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
605 |
|
|
} |
606 |
|
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
607 |
|
|
BlockingQueue<Runnable> q = p.getQueue(); |
608 |
|
|
assertFalse(p.remove((Runnable)tasks[0])); |
609 |
dl |
1.8 |
assertTrue(q.contains((Runnable)tasks[4])); |
610 |
|
|
assertTrue(q.contains((Runnable)tasks[3])); |
611 |
jsr166 |
1.33 |
assertTrue(p.remove((Runnable)tasks[4])); |
612 |
|
|
assertFalse(p.remove((Runnable)tasks[4])); |
613 |
dl |
1.8 |
assertFalse(q.contains((Runnable)tasks[4])); |
614 |
|
|
assertTrue(q.contains((Runnable)tasks[3])); |
615 |
jsr166 |
1.33 |
assertTrue(p.remove((Runnable)tasks[3])); |
616 |
dl |
1.8 |
assertFalse(q.contains((Runnable)tasks[3])); |
617 |
|
|
} finally { |
618 |
jsr166 |
1.33 |
done.countDown(); |
619 |
|
|
joinPool(p); |
620 |
dl |
1.8 |
} |
621 |
|
|
} |
622 |
|
|
|
623 |
|
|
/** |
624 |
jsr166 |
1.40 |
* purge eventually removes cancelled tasks from the queue |
625 |
dl |
1.2 |
*/ |
626 |
jsr166 |
1.25 |
public void testPurge() throws InterruptedException { |
627 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
628 |
dl |
1.9 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
629 |
jsr166 |
1.40 |
for (int i = 0; i < tasks.length; i++) |
630 |
|
|
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
631 |
|
|
LONG_DELAY_MS, MILLISECONDS); |
632 |
dl |
1.19 |
try { |
633 |
jsr166 |
1.33 |
int max = tasks.length; |
634 |
dl |
1.19 |
if (tasks[4].cancel(true)) --max; |
635 |
|
|
if (tasks[3].cancel(true)) --max; |
636 |
|
|
// There must eventually be an interference-free point at |
637 |
|
|
// which purge will not fail. (At worst, when queue is empty.) |
638 |
jsr166 |
1.40 |
long startTime = System.nanoTime(); |
639 |
|
|
do { |
640 |
jsr166 |
1.33 |
p.purge(); |
641 |
|
|
long count = p.getTaskCount(); |
642 |
jsr166 |
1.40 |
if (count == max) |
643 |
|
|
return; |
644 |
|
|
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
645 |
|
|
fail("Purge failed to remove cancelled tasks"); |
646 |
dl |
1.19 |
} finally { |
647 |
jsr166 |
1.33 |
for (ScheduledFuture task : tasks) |
648 |
|
|
task.cancel(true); |
649 |
|
|
joinPool(p); |
650 |
dl |
1.2 |
} |
651 |
|
|
} |
652 |
|
|
|
653 |
|
|
/** |
654 |
jsr166 |
1.41 |
* shutdownNow returns a list containing tasks that were not run |
655 |
dl |
1.2 |
*/ |
656 |
jsr166 |
1.40 |
public void testShutdownNow() { |
657 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
658 |
jsr166 |
1.23 |
for (int i = 0; i < 5; i++) |
659 |
jsr166 |
1.40 |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
660 |
|
|
LONG_DELAY_MS, MILLISECONDS); |
661 |
dl |
1.17 |
try { |
662 |
jsr166 |
1.40 |
List<Runnable> l = p.shutdownNow(); |
663 |
|
|
assertTrue(p.isShutdown()); |
664 |
|
|
assertEquals(5, l.size()); |
665 |
jsr166 |
1.22 |
} catch (SecurityException ok) { |
666 |
jsr166 |
1.40 |
// Allowed in case test doesn't have privs |
667 |
|
|
} finally { |
668 |
|
|
joinPool(p); |
669 |
dl |
1.17 |
} |
670 |
dl |
1.2 |
} |
671 |
|
|
|
672 |
dl |
1.5 |
/** |
673 |
dl |
1.6 |
* In default setting, shutdown cancels periodic but not delayed |
674 |
|
|
* tasks at shutdown |
675 |
dl |
1.5 |
*/ |
676 |
jsr166 |
1.41 |
public void testShutdown1() throws InterruptedException { |
677 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
678 |
|
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
679 |
|
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
680 |
jsr166 |
1.22 |
|
681 |
jsr166 |
1.25 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
682 |
jsr166 |
1.33 |
for (int i = 0; i < tasks.length; i++) |
683 |
|
|
tasks[i] = p.schedule(new NoOpRunnable(), |
684 |
|
|
SHORT_DELAY_MS, MILLISECONDS); |
685 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
686 |
|
|
BlockingQueue<Runnable> q = p.getQueue(); |
687 |
|
|
for (ScheduledFuture task : tasks) { |
688 |
|
|
assertFalse(task.isDone()); |
689 |
|
|
assertFalse(task.isCancelled()); |
690 |
|
|
assertTrue(q.contains(task)); |
691 |
jsr166 |
1.25 |
} |
692 |
jsr166 |
1.33 |
assertTrue(p.isShutdown()); |
693 |
|
|
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
694 |
|
|
assertTrue(p.isTerminated()); |
695 |
|
|
for (ScheduledFuture task : tasks) { |
696 |
|
|
assertTrue(task.isDone()); |
697 |
|
|
assertFalse(task.isCancelled()); |
698 |
dl |
1.2 |
} |
699 |
|
|
} |
700 |
|
|
|
701 |
dl |
1.5 |
/** |
702 |
dl |
1.6 |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
703 |
|
|
* delayed tasks are cancelled at shutdown |
704 |
dl |
1.5 |
*/ |
705 |
jsr166 |
1.41 |
public void testShutdown2() throws InterruptedException { |
706 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
707 |
|
|
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
708 |
jsr166 |
1.34 |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
709 |
|
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
710 |
jsr166 |
1.25 |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
711 |
jsr166 |
1.33 |
for (int i = 0; i < tasks.length; i++) |
712 |
|
|
tasks[i] = p.schedule(new NoOpRunnable(), |
713 |
|
|
SHORT_DELAY_MS, MILLISECONDS); |
714 |
|
|
BlockingQueue q = p.getQueue(); |
715 |
|
|
assertEquals(tasks.length, q.size()); |
716 |
|
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
717 |
|
|
assertTrue(p.isShutdown()); |
718 |
jsr166 |
1.25 |
assertTrue(q.isEmpty()); |
719 |
jsr166 |
1.33 |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
720 |
|
|
assertTrue(p.isTerminated()); |
721 |
|
|
for (ScheduledFuture task : tasks) { |
722 |
|
|
assertTrue(task.isDone()); |
723 |
|
|
assertTrue(task.isCancelled()); |
724 |
|
|
} |
725 |
dl |
1.2 |
} |
726 |
|
|
|
727 |
dl |
1.5 |
/** |
728 |
dl |
1.6 |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
729 |
jsr166 |
1.33 |
* periodic tasks are cancelled at shutdown |
730 |
dl |
1.5 |
*/ |
731 |
jsr166 |
1.41 |
public void testShutdown3() throws InterruptedException { |
732 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
733 |
jsr166 |
1.34 |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
734 |
|
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
735 |
jsr166 |
1.33 |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
736 |
jsr166 |
1.34 |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
737 |
|
|
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
738 |
jsr166 |
1.42 |
long initialDelay = LONG_DELAY_MS; |
739 |
jsr166 |
1.25 |
ScheduledFuture task = |
740 |
jsr166 |
1.42 |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
741 |
|
|
5, MILLISECONDS); |
742 |
jsr166 |
1.33 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
743 |
|
|
assertTrue(p.isShutdown()); |
744 |
|
|
assertTrue(p.getQueue().isEmpty()); |
745 |
|
|
assertTrue(task.isDone()); |
746 |
|
|
assertTrue(task.isCancelled()); |
747 |
jsr166 |
1.42 |
joinPool(p); |
748 |
dl |
1.2 |
} |
749 |
|
|
|
750 |
dl |
1.5 |
/** |
751 |
dl |
1.6 |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
752 |
jsr166 |
1.33 |
* periodic tasks are not cancelled at shutdown |
753 |
dl |
1.5 |
*/ |
754 |
jsr166 |
1.41 |
public void testShutdown4() throws InterruptedException { |
755 |
jsr166 |
1.33 |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
756 |
|
|
final CountDownLatch counter = new CountDownLatch(2); |
757 |
dl |
1.2 |
try { |
758 |
jsr166 |
1.34 |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
759 |
|
|
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
760 |
|
|
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
761 |
jsr166 |
1.33 |
final Runnable r = new CheckedRunnable() { |
762 |
|
|
public void realRun() { |
763 |
|
|
counter.countDown(); |
764 |
|
|
}}; |
765 |
dl |
1.9 |
ScheduledFuture task = |
766 |
jsr166 |
1.33 |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
767 |
|
|
assertFalse(task.isDone()); |
768 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
769 |
jsr166 |
1.33 |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
770 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
771 |
jsr166 |
1.33 |
assertFalse(p.isTerminated()); |
772 |
|
|
assertTrue(p.isShutdown()); |
773 |
|
|
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
774 |
dl |
1.2 |
assertFalse(task.isCancelled()); |
775 |
jsr166 |
1.33 |
assertTrue(task.cancel(false)); |
776 |
dl |
1.15 |
assertTrue(task.isDone()); |
777 |
jsr166 |
1.33 |
assertTrue(task.isCancelled()); |
778 |
|
|
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
779 |
|
|
assertTrue(p.isTerminated()); |
780 |
dl |
1.2 |
} |
781 |
jsr166 |
1.22 |
finally { |
782 |
jsr166 |
1.33 |
joinPool(p); |
783 |
dl |
1.2 |
} |
784 |
dl |
1.1 |
} |
785 |
|
|
|
786 |
dl |
1.10 |
/** |
787 |
|
|
* completed submit of callable returns result |
788 |
|
|
*/ |
789 |
jsr166 |
1.25 |
public void testSubmitCallable() throws Exception { |
790 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
791 |
|
|
try { |
792 |
|
|
Future<String> future = e.submit(new StringTask()); |
793 |
|
|
String result = future.get(); |
794 |
|
|
assertSame(TEST_STRING, result); |
795 |
|
|
} finally { |
796 |
|
|
joinPool(e); |
797 |
|
|
} |
798 |
|
|
} |
799 |
|
|
|
800 |
|
|
/** |
801 |
|
|
* completed submit of runnable returns successfully |
802 |
|
|
*/ |
803 |
jsr166 |
1.25 |
public void testSubmitRunnable() throws Exception { |
804 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
805 |
|
|
try { |
806 |
|
|
Future<?> future = e.submit(new NoOpRunnable()); |
807 |
|
|
future.get(); |
808 |
|
|
assertTrue(future.isDone()); |
809 |
|
|
} finally { |
810 |
|
|
joinPool(e); |
811 |
|
|
} |
812 |
|
|
} |
813 |
|
|
|
814 |
|
|
/** |
815 |
|
|
* completed submit of (runnable, result) returns result |
816 |
|
|
*/ |
817 |
jsr166 |
1.25 |
public void testSubmitRunnable2() throws Exception { |
818 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
819 |
|
|
try { |
820 |
|
|
Future<String> future = e.submit(new NoOpRunnable(), TEST_STRING); |
821 |
|
|
String result = future.get(); |
822 |
|
|
assertSame(TEST_STRING, result); |
823 |
|
|
} finally { |
824 |
|
|
joinPool(e); |
825 |
|
|
} |
826 |
|
|
} |
827 |
|
|
|
828 |
|
|
/** |
829 |
|
|
* invokeAny(null) throws NPE |
830 |
|
|
*/ |
831 |
jsr166 |
1.25 |
public void testInvokeAny1() throws Exception { |
832 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
833 |
|
|
try { |
834 |
|
|
e.invokeAny(null); |
835 |
jsr166 |
1.25 |
shouldThrow(); |
836 |
dl |
1.10 |
} catch (NullPointerException success) { |
837 |
|
|
} finally { |
838 |
|
|
joinPool(e); |
839 |
|
|
} |
840 |
|
|
} |
841 |
|
|
|
842 |
|
|
/** |
843 |
|
|
* invokeAny(empty collection) throws IAE |
844 |
|
|
*/ |
845 |
jsr166 |
1.25 |
public void testInvokeAny2() throws Exception { |
846 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
847 |
|
|
try { |
848 |
|
|
e.invokeAny(new ArrayList<Callable<String>>()); |
849 |
jsr166 |
1.25 |
shouldThrow(); |
850 |
dl |
1.10 |
} catch (IllegalArgumentException success) { |
851 |
|
|
} finally { |
852 |
|
|
joinPool(e); |
853 |
|
|
} |
854 |
|
|
} |
855 |
|
|
|
856 |
|
|
/** |
857 |
|
|
* invokeAny(c) throws NPE if c has null elements |
858 |
|
|
*/ |
859 |
jsr166 |
1.25 |
public void testInvokeAny3() throws Exception { |
860 |
jsr166 |
1.29 |
CountDownLatch latch = new CountDownLatch(1); |
861 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
862 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
863 |
|
|
l.add(latchAwaitingStringTask(latch)); |
864 |
|
|
l.add(null); |
865 |
dl |
1.10 |
try { |
866 |
|
|
e.invokeAny(l); |
867 |
jsr166 |
1.25 |
shouldThrow(); |
868 |
dl |
1.10 |
} catch (NullPointerException success) { |
869 |
|
|
} finally { |
870 |
jsr166 |
1.25 |
latch.countDown(); |
871 |
dl |
1.10 |
joinPool(e); |
872 |
|
|
} |
873 |
|
|
} |
874 |
|
|
|
875 |
|
|
/** |
876 |
|
|
* invokeAny(c) throws ExecutionException if no task completes |
877 |
|
|
*/ |
878 |
jsr166 |
1.25 |
public void testInvokeAny4() throws Exception { |
879 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
880 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
881 |
|
|
l.add(new NPETask()); |
882 |
dl |
1.10 |
try { |
883 |
|
|
e.invokeAny(l); |
884 |
jsr166 |
1.25 |
shouldThrow(); |
885 |
dl |
1.10 |
} catch (ExecutionException success) { |
886 |
jsr166 |
1.25 |
assertTrue(success.getCause() instanceof NullPointerException); |
887 |
dl |
1.10 |
} finally { |
888 |
|
|
joinPool(e); |
889 |
|
|
} |
890 |
|
|
} |
891 |
|
|
|
892 |
|
|
/** |
893 |
|
|
* invokeAny(c) returns result of some task |
894 |
|
|
*/ |
895 |
jsr166 |
1.25 |
public void testInvokeAny5() throws Exception { |
896 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
897 |
|
|
try { |
898 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
899 |
dl |
1.10 |
l.add(new StringTask()); |
900 |
|
|
l.add(new StringTask()); |
901 |
|
|
String result = e.invokeAny(l); |
902 |
|
|
assertSame(TEST_STRING, result); |
903 |
|
|
} finally { |
904 |
|
|
joinPool(e); |
905 |
|
|
} |
906 |
|
|
} |
907 |
|
|
|
908 |
|
|
/** |
909 |
|
|
* invokeAll(null) throws NPE |
910 |
|
|
*/ |
911 |
jsr166 |
1.25 |
public void testInvokeAll1() throws Exception { |
912 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
913 |
|
|
try { |
914 |
|
|
e.invokeAll(null); |
915 |
jsr166 |
1.25 |
shouldThrow(); |
916 |
dl |
1.10 |
} catch (NullPointerException success) { |
917 |
|
|
} finally { |
918 |
|
|
joinPool(e); |
919 |
|
|
} |
920 |
|
|
} |
921 |
|
|
|
922 |
|
|
/** |
923 |
|
|
* invokeAll(empty collection) returns empty collection |
924 |
|
|
*/ |
925 |
jsr166 |
1.25 |
public void testInvokeAll2() throws Exception { |
926 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
927 |
|
|
try { |
928 |
|
|
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>()); |
929 |
|
|
assertTrue(r.isEmpty()); |
930 |
|
|
} finally { |
931 |
|
|
joinPool(e); |
932 |
|
|
} |
933 |
|
|
} |
934 |
|
|
|
935 |
|
|
/** |
936 |
|
|
* invokeAll(c) throws NPE if c has null elements |
937 |
|
|
*/ |
938 |
jsr166 |
1.25 |
public void testInvokeAll3() throws Exception { |
939 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
940 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
941 |
|
|
l.add(new StringTask()); |
942 |
|
|
l.add(null); |
943 |
dl |
1.10 |
try { |
944 |
|
|
e.invokeAll(l); |
945 |
jsr166 |
1.25 |
shouldThrow(); |
946 |
dl |
1.10 |
} catch (NullPointerException success) { |
947 |
|
|
} finally { |
948 |
|
|
joinPool(e); |
949 |
|
|
} |
950 |
|
|
} |
951 |
|
|
|
952 |
|
|
/** |
953 |
|
|
* get of invokeAll(c) throws exception on failed task |
954 |
|
|
*/ |
955 |
jsr166 |
1.25 |
public void testInvokeAll4() throws Exception { |
956 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
957 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
958 |
|
|
l.add(new NPETask()); |
959 |
|
|
List<Future<String>> futures = e.invokeAll(l); |
960 |
|
|
assertEquals(1, futures.size()); |
961 |
dl |
1.10 |
try { |
962 |
jsr166 |
1.29 |
futures.get(0).get(); |
963 |
jsr166 |
1.25 |
shouldThrow(); |
964 |
jsr166 |
1.23 |
} catch (ExecutionException success) { |
965 |
jsr166 |
1.25 |
assertTrue(success.getCause() instanceof NullPointerException); |
966 |
dl |
1.10 |
} finally { |
967 |
|
|
joinPool(e); |
968 |
|
|
} |
969 |
|
|
} |
970 |
|
|
|
971 |
|
|
/** |
972 |
|
|
* invokeAll(c) returns results of all completed tasks |
973 |
|
|
*/ |
974 |
jsr166 |
1.25 |
public void testInvokeAll5() throws Exception { |
975 |
dl |
1.10 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
976 |
|
|
try { |
977 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
978 |
dl |
1.10 |
l.add(new StringTask()); |
979 |
|
|
l.add(new StringTask()); |
980 |
jsr166 |
1.29 |
List<Future<String>> futures = e.invokeAll(l); |
981 |
|
|
assertEquals(2, futures.size()); |
982 |
|
|
for (Future<String> future : futures) |
983 |
jsr166 |
1.25 |
assertSame(TEST_STRING, future.get()); |
984 |
dl |
1.10 |
} finally { |
985 |
|
|
joinPool(e); |
986 |
|
|
} |
987 |
|
|
} |
988 |
|
|
|
989 |
dl |
1.11 |
/** |
990 |
|
|
* timed invokeAny(null) throws NPE |
991 |
|
|
*/ |
992 |
jsr166 |
1.25 |
public void testTimedInvokeAny1() throws Exception { |
993 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
994 |
|
|
try { |
995 |
jsr166 |
1.25 |
e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); |
996 |
|
|
shouldThrow(); |
997 |
dl |
1.11 |
} catch (NullPointerException success) { |
998 |
|
|
} finally { |
999 |
|
|
joinPool(e); |
1000 |
|
|
} |
1001 |
|
|
} |
1002 |
|
|
|
1003 |
|
|
/** |
1004 |
|
|
* timed invokeAny(,,null) throws NPE |
1005 |
|
|
*/ |
1006 |
jsr166 |
1.25 |
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1007 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1008 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1009 |
|
|
l.add(new StringTask()); |
1010 |
dl |
1.11 |
try { |
1011 |
|
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1012 |
jsr166 |
1.25 |
shouldThrow(); |
1013 |
dl |
1.11 |
} catch (NullPointerException success) { |
1014 |
|
|
} finally { |
1015 |
|
|
joinPool(e); |
1016 |
|
|
} |
1017 |
|
|
} |
1018 |
|
|
|
1019 |
|
|
/** |
1020 |
|
|
* timed invokeAny(empty collection) throws IAE |
1021 |
|
|
*/ |
1022 |
jsr166 |
1.25 |
public void testTimedInvokeAny2() throws Exception { |
1023 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1024 |
|
|
try { |
1025 |
jsr166 |
1.25 |
e.invokeAny(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1026 |
|
|
shouldThrow(); |
1027 |
dl |
1.11 |
} catch (IllegalArgumentException success) { |
1028 |
|
|
} finally { |
1029 |
|
|
joinPool(e); |
1030 |
|
|
} |
1031 |
|
|
} |
1032 |
|
|
|
1033 |
|
|
/** |
1034 |
|
|
* timed invokeAny(c) throws NPE if c has null elements |
1035 |
|
|
*/ |
1036 |
jsr166 |
1.25 |
public void testTimedInvokeAny3() throws Exception { |
1037 |
jsr166 |
1.29 |
CountDownLatch latch = new CountDownLatch(1); |
1038 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1039 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1040 |
|
|
l.add(latchAwaitingStringTask(latch)); |
1041 |
|
|
l.add(null); |
1042 |
dl |
1.11 |
try { |
1043 |
jsr166 |
1.25 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1044 |
|
|
shouldThrow(); |
1045 |
dl |
1.11 |
} catch (NullPointerException success) { |
1046 |
|
|
} finally { |
1047 |
jsr166 |
1.25 |
latch.countDown(); |
1048 |
dl |
1.11 |
joinPool(e); |
1049 |
|
|
} |
1050 |
|
|
} |
1051 |
|
|
|
1052 |
|
|
/** |
1053 |
|
|
* timed invokeAny(c) throws ExecutionException if no task completes |
1054 |
|
|
*/ |
1055 |
jsr166 |
1.25 |
public void testTimedInvokeAny4() throws Exception { |
1056 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1057 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1058 |
|
|
l.add(new NPETask()); |
1059 |
dl |
1.11 |
try { |
1060 |
jsr166 |
1.25 |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1061 |
|
|
shouldThrow(); |
1062 |
jsr166 |
1.23 |
} catch (ExecutionException success) { |
1063 |
jsr166 |
1.25 |
assertTrue(success.getCause() instanceof NullPointerException); |
1064 |
dl |
1.11 |
} finally { |
1065 |
|
|
joinPool(e); |
1066 |
|
|
} |
1067 |
|
|
} |
1068 |
|
|
|
1069 |
|
|
/** |
1070 |
|
|
* timed invokeAny(c) returns result of some task |
1071 |
|
|
*/ |
1072 |
jsr166 |
1.25 |
public void testTimedInvokeAny5() throws Exception { |
1073 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1074 |
|
|
try { |
1075 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1076 |
dl |
1.11 |
l.add(new StringTask()); |
1077 |
|
|
l.add(new StringTask()); |
1078 |
jsr166 |
1.25 |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1079 |
dl |
1.11 |
assertSame(TEST_STRING, result); |
1080 |
|
|
} finally { |
1081 |
|
|
joinPool(e); |
1082 |
|
|
} |
1083 |
|
|
} |
1084 |
|
|
|
1085 |
|
|
/** |
1086 |
|
|
* timed invokeAll(null) throws NPE |
1087 |
|
|
*/ |
1088 |
jsr166 |
1.25 |
public void testTimedInvokeAll1() throws Exception { |
1089 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1090 |
|
|
try { |
1091 |
jsr166 |
1.25 |
e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); |
1092 |
|
|
shouldThrow(); |
1093 |
dl |
1.11 |
} catch (NullPointerException success) { |
1094 |
|
|
} finally { |
1095 |
|
|
joinPool(e); |
1096 |
|
|
} |
1097 |
|
|
} |
1098 |
|
|
|
1099 |
|
|
/** |
1100 |
|
|
* timed invokeAll(,,null) throws NPE |
1101 |
|
|
*/ |
1102 |
jsr166 |
1.25 |
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1103 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1104 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1105 |
|
|
l.add(new StringTask()); |
1106 |
dl |
1.11 |
try { |
1107 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1108 |
jsr166 |
1.25 |
shouldThrow(); |
1109 |
dl |
1.11 |
} catch (NullPointerException success) { |
1110 |
|
|
} finally { |
1111 |
|
|
joinPool(e); |
1112 |
|
|
} |
1113 |
|
|
} |
1114 |
|
|
|
1115 |
|
|
/** |
1116 |
|
|
* timed invokeAll(empty collection) returns empty collection |
1117 |
|
|
*/ |
1118 |
jsr166 |
1.25 |
public void testTimedInvokeAll2() throws Exception { |
1119 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1120 |
|
|
try { |
1121 |
jsr166 |
1.25 |
List<Future<String>> r = e.invokeAll(new ArrayList<Callable<String>>(), MEDIUM_DELAY_MS, MILLISECONDS); |
1122 |
dl |
1.11 |
assertTrue(r.isEmpty()); |
1123 |
|
|
} finally { |
1124 |
|
|
joinPool(e); |
1125 |
|
|
} |
1126 |
|
|
} |
1127 |
|
|
|
1128 |
|
|
/** |
1129 |
|
|
* timed invokeAll(c) throws NPE if c has null elements |
1130 |
|
|
*/ |
1131 |
jsr166 |
1.25 |
public void testTimedInvokeAll3() throws Exception { |
1132 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1133 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1134 |
|
|
l.add(new StringTask()); |
1135 |
|
|
l.add(null); |
1136 |
dl |
1.11 |
try { |
1137 |
jsr166 |
1.25 |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1138 |
|
|
shouldThrow(); |
1139 |
dl |
1.11 |
} catch (NullPointerException success) { |
1140 |
|
|
} finally { |
1141 |
|
|
joinPool(e); |
1142 |
|
|
} |
1143 |
|
|
} |
1144 |
|
|
|
1145 |
|
|
/** |
1146 |
|
|
* get of element of invokeAll(c) throws exception on failed task |
1147 |
|
|
*/ |
1148 |
jsr166 |
1.25 |
public void testTimedInvokeAll4() throws Exception { |
1149 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1150 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1151 |
|
|
l.add(new NPETask()); |
1152 |
|
|
List<Future<String>> futures = |
1153 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1154 |
|
|
assertEquals(1, futures.size()); |
1155 |
dl |
1.11 |
try { |
1156 |
jsr166 |
1.29 |
futures.get(0).get(); |
1157 |
jsr166 |
1.25 |
shouldThrow(); |
1158 |
jsr166 |
1.23 |
} catch (ExecutionException success) { |
1159 |
jsr166 |
1.25 |
assertTrue(success.getCause() instanceof NullPointerException); |
1160 |
dl |
1.11 |
} finally { |
1161 |
|
|
joinPool(e); |
1162 |
|
|
} |
1163 |
|
|
} |
1164 |
|
|
|
1165 |
|
|
/** |
1166 |
|
|
* timed invokeAll(c) returns results of all completed tasks |
1167 |
|
|
*/ |
1168 |
jsr166 |
1.25 |
public void testTimedInvokeAll5() throws Exception { |
1169 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1170 |
|
|
try { |
1171 |
jsr166 |
1.29 |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1172 |
dl |
1.11 |
l.add(new StringTask()); |
1173 |
|
|
l.add(new StringTask()); |
1174 |
jsr166 |
1.29 |
List<Future<String>> futures = |
1175 |
|
|
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1176 |
|
|
assertEquals(2, futures.size()); |
1177 |
|
|
for (Future<String> future : futures) |
1178 |
jsr166 |
1.25 |
assertSame(TEST_STRING, future.get()); |
1179 |
dl |
1.11 |
} finally { |
1180 |
|
|
joinPool(e); |
1181 |
|
|
} |
1182 |
|
|
} |
1183 |
|
|
|
1184 |
|
|
/** |
1185 |
|
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1186 |
|
|
*/ |
1187 |
jsr166 |
1.25 |
public void testTimedInvokeAll6() throws Exception { |
1188 |
dl |
1.11 |
ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1189 |
|
|
try { |
1190 |
jsr166 |
1.52 |
for (long timeout = timeoutMillis();;) { |
1191 |
|
|
List<Callable<String>> tasks = new ArrayList<>(); |
1192 |
|
|
tasks.add(new StringTask("0")); |
1193 |
|
|
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1194 |
|
|
tasks.add(new StringTask("2")); |
1195 |
|
|
long startTime = System.nanoTime(); |
1196 |
|
|
List<Future<String>> futures = |
1197 |
|
|
e.invokeAll(tasks, timeout, MILLISECONDS); |
1198 |
|
|
assertEquals(tasks.size(), futures.size()); |
1199 |
|
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1200 |
|
|
for (Future future : futures) |
1201 |
|
|
assertTrue(future.isDone()); |
1202 |
|
|
assertTrue(futures.get(1).isCancelled()); |
1203 |
|
|
try { |
1204 |
|
|
assertEquals("0", futures.get(0).get()); |
1205 |
|
|
assertEquals("2", futures.get(2).get()); |
1206 |
|
|
break; |
1207 |
|
|
} catch (CancellationException retryWithLongerTimeout) { |
1208 |
|
|
timeout *= 2; |
1209 |
|
|
if (timeout >= LONG_DELAY_MS / 2) |
1210 |
|
|
fail("expected exactly one task to be cancelled"); |
1211 |
|
|
} |
1212 |
|
|
} |
1213 |
dl |
1.11 |
} finally { |
1214 |
|
|
joinPool(e); |
1215 |
|
|
} |
1216 |
|
|
} |
1217 |
|
|
|
1218 |
dl |
1.1 |
} |