18 |
|
import java.util.concurrent.CancellationException; |
19 |
|
import java.util.concurrent.CountDownLatch; |
20 |
|
import java.util.concurrent.ExecutionException; |
21 |
– |
import java.util.concurrent.Executors; |
21 |
|
import java.util.concurrent.ExecutorService; |
22 |
|
import java.util.concurrent.Future; |
23 |
|
import java.util.concurrent.RejectedExecutionException; |
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 |
|
|
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) |
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() { |
514 |
– |
|
524 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
525 |
< |
try { |
526 |
< |
assertFalse(p.isShutdown()); |
527 |
< |
} |
528 |
< |
finally { |
529 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
525 |
> |
assertFalse(p.isShutdown()); |
526 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
527 |
> |
try { |
528 |
> |
p.shutdown(); |
529 |
> |
assertTrue(p.isShutdown()); |
530 |
> |
} catch (SecurityException ok) {} |
531 |
|
} |
522 |
– |
assertTrue(p.isShutdown()); |
532 |
|
} |
533 |
|
|
534 |
|
/** |
744 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
745 |
|
*/ |
746 |
|
public void testShutdown_cancellation() throws Exception { |
747 |
< |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
748 |
< |
for (Boolean policy : allBooleans) |
747 |
> |
final int UNSPECIFIED = 0, YES = 1, NO = 2; |
748 |
> |
for (int maybe : new int[] { UNSPECIFIED, YES, NO }) |
749 |
|
{ |
750 |
|
final int poolSize = 2; |
751 |
|
final ScheduledThreadPoolExecutor p |
752 |
|
= new ScheduledThreadPoolExecutor(poolSize); |
753 |
< |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
754 |
< |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
755 |
< |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
756 |
< |
if (policy != null) { |
757 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
758 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
759 |
< |
p.setRemoveOnCancelPolicy(policy); |
753 |
> |
final boolean effectiveDelayedPolicy = (maybe != NO); |
754 |
> |
final boolean effectivePeriodicPolicy = (maybe == YES); |
755 |
> |
final boolean effectiveRemovePolicy = (maybe == YES); |
756 |
> |
if (maybe != UNSPECIFIED) { |
757 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(maybe == YES); |
758 |
> |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(maybe == YES); |
759 |
> |
p.setRemoveOnCancelPolicy(maybe == YES); |
760 |
|
} |
761 |
|
assertEquals(effectiveDelayedPolicy, |
762 |
|
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
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)); |
788 |
< |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
789 |
< |
1, 1, MILLISECONDS)); |
786 |
> |
periodics.add(p.scheduleAtFixedRate( |
787 |
> |
countDowner(periodicLatch1), 1, 1, MILLISECONDS)); |
788 |
> |
periodics.add(p.scheduleWithFixedDelay( |
789 |
> |
countDowner(periodicLatch2), 1, 1, MILLISECONDS)); |
790 |
|
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
791 |
|
|
792 |
|
assertTrue(p.getQueue().containsAll(periodics)); |
808 |
|
assertEquals(effectiveDelayedPolicy, |
809 |
|
p.getQueue().containsAll(delayeds)); |
810 |
|
} |
811 |
< |
// Release all pool threads |
803 |
< |
unblock.countDown(); |
811 |
> |
unblock.countDown(); // Release all pool threads |
812 |
|
|
813 |
< |
for (Future<?> delayed : delayeds) { |
814 |
< |
if (effectiveDelayedPolicy) { |
807 |
< |
assertNull(delayed.get()); |
808 |
< |
} |
809 |
< |
} |
813 |
> |
if (effectiveDelayedPolicy) |
814 |
> |
for (Future<?> delayed : delayeds) assertNull(delayed.get()); |
815 |
|
if (effectivePeriodicPolicy) { |
816 |
< |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
817 |
< |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
816 |
> |
await(periodicLatch1); |
817 |
> |
await(periodicLatch2); |
818 |
|
for (Future<?> periodic : periodics) { |
819 |
|
assertTrue(periodic.cancel(false)); |
820 |
|
assertTrue(periodic.isCancelled()); |
821 |
|
assertTrue(periodic.isDone()); |
822 |
|
} |
823 |
|
} |
824 |
+ |
for (Future<?> blocker : blockers) assertNull(blocker.get()); |
825 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
826 |
|
assertTrue(p.isTerminated()); |
827 |
+ |
|
828 |
+ |
for (Future<?> future : delayeds) { |
829 |
+ |
assertTrue(effectiveDelayedPolicy ^ future.isCancelled()); |
830 |
+ |
assertTrue(future.isDone()); |
831 |
+ |
} |
832 |
+ |
for (Future<?> future : periodics) |
833 |
+ |
assertTrue(future.isCancelled()); |
834 |
+ |
for (Future<?> future : blockers) |
835 |
+ |
assertNull(future.get()); |
836 |
|
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
837 |
|
}} |
838 |
|
|
905 |
|
CountDownLatch latch = new CountDownLatch(1); |
906 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
907 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
908 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
908 |
> |
List<Callable<String>> l = new ArrayList<>(); |
909 |
|
l.add(latchAwaitingStringTask(latch)); |
910 |
|
l.add(null); |
911 |
|
try { |
922 |
|
public void testInvokeAny4() throws Exception { |
923 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
924 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
925 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
925 |
> |
List<Callable<String>> l = new ArrayList<>(); |
926 |
|
l.add(new NPETask()); |
927 |
|
try { |
928 |
|
e.invokeAny(l); |
939 |
|
public void testInvokeAny5() throws Exception { |
940 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
941 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
942 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
942 |
> |
List<Callable<String>> l = new ArrayList<>(); |
943 |
|
l.add(new StringTask()); |
944 |
|
l.add(new StringTask()); |
945 |
|
String result = e.invokeAny(l); |
977 |
|
public void testInvokeAll3() throws Exception { |
978 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
979 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
980 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
980 |
> |
List<Callable<String>> l = new ArrayList<>(); |
981 |
|
l.add(new StringTask()); |
982 |
|
l.add(null); |
983 |
|
try { |
993 |
|
public void testInvokeAll4() throws Exception { |
994 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
995 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
996 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
996 |
> |
List<Callable<String>> l = new ArrayList<>(); |
997 |
|
l.add(new NPETask()); |
998 |
|
List<Future<String>> futures = e.invokeAll(l); |
999 |
|
assertEquals(1, futures.size()); |
1012 |
|
public void testInvokeAll5() throws Exception { |
1013 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1014 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1015 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1015 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1016 |
|
l.add(new StringTask()); |
1017 |
|
l.add(new StringTask()); |
1018 |
|
List<Future<String>> futures = e.invokeAll(l); |
1041 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1042 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1043 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1044 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1044 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1045 |
|
l.add(new StringTask()); |
1046 |
|
try { |
1047 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1070 |
|
CountDownLatch latch = new CountDownLatch(1); |
1071 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1072 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1073 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1073 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1074 |
|
l.add(latchAwaitingStringTask(latch)); |
1075 |
|
l.add(null); |
1076 |
|
try { |
1088 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1089 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1090 |
|
long startTime = System.nanoTime(); |
1091 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1091 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1092 |
|
l.add(new NPETask()); |
1093 |
|
try { |
1094 |
|
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1107 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1108 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1109 |
|
long startTime = System.nanoTime(); |
1110 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1110 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1111 |
|
l.add(new StringTask()); |
1112 |
|
l.add(new StringTask()); |
1113 |
|
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1135 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1136 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1137 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1138 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1138 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1139 |
|
l.add(new StringTask()); |
1140 |
|
try { |
1141 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1162 |
|
public void testTimedInvokeAll3() throws Exception { |
1163 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1164 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1165 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1165 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1166 |
|
l.add(new StringTask()); |
1167 |
|
l.add(null); |
1168 |
|
try { |
1178 |
|
public void testTimedInvokeAll4() throws Exception { |
1179 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1180 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1181 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1181 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1182 |
|
l.add(new NPETask()); |
1183 |
|
List<Future<String>> futures = |
1184 |
|
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1198 |
|
public void testTimedInvokeAll5() throws Exception { |
1199 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1200 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1201 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1201 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1202 |
|
l.add(new StringTask()); |
1203 |
|
l.add(new StringTask()); |
1204 |
|
List<Future<String>> futures = |