7 |
|
*/ |
8 |
|
|
9 |
|
import static java.util.concurrent.TimeUnit.MILLISECONDS; |
10 |
+ |
import static java.util.concurrent.TimeUnit.NANOSECONDS; |
11 |
|
import static java.util.concurrent.TimeUnit.SECONDS; |
12 |
|
|
13 |
|
import java.util.ArrayList; |
18 |
|
import java.util.concurrent.CancellationException; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.ExecutionException; |
20 |
– |
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
22 |
|
import java.util.concurrent.Future; |
23 |
|
import java.util.concurrent.RejectedExecutionException; |
25 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
26 |
|
import java.util.concurrent.ThreadFactory; |
27 |
|
import java.util.concurrent.ThreadPoolExecutor; |
28 |
+ |
import java.util.concurrent.atomic.AtomicBoolean; |
29 |
|
import java.util.concurrent.atomic.AtomicInteger; |
30 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
31 |
|
|
32 |
|
import junit.framework.Test; |
33 |
|
import junit.framework.TestSuite; |
44 |
|
* execute successfully executes a runnable |
45 |
|
*/ |
46 |
|
public void testExecute() throws InterruptedException { |
47 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
47 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
48 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
49 |
|
final CountDownLatch done = new CountDownLatch(1); |
50 |
|
final Runnable task = new CheckedRunnable() { |
51 |
|
public void realRun() { done.countDown(); }}; |
52 |
|
p.execute(task); |
53 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
53 |
> |
await(done); |
54 |
|
} |
55 |
|
} |
56 |
|
|
58 |
|
* delayed schedule of callable successfully executes after delay |
59 |
|
*/ |
60 |
|
public void testSchedule1() throws Exception { |
61 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
61 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
62 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
63 |
|
final long startTime = System.nanoTime(); |
64 |
|
final CountDownLatch done = new CountDownLatch(1); |
79 |
|
* delayed schedule of runnable successfully executes after delay |
80 |
|
*/ |
81 |
|
public void testSchedule3() throws Exception { |
82 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
82 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
83 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
84 |
|
final long startTime = System.nanoTime(); |
85 |
|
final CountDownLatch done = new CountDownLatch(1); |
99 |
|
* scheduleAtFixedRate executes runnable after given initial delay |
100 |
|
*/ |
101 |
|
public void testSchedule4() throws Exception { |
102 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
102 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
103 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
104 |
|
final long startTime = System.nanoTime(); |
105 |
|
final CountDownLatch done = new CountDownLatch(1); |
121 |
|
* scheduleWithFixedDelay executes runnable after given initial delay |
122 |
|
*/ |
123 |
|
public void testSchedule5() throws Exception { |
124 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
124 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
125 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
126 |
|
final long startTime = System.nanoTime(); |
127 |
|
final CountDownLatch done = new CountDownLatch(1); |
145 |
|
} |
146 |
|
|
147 |
|
/** |
148 |
< |
* scheduleAtFixedRate executes series of tasks at given rate |
148 |
> |
* scheduleAtFixedRate executes series of tasks at given rate. |
149 |
> |
* Eventually, it must hold that: |
150 |
> |
* cycles - 1 <= elapsedMillis/delay < cycles |
151 |
|
*/ |
152 |
|
public void testFixedRateSequence() throws InterruptedException { |
153 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
153 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
154 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
155 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
156 |
< |
long startTime = System.nanoTime(); |
157 |
< |
int cycles = 10; |
156 |
> |
final long startTime = System.nanoTime(); |
157 |
> |
final int cycles = 8; |
158 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
159 |
< |
Runnable task = new CheckedRunnable() { |
159 |
> |
final Runnable task = new CheckedRunnable() { |
160 |
|
public void realRun() { done.countDown(); }}; |
161 |
< |
ScheduledFuture h = |
161 |
> |
final ScheduledFuture periodicTask = |
162 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
163 |
< |
done.await(); |
164 |
< |
h.cancel(true); |
165 |
< |
double normalizedTime = |
166 |
< |
(double) millisElapsedSince(startTime) / delay; |
167 |
< |
if (normalizedTime >= cycles - 1 && |
168 |
< |
normalizedTime <= cycles) |
163 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
164 |
> |
await(done, totalDelayMillis + LONG_DELAY_MS); |
165 |
> |
periodicTask.cancel(true); |
166 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
167 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
168 |
> |
if (elapsedMillis <= cycles * delay) |
169 |
|
return; |
170 |
+ |
// else retry with longer delay |
171 |
|
} |
172 |
< |
throw new AssertionError("unexpected execution rate"); |
172 |
> |
fail("unexpected execution rate"); |
173 |
|
} |
174 |
|
} |
175 |
|
|
176 |
|
/** |
177 |
< |
* scheduleWithFixedDelay executes series of tasks with given period |
177 |
> |
* scheduleWithFixedDelay executes series of tasks with given period. |
178 |
> |
* Eventually, it must hold that each task starts at least delay and at |
179 |
> |
* most 2 * delay after the termination of the previous task. |
180 |
|
*/ |
181 |
|
public void testFixedDelaySequence() throws InterruptedException { |
182 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
182 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
183 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
184 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
185 |
< |
long startTime = System.nanoTime(); |
186 |
< |
int cycles = 10; |
185 |
> |
final long startTime = System.nanoTime(); |
186 |
> |
final AtomicLong previous = new AtomicLong(startTime); |
187 |
> |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
188 |
> |
final int cycles = 8; |
189 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
190 |
< |
Runnable task = new CheckedRunnable() { |
191 |
< |
public void realRun() { done.countDown(); }}; |
192 |
< |
ScheduledFuture h = |
190 |
> |
final int d = delay; |
191 |
> |
final Runnable task = new CheckedRunnable() { |
192 |
> |
public void realRun() { |
193 |
> |
long now = System.nanoTime(); |
194 |
> |
long elapsedMillis |
195 |
> |
= NANOSECONDS.toMillis(now - previous.get()); |
196 |
> |
if (done.getCount() == cycles) { // first execution |
197 |
> |
if (elapsedMillis >= d) |
198 |
> |
tryLongerDelay.set(true); |
199 |
> |
} else { |
200 |
> |
assertTrue(elapsedMillis >= d); |
201 |
> |
if (elapsedMillis >= 2 * d) |
202 |
> |
tryLongerDelay.set(true); |
203 |
> |
} |
204 |
> |
previous.set(now); |
205 |
> |
done.countDown(); |
206 |
> |
}}; |
207 |
> |
final ScheduledFuture periodicTask = |
208 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
209 |
< |
done.await(); |
210 |
< |
h.cancel(true); |
211 |
< |
double normalizedTime = |
212 |
< |
(double) millisElapsedSince(startTime) / delay; |
213 |
< |
if (normalizedTime >= cycles - 1 && |
214 |
< |
normalizedTime <= cycles) |
209 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
210 |
> |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
211 |
> |
periodicTask.cancel(true); |
212 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
213 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
214 |
> |
if (!tryLongerDelay.get()) |
215 |
|
return; |
216 |
+ |
// else retry with longer delay |
217 |
|
} |
218 |
< |
throw new AssertionError("unexpected execution rate"); |
218 |
> |
fail("unexpected execution rate"); |
219 |
|
} |
220 |
|
} |
221 |
|
|
223 |
|
* execute(null) throws NPE |
224 |
|
*/ |
225 |
|
public void testExecuteNull() throws InterruptedException { |
226 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
226 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
227 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
228 |
|
try { |
229 |
|
p.execute(null); |
250 |
|
* execute throws RejectedExecutionException if shutdown |
251 |
|
*/ |
252 |
|
public void testSchedule1_RejectedExecutionException() throws InterruptedException { |
253 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
253 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
254 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
255 |
|
try { |
256 |
|
p.shutdown(); |
266 |
|
* schedule throws RejectedExecutionException if shutdown |
267 |
|
*/ |
268 |
|
public void testSchedule2_RejectedExecutionException() throws InterruptedException { |
269 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
269 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
270 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
271 |
|
try { |
272 |
|
p.shutdown(); |
282 |
|
* schedule callable throws RejectedExecutionException if shutdown |
283 |
|
*/ |
284 |
|
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
285 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
285 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
286 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
287 |
|
try { |
288 |
|
p.shutdown(); |
298 |
|
* scheduleAtFixedRate throws RejectedExecutionException if shutdown |
299 |
|
*/ |
300 |
|
public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { |
301 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
301 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
302 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
303 |
|
try { |
304 |
|
p.shutdown(); |
314 |
|
* scheduleWithFixedDelay throws RejectedExecutionException if shutdown |
315 |
|
*/ |
316 |
|
public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { |
317 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
317 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
318 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
319 |
|
try { |
320 |
|
p.shutdown(); |
331 |
|
* thread becomes active |
332 |
|
*/ |
333 |
|
public void testGetActiveCount() throws InterruptedException { |
334 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
335 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(2); |
336 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
336 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
337 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
312 |
– |
final CountDownLatch done = new CountDownLatch(1); |
338 |
|
assertEquals(0, p.getActiveCount()); |
339 |
|
p.execute(new CheckedRunnable() { |
340 |
|
public void realRun() throws InterruptedException { |
341 |
|
threadStarted.countDown(); |
342 |
|
assertEquals(1, p.getActiveCount()); |
343 |
< |
done.await(); |
343 |
> |
await(done); |
344 |
|
}}); |
345 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
345 |
> |
await(threadStarted); |
346 |
|
assertEquals(1, p.getActiveCount()); |
322 |
– |
done.countDown(); |
347 |
|
} |
348 |
|
} |
349 |
|
|
362 |
|
public void realRun() throws InterruptedException { |
363 |
|
threadStarted.countDown(); |
364 |
|
assertEquals(0, p.getCompletedTaskCount()); |
365 |
< |
threadProceed.await(); |
365 |
> |
await(threadProceed); |
366 |
|
threadDone.countDown(); |
367 |
|
}}); |
368 |
|
await(threadStarted); |
369 |
|
assertEquals(0, p.getCompletedTaskCount()); |
370 |
|
threadProceed.countDown(); |
371 |
< |
threadDone.await(); |
371 |
> |
await(threadDone); |
372 |
|
long startTime = System.nanoTime(); |
373 |
|
while (p.getCompletedTaskCount() != 1) { |
374 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
397 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(THREADS); |
398 |
|
final CountDownLatch threadsStarted = new CountDownLatch(THREADS); |
399 |
|
final CountDownLatch done = new CountDownLatch(1); |
400 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
400 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
401 |
|
assertEquals(0, p.getLargestPoolSize()); |
402 |
|
for (int i = 0; i < THREADS; i++) |
403 |
|
p.execute(new CheckedRunnable() { |
404 |
|
public void realRun() throws InterruptedException { |
405 |
|
threadsStarted.countDown(); |
406 |
< |
done.await(); |
406 |
> |
await(done); |
407 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
408 |
|
}}); |
409 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
409 |
> |
await(threadsStarted); |
410 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
387 |
– |
done.countDown(); |
411 |
|
} |
412 |
|
assertEquals(THREADS, p.getLargestPoolSize()); |
413 |
|
} |
420 |
|
final ThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
421 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
422 |
|
final CountDownLatch done = new CountDownLatch(1); |
423 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
423 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
424 |
|
assertEquals(0, p.getPoolSize()); |
425 |
|
p.execute(new CheckedRunnable() { |
426 |
|
public void realRun() throws InterruptedException { |
427 |
|
threadStarted.countDown(); |
428 |
|
assertEquals(1, p.getPoolSize()); |
429 |
< |
done.await(); |
429 |
> |
await(done); |
430 |
|
}}); |
431 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
431 |
> |
await(threadStarted); |
432 |
|
assertEquals(1, p.getPoolSize()); |
410 |
– |
done.countDown(); |
433 |
|
} |
434 |
|
} |
435 |
|
|
448 |
|
p.execute(new CheckedRunnable() { |
449 |
|
public void realRun() throws InterruptedException { |
450 |
|
threadStarted.countDown(); |
451 |
< |
done.await(); |
451 |
> |
await(done); |
452 |
|
}}); |
453 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
453 |
> |
await(threadStarted); |
454 |
|
assertEquals(1, p.getTaskCount()); |
455 |
|
assertEquals(0, p.getCompletedTaskCount()); |
456 |
|
for (int i = 0; i < TASKS; i++) { |
459 |
|
public void realRun() throws InterruptedException { |
460 |
|
threadStarted.countDown(); |
461 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
462 |
< |
done.await(); |
462 |
> |
await(done); |
463 |
|
}}); |
464 |
|
} |
465 |
|
assertEquals(1 + TASKS, p.getTaskCount()); |
486 |
|
*/ |
487 |
|
public void testSetThreadFactory() throws InterruptedException { |
488 |
|
ThreadFactory threadFactory = new SimpleThreadFactory(); |
489 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
489 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
490 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
491 |
|
p.setThreadFactory(threadFactory); |
492 |
|
assertSame(threadFactory, p.getThreadFactory()); |
497 |
|
* setThreadFactory(null) throws NPE |
498 |
|
*/ |
499 |
|
public void testSetThreadFactoryNull() throws InterruptedException { |
500 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
500 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
501 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
502 |
|
try { |
503 |
|
p.setThreadFactory(null); |
507 |
|
} |
508 |
|
|
509 |
|
/** |
510 |
+ |
* The default rejected execution handler is AbortPolicy. |
511 |
+ |
*/ |
512 |
+ |
public void testDefaultRejectedExecutionHandler() { |
513 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
514 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
515 |
+ |
assertTrue(p.getRejectedExecutionHandler() |
516 |
+ |
instanceof ThreadPoolExecutor.AbortPolicy); |
517 |
+ |
} |
518 |
+ |
} |
519 |
+ |
|
520 |
+ |
/** |
521 |
|
* isShutdown is false before shutdown, true after |
522 |
|
*/ |
523 |
|
public void testIsShutdown() { |
524 |
< |
|
525 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
526 |
< |
try { |
527 |
< |
assertFalse(p.isShutdown()); |
528 |
< |
} |
529 |
< |
finally { |
530 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
524 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
525 |
> |
assertFalse(p.isShutdown()); |
526 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
527 |
> |
try { |
528 |
> |
p.shutdown(); |
529 |
> |
assertTrue(p.isShutdown()); |
530 |
> |
} catch (SecurityException ok) {} |
531 |
|
} |
499 |
– |
assertTrue(p.isShutdown()); |
532 |
|
} |
533 |
|
|
534 |
|
/** |
544 |
|
public void realRun() throws InterruptedException { |
545 |
|
assertFalse(p.isTerminated()); |
546 |
|
threadStarted.countDown(); |
547 |
< |
done.await(); |
547 |
> |
await(done); |
548 |
|
}}); |
549 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
549 |
> |
await(threadStarted); |
550 |
|
assertFalse(p.isTerminating()); |
551 |
|
done.countDown(); |
552 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
568 |
|
public void realRun() throws InterruptedException { |
569 |
|
assertFalse(p.isTerminating()); |
570 |
|
threadStarted.countDown(); |
571 |
< |
done.await(); |
571 |
> |
await(done); |
572 |
|
}}); |
573 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
573 |
> |
await(threadStarted); |
574 |
|
assertFalse(p.isTerminating()); |
575 |
|
done.countDown(); |
576 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
584 |
|
* getQueue returns the work queue, which contains queued tasks |
585 |
|
*/ |
586 |
|
public void testGetQueue() throws InterruptedException { |
587 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
588 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
587 |
> |
final CountDownLatch done = new CountDownLatch(1); |
588 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
589 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
590 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
558 |
– |
final CountDownLatch done = new CountDownLatch(1); |
591 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
592 |
|
for (int i = 0; i < tasks.length; i++) { |
593 |
|
Runnable r = new CheckedRunnable() { |
594 |
|
public void realRun() throws InterruptedException { |
595 |
|
threadStarted.countDown(); |
596 |
< |
done.await(); |
596 |
> |
await(done); |
597 |
|
}}; |
598 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
599 |
|
} |
600 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
600 |
> |
await(threadStarted); |
601 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
602 |
|
assertTrue(q.contains(tasks[tasks.length - 1])); |
603 |
|
assertFalse(q.contains(tasks[0])); |
572 |
– |
done.countDown(); |
604 |
|
} |
605 |
|
} |
606 |
|
|
608 |
|
* remove(task) removes queued task, and fails to remove active task |
609 |
|
*/ |
610 |
|
public void testRemove() throws InterruptedException { |
611 |
+ |
final CountDownLatch done = new CountDownLatch(1); |
612 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
613 |
< |
try (PoolCleaner cleaner = cleaner(p)) { |
613 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
614 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
615 |
|
final CountDownLatch threadStarted = new CountDownLatch(1); |
584 |
– |
final CountDownLatch done = new CountDownLatch(1); |
616 |
|
for (int i = 0; i < tasks.length; i++) { |
617 |
|
Runnable r = new CheckedRunnable() { |
618 |
|
public void realRun() throws InterruptedException { |
619 |
|
threadStarted.countDown(); |
620 |
< |
done.await(); |
620 |
> |
await(done); |
621 |
|
}}; |
622 |
|
tasks[i] = p.schedule(r, 1, MILLISECONDS); |
623 |
|
} |
624 |
< |
assertTrue(threadStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
624 |
> |
await(threadStarted); |
625 |
|
BlockingQueue<Runnable> q = p.getQueue(); |
626 |
|
assertFalse(p.remove((Runnable)tasks[0])); |
627 |
|
assertTrue(q.contains((Runnable)tasks[4])); |
632 |
|
assertTrue(q.contains((Runnable)tasks[3])); |
633 |
|
assertTrue(p.remove((Runnable)tasks[3])); |
634 |
|
assertFalse(q.contains((Runnable)tasks[3])); |
604 |
– |
done.countDown(); |
635 |
|
} |
636 |
|
} |
637 |
|
|
684 |
|
}}; |
685 |
|
for (int i = 0; i < count; i++) |
686 |
|
p.execute(waiter); |
687 |
< |
assertTrue(threadsStarted.await(LONG_DELAY_MS, MILLISECONDS)); |
687 |
> |
await(threadsStarted); |
688 |
|
assertEquals(poolSize, p.getActiveCount()); |
689 |
|
assertEquals(0, p.getCompletedTaskCount()); |
690 |
|
final List<Runnable> queuedTasks; |
707 |
|
* and those tasks are drained from the queue |
708 |
|
*/ |
709 |
|
public void testShutdownNow_delayedTasks() throws InterruptedException { |
710 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
710 |
> |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
711 |
|
List<ScheduledFuture> tasks = new ArrayList<>(); |
712 |
|
for (int i = 0; i < 3; i++) { |
713 |
|
Runnable r = new NoOpRunnable(); |
773 |
|
Runnable task = new CheckedRunnable() { public void realRun() |
774 |
|
throws InterruptedException { |
775 |
|
poolBlocked.countDown(); |
776 |
< |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
776 |
> |
await(unblock); |
777 |
|
ran.getAndIncrement(); |
778 |
|
}}; |
779 |
|
List<Future<?>> blockers = new ArrayList<>(); |
781 |
|
List<Future<?>> delayeds = new ArrayList<>(); |
782 |
|
for (int i = 0; i < poolSize; i++) |
783 |
|
blockers.add(p.submit(task)); |
784 |
< |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
784 |
> |
await(poolBlocked); |
785 |
|
|
786 |
|
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
787 |
|
1, 1, MILLISECONDS)); |
817 |
|
} |
818 |
|
} |
819 |
|
if (effectivePeriodicPolicy) { |
820 |
< |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
821 |
< |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
820 |
> |
await(periodicLatch1); |
821 |
> |
await(periodicLatch2); |
822 |
|
for (Future<?> periodic : periodics) { |
823 |
|
assertTrue(periodic.cancel(false)); |
824 |
|
assertTrue(periodic.isCancelled()); |
825 |
|
assertTrue(periodic.isDone()); |
826 |
|
} |
827 |
|
} |
828 |
+ |
for (Future<?> blocker : blockers) assertNull(blocker.get()); |
829 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
830 |
|
assertTrue(p.isTerminated()); |
831 |
|
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
900 |
|
CountDownLatch latch = new CountDownLatch(1); |
901 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
902 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
903 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
903 |
> |
List<Callable<String>> l = new ArrayList<>(); |
904 |
|
l.add(latchAwaitingStringTask(latch)); |
905 |
|
l.add(null); |
906 |
|
try { |
917 |
|
public void testInvokeAny4() throws Exception { |
918 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
919 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
920 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
920 |
> |
List<Callable<String>> l = new ArrayList<>(); |
921 |
|
l.add(new NPETask()); |
922 |
|
try { |
923 |
|
e.invokeAny(l); |
934 |
|
public void testInvokeAny5() throws Exception { |
935 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
936 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
937 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
937 |
> |
List<Callable<String>> l = new ArrayList<>(); |
938 |
|
l.add(new StringTask()); |
939 |
|
l.add(new StringTask()); |
940 |
|
String result = e.invokeAny(l); |
972 |
|
public void testInvokeAll3() throws Exception { |
973 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
974 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
975 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
975 |
> |
List<Callable<String>> l = new ArrayList<>(); |
976 |
|
l.add(new StringTask()); |
977 |
|
l.add(null); |
978 |
|
try { |
988 |
|
public void testInvokeAll4() throws Exception { |
989 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
990 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
991 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
991 |
> |
List<Callable<String>> l = new ArrayList<>(); |
992 |
|
l.add(new NPETask()); |
993 |
|
List<Future<String>> futures = e.invokeAll(l); |
994 |
|
assertEquals(1, futures.size()); |
1007 |
|
public void testInvokeAll5() throws Exception { |
1008 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1009 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1010 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1010 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1011 |
|
l.add(new StringTask()); |
1012 |
|
l.add(new StringTask()); |
1013 |
|
List<Future<String>> futures = e.invokeAll(l); |
1036 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1037 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1038 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1039 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1039 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1040 |
|
l.add(new StringTask()); |
1041 |
|
try { |
1042 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1065 |
|
CountDownLatch latch = new CountDownLatch(1); |
1066 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1067 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1068 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1068 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1069 |
|
l.add(latchAwaitingStringTask(latch)); |
1070 |
|
l.add(null); |
1071 |
|
try { |
1082 |
|
public void testTimedInvokeAny4() throws Exception { |
1083 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1084 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1085 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1085 |
> |
long startTime = System.nanoTime(); |
1086 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1087 |
|
l.add(new NPETask()); |
1088 |
|
try { |
1089 |
< |
e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1089 |
> |
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1090 |
|
shouldThrow(); |
1091 |
|
} catch (ExecutionException success) { |
1092 |
|
assertTrue(success.getCause() instanceof NullPointerException); |
1093 |
|
} |
1094 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1095 |
|
} |
1096 |
|
} |
1097 |
|
|
1101 |
|
public void testTimedInvokeAny5() throws Exception { |
1102 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1103 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1104 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1104 |
> |
long startTime = System.nanoTime(); |
1105 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1106 |
|
l.add(new StringTask()); |
1107 |
|
l.add(new StringTask()); |
1108 |
< |
String result = e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1108 |
> |
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1109 |
|
assertSame(TEST_STRING, result); |
1110 |
+ |
assertTrue(millisElapsedSince(startTime) < LONG_DELAY_MS); |
1111 |
|
} |
1112 |
|
} |
1113 |
|
|
1130 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1131 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1132 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1133 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1133 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1134 |
|
l.add(new StringTask()); |
1135 |
|
try { |
1136 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1157 |
|
public void testTimedInvokeAll3() throws Exception { |
1158 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1159 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1160 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1160 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1161 |
|
l.add(new StringTask()); |
1162 |
|
l.add(null); |
1163 |
|
try { |
1173 |
|
public void testTimedInvokeAll4() throws Exception { |
1174 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1175 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1176 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1176 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1177 |
|
l.add(new NPETask()); |
1178 |
|
List<Future<String>> futures = |
1179 |
< |
e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); |
1179 |
> |
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1180 |
|
assertEquals(1, futures.size()); |
1181 |
|
try { |
1182 |
|
futures.get(0).get(); |
1193 |
|
public void testTimedInvokeAll5() throws Exception { |
1194 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1195 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1196 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1196 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1197 |
|
l.add(new StringTask()); |
1198 |
|
l.add(new StringTask()); |
1199 |
|
List<Future<String>> futures = |
1208 |
|
* timed invokeAll(c) cancels tasks not completed by timeout |
1209 |
|
*/ |
1210 |
|
public void testTimedInvokeAll6() throws Exception { |
1211 |
< |
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1212 |
< |
try (PoolCleaner cleaner = cleaner(e)) { |
1213 |
< |
for (long timeout = timeoutMillis();;) { |
1211 |
> |
for (long timeout = timeoutMillis();;) { |
1212 |
> |
final CountDownLatch done = new CountDownLatch(1); |
1213 |
> |
final Callable<String> waiter = new CheckedCallable<String>() { |
1214 |
> |
public String realCall() { |
1215 |
> |
try { done.await(LONG_DELAY_MS, MILLISECONDS); } |
1216 |
> |
catch (InterruptedException ok) {} |
1217 |
> |
return "1"; }}; |
1218 |
> |
final ExecutorService p = new ScheduledThreadPoolExecutor(2); |
1219 |
> |
try (PoolCleaner cleaner = cleaner(p, done)) { |
1220 |
|
List<Callable<String>> tasks = new ArrayList<>(); |
1221 |
|
tasks.add(new StringTask("0")); |
1222 |
< |
tasks.add(Executors.callable(new LongPossiblyInterruptedRunnable(), TEST_STRING)); |
1222 |
> |
tasks.add(waiter); |
1223 |
|
tasks.add(new StringTask("2")); |
1224 |
|
long startTime = System.nanoTime(); |
1225 |
|
List<Future<String>> futures = |
1226 |
< |
e.invokeAll(tasks, timeout, MILLISECONDS); |
1226 |
> |
p.invokeAll(tasks, timeout, MILLISECONDS); |
1227 |
|
assertEquals(tasks.size(), futures.size()); |
1228 |
|
assertTrue(millisElapsedSince(startTime) >= timeout); |
1229 |
|
for (Future future : futures) |
1242 |
|
} |
1243 |
|
} |
1244 |
|
|
1245 |
+ |
/** |
1246 |
+ |
* A fixed delay task with overflowing period should not prevent a |
1247 |
+ |
* one-shot task from executing. |
1248 |
+ |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1249 |
+ |
*/ |
1250 |
+ |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1251 |
+ |
final CountDownLatch delayedDone = new CountDownLatch(1); |
1252 |
+ |
final CountDownLatch immediateDone = new CountDownLatch(1); |
1253 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1254 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1255 |
+ |
final Runnable immediate = new Runnable() { public void run() { |
1256 |
+ |
immediateDone.countDown(); |
1257 |
+ |
}}; |
1258 |
+ |
final Runnable delayed = new Runnable() { public void run() { |
1259 |
+ |
delayedDone.countDown(); |
1260 |
+ |
p.submit(immediate); |
1261 |
+ |
}}; |
1262 |
+ |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1263 |
+ |
await(delayedDone); |
1264 |
+ |
await(immediateDone); |
1265 |
+ |
} |
1266 |
+ |
} |
1267 |
+ |
|
1268 |
|
} |