24 |
|
import java.util.concurrent.ScheduledFuture; |
25 |
|
import java.util.concurrent.ScheduledThreadPoolExecutor; |
26 |
|
import java.util.concurrent.ThreadFactory; |
27 |
+ |
import java.util.concurrent.ThreadLocalRandom; |
28 |
|
import java.util.concurrent.ThreadPoolExecutor; |
29 |
|
import java.util.concurrent.atomic.AtomicBoolean; |
30 |
|
import java.util.concurrent.atomic.AtomicInteger; |
745 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
746 |
|
*/ |
747 |
|
public void testShutdown_cancellation() throws Exception { |
747 |
– |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
748 |
– |
for (Boolean policy : allBooleans) |
749 |
– |
{ |
748 |
|
final int poolSize = 2; |
749 |
|
final ScheduledThreadPoolExecutor p |
750 |
|
= new ScheduledThreadPoolExecutor(poolSize); |
751 |
< |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
752 |
< |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
753 |
< |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
754 |
< |
if (policy != null) { |
755 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
756 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
757 |
< |
p.setRemoveOnCancelPolicy(policy); |
758 |
< |
} |
751 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
752 |
> |
final boolean effectiveDelayedPolicy; |
753 |
> |
final boolean effectivePeriodicPolicy; |
754 |
> |
final boolean effectiveRemovePolicy; |
755 |
> |
|
756 |
> |
if (rnd.nextBoolean()) |
757 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy( |
758 |
> |
effectiveDelayedPolicy = rnd.nextBoolean()); |
759 |
> |
else |
760 |
> |
effectiveDelayedPolicy = true; |
761 |
|
assertEquals(effectiveDelayedPolicy, |
762 |
|
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
763 |
+ |
|
764 |
+ |
if (rnd.nextBoolean()) |
765 |
+ |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy( |
766 |
+ |
effectivePeriodicPolicy = rnd.nextBoolean()); |
767 |
+ |
else |
768 |
+ |
effectivePeriodicPolicy = false; |
769 |
|
assertEquals(effectivePeriodicPolicy, |
770 |
|
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
771 |
+ |
|
772 |
+ |
if (rnd.nextBoolean()) |
773 |
+ |
p.setRemoveOnCancelPolicy( |
774 |
+ |
effectiveRemovePolicy = rnd.nextBoolean()); |
775 |
+ |
else |
776 |
+ |
effectiveRemovePolicy = false; |
777 |
|
assertEquals(effectiveRemovePolicy, |
778 |
|
p.getRemoveOnCancelPolicy()); |
779 |
+ |
|
780 |
|
// Strategy: Wedge the pool with poolSize "blocker" threads |
781 |
|
final AtomicInteger ran = new AtomicInteger(0); |
782 |
|
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
796 |
|
blockers.add(p.submit(task)); |
797 |
|
await(poolBlocked); |
798 |
|
|
799 |
< |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
800 |
< |
1, 1, MILLISECONDS)); |
801 |
< |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
802 |
< |
1, 1, MILLISECONDS)); |
799 |
> |
periodics.add(p.scheduleAtFixedRate( |
800 |
> |
countDowner(periodicLatch1), 1, 1, MILLISECONDS)); |
801 |
> |
periodics.add(p.scheduleWithFixedDelay( |
802 |
> |
countDowner(periodicLatch2), 1, 1, MILLISECONDS)); |
803 |
|
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
804 |
|
|
805 |
|
assertTrue(p.getQueue().containsAll(periodics)); |
821 |
|
assertEquals(effectiveDelayedPolicy, |
822 |
|
p.getQueue().containsAll(delayeds)); |
823 |
|
} |
824 |
< |
// Release all pool threads |
812 |
< |
unblock.countDown(); |
824 |
> |
unblock.countDown(); // Release all pool threads |
825 |
|
|
826 |
< |
for (Future<?> delayed : delayeds) { |
827 |
< |
if (effectiveDelayedPolicy) { |
816 |
< |
assertNull(delayed.get()); |
817 |
< |
} |
818 |
< |
} |
826 |
> |
if (effectiveDelayedPolicy) |
827 |
> |
for (Future<?> delayed : delayeds) assertNull(delayed.get()); |
828 |
|
if (effectivePeriodicPolicy) { |
829 |
|
await(periodicLatch1); |
830 |
|
await(periodicLatch2); |
837 |
|
for (Future<?> blocker : blockers) assertNull(blocker.get()); |
838 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
839 |
|
assertTrue(p.isTerminated()); |
840 |
+ |
|
841 |
+ |
for (Future<?> future : delayeds) { |
842 |
+ |
assertTrue(effectiveDelayedPolicy ^ future.isCancelled()); |
843 |
+ |
assertTrue(future.isDone()); |
844 |
+ |
} |
845 |
+ |
for (Future<?> future : periodics) |
846 |
+ |
assertTrue(future.isCancelled()); |
847 |
+ |
for (Future<?> future : blockers) |
848 |
+ |
assertNull(future.get()); |
849 |
|
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
850 |
< |
}} |
850 |
> |
} |
851 |
|
|
852 |
|
/** |
853 |
|
* completed submit of callable returns result |