663 |
|
final ScheduledThreadPoolExecutor p = |
664 |
|
new ScheduledThreadPoolExecutor(poolSize); |
665 |
|
CountDownLatch threadsStarted = new CountDownLatch(poolSize); |
666 |
< |
CheckedRunnable waiter = new CheckedRunnable() { public void realRun() { |
666 |
> |
Runnable waiter = new CheckedRunnable() { public void realRun() { |
667 |
|
threadsStarted.countDown(); |
668 |
|
try { |
669 |
|
MILLISECONDS.sleep(2 * LONG_DELAY_MS); |
725 |
|
} |
726 |
|
|
727 |
|
/** |
728 |
< |
* In default setting, shutdown cancels periodic but not delayed |
729 |
< |
* tasks at shutdown |
728 |
> |
* By default, periodic tasks are cancelled at shutdown. |
729 |
> |
* By default, delayed tasks keep running after shutdown. |
730 |
> |
* Check that changing the default values work: |
731 |
> |
* - setExecuteExistingDelayedTasksAfterShutdownPolicy |
732 |
> |
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
733 |
|
*/ |
734 |
< |
public void testShutdown1() throws InterruptedException { |
735 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
736 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
737 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
734 |
> |
public void testShutdown_cancellation() throws Exception { |
735 |
> |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
736 |
> |
for (Boolean policy : allBooleans) |
737 |
> |
{ |
738 |
> |
final int poolSize = 2; |
739 |
> |
final ScheduledThreadPoolExecutor p |
740 |
> |
= new ScheduledThreadPoolExecutor(poolSize); |
741 |
> |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
742 |
> |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
743 |
> |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
744 |
> |
if (policy != null) { |
745 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
746 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
747 |
> |
p.setRemoveOnCancelPolicy(policy); |
748 |
> |
} |
749 |
> |
assertEquals(effectiveDelayedPolicy, |
750 |
> |
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
751 |
> |
assertEquals(effectivePeriodicPolicy, |
752 |
> |
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
753 |
> |
assertEquals(effectiveRemovePolicy, |
754 |
> |
p.getRemoveOnCancelPolicy()); |
755 |
> |
// Strategy: Wedge the pool with poolSize "blocker" threads |
756 |
> |
final AtomicInteger ran = new AtomicInteger(0); |
757 |
> |
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
758 |
> |
final CountDownLatch unblock = new CountDownLatch(1); |
759 |
> |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
760 |
> |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
761 |
> |
Runnable task = new CheckedRunnable() { public void realRun() |
762 |
> |
throws InterruptedException { |
763 |
> |
poolBlocked.countDown(); |
764 |
> |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
765 |
> |
ran.getAndIncrement(); |
766 |
> |
}}; |
767 |
> |
List<Future<?>> blockers = new ArrayList<>(); |
768 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
769 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
770 |
> |
for (int i = 0; i < poolSize; i++) |
771 |
> |
blockers.add(p.submit(task)); |
772 |
> |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
773 |
> |
|
774 |
> |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
775 |
> |
1, 1, MILLISECONDS)); |
776 |
> |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
777 |
> |
1, 1, MILLISECONDS)); |
778 |
> |
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
779 |
|
|
780 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
781 |
< |
for (int i = 0; i < tasks.length; i++) |
738 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
739 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
780 |
> |
assertTrue(p.getQueue().containsAll(periodics)); |
781 |
> |
assertTrue(p.getQueue().containsAll(delayeds)); |
782 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
741 |
– |
BlockingQueue<Runnable> q = p.getQueue(); |
742 |
– |
for (ScheduledFuture task : tasks) { |
743 |
– |
assertFalse(task.isDone()); |
744 |
– |
assertFalse(task.isCancelled()); |
745 |
– |
assertTrue(q.contains(task)); |
746 |
– |
} |
783 |
|
assertTrue(p.isShutdown()); |
784 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
785 |
< |
assertTrue(p.isTerminated()); |
786 |
< |
for (ScheduledFuture task : tasks) { |
787 |
< |
assertTrue(task.isDone()); |
788 |
< |
assertFalse(task.isCancelled()); |
784 |
> |
assertFalse(p.isTerminated()); |
785 |
> |
for (Future<?> periodic : periodics) { |
786 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
787 |
> |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
788 |
> |
} |
789 |
> |
for (Future<?> delayed : delayeds) { |
790 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
791 |
> |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
792 |
> |
} |
793 |
> |
if (testImplementationDetails) { |
794 |
> |
assertEquals(effectivePeriodicPolicy, |
795 |
> |
p.getQueue().containsAll(periodics)); |
796 |
> |
assertEquals(effectiveDelayedPolicy, |
797 |
> |
p.getQueue().containsAll(delayeds)); |
798 |
> |
} |
799 |
> |
// Release all pool threads |
800 |
> |
unblock.countDown(); |
801 |
> |
|
802 |
> |
for (Future<?> delayed : delayeds) { |
803 |
> |
if (effectiveDelayedPolicy) { |
804 |
> |
assertNull(delayed.get()); |
805 |
> |
} |
806 |
> |
} |
807 |
> |
if (effectivePeriodicPolicy) { |
808 |
> |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
809 |
> |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
810 |
> |
for (Future<?> periodic : periodics) { |
811 |
> |
assertTrue(periodic.cancel(false)); |
812 |
> |
assertTrue(periodic.isCancelled()); |
813 |
> |
assertTrue(periodic.isDone()); |
814 |
> |
} |
815 |
|
} |
816 |
< |
} |
755 |
< |
|
756 |
< |
/** |
757 |
< |
* If setExecuteExistingDelayedTasksAfterShutdownPolicy is false, |
758 |
< |
* delayed tasks are cancelled at shutdown |
759 |
< |
*/ |
760 |
< |
public void testShutdown2() throws InterruptedException { |
761 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
762 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(false); |
763 |
< |
assertFalse(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
764 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
765 |
< |
ScheduledFuture[] tasks = new ScheduledFuture[5]; |
766 |
< |
for (int i = 0; i < tasks.length; i++) |
767 |
< |
tasks[i] = p.schedule(new NoOpRunnable(), |
768 |
< |
SHORT_DELAY_MS, MILLISECONDS); |
769 |
< |
BlockingQueue q = p.getQueue(); |
770 |
< |
assertEquals(tasks.length, q.size()); |
771 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
772 |
< |
assertTrue(p.isShutdown()); |
773 |
< |
assertTrue(q.isEmpty()); |
774 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
816 |
> |
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
817 |
|
assertTrue(p.isTerminated()); |
818 |
< |
for (ScheduledFuture task : tasks) { |
819 |
< |
assertTrue(task.isDone()); |
778 |
< |
assertTrue(task.isCancelled()); |
779 |
< |
} |
780 |
< |
} |
781 |
< |
|
782 |
< |
/** |
783 |
< |
* If setContinueExistingPeriodicTasksAfterShutdownPolicy is set false, |
784 |
< |
* periodic tasks are cancelled at shutdown |
785 |
< |
*/ |
786 |
< |
public void testShutdown3() throws InterruptedException { |
787 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
788 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
789 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
790 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(false); |
791 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
792 |
< |
assertFalse(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
793 |
< |
long initialDelay = LONG_DELAY_MS; |
794 |
< |
ScheduledFuture task = |
795 |
< |
p.scheduleAtFixedRate(new NoOpRunnable(), initialDelay, |
796 |
< |
5, MILLISECONDS); |
797 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
798 |
< |
assertTrue(p.isShutdown()); |
799 |
< |
assertTrue(p.getQueue().isEmpty()); |
800 |
< |
assertTrue(task.isDone()); |
801 |
< |
assertTrue(task.isCancelled()); |
802 |
< |
joinPool(p); |
803 |
< |
} |
804 |
< |
|
805 |
< |
/** |
806 |
< |
* if setContinueExistingPeriodicTasksAfterShutdownPolicy is true, |
807 |
< |
* periodic tasks are not cancelled at shutdown |
808 |
< |
*/ |
809 |
< |
public void testShutdown4() throws InterruptedException { |
810 |
< |
ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
811 |
< |
final CountDownLatch counter = new CountDownLatch(2); |
812 |
< |
try { |
813 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(true); |
814 |
< |
assertTrue(p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
815 |
< |
assertTrue(p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
816 |
< |
final Runnable r = new CheckedRunnable() { |
817 |
< |
public void realRun() { |
818 |
< |
counter.countDown(); |
819 |
< |
}}; |
820 |
< |
ScheduledFuture task = |
821 |
< |
p.scheduleAtFixedRate(r, 1, 1, MILLISECONDS); |
822 |
< |
assertFalse(task.isDone()); |
823 |
< |
assertFalse(task.isCancelled()); |
824 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
825 |
< |
assertFalse(task.isCancelled()); |
826 |
< |
assertFalse(p.isTerminated()); |
827 |
< |
assertTrue(p.isShutdown()); |
828 |
< |
assertTrue(counter.await(SMALL_DELAY_MS, MILLISECONDS)); |
829 |
< |
assertFalse(task.isCancelled()); |
830 |
< |
assertTrue(task.cancel(false)); |
831 |
< |
assertTrue(task.isDone()); |
832 |
< |
assertTrue(task.isCancelled()); |
833 |
< |
assertTrue(p.awaitTermination(SMALL_DELAY_MS, MILLISECONDS)); |
834 |
< |
assertTrue(p.isTerminated()); |
835 |
< |
} |
836 |
< |
finally { |
837 |
< |
joinPool(p); |
838 |
< |
} |
839 |
< |
} |
818 |
> |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
819 |
> |
}} |
820 |
|
|
821 |
|
/** |
822 |
|
* completed submit of callable returns result |