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 |
|
*/ |
6 |
|
|
7 |
|
import junit.framework.*; |
8 |
|
import java.util.*; |
9 |
|
import java.util.concurrent.*; |
10 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
11 |
< |
import java.util.concurrent.atomic.*; |
11 |
> |
import java.util.concurrent.atomic.AtomicInteger; |
12 |
|
|
13 |
|
public class ScheduledExecutorSubclassTest extends JSR166TestCase { |
14 |
|
public static void main(String[] args) { |
36 |
|
} |
37 |
|
public boolean isCancelled() { return task.isCancelled(); } |
38 |
|
public boolean isDone() { return task.isDone(); } |
39 |
< |
public V get() throws InterruptedException, ExecutionException { |
39 |
> |
public V get() throws InterruptedException, ExecutionException { |
40 |
|
V v = task.get(); |
41 |
|
assertTrue(ran); |
42 |
|
return v; |
43 |
|
} |
44 |
< |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
44 |
> |
public V get(long time, TimeUnit unit) throws InterruptedException, ExecutionException, TimeoutException { |
45 |
|
V v = task.get(time, unit); |
46 |
|
assertTrue(ran); |
47 |
|
return v; |
48 |
|
} |
49 |
|
} |
50 |
|
|
51 |
– |
|
51 |
|
public class CustomExecutor extends ScheduledThreadPoolExecutor { |
52 |
|
|
53 |
|
protected <V> RunnableScheduledFuture<V> decorateTask(Runnable r, RunnableScheduledFuture<V> task) { |
57 |
|
protected <V> RunnableScheduledFuture<V> decorateTask(Callable<V> c, RunnableScheduledFuture<V> task) { |
58 |
|
return new CustomTask<V>(task); |
59 |
|
} |
60 |
< |
CustomExecutor(int corePoolSize) { super(corePoolSize);} |
60 |
> |
CustomExecutor(int corePoolSize) { super(corePoolSize); } |
61 |
|
CustomExecutor(int corePoolSize, RejectedExecutionHandler handler) { |
62 |
|
super(corePoolSize, handler); |
63 |
|
} |
72 |
|
|
73 |
|
} |
74 |
|
|
76 |
– |
|
75 |
|
/** |
76 |
|
* execute successfully executes a runnable |
77 |
|
*/ |
78 |
|
public void testExecute() throws InterruptedException { |
79 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
80 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
81 |
< |
p1.execute(runnable); |
82 |
< |
assertFalse(runnable.done); |
83 |
< |
Thread.sleep(SHORT_DELAY_MS); |
84 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
85 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
86 |
< |
assertTrue(runnable.done); |
87 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
88 |
< |
joinPool(p1); |
79 |
> |
CustomExecutor p = new CustomExecutor(1); |
80 |
> |
final CountDownLatch done = new CountDownLatch(1); |
81 |
> |
final Runnable task = new CheckedRunnable() { |
82 |
> |
public void realRun() { |
83 |
> |
done.countDown(); |
84 |
> |
}}; |
85 |
> |
try { |
86 |
> |
p.execute(task); |
87 |
> |
assertTrue(done.await(SMALL_DELAY_MS, MILLISECONDS)); |
88 |
> |
} finally { |
89 |
> |
joinPool(p); |
90 |
> |
} |
91 |
|
} |
92 |
|
|
93 |
– |
|
93 |
|
/** |
94 |
|
* delayed schedule of callable successfully executes after delay |
95 |
|
*/ |
96 |
|
public void testSchedule1() throws Exception { |
97 |
< |
TrackedCallable callable = new TrackedCallable(); |
98 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
99 |
< |
Future f = p1.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); |
100 |
< |
assertFalse(callable.done); |
101 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
102 |
< |
assertTrue(callable.done); |
103 |
< |
assertEquals(Boolean.TRUE, f.get()); |
104 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
105 |
< |
joinPool(p1); |
97 |
> |
CustomExecutor p = new CustomExecutor(1); |
98 |
> |
final long startTime = System.nanoTime(); |
99 |
> |
final CountDownLatch done = new CountDownLatch(1); |
100 |
> |
try { |
101 |
> |
Callable task = new CheckedCallable<Boolean>() { |
102 |
> |
public Boolean realCall() { |
103 |
> |
done.countDown(); |
104 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
105 |
> |
return Boolean.TRUE; |
106 |
> |
}}; |
107 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
108 |
> |
assertSame(Boolean.TRUE, f.get()); |
109 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
110 |
> |
assertTrue(done.await(0L, MILLISECONDS)); |
111 |
> |
} finally { |
112 |
> |
joinPool(p); |
113 |
> |
} |
114 |
|
} |
115 |
|
|
116 |
|
/** |
117 |
< |
* delayed schedule of runnable successfully executes after delay |
118 |
< |
*/ |
119 |
< |
public void testSchedule3() throws InterruptedException { |
120 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
121 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
122 |
< |
p1.schedule(runnable, SMALL_DELAY_MS, MILLISECONDS); |
123 |
< |
Thread.sleep(SHORT_DELAY_MS); |
124 |
< |
assertFalse(runnable.done); |
125 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
126 |
< |
assertTrue(runnable.done); |
127 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
128 |
< |
joinPool(p1); |
117 |
> |
* delayed schedule of runnable successfully executes after delay |
118 |
> |
*/ |
119 |
> |
public void testSchedule3() throws Exception { |
120 |
> |
CustomExecutor p = new CustomExecutor(1); |
121 |
> |
final long startTime = System.nanoTime(); |
122 |
> |
final CountDownLatch done = new CountDownLatch(1); |
123 |
> |
try { |
124 |
> |
Runnable task = new CheckedRunnable() { |
125 |
> |
public void realRun() { |
126 |
> |
done.countDown(); |
127 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
128 |
> |
}}; |
129 |
> |
Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); |
130 |
> |
await(done); |
131 |
> |
assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); |
132 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
133 |
> |
} finally { |
134 |
> |
joinPool(p); |
135 |
> |
} |
136 |
|
} |
137 |
|
|
138 |
|
/** |
139 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
140 |
|
*/ |
141 |
|
public void testSchedule4() throws InterruptedException { |
142 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
143 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
144 |
< |
ScheduledFuture h = p1.scheduleAtFixedRate(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
145 |
< |
assertFalse(runnable.done); |
146 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
147 |
< |
assertTrue(runnable.done); |
148 |
< |
h.cancel(true); |
149 |
< |
joinPool(p1); |
150 |
< |
} |
151 |
< |
|
152 |
< |
static class RunnableCounter implements Runnable { |
153 |
< |
AtomicInteger count = new AtomicInteger(0); |
154 |
< |
public void run() { count.getAndIncrement(); } |
142 |
> |
CustomExecutor p = new CustomExecutor(1); |
143 |
> |
final long startTime = System.nanoTime(); |
144 |
> |
final CountDownLatch done = new CountDownLatch(1); |
145 |
> |
try { |
146 |
> |
Runnable task = new CheckedRunnable() { |
147 |
> |
public void realRun() { |
148 |
> |
done.countDown(); |
149 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
150 |
> |
}}; |
151 |
> |
ScheduledFuture f = |
152 |
> |
p.scheduleAtFixedRate(task, timeoutMillis(), |
153 |
> |
LONG_DELAY_MS, MILLISECONDS); |
154 |
> |
await(done); |
155 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
156 |
> |
f.cancel(true); |
157 |
> |
} finally { |
158 |
> |
joinPool(p); |
159 |
> |
} |
160 |
|
} |
161 |
|
|
162 |
|
/** |
163 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
164 |
|
*/ |
165 |
|
public void testSchedule5() throws InterruptedException { |
166 |
< |
TrackedShortRunnable runnable = new TrackedShortRunnable(); |
167 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
168 |
< |
ScheduledFuture h = p1.scheduleWithFixedDelay(runnable, SHORT_DELAY_MS, SHORT_DELAY_MS, MILLISECONDS); |
169 |
< |
assertFalse(runnable.done); |
170 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
171 |
< |
assertTrue(runnable.done); |
172 |
< |
h.cancel(true); |
173 |
< |
joinPool(p1); |
166 |
> |
CustomExecutor p = new CustomExecutor(1); |
167 |
> |
final long startTime = System.nanoTime(); |
168 |
> |
final CountDownLatch done = new CountDownLatch(1); |
169 |
> |
try { |
170 |
> |
Runnable task = new CheckedRunnable() { |
171 |
> |
public void realRun() { |
172 |
> |
done.countDown(); |
173 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
174 |
> |
}}; |
175 |
> |
ScheduledFuture f = |
176 |
> |
p.scheduleWithFixedDelay(task, timeoutMillis(), |
177 |
> |
LONG_DELAY_MS, MILLISECONDS); |
178 |
> |
await(done); |
179 |
> |
assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); |
180 |
> |
f.cancel(true); |
181 |
> |
} finally { |
182 |
> |
joinPool(p); |
183 |
> |
} |
184 |
> |
} |
185 |
> |
|
186 |
> |
static class RunnableCounter implements Runnable { |
187 |
> |
AtomicInteger count = new AtomicInteger(0); |
188 |
> |
public void run() { count.getAndIncrement(); } |
189 |
|
} |
190 |
|
|
191 |
|
/** |
192 |
|
* scheduleAtFixedRate executes series of tasks at given rate |
193 |
|
*/ |
194 |
|
public void testFixedRateSequence() throws InterruptedException { |
195 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
196 |
< |
RunnableCounter counter = new RunnableCounter(); |
197 |
< |
ScheduledFuture h = |
198 |
< |
p1.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
199 |
< |
Thread.sleep(SMALL_DELAY_MS); |
200 |
< |
h.cancel(true); |
201 |
< |
int c = counter.count.get(); |
202 |
< |
// By time scaling conventions, we must have at least |
203 |
< |
// an execution per SHORT delay, but no more than one SHORT more |
204 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
205 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
206 |
< |
joinPool(p1); |
195 |
> |
CustomExecutor p = new CustomExecutor(1); |
196 |
> |
try { |
197 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
198 |
> |
long startTime = System.nanoTime(); |
199 |
> |
int cycles = 10; |
200 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
201 |
> |
Runnable task = new CheckedRunnable() { |
202 |
> |
public void realRun() { done.countDown(); }}; |
203 |
> |
ScheduledFuture h = |
204 |
> |
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
205 |
> |
done.await(); |
206 |
> |
h.cancel(true); |
207 |
> |
double normalizedTime = |
208 |
> |
(double) millisElapsedSince(startTime) / delay; |
209 |
> |
if (normalizedTime >= cycles - 1 && |
210 |
> |
normalizedTime <= cycles) |
211 |
> |
return; |
212 |
> |
} |
213 |
> |
throw new AssertionError("unexpected execution rate"); |
214 |
> |
} finally { |
215 |
> |
joinPool(p); |
216 |
> |
} |
217 |
|
} |
218 |
|
|
219 |
|
/** |
220 |
|
* scheduleWithFixedDelay executes series of tasks with given period |
221 |
|
*/ |
222 |
|
public void testFixedDelaySequence() throws InterruptedException { |
223 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
224 |
< |
RunnableCounter counter = new RunnableCounter(); |
225 |
< |
ScheduledFuture h = |
226 |
< |
p1.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
227 |
< |
Thread.sleep(SMALL_DELAY_MS); |
228 |
< |
h.cancel(true); |
229 |
< |
int c = counter.count.get(); |
230 |
< |
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
231 |
< |
assertTrue(c <= SMALL_DELAY_MS + SHORT_DELAY_MS); |
232 |
< |
joinPool(p1); |
223 |
> |
CustomExecutor p = new CustomExecutor(1); |
224 |
> |
try { |
225 |
> |
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
226 |
> |
long startTime = System.nanoTime(); |
227 |
> |
int cycles = 10; |
228 |
> |
final CountDownLatch done = new CountDownLatch(cycles); |
229 |
> |
Runnable task = new CheckedRunnable() { |
230 |
> |
public void realRun() { done.countDown(); }}; |
231 |
> |
ScheduledFuture h = |
232 |
> |
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
233 |
> |
done.await(); |
234 |
> |
h.cancel(true); |
235 |
> |
double normalizedTime = |
236 |
> |
(double) millisElapsedSince(startTime) / delay; |
237 |
> |
if (normalizedTime >= cycles - 1 && |
238 |
> |
normalizedTime <= cycles) |
239 |
> |
return; |
240 |
> |
} |
241 |
> |
throw new AssertionError("unexpected execution rate"); |
242 |
> |
} finally { |
243 |
> |
joinPool(p); |
244 |
> |
} |
245 |
|
} |
246 |
|
|
191 |
– |
|
247 |
|
/** |
248 |
|
* execute(null) throws NPE |
249 |
|
*/ |
305 |
|
/** |
306 |
|
* schedule callable throws RejectedExecutionException if shutdown |
307 |
|
*/ |
308 |
< |
public void testSchedule3_RejectedExecutionException() { |
309 |
< |
CustomExecutor se = new CustomExecutor(1); |
310 |
< |
try { |
311 |
< |
se.shutdown(); |
312 |
< |
se.schedule(new NoOpCallable(), |
313 |
< |
MEDIUM_DELAY_MS, MILLISECONDS); |
314 |
< |
shouldThrow(); |
315 |
< |
} catch (RejectedExecutionException success) { |
316 |
< |
} catch (SecurityException ok) { |
317 |
< |
} |
318 |
< |
joinPool(se); |
308 |
> |
public void testSchedule3_RejectedExecutionException() { |
309 |
> |
CustomExecutor se = new CustomExecutor(1); |
310 |
> |
try { |
311 |
> |
se.shutdown(); |
312 |
> |
se.schedule(new NoOpCallable(), |
313 |
> |
MEDIUM_DELAY_MS, MILLISECONDS); |
314 |
> |
shouldThrow(); |
315 |
> |
} catch (RejectedExecutionException success) { |
316 |
> |
} catch (SecurityException ok) { |
317 |
> |
} |
318 |
> |
joinPool(se); |
319 |
|
} |
320 |
|
|
321 |
|
/** |
322 |
< |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
322 |
> |
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
323 |
|
*/ |
324 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() { |
325 |
|
CustomExecutor se = new CustomExecutor(1); |
351 |
|
} |
352 |
|
|
353 |
|
/** |
354 |
< |
* getActiveCount increases but doesn't overestimate, when a |
355 |
< |
* thread becomes active |
354 |
> |
* getActiveCount increases but doesn't overestimate, when a |
355 |
> |
* thread becomes active |
356 |
|
*/ |
357 |
|
public void testGetActiveCount() throws InterruptedException { |
358 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
359 |
< |
assertEquals(0, p2.getActiveCount()); |
360 |
< |
p2.execute(new SmallRunnable()); |
361 |
< |
Thread.sleep(SHORT_DELAY_MS); |
362 |
< |
assertEquals(1, p2.getActiveCount()); |
363 |
< |
joinPool(p2); |
358 |
> |
final ThreadPoolExecutor p = new CustomExecutor(2); |
359 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
360 |
> |
final CountDownLatch done = new CountDownLatch(1); |
361 |
> |
try { |
362 |
> |
assertEquals(0, p.getActiveCount()); |
363 |
> |
p.execute(new CheckedRunnable() { |
364 |
> |
public void realRun() throws InterruptedException { |
365 |
> |
threadStarted.countDown(); |
366 |
> |
assertEquals(1, p.getActiveCount()); |
367 |
> |
done.await(); |
368 |
> |
}}); |
369 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
370 |
> |
assertEquals(1, p.getActiveCount()); |
371 |
> |
} finally { |
372 |
> |
done.countDown(); |
373 |
> |
joinPool(p); |
374 |
> |
} |
375 |
|
} |
376 |
|
|
377 |
|
/** |
378 |
< |
* getCompletedTaskCount increases, but doesn't overestimate, |
379 |
< |
* when tasks complete |
378 |
> |
* getCompletedTaskCount increases, but doesn't overestimate, |
379 |
> |
* when tasks complete |
380 |
|
*/ |
381 |
|
public void testGetCompletedTaskCount() throws InterruptedException { |
382 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
383 |
< |
assertEquals(0, p2.getCompletedTaskCount()); |
384 |
< |
p2.execute(new SmallRunnable()); |
385 |
< |
Thread.sleep(MEDIUM_DELAY_MS); |
386 |
< |
assertEquals(1, p2.getCompletedTaskCount()); |
387 |
< |
joinPool(p2); |
382 |
> |
final ThreadPoolExecutor p = new CustomExecutor(2); |
383 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
384 |
> |
final CountDownLatch threadProceed = new CountDownLatch(1); |
385 |
> |
final CountDownLatch threadDone = new CountDownLatch(1); |
386 |
> |
try { |
387 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
388 |
> |
p.execute(new CheckedRunnable() { |
389 |
> |
public void realRun() throws InterruptedException { |
390 |
> |
threadStarted.countDown(); |
391 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
392 |
> |
threadProceed.await(); |
393 |
> |
threadDone.countDown(); |
394 |
> |
}}); |
395 |
> |
await(threadStarted); |
396 |
> |
assertEquals(0, p.getCompletedTaskCount()); |
397 |
> |
threadProceed.countDown(); |
398 |
> |
threadDone.await(); |
399 |
> |
long startTime = System.nanoTime(); |
400 |
> |
while (p.getCompletedTaskCount() != 1) { |
401 |
> |
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
402 |
> |
fail("timed out"); |
403 |
> |
Thread.yield(); |
404 |
> |
} |
405 |
> |
} finally { |
406 |
> |
joinPool(p); |
407 |
> |
} |
408 |
|
} |
409 |
|
|
410 |
|
/** |
411 |
< |
* getCorePoolSize returns size given in constructor if not otherwise set |
411 |
> |
* getCorePoolSize returns size given in constructor if not otherwise set |
412 |
|
*/ |
413 |
|
public void testGetCorePoolSize() { |
414 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
415 |
< |
assertEquals(1, p1.getCorePoolSize()); |
416 |
< |
joinPool(p1); |
414 |
> |
CustomExecutor p = new CustomExecutor(1); |
415 |
> |
assertEquals(1, p.getCorePoolSize()); |
416 |
> |
joinPool(p); |
417 |
|
} |
418 |
|
|
419 |
|
/** |
420 |
< |
* getLargestPoolSize increases, but doesn't overestimate, when |
421 |
< |
* multiple threads active |
420 |
> |
* getLargestPoolSize increases, but doesn't overestimate, when |
421 |
> |
* multiple threads active |
422 |
|
*/ |
423 |
|
public void testGetLargestPoolSize() throws InterruptedException { |
424 |
< |
CustomExecutor p2 = new CustomExecutor(2); |
425 |
< |
assertEquals(0, p2.getLargestPoolSize()); |
426 |
< |
p2.execute(new SmallRunnable()); |
427 |
< |
p2.execute(new SmallRunnable()); |
428 |
< |
Thread.sleep(SHORT_DELAY_MS); |
429 |
< |
assertEquals(2, p2.getLargestPoolSize()); |
430 |
< |
joinPool(p2); |
424 |
> |
final int THREADS = 3; |
425 |
> |
final ThreadPoolExecutor p = new CustomExecutor(THREADS); |
426 |
> |
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
427 |
> |
final CountDownLatch done = new CountDownLatch(1); |
428 |
> |
try { |
429 |
> |
assertEquals(0, p.getLargestPoolSize()); |
430 |
> |
for (int i = 0; i < THREADS; i++) |
431 |
> |
p.execute(new CheckedRunnable() { |
432 |
> |
public void realRun() throws InterruptedException { |
433 |
> |
threadsStarted.countDown(); |
434 |
> |
done.await(); |
435 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
436 |
> |
}}); |
437 |
> |
assertTrue(threadsStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
438 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
439 |
> |
} finally { |
440 |
> |
done.countDown(); |
441 |
> |
joinPool(p); |
442 |
> |
assertEquals(THREADS, p.getLargestPoolSize()); |
443 |
> |
} |
444 |
|
} |
445 |
|
|
446 |
|
/** |
447 |
< |
* getPoolSize increases, but doesn't overestimate, when threads |
448 |
< |
* become active |
447 |
> |
* getPoolSize increases, but doesn't overestimate, when threads |
448 |
> |
* become active |
449 |
|
*/ |
450 |
< |
public void testGetPoolSize() { |
451 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
452 |
< |
assertEquals(0, p1.getPoolSize()); |
453 |
< |
p1.execute(new SmallRunnable()); |
454 |
< |
assertEquals(1, p1.getPoolSize()); |
455 |
< |
joinPool(p1); |
450 |
> |
public void testGetPoolSize() throws InterruptedException { |
451 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
452 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
453 |
> |
final CountDownLatch done = new CountDownLatch(1); |
454 |
> |
try { |
455 |
> |
assertEquals(0, p.getPoolSize()); |
456 |
> |
p.execute(new CheckedRunnable() { |
457 |
> |
public void realRun() throws InterruptedException { |
458 |
> |
threadStarted.countDown(); |
459 |
> |
assertEquals(1, p.getPoolSize()); |
460 |
> |
done.await(); |
461 |
> |
}}); |
462 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
463 |
> |
assertEquals(1, p.getPoolSize()); |
464 |
> |
} finally { |
465 |
> |
done.countDown(); |
466 |
> |
joinPool(p); |
467 |
> |
} |
468 |
|
} |
469 |
|
|
470 |
|
/** |
471 |
< |
* getTaskCount increases, but doesn't overestimate, when tasks |
472 |
< |
* submitted |
471 |
> |
* getTaskCount increases, but doesn't overestimate, when tasks |
472 |
> |
* submitted |
473 |
|
*/ |
474 |
|
public void testGetTaskCount() throws InterruptedException { |
475 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
476 |
< |
assertEquals(0, p1.getTaskCount()); |
477 |
< |
for (int i = 0; i < 5; i++) |
478 |
< |
p1.execute(new SmallRunnable()); |
479 |
< |
Thread.sleep(SHORT_DELAY_MS); |
480 |
< |
assertEquals(5, p1.getTaskCount()); |
481 |
< |
joinPool(p1); |
475 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
476 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
477 |
> |
final CountDownLatch done = new CountDownLatch(1); |
478 |
> |
final int TASKS = 5; |
479 |
> |
try { |
480 |
> |
assertEquals(0, p.getTaskCount()); |
481 |
> |
for (int i = 0; i < TASKS; i++) |
482 |
> |
p.execute(new CheckedRunnable() { |
483 |
> |
public void realRun() throws InterruptedException { |
484 |
> |
threadStarted.countDown(); |
485 |
> |
done.await(); |
486 |
> |
}}); |
487 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
488 |
> |
assertEquals(TASKS, p.getTaskCount()); |
489 |
> |
} finally { |
490 |
> |
done.countDown(); |
491 |
> |
joinPool(p); |
492 |
> |
} |
493 |
|
} |
494 |
|
|
495 |
|
/** |
528 |
|
} |
529 |
|
|
530 |
|
/** |
531 |
< |
* is isShutDown is false before shutdown, true after |
531 |
> |
* isShutdown is false before shutdown, true after |
532 |
|
*/ |
533 |
|
public void testIsShutdown() { |
534 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
534 |
> |
CustomExecutor p = new CustomExecutor(1); |
535 |
|
try { |
536 |
< |
assertFalse(p1.isShutdown()); |
536 |
> |
assertFalse(p.isShutdown()); |
537 |
|
} |
538 |
|
finally { |
539 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
539 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
540 |
|
} |
541 |
< |
assertTrue(p1.isShutdown()); |
541 |
> |
assertTrue(p.isShutdown()); |
542 |
|
} |
543 |
|
|
422 |
– |
|
544 |
|
/** |
545 |
< |
* isTerminated is false before termination, true after |
545 |
> |
* isTerminated is false before termination, true after |
546 |
|
*/ |
547 |
|
public void testIsTerminated() throws InterruptedException { |
548 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
548 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
549 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
550 |
> |
final CountDownLatch done = new CountDownLatch(1); |
551 |
> |
assertFalse(p.isTerminated()); |
552 |
|
try { |
553 |
< |
p1.execute(new SmallRunnable()); |
553 |
> |
p.execute(new CheckedRunnable() { |
554 |
> |
public void realRun() throws InterruptedException { |
555 |
> |
assertFalse(p.isTerminated()); |
556 |
> |
threadStarted.countDown(); |
557 |
> |
done.await(); |
558 |
> |
}}); |
559 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
560 |
> |
assertFalse(p.isTerminating()); |
561 |
> |
done.countDown(); |
562 |
|
} finally { |
563 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
563 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
564 |
|
} |
565 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
566 |
< |
assertTrue(p1.isTerminated()); |
565 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
566 |
> |
assertTrue(p.isTerminated()); |
567 |
|
} |
568 |
|
|
569 |
|
/** |
570 |
< |
* isTerminating is not true when running or when terminated |
570 |
> |
* isTerminating is not true when running or when terminated |
571 |
|
*/ |
572 |
|
public void testIsTerminating() throws InterruptedException { |
573 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
574 |
< |
assertFalse(p1.isTerminating()); |
575 |
< |
try { |
576 |
< |
p1.execute(new SmallRunnable()); |
577 |
< |
assertFalse(p1.isTerminating()); |
578 |
< |
} finally { |
579 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
580 |
< |
} |
581 |
< |
assertTrue(p1.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
582 |
< |
assertTrue(p1.isTerminated()); |
583 |
< |
assertFalse(p1.isTerminating()); |
573 |
> |
final ThreadPoolExecutor p = new CustomExecutor(1); |
574 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
575 |
> |
final CountDownLatch done = new CountDownLatch(1); |
576 |
> |
try { |
577 |
> |
assertFalse(p.isTerminating()); |
578 |
> |
p.execute(new CheckedRunnable() { |
579 |
> |
public void realRun() throws InterruptedException { |
580 |
> |
assertFalse(p.isTerminating()); |
581 |
> |
threadStarted.countDown(); |
582 |
> |
done.await(); |
583 |
> |
}}); |
584 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
585 |
> |
assertFalse(p.isTerminating()); |
586 |
> |
done.countDown(); |
587 |
> |
} finally { |
588 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
589 |
> |
} |
590 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
591 |
> |
assertTrue(p.isTerminated()); |
592 |
> |
assertFalse(p.isTerminating()); |
593 |
|
} |
594 |
|
|
595 |
|
/** |
596 |
|
* getQueue returns the work queue, which contains queued tasks |
597 |
|
*/ |
598 |
|
public void testGetQueue() throws InterruptedException { |
599 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
600 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
601 |
< |
for (int i = 0; i < 5; i++) { |
602 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
603 |
< |
} |
604 |
< |
try { |
605 |
< |
Thread.sleep(SHORT_DELAY_MS); |
606 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
607 |
< |
assertTrue(q.contains(tasks[4])); |
599 |
> |
ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
600 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
601 |
> |
final CountDownLatch done = new CountDownLatch(1); |
602 |
> |
try { |
603 |
> |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
604 |
> |
for (int i = 0; i < tasks.length; i++) { |
605 |
> |
Runnable r = new CheckedRunnable() { |
606 |
> |
public void realRun() throws InterruptedException { |
607 |
> |
threadStarted.countDown(); |
608 |
> |
done.await(); |
609 |
> |
}}; |
610 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
611 |
> |
} |
612 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
613 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
614 |
> |
assertTrue(q.contains(tasks[tasks.length - 1])); |
615 |
|
assertFalse(q.contains(tasks[0])); |
616 |
|
} finally { |
617 |
< |
joinPool(p1); |
617 |
> |
done.countDown(); |
618 |
> |
joinPool(p); |
619 |
|
} |
620 |
|
} |
621 |
|
|
623 |
|
* remove(task) removes queued task, and fails to remove active task |
624 |
|
*/ |
625 |
|
public void testRemove() throws InterruptedException { |
626 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
626 |
> |
final ScheduledThreadPoolExecutor p = new CustomExecutor(1); |
627 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
628 |
< |
for (int i = 0; i < 5; i++) { |
629 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), 1, MILLISECONDS); |
481 |
< |
} |
628 |
> |
final CountDownLatch threadStarted = new CountDownLatch(1); |
629 |
> |
final CountDownLatch done = new CountDownLatch(1); |
630 |
|
try { |
631 |
< |
Thread.sleep(SHORT_DELAY_MS); |
632 |
< |
BlockingQueue<Runnable> q = p1.getQueue(); |
633 |
< |
assertFalse(p1.remove((Runnable)tasks[0])); |
631 |
> |
for (int i = 0; i < tasks.length; i++) { |
632 |
> |
Runnable r = new CheckedRunnable() { |
633 |
> |
public void realRun() throws InterruptedException { |
634 |
> |
threadStarted.countDown(); |
635 |
> |
done.await(); |
636 |
> |
}}; |
637 |
> |
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
638 |
> |
} |
639 |
> |
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
640 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
641 |
> |
assertFalse(p.remove((Runnable)tasks[0])); |
642 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
643 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
644 |
< |
assertTrue(p1.remove((Runnable)tasks[4])); |
645 |
< |
assertFalse(p1.remove((Runnable)tasks[4])); |
644 |
> |
assertTrue(p.remove((Runnable)tasks[4])); |
645 |
> |
assertFalse(p.remove((Runnable)tasks[4])); |
646 |
|
assertFalse(q.contains((Runnable)tasks[4])); |
647 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
648 |
< |
assertTrue(p1.remove((Runnable)tasks[3])); |
648 |
> |
assertTrue(p.remove((Runnable)tasks[3])); |
649 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
650 |
|
} finally { |
651 |
< |
joinPool(p1); |
651 |
> |
done.countDown(); |
652 |
> |
joinPool(p); |
653 |
|
} |
654 |
|
} |
655 |
|
|
656 |
|
/** |
657 |
< |
* purge removes cancelled tasks from the queue |
657 |
> |
* purge removes cancelled tasks from the queue |
658 |
|
*/ |
659 |
|
public void testPurge() throws InterruptedException { |
660 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
660 |
> |
CustomExecutor p = new CustomExecutor(1); |
661 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
662 |
< |
for (int i = 0; i < 5; i++) { |
663 |
< |
tasks[i] = p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
664 |
< |
} |
662 |
> |
for (int i = 0; i < tasks.length; i++) |
663 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
664 |
> |
LONG_DELAY_MS, MILLISECONDS); |
665 |
|
try { |
666 |
< |
int max = 5; |
666 |
> |
int max = tasks.length; |
667 |
|
if (tasks[4].cancel(true)) --max; |
668 |
|
if (tasks[3].cancel(true)) --max; |
669 |
|
// There must eventually be an interference-free point at |
670 |
|
// which purge will not fail. (At worst, when queue is empty.) |
671 |
< |
int k; |
672 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
673 |
< |
p1.purge(); |
674 |
< |
long count = p1.getTaskCount(); |
675 |
< |
if (count >= 0 && count <= max) |
676 |
< |
break; |
677 |
< |
Thread.sleep(1); |
678 |
< |
} |
522 |
< |
assertTrue(k < SMALL_DELAY_MS); |
671 |
> |
long startTime = System.nanoTime(); |
672 |
> |
do { |
673 |
> |
p.purge(); |
674 |
> |
long count = p.getTaskCount(); |
675 |
> |
if (count == max) |
676 |
> |
return; |
677 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
678 |
> |
fail("Purge failed to remove cancelled tasks"); |
679 |
|
} finally { |
680 |
< |
joinPool(p1); |
680 |
> |
for (ScheduledFuture task : tasks) |
681 |
> |
task.cancel(true); |
682 |
> |
joinPool(p); |
683 |
|
} |
684 |
|
} |
685 |
|
|
686 |
|
/** |
687 |
< |
* shutDownNow returns a list containing tasks that were not run |
687 |
> |
* shutdownNow returns a list containing tasks that were not run |
688 |
|
*/ |
689 |
< |
public void testShutDownNow() { |
690 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
689 |
> |
public void testShutdownNow() { |
690 |
> |
CustomExecutor p = new CustomExecutor(1); |
691 |
|
for (int i = 0; i < 5; i++) |
692 |
< |
p1.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
693 |
< |
List l; |
692 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
693 |
> |
LONG_DELAY_MS, MILLISECONDS); |
694 |
|
try { |
695 |
< |
l = p1.shutdownNow(); |
695 |
> |
List<Runnable> l = p.shutdownNow(); |
696 |
> |
assertTrue(p.isShutdown()); |
697 |
> |
assertEquals(5, l.size()); |
698 |
|
} catch (SecurityException ok) { |
699 |
< |
return; |
699 |
> |
// Allowed in case test doesn't have privs |
700 |
> |
} finally { |
701 |
> |
joinPool(p); |
702 |
|
} |
541 |
– |
assertTrue(p1.isShutdown()); |
542 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
543 |
– |
joinPool(p1); |
703 |
|
} |
704 |
|
|
705 |
|
/** |
706 |
|
* In default setting, shutdown cancels periodic but not delayed |
707 |
|
* tasks at shutdown |
708 |
|
*/ |
709 |
< |
public void testShutDown1() throws InterruptedException { |
710 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
711 |
< |
assertTrue(p1.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
712 |
< |
assertFalse(p1.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
709 |
> |
public void testShutdown1() throws InterruptedException { |
710 |
> |
CustomExecutor p = new CustomExecutor(1); |
711 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
712 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
713 |
|
|
714 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
715 |
< |
for (int i = 0; i < 5; i++) |
716 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
717 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
718 |
< |
BlockingQueue q = p1.getQueue(); |
719 |
< |
for (Iterator it = q.iterator(); it.hasNext();) { |
720 |
< |
ScheduledFuture t = (ScheduledFuture)it.next(); |
721 |
< |
assertFalse(t.isCancelled()); |
722 |
< |
} |
723 |
< |
assertTrue(p1.isShutdown()); |
724 |
< |
Thread.sleep(SMALL_DELAY_MS); |
725 |
< |
for (int i = 0; i < 5; ++i) { |
726 |
< |
assertTrue(tasks[i].isDone()); |
727 |
< |
assertFalse(tasks[i].isCancelled()); |
715 |
> |
for (int i = 0; i < tasks.length; i++) |
716 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
717 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
718 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
719 |
> |
BlockingQueue<Runnable> q = p.getQueue(); |
720 |
> |
for (ScheduledFuture task : tasks) { |
721 |
> |
assertFalse(task.isDone()); |
722 |
> |
assertFalse(task.isCancelled()); |
723 |
> |
assertTrue(q.contains(task)); |
724 |
> |
} |
725 |
> |
assertTrue(p.isShutdown()); |
726 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
727 |
> |
assertTrue(p.isTerminated()); |
728 |
> |
for (ScheduledFuture task : tasks) { |
729 |
> |
assertTrue(task.isDone()); |
730 |
> |
assertFalse(task.isCancelled()); |
731 |
|
} |
732 |
|
} |
733 |
|
|
572 |
– |
|
734 |
|
/** |
735 |
|
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
736 |
|
* delayed tasks are cancelled at shutdown |
737 |
|
*/ |
738 |
< |
public void testShutDown2() throws InterruptedException { |
739 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
740 |
< |
p1.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
738 |
> |
public void testShutdown2() throws InterruptedException { |
739 |
> |
CustomExecutor p = new CustomExecutor(1); |
740 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
741 |
> |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
742 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
743 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
744 |
< |
for (int i = 0; i < 5; i++) |
745 |
< |
tasks[i] = p1.schedule(new NoOpRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
746 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
747 |
< |
assertTrue(p1.isShutdown()); |
748 |
< |
BlockingQueue q = p1.getQueue(); |
744 |
> |
for (int i = 0; i < tasks.length; i++) |
745 |
> |
tasks[i] = p.schedule(new NoOpRunnable(), |
746 |
> |
SHORT_DELAY_MS, MILLISECONDS); |
747 |
> |
BlockingQueue q = p.getQueue(); |
748 |
> |
assertEquals(tasks.length, q.size()); |
749 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
750 |
> |
assertTrue(p.isShutdown()); |
751 |
|
assertTrue(q.isEmpty()); |
752 |
< |
Thread.sleep(SMALL_DELAY_MS); |
753 |
< |
assertTrue(p1.isTerminated()); |
752 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
753 |
> |
assertTrue(p.isTerminated()); |
754 |
> |
for (ScheduledFuture task : tasks) { |
755 |
> |
assertTrue(task.isDone()); |
756 |
> |
assertTrue(task.isCancelled()); |
757 |
> |
} |
758 |
|
} |
759 |
|
|
591 |
– |
|
760 |
|
/** |
761 |
|
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
762 |
< |
* periodic tasks are not cancelled at shutdown |
762 |
> |
* periodic tasks are cancelled at shutdown |
763 |
|
*/ |
764 |
< |
public void testShutDown3() throws InterruptedException { |
765 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
766 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
764 |
> |
public void testShutdown3() throws InterruptedException { |
765 |
> |
CustomExecutor p = new CustomExecutor(1); |
766 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
767 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
768 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
769 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
770 |
> |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
771 |
> |
long initialDelay = LONG_DELAY_MS; |
772 |
|
ScheduledFuture task = |
773 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
774 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
775 |
< |
assertTrue(p1.isShutdown()); |
776 |
< |
BlockingQueue q = p1.getQueue(); |
777 |
< |
assertTrue(q.isEmpty()); |
778 |
< |
Thread.sleep(SHORT_DELAY_MS); |
779 |
< |
assertTrue(p1.isTerminated()); |
773 |
> |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
774 |
> |
5, MILLISECONDS); |
775 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
776 |
> |
assertTrue(p.isShutdown()); |
777 |
> |
assertTrue(p.getQueue().isEmpty()); |
778 |
> |
assertTrue(task.isDone()); |
779 |
> |
assertTrue(task.isCancelled()); |
780 |
> |
joinPool(p); |
781 |
|
} |
782 |
|
|
783 |
|
/** |
784 |
|
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
785 |
< |
* periodic tasks are cancelled at shutdown |
785 |
> |
* periodic tasks are not cancelled at shutdown |
786 |
|
*/ |
787 |
< |
public void testShutDown4() throws InterruptedException { |
788 |
< |
CustomExecutor p1 = new CustomExecutor(1); |
787 |
> |
public void testShutdown4() throws InterruptedException { |
788 |
> |
CustomExecutor p = new CustomExecutor(1); |
789 |
> |
final CountDownLatch counter = new CountDownLatch(2); |
790 |
|
try { |
791 |
< |
p1.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
791 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
792 |
> |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
793 |
> |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
794 |
> |
final Runnable r = new CheckedRunnable() { |
795 |
> |
public void realRun() { |
796 |
> |
counter.countDown(); |
797 |
> |
}}; |
798 |
|
ScheduledFuture task = |
799 |
< |
p1.scheduleAtFixedRate(new NoOpRunnable(), 1, 1, MILLISECONDS); |
799 |
> |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
800 |
> |
assertFalse(task.isDone()); |
801 |
|
assertFalse(task.isCancelled()); |
802 |
< |
try { p1.shutdown(); } catch (SecurityException ok) { return; } |
802 |
> |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
803 |
|
assertFalse(task.isCancelled()); |
804 |
< |
assertFalse(p1.isTerminated()); |
805 |
< |
assertTrue(p1.isShutdown()); |
806 |
< |
Thread.sleep(SHORT_DELAY_MS); |
804 |
> |
assertFalse(p.isTerminated()); |
805 |
> |
assertTrue(p.isShutdown()); |
806 |
> |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
807 |
|
assertFalse(task.isCancelled()); |
808 |
< |
assertTrue(task.cancel(true)); |
808 |
> |
assertTrue(task.cancel(false)); |
809 |
|
assertTrue(task.isDone()); |
810 |
< |
Thread.sleep(SHORT_DELAY_MS); |
811 |
< |
assertTrue(p1.isTerminated()); |
810 |
> |
assertTrue(task.isCancelled()); |
811 |
> |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
812 |
> |
assertTrue(p.isTerminated()); |
813 |
|
} |
814 |
|
finally { |
815 |
< |
joinPool(p1); |
815 |
> |
joinPool(p); |
816 |
|
} |
817 |
|
} |
818 |
|
|
1226 |
|
l.add(new StringTask()); |
1227 |
|
List<Future<String>> futures = |
1228 |
|
e.invokeAll(l, SHORT_DELAY_MS, MILLISECONDS); |
1229 |
< |
assertEquals(3, futures.size()); |
1230 |
< |
Iterator<Future<String>> it = futures.iterator(); |
1231 |
< |
Future<String> f1 = it.next(); |
1232 |
< |
Future<String> f2 = it.next(); |
1233 |
< |
Future<String> f3 = it.next(); |
1051 |
< |
assertTrue(f1.isDone()); |
1052 |
< |
assertTrue(f2.isDone()); |
1053 |
< |
assertTrue(f3.isDone()); |
1054 |
< |
assertFalse(f1.isCancelled()); |
1055 |
< |
assertTrue(f2.isCancelled()); |
1229 |
> |
assertEquals(l.size(), futures.size()); |
1230 |
> |
for (Future future : futures) |
1231 |
> |
assertTrue(future.isDone()); |
1232 |
> |
assertFalse(futures.get(0).isCancelled()); |
1233 |
> |
assertTrue(futures.get(1).isCancelled()); |
1234 |
|
} finally { |
1235 |
|
joinPool(e); |
1236 |
|
} |