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 |
|
*/ |
10 |
|
import java.util.*; |
11 |
|
import java.util.concurrent.*; |
12 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
13 |
< |
import java.util.concurrent.atomic.*; |
13 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
14 |
|
|
15 |
|
public class ScheduledExecutorTest extends JSR166TestCase { |
16 |
|
public static void main(String[] args) { |
20 |
|
return new TestSuite(ScheduledExecutorTest.class); |
21 |
|
} |
22 |
|
|
23 |
– |
|
23 |
|
/** |
24 |
|
* execute successfully executes a runnable |
25 |
|
*/ |
26 |
|
public void testExecute() throws InterruptedException { |
27 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
28 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
29 |
< |
p1.execute(runnable); |
30 |
< |
assertFalse(runnable.done); |
31 |
< |
Thread.sleep(SHORT_DELAY_MS); |
32 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
33 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
34 |
< |
assertTrue(runnable.done); |
35 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
36 |
< |
joinPool(p1); |
27 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
28 |
> |
final CountDownLatch done = new CountDownLatch(1); |
29 |
> |
final Runnable task = new CheckedRunnable() { |
30 |
> |
public void realRun() { |
31 |
> |
done.countDown(); |
32 |
> |
}}; |
33 |
> |
try { |
34 |
> |
p.execute(task); |
35 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
36 |
> |
} finally { |
37 |
> |
joinPool(p); |
38 |
> |
} |
39 |
|
} |
40 |
|
|
40 |
– |
|
41 |
|
/** |
42 |
|
* delayed schedule of callable successfully executes after delay |
43 |
|
*/ |
44 |
|
public void testSchedule1() throws Exception { |
45 |
< |
TrackedCallable callable = new TrackedCallable(); |
46 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
47 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
48 |
< |
assertFalse(callable.done); |
49 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
50 |
< |
assertTrue(callable.done); |
51 |
< |
assertEquals(Boolean.TRUE, f.get()); |
52 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
53 |
< |
joinPool(p1); |
45 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
46 |
> |
final long startTime = System.nanoTime(); |
47 |
> |
final CountDownLatch done = new CountDownLatch(1); |
48 |
> |
try { |
49 |
> |
Callable task = new CheckedCallable<Boolean>() { |
50 |
> |
public Boolean realCall() { |
51 |
> |
done.countDown(); |
52 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
53 |
> |
return Boolean.TRUE; |
54 |
> |
}}; |
55 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
56 |
> |
assertSame(Boolean.TRUE, f.get()); |
57 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
58 |
> |
assertTrue(done.await(0L, MILLISECONDS)); |
59 |
> |
} finally { |
60 |
> |
joinPool(p); |
61 |
> |
} |
62 |
|
} |
63 |
|
|
64 |
|
/** |
65 |
< |
* delayed schedule of runnable successfully executes after delay |
66 |
< |
*/ |
67 |
< |
public void testSchedule3() throws InterruptedException { |
68 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
69 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
70 |
< |
p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS); |
71 |
< |
Thread.sleep(SHORT_DELAY_MS); |
72 |
< |
assertFalse(runnable.done); |
73 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
74 |
< |
assertTrue(runnable.done); |
75 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
76 |
< |
joinPool(p1); |
65 |
> |
* delayed schedule of runnable successfully executes after delay |
66 |
> |
*/ |
67 |
> |
public void testSchedule3() throws Exception { |
68 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
69 |
> |
final long startTime = System.nanoTime(); |
70 |
> |
final CountDownLatch done = new CountDownLatch(1); |
71 |
> |
try { |
72 |
> |
Runnable task = new CheckedRunnable() { |
73 |
> |
public void realRun() { |
74 |
> |
done.countDown(); |
75 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
76 |
> |
}}; |
77 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
78 |
> |
await(done); |
79 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
80 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
81 |
> |
} finally { |
82 |
> |
joinPool(p); |
83 |
> |
} |
84 |
|
} |
85 |
|
|
86 |
|
/** |
87 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
88 |
|
*/ |
89 |
< |
public void testSchedule4() throws InterruptedException { |
90 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
91 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
92 |
< |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
93 |
< |
assertFalse(runnable.done); |
94 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
95 |
< |
assertTrue(runnable.done); |
96 |
< |
h.cancel(true); |
97 |
< |
joinPool(p1); |
98 |
< |
} |
99 |
< |
|
100 |
< |
static class RunnableCounter implements Runnable { |
101 |
< |
AtomicInteger count = new AtomicInteger(0); |
102 |
< |
public void run() { count.getAndIncrement(); } |
89 |
> |
public void testSchedule4() throws Exception { |
90 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
91 |
> |
final long startTime = System.nanoTime(); |
92 |
> |
final CountDownLatch done = new CountDownLatch(1); |
93 |
> |
try { |
94 |
> |
Runnable task = new CheckedRunnable() { |
95 |
> |
public void realRun() { |
96 |
> |
done.countDown(); |
97 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
98 |
> |
}}; |
99 |
> |
ScheduledFuture f = |
100 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
101 |
> |
LONG_DELAY_MS, MILLISECONDS); |
102 |
> |
await(done); |
103 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
104 |
> |
f.cancel(true); |
105 |
> |
} finally { |
106 |
> |
joinPool(p); |
107 |
> |
} |
108 |
|
} |
109 |
|
|
110 |
|
/** |
111 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
112 |
|
*/ |
113 |
< |
public void testSchedule5() throws InterruptedException { |
114 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
115 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
116 |
< |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
117 |
< |
assertFalse(runnable.done); |
118 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
119 |
< |
assertTrue(runnable.done); |
120 |
< |
h.cancel(true); |
121 |
< |
joinPool(p1); |
113 |
> |
public void testSchedule5() throws Exception { |
114 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
115 |
> |
final long startTime = System.nanoTime(); |
116 |
> |
final CountDownLatch done = new CountDownLatch(1); |
117 |
> |
try { |
118 |
> |
Runnable task = new CheckedRunnable() { |
119 |
> |
public void realRun() { |
120 |
> |
done.countDown(); |
121 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
122 |
> |
}}; |
123 |
> |
ScheduledFuture f = |
124 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
125 |
> |
LONG_DELAY_MS, MILLISECONDS); |
126 |
> |
await(done); |
127 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
128 |
> |
f.cancel(true); |
129 |
> |
} finally { |
130 |
> |
joinPool(p); |
131 |
> |
} |
132 |
> |
} |
133 |
> |
|
134 |
> |
static class RunnableCounter implements Runnable { |
135 |
> |
AtomicInteger count = new AtomicInteger(0); |
136 |
> |
public void run() { count.getAndIncrement(); } |
137 |
|
} |
138 |
|
|
139 |
|
/** |
140 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
141 |
|
*/ |
142 |
|
public void testFixedRateSequence() throws InterruptedException { |
143 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
144 |
< |
RunnableCounter counter = new RunnableCounter(); |
145 |
< |
ScheduledFuture h = |
146 |
< |
p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
147 |
< |
Thread.sleep(SMALL_DELAY_MS); |
148 |
< |
h.cancel(true); |
149 |
< |
int c = counter.count.get(); |
150 |
< |
// By time scaling conventions, we must have at least |
151 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
152 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
153 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
154 |
< |
joinPool(p1); |
143 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
144 |
> |
try { |
145 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
146 |
> |
long startTime = System.nanoTime(); |
147 |
> |
int cycles = 10; |
148 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
149 |
> |
CheckedRunnable task = new CheckedRunnable() { |
150 |
> |
public void realRun() { done.countDown(); }}; |
151 |
> |
|
152 |
> |
ScheduledFuture h = |
153 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
154 |
> |
done.await(); |
155 |
> |
h.cancel(true); |
156 |
> |
double normalizedTime = |
157 |
> |
(double) millisElapsedSince(startTime) / delay; |
158 |
> |
if (normalizedTime >= cycles - 1 && |
159 |
> |
normalizedTime <= cycles) |
160 |
> |
return; |
161 |
> |
} |
162 |
> |
throw new AssertionError("unexpected execution rate"); |
163 |
> |
} finally { |
164 |
> |
joinPool(p); |
165 |
> |
} |
166 |
|
} |
167 |
|
|
168 |
|
/** |
169 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
170 |
|
*/ |
171 |
|
public void testFixedDelaySequence() throws InterruptedException { |
172 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
173 |
< |
RunnableCounter counter = new RunnableCounter(); |
174 |
< |
ScheduledFuture h = |
175 |
< |
p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
176 |
< |
Thread.sleep(SMALL_DELAY_MS); |
177 |
< |
h.cancel(true); |
178 |
< |
int c = counter.count.get(); |
179 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
180 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
181 |
< |
joinPool(p1); |
172 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
173 |
> |
try { |
174 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
175 |
> |
long startTime = System.nanoTime(); |
176 |
> |
int cycles = 10; |
177 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
178 |
> |
CheckedRunnable task = new CheckedRunnable() { |
179 |
> |
public void realRun() { done.countDown(); }}; |
180 |
> |
|
181 |
> |
ScheduledFuture h = |
182 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
183 |
> |
done.await(); |
184 |
> |
h.cancel(true); |
185 |
> |
double normalizedTime = |
186 |
> |
(double) millisElapsedSince(startTime) / delay; |
187 |
> |
if (normalizedTime >= cycles - 1 && |
188 |
> |
normalizedTime <= cycles) |
189 |
> |
return; |
190 |
> |
} |
191 |
> |
throw new AssertionError("unexpected execution rate"); |
192 |
> |
} finally { |
193 |
> |
joinPool(p); |
194 |
> |
} |
195 |
|
} |
196 |
|
|
138 |
– |
|
197 |
|
/** |
198 |
< |
* execute(null) throws NPE |
198 |
> |
* execute(null) throws NPE |
199 |
|
*/ |
200 |
|
public void testExecuteNull() throws InterruptedException { |
201 |
|
ScheduledThreadPoolExecutor se = null; |
257 |
|
/** |
258 |
|
* schedule callable throws RejectedExecutionException if shutdown |
259 |
|
*/ |
260 |
< |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
261 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
262 |
< |
try { |
260 |
> |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
261 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
262 |
> |
try { |
263 |
|
se.shutdown(); |
264 |
|
se.schedule(new NoOpCallable(), |
265 |
|
MEDIUM_DELAY_MS, MILLISECONDS); |
267 |
|
} catch (RejectedExecutionException success) { |
268 |
|
} catch (SecurityException ok) { |
269 |
|
} |
270 |
< |
joinPool(se); |
270 |
> |
joinPool(se); |
271 |
|
} |
272 |
|
|
273 |
|
/** |
274 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
274 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
275 |
|
*/ |
276 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
277 |
|
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
303 |
|
} |
304 |
|
|
305 |
|
/** |
306 |
< |
* getActiveCount increases but doesn't overestimate, when a |
307 |
< |
* thread becomes active |
306 |
> |
* getActiveCount increases but doesn't overestimate, when a |
307 |
> |
* thread becomes active |
308 |
|
*/ |
309 |
|
public void testGetActiveCount() throws InterruptedException { |
310 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
311 |
< |
assertEquals(0, p2.getActiveCount()); |
312 |
< |
p2.execute(new SmallRunnable()); |
313 |
< |
Thread.sleep(SHORT_DELAY_MS); |
314 |
< |
assertEquals(1, p2.getActiveCount()); |
315 |
< |
joinPool(p2); |
310 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
311 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
312 |
> |
final CountDownLatch done = new CountDownLatch(1); |
313 |
> |
try { |
314 |
> |
assertEquals(0, p.getActiveCount()); |
315 |
> |
p.execute(new CheckedRunnable() { |
316 |
> |
public void realRun() throws InterruptedException { |
317 |
> |
threadStarted.countDown(); |
318 |
> |
assertEquals(1, p.getActiveCount()); |
319 |
> |
done.await(); |
320 |
> |
}}); |
321 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
322 |
> |
assertEquals(1, p.getActiveCount()); |
323 |
> |
} finally { |
324 |
> |
done.countDown(); |
325 |
> |
joinPool(p); |
326 |
> |
} |
327 |
|
} |
328 |
|
|
329 |
|
/** |
330 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
331 |
< |
* when tasks complete |
330 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
331 |
> |
* when tasks complete |
332 |
|
*/ |
333 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
334 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
335 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
336 |
< |
p2.execute(new SmallRunnable()); |
337 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
338 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
339 |
< |
joinPool(p2); |
334 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
335 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
336 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
337 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
338 |
> |
try { |
339 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
340 |
> |
p.execute(new CheckedRunnable() { |
341 |
> |
public void realRun() throws InterruptedException { |
342 |
> |
threadStarted.countDown(); |
343 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
344 |
> |
threadProceed.await(); |
345 |
> |
threadDone.countDown(); |
346 |
> |
}}); |
347 |
> |
await(threadStarted); |
348 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
349 |
> |
threadProceed.countDown(); |
350 |
> |
threadDone.await(); |
351 |
> |
long startTime = System.nanoTime(); |
352 |
> |
while (p.getCompletedTaskCount() != 1) { |
353 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
354 |
> |
fail("timed out"); |
355 |
> |
Thread.yield(); |
356 |
> |
} |
357 |
> |
} finally { |
358 |
> |
joinPool(p); |
359 |
> |
} |
360 |
|
} |
361 |
|
|
362 |
|
/** |
363 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
363 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
364 |
|
*/ |
365 |
|
public void testGetCorePoolSize() throws InterruptedException { |
366 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
367 |
< |
assertEquals(1, p1.getCorePoolSize()); |
368 |
< |
joinPool(p1); |
366 |
> |
ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
367 |
> |
assertEquals(1, p.getCorePoolSize()); |
368 |
> |
joinPool(p); |
369 |
|
} |
370 |
|
|
371 |
|
/** |
372 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
373 |
< |
* multiple threads active |
372 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
373 |
> |
* multiple threads active |
374 |
|
*/ |
375 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
376 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
377 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
378 |
< |
p2.execute(new SmallRunnable()); |
379 |
< |
p2.execute(new SmallRunnable()); |
380 |
< |
Thread.sleep(SHORT_DELAY_MS); |
381 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
382 |
< |
joinPool(p2); |
376 |
> |
final int THREADS = 3; |
377 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
378 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
379 |
> |
final CountDownLatch done = new CountDownLatch(1); |
380 |
> |
try { |
381 |
> |
assertEquals(0, p.getLargestPoolSize()); |
382 |
> |
for (int i = 0; i < THREADS; i++) |
383 |
> |
p.execute(new CheckedRunnable() { |
384 |
> |
public void realRun() throws InterruptedException { |
385 |
> |
threadsStarted.countDown(); |
386 |
> |
done.await(); |
387 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
388 |
> |
}}); |
389 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
390 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
391 |
> |
} finally { |
392 |
> |
done.countDown(); |
393 |
> |
joinPool(p); |
394 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
395 |
> |
} |
396 |
|
} |
397 |
|
|
398 |
|
/** |
399 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
400 |
< |
* become active |
399 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
400 |
> |
* become active |
401 |
|
*/ |
402 |
|
public void testGetPoolSize() throws InterruptedException { |
403 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
404 |
< |
assertEquals(0, p1.getPoolSize()); |
405 |
< |
p1.execute(new SmallRunnable()); |
406 |
< |
assertEquals(1, p1.getPoolSize()); |
407 |
< |
joinPool(p1); |
403 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
404 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
405 |
> |
final CountDownLatch done = new CountDownLatch(1); |
406 |
> |
try { |
407 |
> |
assertEquals(0, p.getPoolSize()); |
408 |
> |
p.execute(new CheckedRunnable() { |
409 |
> |
public void realRun() throws InterruptedException { |
410 |
> |
threadStarted.countDown(); |
411 |
> |
assertEquals(1, p.getPoolSize()); |
412 |
> |
done.await(); |
413 |
> |
}}); |
414 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
415 |
> |
assertEquals(1, p.getPoolSize()); |
416 |
> |
} finally { |
417 |
> |
done.countDown(); |
418 |
> |
joinPool(p); |
419 |
> |
} |
420 |
|
} |
421 |
|
|
422 |
|
/** |
423 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks |
424 |
< |
* submitted |
423 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
424 |
> |
* submitted |
425 |
|
*/ |
426 |
|
public void testGetTaskCount() throws InterruptedException { |
427 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
428 |
< |
assertEquals(0, p1.getTaskCount()); |
429 |
< |
for (int i = 0; i < 5; i++) |
430 |
< |
p1.execute(new SmallRunnable()); |
431 |
< |
Thread.sleep(SHORT_DELAY_MS); |
432 |
< |
assertEquals(5, p1.getTaskCount()); |
433 |
< |
joinPool(p1); |
427 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
428 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
429 |
> |
final CountDownLatch done = new CountDownLatch(1); |
430 |
> |
final int TASKS = 5; |
431 |
> |
try { |
432 |
> |
assertEquals(0, p.getTaskCount()); |
433 |
> |
for (int i = 0; i < TASKS; i++) |
434 |
> |
p.execute(new CheckedRunnable() { |
435 |
> |
public void realRun() throws InterruptedException { |
436 |
> |
threadStarted.countDown(); |
437 |
> |
done.await(); |
438 |
> |
}}); |
439 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
440 |
> |
assertEquals(TASKS, p.getTaskCount()); |
441 |
> |
} finally { |
442 |
> |
done.countDown(); |
443 |
> |
joinPool(p); |
444 |
> |
} |
445 |
|
} |
446 |
|
|
447 |
|
/** |
480 |
|
} |
481 |
|
|
482 |
|
/** |
483 |
< |
* is isShutDown is false before shutdown, true after |
483 |
> |
* isShutdown is false before shutdown, true after |
484 |
|
*/ |
485 |
|
public void testIsShutdown() { |
486 |
|
|
487 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
487 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
488 |
|
try { |
489 |
< |
assertFalse(p1.isShutdown()); |
489 |
> |
assertFalse(p.isShutdown()); |
490 |
|
} |
491 |
|
finally { |
492 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
492 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
493 |
|
} |
494 |
< |
assertTrue(p1.isShutdown()); |
494 |
> |
assertTrue(p.isShutdown()); |
495 |
|
} |
496 |
|
|
372 |
– |
|
497 |
|
/** |
498 |
< |
* isTerminated is false before termination, true after |
498 |
> |
* isTerminated is false before termination, true after |
499 |
|
*/ |
500 |
|
public void testIsTerminated() throws InterruptedException { |
501 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
501 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
502 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
503 |
> |
final CountDownLatch done = new CountDownLatch(1); |
504 |
> |
assertFalse(p.isTerminated()); |
505 |
|
try { |
506 |
< |
p1.execute(new SmallRunnable()); |
506 |
> |
p.execute(new CheckedRunnable() { |
507 |
> |
public void realRun() throws InterruptedException { |
508 |
> |
assertFalse(p.isTerminated()); |
509 |
> |
threadStarted.countDown(); |
510 |
> |
done.await(); |
511 |
> |
}}); |
512 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
513 |
> |
assertFalse(p.isTerminating()); |
514 |
> |
done.countDown(); |
515 |
|
} finally { |
516 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
516 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
517 |
|
} |
518 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
519 |
< |
assertTrue(p1.isTerminated()); |
518 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
519 |
> |
assertTrue(p.isTerminated()); |
520 |
|
} |
521 |
|
|
522 |
|
/** |
523 |
< |
* isTerminating is not true when running or when terminated |
523 |
> |
* isTerminating is not true when running or when terminated |
524 |
|
*/ |
525 |
|
public void testIsTerminating() throws InterruptedException { |
526 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
527 |
< |
assertFalse(p1.isTerminating()); |
528 |
< |
try { |
529 |
< |
p1.execute(new SmallRunnable()); |
530 |
< |
assertFalse(p1.isTerminating()); |
531 |
< |
} finally { |
532 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
533 |
< |
} |
534 |
< |
|
535 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
536 |
< |
assertTrue(p1.isTerminated()); |
537 |
< |
assertFalse(p1.isTerminating()); |
526 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
527 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
528 |
> |
final CountDownLatch done = new CountDownLatch(1); |
529 |
> |
try { |
530 |
> |
assertFalse(p.isTerminating()); |
531 |
> |
p.execute(new CheckedRunnable() { |
532 |
> |
public void realRun() throws InterruptedException { |
533 |
> |
assertFalse(p.isTerminating()); |
534 |
> |
threadStarted.countDown(); |
535 |
> |
done.await(); |
536 |
> |
}}); |
537 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
538 |
> |
assertFalse(p.isTerminating()); |
539 |
> |
done.countDown(); |
540 |
> |
} finally { |
541 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
542 |
> |
} |
543 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
544 |
> |
assertTrue(p.isTerminated()); |
545 |
> |
assertFalse(p.isTerminating()); |
546 |
|
} |
547 |
|
|
548 |
|
/** |
549 |
|
* getQueue returns the work queue, which contains queued tasks |
550 |
|
*/ |
551 |
|
public void testGetQueue() throws InterruptedException { |
552 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
553 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
554 |
< |
for (int i = 0; i < 5; i++) { |
412 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
413 |
< |
} |
552 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
553 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
554 |
> |
final CountDownLatch done = new CountDownLatch(1); |
555 |
|
try { |
556 |
< |
Thread.sleep(SHORT_DELAY_MS); |
557 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
558 |
< |
assertTrue(q.contains(tasks[4])); |
556 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
557 |
> |
for (int i = 0; i < tasks.length; i++) { |
558 |
> |
Runnable r = new CheckedRunnable() { |
559 |
> |
public void realRun() throws InterruptedException { |
560 |
> |
threadStarted.countDown(); |
561 |
> |
done.await(); |
562 |
> |
}}; |
563 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
564 |
> |
} |
565 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
566 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
567 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
568 |
|
assertFalse(q.contains(tasks[0])); |
569 |
|
} finally { |
570 |
< |
joinPool(p1); |
570 |
> |
done.countDown(); |
571 |
> |
joinPool(p); |
572 |
|
} |
573 |
|
} |
574 |
|
|
576 |
|
* remove(task) removes queued task, and fails to remove active task |
577 |
|
*/ |
578 |
|
public void testRemove() throws InterruptedException { |
579 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
579 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
580 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
581 |
< |
for (int i = 0; i < 5; i++) { |
582 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
432 |
< |
} |
581 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
582 |
> |
final CountDownLatch done = new CountDownLatch(1); |
583 |
|
try { |
584 |
< |
Thread.sleep(SHORT_DELAY_MS); |
585 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
586 |
< |
assertFalse(p1.remove((Runnable)tasks[0])); |
584 |
> |
for (int i = 0; i < tasks.length; i++) { |
585 |
> |
Runnable r = new CheckedRunnable() { |
586 |
> |
public void realRun() throws InterruptedException { |
587 |
> |
threadStarted.countDown(); |
588 |
> |
done.await(); |
589 |
> |
}}; |
590 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
591 |
> |
} |
592 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
593 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
594 |
> |
assertFalse(p.remove((Runnable)tasks[0])); |
595 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
596 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
597 |
< |
assertTrue(p1.remove((Runnable)tasks[4])); |
598 |
< |
assertFalse(p1.remove((Runnable)tasks[4])); |
597 |
> |
assertTrue(p.remove((Runnable)tasks[4])); |
598 |
> |
assertFalse(p.remove((Runnable)tasks[4])); |
599 |
|
assertFalse(q.contains((Runnable)tasks[4])); |
600 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
601 |
< |
assertTrue(p1.remove((Runnable)tasks[3])); |
601 |
> |
assertTrue(p.remove((Runnable)tasks[3])); |
602 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
603 |
|
} finally { |
604 |
< |
joinPool(p1); |
604 |
> |
done.countDown(); |
605 |
> |
joinPool(p); |
606 |
|
} |
607 |
|
} |
608 |
|
|
609 |
|
/** |
610 |
< |
* purge removes cancelled tasks from the queue |
610 |
> |
* purge eventually removes cancelled tasks from the queue |
611 |
|
*/ |
612 |
|
public void testPurge() throws InterruptedException { |
613 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
613 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
614 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
615 |
< |
for (int i = 0; i < 5; i++) { |
616 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
617 |
< |
} |
615 |
> |
for (int i = 0; i < tasks.length; i++) |
616 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
617 |
> |
LONG_DELAY_MS, MILLISECONDS); |
618 |
|
try { |
619 |
< |
int max = 5; |
619 |
> |
int max = tasks.length; |
620 |
|
if (tasks[4].cancel(true)) --max; |
621 |
|
if (tasks[3].cancel(true)) --max; |
622 |
|
// There must eventually be an interference-free point at |
623 |
|
// which purge will not fail. (At worst, when queue is empty.) |
624 |
< |
int k; |
625 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
626 |
< |
p1.purge(); |
627 |
< |
long count = p1.getTaskCount(); |
628 |
< |
if (count >= 0 && count <= max) |
629 |
< |
break; |
630 |
< |
Thread.sleep(1); |
631 |
< |
} |
473 |
< |
assertTrue(k < SMALL_DELAY_MS); |
624 |
> |
long startTime = System.nanoTime(); |
625 |
> |
do { |
626 |
> |
p.purge(); |
627 |
> |
long count = p.getTaskCount(); |
628 |
> |
if (count == max) |
629 |
> |
return; |
630 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
631 |
> |
fail("Purge failed to remove cancelled tasks"); |
632 |
|
} finally { |
633 |
< |
joinPool(p1); |
633 |
> |
for (ScheduledFuture task : tasks) |
634 |
> |
task.cancel(true); |
635 |
> |
joinPool(p); |
636 |
|
} |
637 |
|
} |
638 |
|
|
639 |
|
/** |
640 |
< |
* shutDownNow returns a list containing tasks that were not run |
640 |
> |
* shutdownNow returns a list containing tasks that were not run |
641 |
|
*/ |
642 |
< |
public void testShutDownNow() throws InterruptedException { |
643 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
642 |
> |
public void testShutdownNow() { |
643 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
644 |
|
for (int i = 0; i < 5; i++) |
645 |
< |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
646 |
< |
List l; |
645 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
646 |
> |
LONG_DELAY_MS, MILLISECONDS); |
647 |
|
try { |
648 |
< |
l = p1.shutdownNow(); |
648 |
> |
List<Runnable> l = p.shutdownNow(); |
649 |
> |
assertTrue(p.isShutdown()); |
650 |
> |
assertEquals(5, l.size()); |
651 |
|
} catch (SecurityException ok) { |
652 |
< |
return; |
652 |
> |
// Allowed in case test doesn't have privs |
653 |
> |
} finally { |
654 |
> |
joinPool(p); |
655 |
|
} |
492 |
– |
assertTrue(p1.isShutdown()); |
493 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
494 |
– |
joinPool(p1); |
656 |
|
} |
657 |
|
|
658 |
|
/** |
659 |
|
* In default setting, shutdown cancels periodic but not delayed |
660 |
|
* tasks at shutdown |
661 |
|
*/ |
662 |
< |
public void testShutDown1() throws InterruptedException { |
663 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
664 |
< |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
665 |
< |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
662 |
> |
public void testShutdown1() throws InterruptedException { |
663 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
664 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
665 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
666 |
|
|
667 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
668 |
< |
for (int i = 0; i < 5; i++) |
669 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
670 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
671 |
< |
BlockingQueue q = p1.getQueue(); |
672 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
673 |
< |
ScheduledFuture t = (ScheduledFuture)it.next(); |
674 |
< |
assertFalse(t.isCancelled()); |
675 |
< |
} |
676 |
< |
assertTrue(p1.isShutdown()); |
677 |
< |
Thread.sleep(SMALL_DELAY_MS); |
678 |
< |
for (int i = 0; i < 5; ++i) { |
679 |
< |
assertTrue(tasks[i].isDone()); |
680 |
< |
assertFalse(tasks[i].isCancelled()); |
668 |
> |
for (int i = 0; i < tasks.length; i++) |
669 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
670 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
671 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
672 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
673 |
> |
for (ScheduledFuture task : tasks) { |
674 |
> |
assertFalse(task.isDone()); |
675 |
> |
assertFalse(task.isCancelled()); |
676 |
> |
assertTrue(q.contains(task)); |
677 |
> |
} |
678 |
> |
assertTrue(p.isShutdown()); |
679 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
680 |
> |
assertTrue(p.isTerminated()); |
681 |
> |
for (ScheduledFuture task : tasks) { |
682 |
> |
assertTrue(task.isDone()); |
683 |
> |
assertFalse(task.isCancelled()); |
684 |
|
} |
685 |
|
} |
686 |
|
|
523 |
– |
|
687 |
|
/** |
688 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
689 |
|
* delayed tasks are cancelled at shutdown |
690 |
|
*/ |
691 |
< |
public void testShutDown2() throws InterruptedException { |
692 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
693 |
< |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
691 |
> |
public void testShutdown2() throws InterruptedException { |
692 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
693 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
694 |
> |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
695 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
696 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
697 |
< |
for (int i = 0; i < 5; i++) |
698 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
699 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
700 |
< |
assertTrue(p1.isShutdown()); |
701 |
< |
BlockingQueue q = p1.getQueue(); |
697 |
> |
for (int i = 0; i < tasks.length; i++) |
698 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
699 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
700 |
> |
BlockingQueue q = p.getQueue(); |
701 |
> |
assertEquals(tasks.length, q.size()); |
702 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
703 |
> |
assertTrue(p.isShutdown()); |
704 |
|
assertTrue(q.isEmpty()); |
705 |
< |
Thread.sleep(SMALL_DELAY_MS); |
706 |
< |
assertTrue(p1.isTerminated()); |
705 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
706 |
> |
assertTrue(p.isTerminated()); |
707 |
> |
for (ScheduledFuture task : tasks) { |
708 |
> |
assertTrue(task.isDone()); |
709 |
> |
assertTrue(task.isCancelled()); |
710 |
> |
} |
711 |
|
} |
712 |
|
|
542 |
– |
|
713 |
|
/** |
714 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
715 |
< |
* periodic tasks are not cancelled at shutdown |
715 |
> |
* periodic tasks are cancelled at shutdown |
716 |
|
*/ |
717 |
< |
public void testShutDown3() throws InterruptedException { |
718 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
719 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
717 |
> |
public void testShutdown3() throws InterruptedException { |
718 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
719 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
720 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
721 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
722 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
723 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
724 |
> |
long initialDelay = LONG_DELAY_MS; |
725 |
|
ScheduledFuture task = |
726 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
727 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
728 |
< |
assertTrue(p1.isShutdown()); |
729 |
< |
BlockingQueue q = p1.getQueue(); |
730 |
< |
assertTrue(q.isEmpty()); |
731 |
< |
Thread.sleep(SHORT_DELAY_MS); |
732 |
< |
assertTrue(p1.isTerminated()); |
726 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
727 |
> |
5, MILLISECONDS); |
728 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
729 |
> |
assertTrue(p.isShutdown()); |
730 |
> |
assertTrue(p.getQueue().isEmpty()); |
731 |
> |
assertTrue(task.isDone()); |
732 |
> |
assertTrue(task.isCancelled()); |
733 |
> |
joinPool(p); |
734 |
|
} |
735 |
|
|
736 |
|
/** |
737 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
738 |
< |
* periodic tasks are cancelled at shutdown |
738 |
> |
* periodic tasks are not cancelled at shutdown |
739 |
|
*/ |
740 |
< |
public void testShutDown4() throws InterruptedException { |
741 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
740 |
> |
public void testShutdown4() throws InterruptedException { |
741 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
742 |
> |
final CountDownLatch counter = new CountDownLatch(2); |
743 |
|
try { |
744 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
744 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
745 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
746 |
> |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
747 |
> |
final Runnable r = new CheckedRunnable() { |
748 |
> |
public void realRun() { |
749 |
> |
counter.countDown(); |
750 |
> |
}}; |
751 |
|
ScheduledFuture task = |
752 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); |
752 |
> |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
753 |
> |
assertFalse(task.isDone()); |
754 |
|
assertFalse(task.isCancelled()); |
755 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
755 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
756 |
|
assertFalse(task.isCancelled()); |
757 |
< |
assertFalse(p1.isTerminated()); |
758 |
< |
assertTrue(p1.isShutdown()); |
759 |
< |
Thread.sleep(SHORT_DELAY_MS); |
757 |
> |
assertFalse(p.isTerminated()); |
758 |
> |
assertTrue(p.isShutdown()); |
759 |
> |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
760 |
|
assertFalse(task.isCancelled()); |
761 |
< |
assertTrue(task.cancel(true)); |
761 |
> |
assertTrue(task.cancel(false)); |
762 |
|
assertTrue(task.isDone()); |
763 |
< |
Thread.sleep(SHORT_DELAY_MS); |
764 |
< |
assertTrue(p1.isTerminated()); |
763 |
> |
assertTrue(task.isCancelled()); |
764 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
765 |
> |
assertTrue(p.isTerminated()); |
766 |
|
} |
767 |
|
finally { |
768 |
< |
joinPool(p1); |
768 |
> |
joinPool(p); |
769 |
|
} |
770 |
|
} |
771 |
|
|
1179 |
|
l.add(new StringTask()); |
1180 |
|
List<Future<String>> futures = |
1181 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1182 |
< |
assertEquals(3, futures.size()); |
1183 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1184 |
< |
Future<String> f1 = it.next(); |
1185 |
< |
Future<String> f2 = it.next(); |
1186 |
< |
Future<String> f3 = it.next(); |
1002 |
< |
assertTrue(f1.isDone()); |
1003 |
< |
assertTrue(f2.isDone()); |
1004 |
< |
assertTrue(f3.isDone()); |
1005 |
< |
assertFalse(f1.isCancelled()); |
1006 |
< |
assertTrue(f2.isCancelled()); |
1182 |
> |
assertEquals(l.size(), futures.size()); |
1183 |
> |
for (Future future : futures) |
1184 |
> |
assertTrue(future.isDone()); |
1185 |
> |
assertFalse(futures.get(0).isCancelled()); |
1186 |
> |
assertTrue(futures.get(1).isCancelled()); |
1187 |
|
} finally { |
1188 |
|
joinPool(e); |
1189 |
|
} |
1190 |
|
} |
1191 |
|
|
1012 |
– |
|
1192 |
|
} |