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 |
> |
Runnable task = new CheckedRunnable() { |
150 |
> |
public void realRun() { done.countDown(); }}; |
151 |
> |
ScheduledFuture h = |
152 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
153 |
> |
done.await(); |
154 |
> |
h.cancel(true); |
155 |
> |
double normalizedTime = |
156 |
> |
(double) millisElapsedSince(startTime) / delay; |
157 |
> |
if (normalizedTime >= cycles - 1 && |
158 |
> |
normalizedTime <= cycles) |
159 |
> |
return; |
160 |
> |
} |
161 |
> |
throw new AssertionError("unexpected execution rate"); |
162 |
> |
} finally { |
163 |
> |
joinPool(p); |
164 |
> |
} |
165 |
|
} |
166 |
|
|
167 |
|
/** |
168 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
169 |
|
*/ |
170 |
|
public void testFixedDelaySequence() throws InterruptedException { |
171 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
172 |
< |
RunnableCounter counter = new RunnableCounter(); |
173 |
< |
ScheduledFuture h = |
174 |
< |
p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
175 |
< |
Thread.sleep(SMALL_DELAY_MS); |
176 |
< |
h.cancel(true); |
177 |
< |
int c = counter.count.get(); |
178 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
179 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
180 |
< |
joinPool(p1); |
171 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
172 |
> |
try { |
173 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
174 |
> |
long startTime = System.nanoTime(); |
175 |
> |
int cycles = 10; |
176 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
177 |
> |
Runnable task = new CheckedRunnable() { |
178 |
> |
public void realRun() { done.countDown(); }}; |
179 |
> |
ScheduledFuture h = |
180 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
181 |
> |
done.await(); |
182 |
> |
h.cancel(true); |
183 |
> |
double normalizedTime = |
184 |
> |
(double) millisElapsedSince(startTime) / delay; |
185 |
> |
if (normalizedTime >= cycles - 1 && |
186 |
> |
normalizedTime <= cycles) |
187 |
> |
return; |
188 |
> |
} |
189 |
> |
throw new AssertionError("unexpected execution rate"); |
190 |
> |
} finally { |
191 |
> |
joinPool(p); |
192 |
> |
} |
193 |
|
} |
194 |
|
|
138 |
– |
|
195 |
|
/** |
196 |
< |
* execute(null) throws NPE |
196 |
> |
* execute(null) throws NPE |
197 |
|
*/ |
198 |
|
public void testExecuteNull() throws InterruptedException { |
199 |
|
ScheduledThreadPoolExecutor se = null; |
255 |
|
/** |
256 |
|
* schedule callable throws RejectedExecutionException if shutdown |
257 |
|
*/ |
258 |
< |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
259 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
260 |
< |
try { |
258 |
> |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
259 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
260 |
> |
try { |
261 |
|
se.shutdown(); |
262 |
|
se.schedule(new NoOpCallable(), |
263 |
|
MEDIUM_DELAY_MS, MILLISECONDS); |
265 |
|
} catch (RejectedExecutionException success) { |
266 |
|
} catch (SecurityException ok) { |
267 |
|
} |
268 |
< |
joinPool(se); |
268 |
> |
joinPool(se); |
269 |
|
} |
270 |
|
|
271 |
|
/** |
272 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
272 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
273 |
|
*/ |
274 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
275 |
|
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
301 |
|
} |
302 |
|
|
303 |
|
/** |
304 |
< |
* getActiveCount increases but doesn't overestimate, when a |
305 |
< |
* thread becomes active |
304 |
> |
* getActiveCount increases but doesn't overestimate, when a |
305 |
> |
* thread becomes active |
306 |
|
*/ |
307 |
|
public void testGetActiveCount() throws InterruptedException { |
308 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
309 |
< |
assertEquals(0, p2.getActiveCount()); |
310 |
< |
p2.execute(new SmallRunnable()); |
311 |
< |
Thread.sleep(SHORT_DELAY_MS); |
312 |
< |
assertEquals(1, p2.getActiveCount()); |
313 |
< |
joinPool(p2); |
308 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
309 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
310 |
> |
final CountDownLatch done = new CountDownLatch(1); |
311 |
> |
try { |
312 |
> |
assertEquals(0, p.getActiveCount()); |
313 |
> |
p.execute(new CheckedRunnable() { |
314 |
> |
public void realRun() throws InterruptedException { |
315 |
> |
threadStarted.countDown(); |
316 |
> |
assertEquals(1, p.getActiveCount()); |
317 |
> |
done.await(); |
318 |
> |
}}); |
319 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
320 |
> |
assertEquals(1, p.getActiveCount()); |
321 |
> |
} finally { |
322 |
> |
done.countDown(); |
323 |
> |
joinPool(p); |
324 |
> |
} |
325 |
|
} |
326 |
|
|
327 |
|
/** |
328 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
329 |
< |
* when tasks complete |
328 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
329 |
> |
* when tasks complete |
330 |
|
*/ |
331 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
332 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
333 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
334 |
< |
p2.execute(new SmallRunnable()); |
335 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
336 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
337 |
< |
joinPool(p2); |
332 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
333 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
334 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
335 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
336 |
> |
try { |
337 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
338 |
> |
p.execute(new CheckedRunnable() { |
339 |
> |
public void realRun() throws InterruptedException { |
340 |
> |
threadStarted.countDown(); |
341 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
342 |
> |
threadProceed.await(); |
343 |
> |
threadDone.countDown(); |
344 |
> |
}}); |
345 |
> |
await(threadStarted); |
346 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
347 |
> |
threadProceed.countDown(); |
348 |
> |
threadDone.await(); |
349 |
> |
long startTime = System.nanoTime(); |
350 |
> |
while (p.getCompletedTaskCount() != 1) { |
351 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
352 |
> |
fail("timed out"); |
353 |
> |
Thread.yield(); |
354 |
> |
} |
355 |
> |
} finally { |
356 |
> |
joinPool(p); |
357 |
> |
} |
358 |
|
} |
359 |
|
|
360 |
|
/** |
361 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
361 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
362 |
|
*/ |
363 |
|
public void testGetCorePoolSize() throws InterruptedException { |
364 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
365 |
< |
assertEquals(1, p1.getCorePoolSize()); |
366 |
< |
joinPool(p1); |
364 |
> |
ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
365 |
> |
assertEquals(1, p.getCorePoolSize()); |
366 |
> |
joinPool(p); |
367 |
|
} |
368 |
|
|
369 |
|
/** |
370 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
371 |
< |
* multiple threads active |
370 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
371 |
> |
* multiple threads active |
372 |
|
*/ |
373 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
374 |
< |
ScheduledThreadPoolExecutor p2 = new ScheduledThreadPoolExecutor(2); |
375 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
376 |
< |
p2.execute(new SmallRunnable()); |
377 |
< |
p2.execute(new SmallRunnable()); |
378 |
< |
Thread.sleep(SHORT_DELAY_MS); |
379 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
380 |
< |
joinPool(p2); |
374 |
> |
final int THREADS = 3; |
375 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
376 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
377 |
> |
final CountDownLatch done = new CountDownLatch(1); |
378 |
> |
try { |
379 |
> |
assertEquals(0, p.getLargestPoolSize()); |
380 |
> |
for (int i = 0; i < THREADS; i++) |
381 |
> |
p.execute(new CheckedRunnable() { |
382 |
> |
public void realRun() throws InterruptedException { |
383 |
> |
threadsStarted.countDown(); |
384 |
> |
done.await(); |
385 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
386 |
> |
}}); |
387 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
388 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
389 |
> |
} finally { |
390 |
> |
done.countDown(); |
391 |
> |
joinPool(p); |
392 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
393 |
> |
} |
394 |
|
} |
395 |
|
|
396 |
|
/** |
397 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
398 |
< |
* become active |
397 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
398 |
> |
* become active |
399 |
|
*/ |
400 |
|
public void testGetPoolSize() throws InterruptedException { |
401 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
402 |
< |
assertEquals(0, p1.getPoolSize()); |
403 |
< |
p1.execute(new SmallRunnable()); |
404 |
< |
assertEquals(1, p1.getPoolSize()); |
405 |
< |
joinPool(p1); |
401 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
402 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
403 |
> |
final CountDownLatch done = new CountDownLatch(1); |
404 |
> |
try { |
405 |
> |
assertEquals(0, p.getPoolSize()); |
406 |
> |
p.execute(new CheckedRunnable() { |
407 |
> |
public void realRun() throws InterruptedException { |
408 |
> |
threadStarted.countDown(); |
409 |
> |
assertEquals(1, p.getPoolSize()); |
410 |
> |
done.await(); |
411 |
> |
}}); |
412 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
413 |
> |
assertEquals(1, p.getPoolSize()); |
414 |
> |
} finally { |
415 |
> |
done.countDown(); |
416 |
> |
joinPool(p); |
417 |
> |
} |
418 |
|
} |
419 |
|
|
420 |
|
/** |
421 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks |
422 |
< |
* submitted |
421 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
422 |
> |
* submitted |
423 |
|
*/ |
424 |
|
public void testGetTaskCount() throws InterruptedException { |
425 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
426 |
< |
assertEquals(0, p1.getTaskCount()); |
427 |
< |
for (int i = 0; i < 5; i++) |
428 |
< |
p1.execute(new SmallRunnable()); |
429 |
< |
Thread.sleep(SHORT_DELAY_MS); |
430 |
< |
assertEquals(5, p1.getTaskCount()); |
431 |
< |
joinPool(p1); |
425 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
426 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
427 |
> |
final CountDownLatch done = new CountDownLatch(1); |
428 |
> |
final int TASKS = 5; |
429 |
> |
try { |
430 |
> |
assertEquals(0, p.getTaskCount()); |
431 |
> |
for (int i = 0; i < TASKS; i++) |
432 |
> |
p.execute(new CheckedRunnable() { |
433 |
> |
public void realRun() throws InterruptedException { |
434 |
> |
threadStarted.countDown(); |
435 |
> |
done.await(); |
436 |
> |
}}); |
437 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
438 |
> |
assertEquals(TASKS, p.getTaskCount()); |
439 |
> |
} finally { |
440 |
> |
done.countDown(); |
441 |
> |
joinPool(p); |
442 |
> |
} |
443 |
|
} |
444 |
|
|
445 |
|
/** |
478 |
|
} |
479 |
|
|
480 |
|
/** |
481 |
< |
* is isShutDown is false before shutdown, true after |
481 |
> |
* isShutdown is false before shutdown, true after |
482 |
|
*/ |
483 |
|
public void testIsShutdown() { |
484 |
|
|
485 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
485 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
486 |
|
try { |
487 |
< |
assertFalse(p1.isShutdown()); |
487 |
> |
assertFalse(p.isShutdown()); |
488 |
|
} |
489 |
|
finally { |
490 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
490 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
491 |
|
} |
492 |
< |
assertTrue(p1.isShutdown()); |
492 |
> |
assertTrue(p.isShutdown()); |
493 |
|
} |
494 |
|
|
372 |
– |
|
495 |
|
/** |
496 |
< |
* isTerminated is false before termination, true after |
496 |
> |
* isTerminated is false before termination, true after |
497 |
|
*/ |
498 |
|
public void testIsTerminated() throws InterruptedException { |
499 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
499 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
500 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
501 |
> |
final CountDownLatch done = new CountDownLatch(1); |
502 |
> |
assertFalse(p.isTerminated()); |
503 |
|
try { |
504 |
< |
p1.execute(new SmallRunnable()); |
504 |
> |
p.execute(new CheckedRunnable() { |
505 |
> |
public void realRun() throws InterruptedException { |
506 |
> |
assertFalse(p.isTerminated()); |
507 |
> |
threadStarted.countDown(); |
508 |
> |
done.await(); |
509 |
> |
}}); |
510 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
511 |
> |
assertFalse(p.isTerminating()); |
512 |
> |
done.countDown(); |
513 |
|
} finally { |
514 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
514 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
515 |
|
} |
516 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
517 |
< |
assertTrue(p1.isTerminated()); |
516 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
517 |
> |
assertTrue(p.isTerminated()); |
518 |
|
} |
519 |
|
|
520 |
|
/** |
521 |
< |
* isTerminating is not true when running or when terminated |
521 |
> |
* isTerminating is not true when running or when terminated |
522 |
|
*/ |
523 |
|
public void testIsTerminating() throws InterruptedException { |
524 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
525 |
< |
assertFalse(p1.isTerminating()); |
526 |
< |
try { |
527 |
< |
p1.execute(new SmallRunnable()); |
528 |
< |
assertFalse(p1.isTerminating()); |
529 |
< |
} finally { |
530 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
531 |
< |
} |
532 |
< |
|
533 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
534 |
< |
assertTrue(p1.isTerminated()); |
535 |
< |
assertFalse(p1.isTerminating()); |
524 |
> |
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
525 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
526 |
> |
final CountDownLatch done = new CountDownLatch(1); |
527 |
> |
try { |
528 |
> |
assertFalse(p.isTerminating()); |
529 |
> |
p.execute(new CheckedRunnable() { |
530 |
> |
public void realRun() throws InterruptedException { |
531 |
> |
assertFalse(p.isTerminating()); |
532 |
> |
threadStarted.countDown(); |
533 |
> |
done.await(); |
534 |
> |
}}); |
535 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
536 |
> |
assertFalse(p.isTerminating()); |
537 |
> |
done.countDown(); |
538 |
> |
} finally { |
539 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
540 |
> |
} |
541 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
542 |
> |
assertTrue(p.isTerminated()); |
543 |
> |
assertFalse(p.isTerminating()); |
544 |
|
} |
545 |
|
|
546 |
|
/** |
547 |
|
* getQueue returns the work queue, which contains queued tasks |
548 |
|
*/ |
549 |
|
public void testGetQueue() throws InterruptedException { |
550 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
551 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
552 |
< |
for (int i = 0; i < 5; i++) { |
412 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
413 |
< |
} |
550 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
551 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
552 |
> |
final CountDownLatch done = new CountDownLatch(1); |
553 |
|
try { |
554 |
< |
Thread.sleep(SHORT_DELAY_MS); |
555 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
556 |
< |
assertTrue(q.contains(tasks[4])); |
554 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
555 |
> |
for (int i = 0; i < tasks.length; i++) { |
556 |
> |
Runnable r = new CheckedRunnable() { |
557 |
> |
public void realRun() throws InterruptedException { |
558 |
> |
threadStarted.countDown(); |
559 |
> |
done.await(); |
560 |
> |
}}; |
561 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
562 |
> |
} |
563 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
564 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
565 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
566 |
|
assertFalse(q.contains(tasks[0])); |
567 |
|
} finally { |
568 |
< |
joinPool(p1); |
568 |
> |
done.countDown(); |
569 |
> |
joinPool(p); |
570 |
|
} |
571 |
|
} |
572 |
|
|
574 |
|
* remove(task) removes queued task, and fails to remove active task |
575 |
|
*/ |
576 |
|
public void testRemove() throws InterruptedException { |
577 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
577 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
578 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
579 |
< |
for (int i = 0; i < 5; i++) { |
580 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
432 |
< |
} |
579 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
580 |
> |
final CountDownLatch done = new CountDownLatch(1); |
581 |
|
try { |
582 |
< |
Thread.sleep(SHORT_DELAY_MS); |
583 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
584 |
< |
assertFalse(p1.remove((Runnable)tasks[0])); |
582 |
> |
for (int i = 0; i < tasks.length; i++) { |
583 |
> |
Runnable r = new CheckedRunnable() { |
584 |
> |
public void realRun() throws InterruptedException { |
585 |
> |
threadStarted.countDown(); |
586 |
> |
done.await(); |
587 |
> |
}}; |
588 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
589 |
> |
} |
590 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
591 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
592 |
> |
assertFalse(p.remove((Runnable)tasks[0])); |
593 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
594 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
595 |
< |
assertTrue(p1.remove((Runnable)tasks[4])); |
596 |
< |
assertFalse(p1.remove((Runnable)tasks[4])); |
595 |
> |
assertTrue(p.remove((Runnable)tasks[4])); |
596 |
> |
assertFalse(p.remove((Runnable)tasks[4])); |
597 |
|
assertFalse(q.contains((Runnable)tasks[4])); |
598 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
599 |
< |
assertTrue(p1.remove((Runnable)tasks[3])); |
599 |
> |
assertTrue(p.remove((Runnable)tasks[3])); |
600 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
601 |
|
} finally { |
602 |
< |
joinPool(p1); |
602 |
> |
done.countDown(); |
603 |
> |
joinPool(p); |
604 |
|
} |
605 |
|
} |
606 |
|
|
607 |
|
/** |
608 |
< |
* purge removes cancelled tasks from the queue |
608 |
> |
* purge eventually removes cancelled tasks from the queue |
609 |
|
*/ |
610 |
|
public void testPurge() throws InterruptedException { |
611 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
611 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
612 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
613 |
< |
for (int i = 0; i < 5; i++) { |
614 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
615 |
< |
} |
613 |
> |
for (int i = 0; i < tasks.length; i++) |
614 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
615 |
> |
LONG_DELAY_MS, MILLISECONDS); |
616 |
|
try { |
617 |
< |
int max = 5; |
617 |
> |
int max = tasks.length; |
618 |
|
if (tasks[4].cancel(true)) --max; |
619 |
|
if (tasks[3].cancel(true)) --max; |
620 |
|
// There must eventually be an interference-free point at |
621 |
|
// which purge will not fail. (At worst, when queue is empty.) |
622 |
< |
int k; |
623 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
624 |
< |
p1.purge(); |
625 |
< |
long count = p1.getTaskCount(); |
626 |
< |
if (count >= 0 && count <= max) |
627 |
< |
break; |
628 |
< |
Thread.sleep(1); |
629 |
< |
} |
473 |
< |
assertTrue(k < SMALL_DELAY_MS); |
622 |
> |
long startTime = System.nanoTime(); |
623 |
> |
do { |
624 |
> |
p.purge(); |
625 |
> |
long count = p.getTaskCount(); |
626 |
> |
if (count == max) |
627 |
> |
return; |
628 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
629 |
> |
fail("Purge failed to remove cancelled tasks"); |
630 |
|
} finally { |
631 |
< |
joinPool(p1); |
631 |
> |
for (ScheduledFuture task : tasks) |
632 |
> |
task.cancel(true); |
633 |
> |
joinPool(p); |
634 |
|
} |
635 |
|
} |
636 |
|
|
637 |
|
/** |
638 |
< |
* shutDownNow returns a list containing tasks that were not run |
638 |
> |
* shutdownNow returns a list containing tasks that were not run |
639 |
|
*/ |
640 |
< |
public void testShutDownNow() throws InterruptedException { |
641 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
640 |
> |
public void testShutdownNow() { |
641 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
642 |
|
for (int i = 0; i < 5; i++) |
643 |
< |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
644 |
< |
List l; |
643 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
644 |
> |
LONG_DELAY_MS, MILLISECONDS); |
645 |
|
try { |
646 |
< |
l = p1.shutdownNow(); |
646 |
> |
List<Runnable> l = p.shutdownNow(); |
647 |
> |
assertTrue(p.isShutdown()); |
648 |
> |
assertEquals(5, l.size()); |
649 |
|
} catch (SecurityException ok) { |
650 |
< |
return; |
650 |
> |
// Allowed in case test doesn't have privs |
651 |
> |
} finally { |
652 |
> |
joinPool(p); |
653 |
|
} |
492 |
– |
assertTrue(p1.isShutdown()); |
493 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
494 |
– |
joinPool(p1); |
654 |
|
} |
655 |
|
|
656 |
|
/** |
657 |
|
* In default setting, shutdown cancels periodic but not delayed |
658 |
|
* tasks at shutdown |
659 |
|
*/ |
660 |
< |
public void testShutDown1() throws InterruptedException { |
661 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
662 |
< |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
663 |
< |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
660 |
> |
public void testShutdown1() throws InterruptedException { |
661 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
662 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
663 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
664 |
|
|
665 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
666 |
< |
for (int i = 0; i < 5; i++) |
667 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
668 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
669 |
< |
BlockingQueue q = p1.getQueue(); |
670 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
671 |
< |
ScheduledFuture t = (ScheduledFuture)it.next(); |
672 |
< |
assertFalse(t.isCancelled()); |
673 |
< |
} |
674 |
< |
assertTrue(p1.isShutdown()); |
675 |
< |
Thread.sleep(SMALL_DELAY_MS); |
676 |
< |
for (int i = 0; i < 5; ++i) { |
677 |
< |
assertTrue(tasks[i].isDone()); |
678 |
< |
assertFalse(tasks[i].isCancelled()); |
666 |
> |
for (int i = 0; i < tasks.length; i++) |
667 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
668 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
669 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
670 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
671 |
> |
for (ScheduledFuture task : tasks) { |
672 |
> |
assertFalse(task.isDone()); |
673 |
> |
assertFalse(task.isCancelled()); |
674 |
> |
assertTrue(q.contains(task)); |
675 |
> |
} |
676 |
> |
assertTrue(p.isShutdown()); |
677 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
678 |
> |
assertTrue(p.isTerminated()); |
679 |
> |
for (ScheduledFuture task : tasks) { |
680 |
> |
assertTrue(task.isDone()); |
681 |
> |
assertFalse(task.isCancelled()); |
682 |
|
} |
683 |
|
} |
684 |
|
|
523 |
– |
|
685 |
|
/** |
686 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
687 |
|
* delayed tasks are cancelled at shutdown |
688 |
|
*/ |
689 |
< |
public void testShutDown2() throws InterruptedException { |
690 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
691 |
< |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
689 |
> |
public void testShutdown2() throws InterruptedException { |
690 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
691 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
692 |
> |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
693 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
694 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
695 |
< |
for (int i = 0; i < 5; i++) |
696 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
697 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
698 |
< |
assertTrue(p1.isShutdown()); |
699 |
< |
BlockingQueue q = p1.getQueue(); |
695 |
> |
for (int i = 0; i < tasks.length; i++) |
696 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
697 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
698 |
> |
BlockingQueue q = p.getQueue(); |
699 |
> |
assertEquals(tasks.length, q.size()); |
700 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
701 |
> |
assertTrue(p.isShutdown()); |
702 |
|
assertTrue(q.isEmpty()); |
703 |
< |
Thread.sleep(SMALL_DELAY_MS); |
704 |
< |
assertTrue(p1.isTerminated()); |
703 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
704 |
> |
assertTrue(p.isTerminated()); |
705 |
> |
for (ScheduledFuture task : tasks) { |
706 |
> |
assertTrue(task.isDone()); |
707 |
> |
assertTrue(task.isCancelled()); |
708 |
> |
} |
709 |
|
} |
710 |
|
|
542 |
– |
|
711 |
|
/** |
712 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
713 |
< |
* periodic tasks are not cancelled at shutdown |
713 |
> |
* periodic tasks are cancelled at shutdown |
714 |
|
*/ |
715 |
< |
public void testShutDown3() throws InterruptedException { |
716 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
717 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
715 |
> |
public void testShutdown3() throws InterruptedException { |
716 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
717 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
718 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
719 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
720 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
721 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
722 |
> |
long initialDelay = LONG_DELAY_MS; |
723 |
|
ScheduledFuture task = |
724 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
725 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
726 |
< |
assertTrue(p1.isShutdown()); |
727 |
< |
BlockingQueue q = p1.getQueue(); |
728 |
< |
assertTrue(q.isEmpty()); |
729 |
< |
Thread.sleep(SHORT_DELAY_MS); |
730 |
< |
assertTrue(p1.isTerminated()); |
724 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
725 |
> |
5, MILLISECONDS); |
726 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
727 |
> |
assertTrue(p.isShutdown()); |
728 |
> |
assertTrue(p.getQueue().isEmpty()); |
729 |
> |
assertTrue(task.isDone()); |
730 |
> |
assertTrue(task.isCancelled()); |
731 |
> |
joinPool(p); |
732 |
|
} |
733 |
|
|
734 |
|
/** |
735 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
736 |
< |
* periodic tasks are cancelled at shutdown |
736 |
> |
* periodic tasks are not cancelled at shutdown |
737 |
|
*/ |
738 |
< |
public void testShutDown4() throws InterruptedException { |
739 |
< |
ScheduledThreadPoolExecutor p1 = new ScheduledThreadPoolExecutor(1); |
738 |
> |
public void testShutdown4() throws InterruptedException { |
739 |
> |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
740 |
> |
final CountDownLatch counter = new CountDownLatch(2); |
741 |
|
try { |
742 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
742 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
743 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
744 |
> |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
745 |
> |
final Runnable r = new CheckedRunnable() { |
746 |
> |
public void realRun() { |
747 |
> |
counter.countDown(); |
748 |
> |
}}; |
749 |
|
ScheduledFuture task = |
750 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); |
750 |
> |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
751 |
> |
assertFalse(task.isDone()); |
752 |
|
assertFalse(task.isCancelled()); |
753 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
753 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
754 |
|
assertFalse(task.isCancelled()); |
755 |
< |
assertFalse(p1.isTerminated()); |
756 |
< |
assertTrue(p1.isShutdown()); |
757 |
< |
Thread.sleep(SHORT_DELAY_MS); |
755 |
> |
assertFalse(p.isTerminated()); |
756 |
> |
assertTrue(p.isShutdown()); |
757 |
> |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
758 |
|
assertFalse(task.isCancelled()); |
759 |
< |
assertTrue(task.cancel(true)); |
759 |
> |
assertTrue(task.cancel(false)); |
760 |
|
assertTrue(task.isDone()); |
761 |
< |
Thread.sleep(SHORT_DELAY_MS); |
762 |
< |
assertTrue(p1.isTerminated()); |
761 |
> |
assertTrue(task.isCancelled()); |
762 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
763 |
> |
assertTrue(p.isTerminated()); |
764 |
|
} |
765 |
|
finally { |
766 |
< |
joinPool(p1); |
766 |
> |
joinPool(p); |
767 |
|
} |
768 |
|
} |
769 |
|
|
1177 |
|
l.add(new StringTask()); |
1178 |
|
List<Future<String>> futures = |
1179 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1180 |
< |
assertEquals(3, futures.size()); |
1181 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1182 |
< |
Future<String> f1 = it.next(); |
1183 |
< |
Future<String> f2 = it.next(); |
1184 |
< |
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()); |
1180 |
> |
assertEquals(l.size(), futures.size()); |
1181 |
> |
for (Future future : futures) |
1182 |
> |
assertTrue(future.isDone()); |
1183 |
> |
assertFalse(futures.get(0).isCancelled()); |
1184 |
> |
assertTrue(futures.get(1).isCancelled()); |
1185 |
|
} finally { |
1186 |
|
joinPool(e); |
1187 |
|
} |
1188 |
|
} |
1189 |
|
|
1012 |
– |
|
1190 |
|
} |