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 |
|
*/ |
150 |
|
RunnableCounter counter = new RunnableCounter(); |
151 |
|
ScheduledFuture h = |
152 |
|
p.scheduleAtFixedRate(counter, 0, 1, MILLISECONDS); |
153 |
< |
Thread.sleep(SMALL_DELAY_MS); |
153 |
> |
delay(SMALL_DELAY_MS); |
154 |
|
h.cancel(true); |
155 |
|
int c = counter.count.get(); |
156 |
|
// By time scaling conventions, we must have at least |
168 |
|
RunnableCounter counter = new RunnableCounter(); |
169 |
|
ScheduledFuture h = |
170 |
|
p.scheduleWithFixedDelay(counter, 0, 1, MILLISECONDS); |
171 |
< |
Thread.sleep(SMALL_DELAY_MS); |
171 |
> |
delay(SMALL_DELAY_MS); |
172 |
|
h.cancel(true); |
173 |
|
int c = counter.count.get(); |
174 |
|
assertTrue(c >= SMALL_DELAY_MS / SHORT_DELAY_MS); |
240 |
|
/** |
241 |
|
* schedule callable throws RejectedExecutionException if shutdown |
242 |
|
*/ |
243 |
< |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
244 |
< |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
245 |
< |
try { |
243 |
> |
public void testSchedule3_RejectedExecutionException() throws InterruptedException { |
244 |
> |
ScheduledThreadPoolExecutor se = new ScheduledThreadPoolExecutor(1); |
245 |
> |
try { |
246 |
|
se.shutdown(); |
247 |
|
se.schedule(new NoOpCallable(), |
248 |
|
MEDIUM_DELAY_MS, MILLISECONDS); |
250 |
|
} catch (RejectedExecutionException success) { |
251 |
|
} catch (SecurityException ok) { |
252 |
|
} |
253 |
< |
joinPool(se); |
253 |
> |
joinPool(se); |
254 |
|
} |
255 |
|
|
256 |
|
/** |
331 |
|
assertEquals(0, p.getCompletedTaskCount()); |
332 |
|
threadProceed.countDown(); |
333 |
|
threadDone.await(); |
334 |
< |
Thread.sleep(SHORT_DELAY_MS); |
334 |
> |
delay(SHORT_DELAY_MS); |
335 |
|
assertEquals(1, p.getCompletedTaskCount()); |
336 |
|
} finally { |
337 |
|
joinPool(p); |
485 |
|
try { |
486 |
|
p.execute(new CheckedRunnable() { |
487 |
|
public void realRun() throws InterruptedException { |
488 |
– |
threadStarted.countDown(); |
488 |
|
assertFalse(p.isTerminated()); |
489 |
+ |
threadStarted.countDown(); |
490 |
|
done.await(); |
491 |
|
}}); |
492 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
493 |
+ |
assertFalse(p.isTerminating()); |
494 |
|
done.countDown(); |
495 |
|
} finally { |
496 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
510 |
|
assertFalse(p.isTerminating()); |
511 |
|
p.execute(new CheckedRunnable() { |
512 |
|
public void realRun() throws InterruptedException { |
512 |
– |
threadStarted.countDown(); |
513 |
|
assertFalse(p.isTerminating()); |
514 |
+ |
threadStarted.countDown(); |
515 |
|
done.await(); |
516 |
|
}}); |
517 |
|
assertTrue(threadStarted.await(SMALL_DELAY_MS, MILLISECONDS)); |
587 |
|
} |
588 |
|
|
589 |
|
/** |
590 |
< |
* purge removes cancelled tasks from the queue |
590 |
> |
* purge eventually removes cancelled tasks from the queue |
591 |
|
*/ |
592 |
|
public void testPurge() throws InterruptedException { |
593 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
594 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
595 |
< |
for (int i = 0; i < tasks.length; i++) { |
596 |
< |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
597 |
< |
} |
595 |
> |
for (int i = 0; i < tasks.length; i++) |
596 |
> |
tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), |
597 |
> |
LONG_DELAY_MS, MILLISECONDS); |
598 |
|
try { |
599 |
|
int max = tasks.length; |
600 |
|
if (tasks[4].cancel(true)) --max; |
601 |
|
if (tasks[3].cancel(true)) --max; |
602 |
|
// There must eventually be an interference-free point at |
603 |
|
// which purge will not fail. (At worst, when queue is empty.) |
604 |
< |
int k; |
605 |
< |
for (k = 0; k < SMALL_DELAY_MS; ++k) { |
604 |
> |
long startTime = System.nanoTime(); |
605 |
> |
do { |
606 |
|
p.purge(); |
607 |
|
long count = p.getTaskCount(); |
608 |
< |
if (count >= 0 && count <= max) |
609 |
< |
break; |
610 |
< |
Thread.sleep(1); |
611 |
< |
} |
611 |
< |
assertTrue(k < SMALL_DELAY_MS); |
608 |
> |
if (count == max) |
609 |
> |
return; |
610 |
> |
} while (millisElapsedSince(startTime) < MEDIUM_DELAY_MS); |
611 |
> |
fail("Purge failed to remove cancelled tasks"); |
612 |
|
} finally { |
613 |
|
for (ScheduledFuture task : tasks) |
614 |
|
task.cancel(true); |
619 |
|
/** |
620 |
|
* shutDownNow returns a list containing tasks that were not run |
621 |
|
*/ |
622 |
< |
public void testShutDownNow() throws InterruptedException { |
622 |
> |
public void testShutdownNow() { |
623 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
624 |
|
for (int i = 0; i < 5; i++) |
625 |
< |
p.schedule(new SmallPossiblyInterruptedRunnable(), SHORT_DELAY_MS, MILLISECONDS); |
626 |
< |
List l; |
625 |
> |
p.schedule(new SmallPossiblyInterruptedRunnable(), |
626 |
> |
LONG_DELAY_MS, MILLISECONDS); |
627 |
|
try { |
628 |
< |
l = p.shutdownNow(); |
628 |
> |
List<Runnable> l = p.shutdownNow(); |
629 |
> |
assertTrue(p.isShutdown()); |
630 |
> |
assertEquals(5, l.size()); |
631 |
|
} catch (SecurityException ok) { |
632 |
< |
return; |
632 |
> |
// Allowed in case test doesn't have privs |
633 |
> |
} finally { |
634 |
> |
joinPool(p); |
635 |
|
} |
632 |
– |
assertTrue(p.isShutdown()); |
633 |
– |
assertTrue(l.size() > 0 && l.size() <= 5); |
634 |
– |
joinPool(p); |
636 |
|
} |
637 |
|
|
638 |
|
/** |
672 |
|
public void testShutDown2() throws InterruptedException { |
673 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
674 |
|
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
675 |
+ |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
676 |
+ |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
677 |
|
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
678 |
|
for (int i = 0; i < tasks.length; i++) |
679 |
|
tasks[i] = p.schedule(new NoOpRunnable(), |
697 |
|
*/ |
698 |
|
public void testShutDown3() throws InterruptedException { |
699 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
700 |
+ |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
701 |
+ |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
702 |
|
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
703 |
+ |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
704 |
+ |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
705 |
|
ScheduledFuture task = |
706 |
|
p.scheduleAtFixedRate(new NoOpRunnable(), 5, 5, MILLISECONDS); |
707 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
720 |
|
*/ |
721 |
|
public void testShutDown4() throws InterruptedException { |
722 |
|
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
716 |
– |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
723 |
|
final CountDownLatch counter = new CountDownLatch(2); |
724 |
|
try { |
725 |
+ |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
726 |
+ |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
727 |
+ |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
728 |
|
final Runnable r = new CheckedRunnable() { |
729 |
|
public void realRun() { |
730 |
|
counter.countDown(); |