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