1 |
|
/* |
2 |
|
* Written by Doug Lea with assistance from members of JCP JSR-166 |
3 |
|
* Expert Group and released to the public domain, as explained at |
4 |
< |
* http://creativecommons.org/licenses/publicdomain |
4 |
> |
* http://creativecommons.org/publicdomain/zero/1.0/ |
5 |
|
* Other contributors include Andrew Wright, Jeffrey Hayes, |
6 |
|
* Pat Fisher, Mike Judd. |
7 |
|
*/ |
25 |
|
* execute successfully executes a runnable |
26 |
|
*/ |
27 |
|
public void testExecute() throws InterruptedException { |
28 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
29 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
30 |
< |
p1.execute(runnable); |
31 |
< |
assertFalse(runnable.done); |
32 |
< |
Thread.sleep(SHORT_DELAY_MS); |
33 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
34 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
35 |
< |
assertTrue(runnable.done); |
36 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
37 |
< |
joinPool(p1); |
28 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
29 |
> |
final CountDownLatch done = new CountDownLatch(1); |
30 |
> |
final Runnable task = new CheckedRunnable() { |
31 |
> |
public void realRun() { |
32 |
> |
done.countDown(); |
33 |
> |
}}; |
34 |
> |
try { |
35 |
> |
p.execute(task); |
36 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
37 |
> |
} finally { |
38 |
> |
joinPool(p); |
39 |
> |
} |
40 |
|
} |
41 |
|
|
42 |
|
|
44 |
|
* delayed schedule of callable successfully executes after delay |
45 |
|
*/ |
46 |
|
public void testSchedule1() throws Exception { |
47 |
< |
TrackedCallable callable = new TrackedCallable(); |
48 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
49 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
50 |
< |
assertFalse(callable.done); |
51 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
52 |
< |
assertTrue(callable.done); |
53 |
< |
assertEquals(Boolean.TRUE, f.get()); |
54 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
55 |
< |
joinPool(p1); |
47 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
48 |
> |
final long t0 = System.nanoTime(); |
49 |
> |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
50 |
> |
final CountDownLatch done = new CountDownLatch(1); |
51 |
> |
try { |
52 |
> |
Callable task = new CheckedCallable<Boolean>() { |
53 |
> |
public Boolean realCall() { |
54 |
> |
done.countDown(); |
55 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
56 |
> |
return Boolean.TRUE; |
57 |
> |
}}; |
58 |
> |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
59 |
> |
assertEquals(Boolean.TRUE, f.get()); |
60 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
61 |
> |
assertTrue(done.await(0L, MILLISECONDS)); |
62 |
> |
} finally { |
63 |
> |
joinPool(p); |
64 |
> |
} |
65 |
|
} |
66 |
|
|
67 |
|
/** |
68 |
< |
* delayed schedule of runnable successfully executes after delay |
69 |
< |
*/ |
70 |
< |
public void testSchedule3() throws InterruptedException { |
71 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
72 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
73 |
< |
p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS); |
74 |
< |
Thread.sleep(SHORT_DELAY_MS); |
75 |
< |
assertFalse(runnable.done); |
76 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
77 |
< |
assertTrue(runnable.done); |
78 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
79 |
< |
joinPool(p1); |
68 |
> |
* delayed schedule of runnable successfully executes after delay |
69 |
> |
*/ |
70 |
> |
public void testSchedule3() throws Exception { |
71 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
72 |
> |
final long t0 = System.nanoTime(); |
73 |
> |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
74 |
> |
final CountDownLatch done = new CountDownLatch(1); |
75 |
> |
try { |
76 |
> |
Runnable task = new CheckedRunnable() { |
77 |
> |
public void realRun() { |
78 |
> |
done.countDown(); |
79 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
80 |
> |
}}; |
81 |
> |
Future f = p.schedule(task, SHORT_DELAY_MS, MILLISECONDS); |
82 |
> |
assertNull(f.get()); |
83 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
84 |
> |
assertTrue(done.await(0L, MILLISECONDS)); |
85 |
> |
} finally { |
86 |
> |
joinPool(p); |
87 |
> |
} |
88 |
|
} |
89 |
|
|
90 |
|
/** |
91 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
92 |
|
*/ |
93 |
< |
public void testSchedule4() throws InterruptedException { |
94 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
95 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
96 |
< |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
97 |
< |
assertFalse(runnable.done); |
98 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
99 |
< |
assertTrue(runnable.done); |
100 |
< |
h.cancel(true); |
101 |
< |
joinPool(p1); |
102 |
< |
} |
103 |
< |
|
104 |
< |
static class RunnableCounter implements Runnable { |
105 |
< |
AtomicInteger count = new AtomicInteger(0); |
106 |
< |
public void run() { count.getAndIncrement(); } |
93 |
> |
public void testSchedule4() throws Exception { |
94 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
95 |
> |
final long t0 = System.nanoTime(); |
96 |
> |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
97 |
> |
final CountDownLatch done = new CountDownLatch(1); |
98 |
> |
try { |
99 |
> |
Runnable task = new CheckedRunnable() { |
100 |
> |
public void realRun() { |
101 |
> |
done.countDown(); |
102 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
103 |
> |
}}; |
104 |
> |
ScheduledFuture f = |
105 |
> |
p.scheduleAtFixedRate(task, SHORT_DELAY_MS, |
106 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
107 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
108 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
109 |
> |
f.cancel(true); |
110 |
> |
} finally { |
111 |
> |
joinPool(p); |
112 |
> |
} |
113 |
|
} |
114 |
|
|
115 |
|
/** |
116 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
117 |
|
*/ |
118 |
|
public void testSchedule5() throws InterruptedException { |
119 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
120 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
121 |
< |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
122 |
< |
assertFalse(runnable.done); |
123 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
124 |
< |
assertTrue(runnable.done); |
125 |
< |
h.cancel(true); |
126 |
< |
joinPool(p1); |
119 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
120 |
> |
final long t0 = System.nanoTime(); |
121 |
> |
final long timeoutNanos = SHORT_DELAY_MS * 1000L * 1000L; |
122 |
> |
final CountDownLatch done = new CountDownLatch(1); |
123 |
> |
try { |
124 |
> |
Runnable task = new CheckedRunnable() { |
125 |
> |
public void realRun() { |
126 |
> |
done.countDown(); |
127 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
128 |
> |
}}; |
129 |
> |
ScheduledFuture f = |
130 |
> |
p.scheduleWithFixedDelay(task, SHORT_DELAY_MS, |
131 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
132 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
133 |
> |
assertTrue(System.nanoTime() - t0 >= timeoutNanos); |
134 |
> |
f.cancel(true); |
135 |
> |
} finally { |
136 |
> |
joinPool(p); |
137 |
> |
} |
138 |
> |
} |
139 |
> |
|
140 |
> |
static class RunnableCounter implements Runnable { |
141 |
> |
AtomicInteger count = new AtomicInteger(0); |
142 |
> |
public void run() { count.getAndIncrement(); } |
143 |
|
} |
144 |
|
|
145 |
|
/** |
146 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
147 |
|
*/ |
148 |
|
public void testFixedRateSequence() throws InterruptedException { |
149 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
149 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
150 |
|
RunnableCounter counter = new RunnableCounter(); |
151 |
|
ScheduledFuture h = |
152 |
< |
p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
153 |
< |
Thread.sleep(SMALL_DELAY_MS); |
152 |
> |
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
153 |
> |
delay(SMALL_DELAY_MS); |
154 |
|
h.cancel(true); |
155 |
|
int c = counter.count.get(); |
156 |
|
// By time scaling conventions, we must have at least |
157 |
|
// an execution per SHORT delay, but no more than one SHORT more |
158 |
|
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
159 |
|
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
160 |
< |
joinPool(p1); |
160 |
> |
joinPool(p); |
161 |
|
} |
162 |
|
|
163 |
|
/** |
164 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
165 |
|
*/ |
166 |
|
public void testFixedDelaySequence() throws InterruptedException { |
167 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
167 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
168 |
|
RunnableCounter counter = new RunnableCounter(); |
169 |
|
ScheduledFuture h = |
170 |
< |
p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
171 |
< |
Thread.sleep(SMALL_DELAY_MS); |
170 |
> |
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
171 |
> |
delay(SMALL_DELAY_MS); |
172 |
|
h.cancel(true); |
173 |
|
int c = counter.count.get(); |
174 |
|
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
175 |
|
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
176 |
< |
joinPool(p1); |
176 |
> |
joinPool(p); |
177 |
|
} |
178 |
|
|
179 |
|
|
180 |
|
/** |
181 |
< |
* execute(null) throws NPE |
181 |
> |
* execute(null) throws NPE |
182 |
|
*/ |
183 |
|
public void testExecuteNull() throws InterruptedException { |
184 |
|
ScheduledThreadPoolExecutor se = null; |
240 |
|
/** |
241 |
|
* schedule callable throws RejectedExecutionException if shutdown |
242 |
|
*/ |
243 |
< |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
244 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
245 |
< |
try { |
243 |
> |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
244 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
245 |
> |
try { |
246 |
|
se.shutdown(); |
247 |
|
se.schedule(new NoOpCallable(), |
248 |
|
MEDIUM_DELAY_MS, MILLISECONDS); |
250 |
|
} catch (RejectedExecutionException success) { |
251 |
|
} catch (SecurityException ok) { |
252 |
|
} |
253 |
< |
joinPool(se); |
253 |
> |
joinPool(se); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
257 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
257 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
258 |
|
*/ |
259 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
260 |
|
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
286 |
|
} |
287 |
|
|
288 |
|
/** |
289 |
< |
* getActiveCount increases but doesn't overestimate, when a |
290 |
< |
* thread becomes active |
289 |
> |
* getActiveCount increases but doesn't overestimate, when a |
290 |
> |
* thread becomes active |
291 |
|
*/ |
292 |
|
public void testGetActiveCount() throws InterruptedException { |
293 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
294 |
< |
assertEquals(0, p2.getActiveCount()); |
295 |
< |
p2.execute(new SmallRunnable()); |
296 |
< |
Thread.sleep(SHORT_DELAY_MS); |
297 |
< |
assertEquals(1, p2.getActiveCount()); |
298 |
< |
joinPool(p2); |
293 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
294 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
295 |
> |
final CountDownLatch done = new CountDownLatch(1); |
296 |
> |
try { |
297 |
> |
assertEquals(0, p.getActiveCount()); |
298 |
> |
p.execute(new CheckedRunnable() { |
299 |
> |
public void realRun() throws InterruptedException { |
300 |
> |
threadStarted.countDown(); |
301 |
> |
assertEquals(1, p.getActiveCount()); |
302 |
> |
done.await(); |
303 |
> |
}}); |
304 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
305 |
> |
assertEquals(1, p.getActiveCount()); |
306 |
> |
} finally { |
307 |
> |
done.countDown(); |
308 |
> |
joinPool(p); |
309 |
> |
} |
310 |
|
} |
311 |
|
|
312 |
|
/** |
313 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
314 |
< |
* when tasks complete |
313 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
314 |
> |
* when tasks complete |
315 |
|
*/ |
316 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
317 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
318 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
319 |
< |
p2.execute(new SmallRunnable()); |
320 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
321 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
322 |
< |
joinPool(p2); |
317 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
318 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
319 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
320 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
321 |
> |
try { |
322 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
323 |
> |
p.execute(new CheckedRunnable() { |
324 |
> |
public void realRun() throws InterruptedException { |
325 |
> |
threadStarted.countDown(); |
326 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
327 |
> |
threadProceed.await(); |
328 |
> |
threadDone.countDown(); |
329 |
> |
}}); |
330 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
331 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
332 |
> |
threadProceed.countDown(); |
333 |
> |
threadDone.await(); |
334 |
> |
delay(SHORT_DELAY_MS); |
335 |
> |
assertEquals(1, p.getCompletedTaskCount()); |
336 |
> |
} finally { |
337 |
> |
joinPool(p); |
338 |
> |
} |
339 |
|
} |
340 |
|
|
341 |
|
/** |
342 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
342 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
343 |
|
*/ |
344 |
|
public void testGetCorePoolSize() throws InterruptedException { |
345 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
346 |
< |
assertEquals(1, p1.getCorePoolSize()); |
347 |
< |
joinPool(p1); |
345 |
> |
ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
346 |
> |
assertEquals(1, p.getCorePoolSize()); |
347 |
> |
joinPool(p); |
348 |
|
} |
349 |
|
|
350 |
|
/** |
351 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
352 |
< |
* multiple threads active |
351 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
352 |
> |
* multiple threads active |
353 |
|
*/ |
354 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
355 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
356 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
357 |
< |
p2.execute(new SmallRunnable()); |
358 |
< |
p2.execute(new SmallRunnable()); |
359 |
< |
Thread.sleep(SHORT_DELAY_MS); |
360 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
361 |
< |
joinPool(p2); |
355 |
> |
final int THREADS = 3; |
356 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
357 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
358 |
> |
final CountDownLatch done = new CountDownLatch(1); |
359 |
> |
try { |
360 |
> |
assertEquals(0, p.getLargestPoolSize()); |
361 |
> |
for (int i = 0; i < THREADS; i++) |
362 |
> |
p.execute(new CheckedRunnable() { |
363 |
> |
public void realRun() throws InterruptedException { |
364 |
> |
threadsStarted.countDown(); |
365 |
> |
done.await(); |
366 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
367 |
> |
}}); |
368 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
369 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
370 |
> |
} finally { |
371 |
> |
done.countDown(); |
372 |
> |
joinPool(p); |
373 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
374 |
> |
} |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
379 |
< |
* become active |
378 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
379 |
> |
* become active |
380 |
|
*/ |
381 |
|
public void testGetPoolSize() throws InterruptedException { |
382 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
383 |
< |
assertEquals(0, p1.getPoolSize()); |
384 |
< |
p1.execute(new SmallRunnable()); |
385 |
< |
assertEquals(1, p1.getPoolSize()); |
386 |
< |
joinPool(p1); |
382 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
383 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
384 |
> |
final CountDownLatch done = new CountDownLatch(1); |
385 |
> |
try { |
386 |
> |
assertEquals(0, p.getPoolSize()); |
387 |
> |
p.execute(new CheckedRunnable() { |
388 |
> |
public void realRun() throws InterruptedException { |
389 |
> |
threadStarted.countDown(); |
390 |
> |
assertEquals(1, p.getPoolSize()); |
391 |
> |
done.await(); |
392 |
> |
}}); |
393 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
394 |
> |
assertEquals(1, p.getPoolSize()); |
395 |
> |
} finally { |
396 |
> |
done.countDown(); |
397 |
> |
joinPool(p); |
398 |
> |
} |
399 |
|
} |
400 |
|
|
401 |
|
/** |
402 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks |
403 |
< |
* submitted |
402 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
403 |
> |
* submitted |
404 |
|
*/ |
405 |
|
public void testGetTaskCount() throws InterruptedException { |
406 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
407 |
< |
assertEquals(0, p1.getTaskCount()); |
408 |
< |
for (int i = 0; i < 5; i++) |
409 |
< |
p1.execute(new SmallRunnable()); |
410 |
< |
Thread.sleep(SHORT_DELAY_MS); |
411 |
< |
assertEquals(5, p1.getTaskCount()); |
412 |
< |
joinPool(p1); |
406 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
407 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
408 |
> |
final CountDownLatch done = new CountDownLatch(1); |
409 |
> |
final int TASKS = 5; |
410 |
> |
try { |
411 |
> |
assertEquals(0, p.getTaskCount()); |
412 |
> |
for (int i = 0; i < TASKS; i++) |
413 |
> |
p.execute(new CheckedRunnable() { |
414 |
> |
public void realRun() throws InterruptedException { |
415 |
> |
threadStarted.countDown(); |
416 |
> |
done.await(); |
417 |
> |
}}); |
418 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
419 |
> |
assertEquals(TASKS, p.getTaskCount()); |
420 |
> |
} finally { |
421 |
> |
done.countDown(); |
422 |
> |
joinPool(p); |
423 |
> |
} |
424 |
|
} |
425 |
|
|
426 |
|
/** |
459 |
|
} |
460 |
|
|
461 |
|
/** |
462 |
< |
* is isShutDown is false before shutdown, true after |
462 |
> |
* isShutdown is false before shutdown, true after |
463 |
|
*/ |
464 |
|
public void testIsShutdown() { |
465 |
|
|
466 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
466 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
467 |
|
try { |
468 |
< |
assertFalse(p1.isShutdown()); |
468 |
> |
assertFalse(p.isShutdown()); |
469 |
|
} |
470 |
|
finally { |
471 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
471 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
472 |
|
} |
473 |
< |
assertTrue(p1.isShutdown()); |
473 |
> |
assertTrue(p.isShutdown()); |
474 |
|
} |
475 |
|
|
476 |
|
|
477 |
|
/** |
478 |
< |
* isTerminated is false before termination, true after |
478 |
> |
* isTerminated is false before termination, true after |
479 |
|
*/ |
480 |
|
public void testIsTerminated() throws InterruptedException { |
481 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
482 |
< |
try { |
483 |
< |
p1.execute(new SmallRunnable()); |
481 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
482 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
483 |
> |
final CountDownLatch done = new CountDownLatch(1); |
484 |
> |
assertFalse(p.isTerminated()); |
485 |
> |
try { |
486 |
> |
p.execute(new CheckedRunnable() { |
487 |
> |
public void realRun() throws InterruptedException { |
488 |
> |
assertFalse(p.isTerminated()); |
489 |
> |
threadStarted.countDown(); |
490 |
> |
done.await(); |
491 |
> |
}}); |
492 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
493 |
> |
assertFalse(p.isTerminating()); |
494 |
> |
done.countDown(); |
495 |
|
} finally { |
496 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
496 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
497 |
|
} |
498 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
499 |
< |
assertTrue(p1.isTerminated()); |
498 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
499 |
> |
assertTrue(p.isTerminated()); |
500 |
|
} |
501 |
|
|
502 |
|
/** |
503 |
< |
* isTerminating is not true when running or when terminated |
503 |
> |
* isTerminating is not true when running or when terminated |
504 |
|
*/ |
505 |
|
public void testIsTerminating() throws InterruptedException { |
506 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
507 |
< |
assertFalse(p1.isTerminating()); |
508 |
< |
try { |
509 |
< |
p1.execute(new SmallRunnable()); |
510 |
< |
assertFalse(p1.isTerminating()); |
506 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
507 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
508 |
> |
final CountDownLatch done = new CountDownLatch(1); |
509 |
> |
try { |
510 |
> |
assertFalse(p.isTerminating()); |
511 |
> |
p.execute(new CheckedRunnable() { |
512 |
> |
public void realRun() throws InterruptedException { |
513 |
> |
assertFalse(p.isTerminating()); |
514 |
> |
threadStarted.countDown(); |
515 |
> |
done.await(); |
516 |
> |
}}); |
517 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
518 |
> |
assertFalse(p.isTerminating()); |
519 |
> |
done.countDown(); |
520 |
|
} finally { |
521 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
521 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
522 |
|
} |
523 |
< |
|
524 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
525 |
< |
assertTrue(p1.isTerminated()); |
402 |
< |
assertFalse(p1.isTerminating()); |
523 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
524 |
> |
assertTrue(p.isTerminated()); |
525 |
> |
assertFalse(p.isTerminating()); |
526 |
|
} |
527 |
|
|
528 |
|
/** |
529 |
|
* getQueue returns the work queue, which contains queued tasks |
530 |
|
*/ |
531 |
|
public void testGetQueue() throws InterruptedException { |
532 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
533 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
534 |
< |
for (int i = 0; i < 5; i++) { |
412 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
413 |
< |
} |
532 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
533 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
534 |
> |
final CountDownLatch done = new CountDownLatch(1); |
535 |
|
try { |
536 |
< |
Thread.sleep(SHORT_DELAY_MS); |
537 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
538 |
< |
assertTrue(q.contains(tasks[4])); |
536 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
537 |
> |
for (int i = 0; i < tasks.length; i++) { |
538 |
> |
Runnable r = new CheckedRunnable() { |
539 |
> |
public void realRun() throws InterruptedException { |
540 |
> |
threadStarted.countDown(); |
541 |
> |
done.await(); |
542 |
> |
}}; |
543 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
544 |
> |
} |
545 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
546 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
547 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
548 |
|
assertFalse(q.contains(tasks[0])); |
549 |
|
} finally { |
550 |
< |
joinPool(p1); |
550 |
> |
done.countDown(); |
551 |
> |
joinPool(p); |
552 |
|
} |
553 |
|
} |
554 |
|
|
556 |
|
* remove(task) removes queued task, and fails to remove active task |
557 |
|
*/ |
558 |
|
public void testRemove() throws InterruptedException { |
559 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
559 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
560 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
561 |
< |
for (int i = 0; i < 5; i++) { |
562 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
432 |
< |
} |
561 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
562 |
> |
final CountDownLatch done = new CountDownLatch(1); |
563 |
|
try { |
564 |
< |
Thread.sleep(SHORT_DELAY_MS); |
565 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
566 |
< |
assertFalse(p1.remove((Runnable)tasks[0])); |
564 |
> |
for (int i = 0; i < tasks.length; i++) { |
565 |
> |
Runnable r = new CheckedRunnable() { |
566 |
> |
public void realRun() throws InterruptedException { |
567 |
> |
threadStarted.countDown(); |
568 |
> |
done.await(); |
569 |
> |
}}; |
570 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
571 |
> |
} |
572 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
573 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
574 |
> |
assertFalse(p.remove((Runnable)tasks[0])); |
575 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
576 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
577 |
< |
assertTrue(p1.remove((Runnable)tasks[4])); |
578 |
< |
assertFalse(p1.remove((Runnable)tasks[4])); |
577 |
> |
assertTrue(p.remove((Runnable)tasks[4])); |
578 |
> |
assertFalse(p.remove((Runnable)tasks[4])); |
579 |
|
assertFalse(q.contains((Runnable)tasks[4])); |
580 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
581 |
< |
assertTrue(p1.remove((Runnable)tasks[3])); |
581 |
> |
assertTrue(p.remove((Runnable)tasks[3])); |
582 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
583 |
|
} finally { |
584 |
< |
joinPool(p1); |
584 |
> |
done.countDown(); |
585 |
> |
joinPool(p); |
586 |
|
} |
587 |
|
} |
588 |
|
|
589 |
|
/** |
590 |
< |
* purge removes cancelled tasks from the queue |
590 |
> |
* purge eventually removes cancelled tasks from the queue |
591 |
|
*/ |
592 |
|
public void testPurge() throws InterruptedException { |
593 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
593 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
594 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
595 |
< |
for (int i = 0; i < 5; i++) { |
596 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
597 |
< |
} |
595 |
> |
for (int i = 0; i < tasks.length; i++) |
596 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
597 |
> |
LONG_DELAY_MS, MILLISECONDS); |
598 |
|
try { |
599 |
< |
int max = 5; |
599 |
> |
int max = tasks.length; |
600 |
|
if (tasks[4].cancel(true)) --max; |
601 |
|
if (tasks[3].cancel(true)) --max; |
602 |
|
// There must eventually be an interference-free point at |
603 |
|
// which purge will not fail. (At worst, when queue is empty.) |
604 |
< |
int k; |
605 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
606 |
< |
p1.purge(); |
607 |
< |
long count = p1.getTaskCount(); |
608 |
< |
if (count >= 0 && count <= max) |
609 |
< |
break; |
610 |
< |
Thread.sleep(1); |
611 |
< |
} |
473 |
< |
assertTrue(k < SMALL_DELAY_MS); |
604 |
> |
long startTime = System.nanoTime(); |
605 |
> |
do { |
606 |
> |
p.purge(); |
607 |
> |
long count = p.getTaskCount(); |
608 |
> |
if (count == max) |
609 |
> |
return; |
610 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
611 |
> |
fail("Purge failed to remove cancelled tasks"); |
612 |
|
} finally { |
613 |
< |
joinPool(p1); |
613 |
> |
for (ScheduledFuture task : tasks) |
614 |
> |
task.cancel(true); |
615 |
> |
joinPool(p); |
616 |
|
} |
617 |
|
} |
618 |
|
|
619 |
|
/** |
620 |
< |
* shutDownNow returns a list containing tasks that were not run |
620 |
> |
* shutdownNow returns a list containing tasks that were not run |
621 |
|
*/ |
622 |
< |
public void testShutDownNow() throws InterruptedException { |
623 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
622 |
> |
public void testShutdownNow() { |
623 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
624 |
|
for (int i = 0; i < 5; i++) |
625 |
< |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
626 |
< |
List l; |
625 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
626 |
> |
LONG_DELAY_MS, MILLISECONDS); |
627 |
|
try { |
628 |
< |
l = p1.shutdownNow(); |
628 |
> |
List<Runnable> l = p.shutdownNow(); |
629 |
> |
assertTrue(p.isShutdown()); |
630 |
> |
assertEquals(5, l.size()); |
631 |
|
} catch (SecurityException ok) { |
632 |
< |
return; |
632 |
> |
// Allowed in case test doesn't have privs |
633 |
> |
} finally { |
634 |
> |
joinPool(p); |
635 |
|
} |
492 |
– |
assertTrue(p1.isShutdown()); |
493 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
494 |
– |
joinPool(p1); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
639 |
|
* In default setting, shutdown cancels periodic but not delayed |
640 |
|
* tasks at shutdown |
641 |
|
*/ |
642 |
< |
public void testShutDown1() throws InterruptedException { |
643 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
644 |
< |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
645 |
< |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
642 |
> |
public void testShutdown1() throws InterruptedException { |
643 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
644 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
645 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
646 |
|
|
647 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
648 |
< |
for (int i = 0; i < 5; i++) |
649 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
650 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
651 |
< |
BlockingQueue q = p1.getQueue(); |
652 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
653 |
< |
ScheduledFuture t = (ScheduledFuture)it.next(); |
654 |
< |
assertFalse(t.isCancelled()); |
655 |
< |
} |
656 |
< |
assertTrue(p1.isShutdown()); |
657 |
< |
Thread.sleep(SMALL_DELAY_MS); |
658 |
< |
for (int i = 0; i < 5; ++i) { |
659 |
< |
assertTrue(tasks[i].isDone()); |
660 |
< |
assertFalse(tasks[i].isCancelled()); |
648 |
> |
for (int i = 0; i < tasks.length; i++) |
649 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
650 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
651 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
652 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
653 |
> |
for (ScheduledFuture task : tasks) { |
654 |
> |
assertFalse(task.isDone()); |
655 |
> |
assertFalse(task.isCancelled()); |
656 |
> |
assertTrue(q.contains(task)); |
657 |
> |
} |
658 |
> |
assertTrue(p.isShutdown()); |
659 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
660 |
> |
assertTrue(p.isTerminated()); |
661 |
> |
for (ScheduledFuture task : tasks) { |
662 |
> |
assertTrue(task.isDone()); |
663 |
> |
assertFalse(task.isCancelled()); |
664 |
|
} |
665 |
|
} |
666 |
|
|
669 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
670 |
|
* delayed tasks are cancelled at shutdown |
671 |
|
*/ |
672 |
< |
public void testShutDown2() throws InterruptedException { |
673 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
674 |
< |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
672 |
> |
public void testShutdown2() throws InterruptedException { |
673 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
674 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
675 |
> |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
676 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
677 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
678 |
< |
for (int i = 0; i < 5; i++) |
679 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
680 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
681 |
< |
assertTrue(p1.isShutdown()); |
682 |
< |
BlockingQueue q = p1.getQueue(); |
678 |
> |
for (int i = 0; i < tasks.length; i++) |
679 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
680 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
681 |
> |
BlockingQueue q = p.getQueue(); |
682 |
> |
assertEquals(tasks.length, q.size()); |
683 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
684 |
> |
assertTrue(p.isShutdown()); |
685 |
|
assertTrue(q.isEmpty()); |
686 |
< |
Thread.sleep(SMALL_DELAY_MS); |
687 |
< |
assertTrue(p1.isTerminated()); |
686 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
687 |
> |
assertTrue(p.isTerminated()); |
688 |
> |
for (ScheduledFuture task : tasks) { |
689 |
> |
assertTrue(task.isDone()); |
690 |
> |
assertTrue(task.isCancelled()); |
691 |
> |
} |
692 |
|
} |
693 |
|
|
542 |
– |
|
694 |
|
/** |
695 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
696 |
< |
* periodic tasks are not cancelled at shutdown |
696 |
> |
* periodic tasks are cancelled at shutdown |
697 |
|
*/ |
698 |
< |
public void testShutDown3() throws InterruptedException { |
699 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
700 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
698 |
> |
public void testShutdown3() throws InterruptedException { |
699 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
700 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
701 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
702 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
703 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
704 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
705 |
> |
long initialDelay = LONG_DELAY_MS; |
706 |
|
ScheduledFuture task = |
707 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
708 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
709 |
< |
assertTrue(p1.isShutdown()); |
710 |
< |
BlockingQueue q = p1.getQueue(); |
711 |
< |
assertTrue(q.isEmpty()); |
712 |
< |
Thread.sleep(SHORT_DELAY_MS); |
713 |
< |
assertTrue(p1.isTerminated()); |
707 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
708 |
> |
5, MILLISECONDS); |
709 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
710 |
> |
assertTrue(p.isShutdown()); |
711 |
> |
assertTrue(p.getQueue().isEmpty()); |
712 |
> |
assertTrue(task.isDone()); |
713 |
> |
assertTrue(task.isCancelled()); |
714 |
> |
joinPool(p); |
715 |
|
} |
716 |
|
|
717 |
|
/** |
718 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
719 |
< |
* periodic tasks are cancelled at shutdown |
719 |
> |
* periodic tasks are not cancelled at shutdown |
720 |
|
*/ |
721 |
< |
public void testShutDown4() throws InterruptedException { |
722 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
721 |
> |
public void testShutdown4() throws InterruptedException { |
722 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
723 |
> |
final CountDownLatch counter = new CountDownLatch(2); |
724 |
|
try { |
725 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
725 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
726 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
727 |
> |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
728 |
> |
final Runnable r = new CheckedRunnable() { |
729 |
> |
public void realRun() { |
730 |
> |
counter.countDown(); |
731 |
> |
}}; |
732 |
|
ScheduledFuture task = |
733 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); |
733 |
> |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
734 |
> |
assertFalse(task.isDone()); |
735 |
|
assertFalse(task.isCancelled()); |
736 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
736 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
737 |
|
assertFalse(task.isCancelled()); |
738 |
< |
assertFalse(p1.isTerminated()); |
739 |
< |
assertTrue(p1.isShutdown()); |
740 |
< |
Thread.sleep(SHORT_DELAY_MS); |
738 |
> |
assertFalse(p.isTerminated()); |
739 |
> |
assertTrue(p.isShutdown()); |
740 |
> |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
741 |
|
assertFalse(task.isCancelled()); |
742 |
< |
assertTrue(task.cancel(true)); |
742 |
> |
assertTrue(task.cancel(false)); |
743 |
|
assertTrue(task.isDone()); |
744 |
< |
Thread.sleep(SHORT_DELAY_MS); |
745 |
< |
assertTrue(p1.isTerminated()); |
744 |
> |
assertTrue(task.isCancelled()); |
745 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
746 |
> |
assertTrue(p.isTerminated()); |
747 |
|
} |
748 |
|
finally { |
749 |
< |
joinPool(p1); |
749 |
> |
joinPool(p); |
750 |
|
} |
751 |
|
} |
752 |
|
|