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; |
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; |
31 |
+ |
import java.util.concurrent.atomic.AtomicLong; |
32 |
+ |
import java.util.stream.Stream; |
33 |
|
|
34 |
|
import junit.framework.Test; |
35 |
|
import junit.framework.TestSuite; |
52 |
|
final Runnable task = new CheckedRunnable() { |
53 |
|
public void realRun() { done.countDown(); }}; |
54 |
|
p.execute(task); |
55 |
< |
assertTrue(done.await(LONG_DELAY_MS, MILLISECONDS)); |
55 |
> |
await(done); |
56 |
|
} |
57 |
|
} |
58 |
|
|
147 |
|
} |
148 |
|
|
149 |
|
/** |
150 |
< |
* scheduleAtFixedRate executes series of tasks at given rate |
150 |
> |
* scheduleAtFixedRate executes series of tasks at given rate. |
151 |
> |
* Eventually, it must hold that: |
152 |
> |
* cycles - 1 <= elapsedMillis/delay < cycles |
153 |
|
*/ |
154 |
|
public void testFixedRateSequence() throws InterruptedException { |
155 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
156 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
157 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
158 |
< |
long startTime = System.nanoTime(); |
159 |
< |
int cycles = 10; |
158 |
> |
final long startTime = System.nanoTime(); |
159 |
> |
final int cycles = 8; |
160 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
161 |
< |
Runnable task = new CheckedRunnable() { |
161 |
> |
final Runnable task = new CheckedRunnable() { |
162 |
|
public void realRun() { done.countDown(); }}; |
163 |
< |
ScheduledFuture h = |
163 |
> |
final ScheduledFuture periodicTask = |
164 |
|
p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); |
165 |
< |
await(done); |
166 |
< |
h.cancel(true); |
167 |
< |
double normalizedTime = |
168 |
< |
(double) millisElapsedSince(startTime) / delay; |
169 |
< |
if (normalizedTime >= cycles - 1 && |
170 |
< |
normalizedTime <= cycles) |
165 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
166 |
> |
await(done, totalDelayMillis + LONG_DELAY_MS); |
167 |
> |
periodicTask.cancel(true); |
168 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
169 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
170 |
> |
if (elapsedMillis <= cycles * delay) |
171 |
|
return; |
172 |
+ |
// else retry with longer delay |
173 |
|
} |
174 |
< |
throw new AssertionError("unexpected execution rate"); |
174 |
> |
fail("unexpected execution rate"); |
175 |
|
} |
176 |
|
} |
177 |
|
|
178 |
|
/** |
179 |
< |
* scheduleWithFixedDelay executes series of tasks with given period |
179 |
> |
* scheduleWithFixedDelay executes series of tasks with given period. |
180 |
> |
* Eventually, it must hold that each task starts at least delay and at |
181 |
> |
* most 2 * delay after the termination of the previous task. |
182 |
|
*/ |
183 |
|
public void testFixedDelaySequence() throws InterruptedException { |
184 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
185 |
|
try (PoolCleaner cleaner = cleaner(p)) { |
186 |
|
for (int delay = 1; delay <= LONG_DELAY_MS; delay *= 3) { |
187 |
< |
long startTime = System.nanoTime(); |
188 |
< |
int cycles = 10; |
187 |
> |
final long startTime = System.nanoTime(); |
188 |
> |
final AtomicLong previous = new AtomicLong(startTime); |
189 |
> |
final AtomicBoolean tryLongerDelay = new AtomicBoolean(false); |
190 |
> |
final int cycles = 8; |
191 |
|
final CountDownLatch done = new CountDownLatch(cycles); |
192 |
< |
Runnable task = new CheckedRunnable() { |
193 |
< |
public void realRun() { done.countDown(); }}; |
194 |
< |
ScheduledFuture h = |
192 |
> |
final int d = delay; |
193 |
> |
final Runnable task = new CheckedRunnable() { |
194 |
> |
public void realRun() { |
195 |
> |
long now = System.nanoTime(); |
196 |
> |
long elapsedMillis |
197 |
> |
= NANOSECONDS.toMillis(now - previous.get()); |
198 |
> |
if (done.getCount() == cycles) { // first execution |
199 |
> |
if (elapsedMillis >= d) |
200 |
> |
tryLongerDelay.set(true); |
201 |
> |
} else { |
202 |
> |
assertTrue(elapsedMillis >= d); |
203 |
> |
if (elapsedMillis >= 2 * d) |
204 |
> |
tryLongerDelay.set(true); |
205 |
> |
} |
206 |
> |
previous.set(now); |
207 |
> |
done.countDown(); |
208 |
> |
}}; |
209 |
> |
final ScheduledFuture periodicTask = |
210 |
|
p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); |
211 |
< |
await(done); |
212 |
< |
h.cancel(true); |
213 |
< |
double normalizedTime = |
214 |
< |
(double) millisElapsedSince(startTime) / delay; |
215 |
< |
if (normalizedTime >= cycles - 1 && |
216 |
< |
normalizedTime <= cycles) |
211 |
> |
final int totalDelayMillis = (cycles - 1) * delay; |
212 |
> |
await(done, totalDelayMillis + cycles * LONG_DELAY_MS); |
213 |
> |
periodicTask.cancel(true); |
214 |
> |
final long elapsedMillis = millisElapsedSince(startTime); |
215 |
> |
assertTrue(elapsedMillis >= totalDelayMillis); |
216 |
> |
if (!tryLongerDelay.get()) |
217 |
|
return; |
218 |
+ |
// else retry with longer delay |
219 |
|
} |
220 |
< |
throw new AssertionError("unexpected execution rate"); |
220 |
> |
fail("unexpected execution rate"); |
221 |
|
} |
222 |
|
} |
223 |
|
|
364 |
|
public void realRun() throws InterruptedException { |
365 |
|
threadStarted.countDown(); |
366 |
|
assertEquals(0, p.getCompletedTaskCount()); |
367 |
< |
threadProceed.await(); |
367 |
> |
await(threadProceed); |
368 |
|
threadDone.countDown(); |
369 |
|
}}); |
370 |
|
await(threadStarted); |
371 |
|
assertEquals(0, p.getCompletedTaskCount()); |
372 |
|
threadProceed.countDown(); |
373 |
< |
threadDone.await(); |
373 |
> |
await(threadDone); |
374 |
|
long startTime = System.nanoTime(); |
375 |
|
while (p.getCompletedTaskCount() != 1) { |
376 |
|
if (millisElapsedSince(startTime) > LONG_DELAY_MS) |
509 |
|
} |
510 |
|
|
511 |
|
/** |
512 |
+ |
* The default rejected execution handler is AbortPolicy. |
513 |
+ |
*/ |
514 |
+ |
public void testDefaultRejectedExecutionHandler() { |
515 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
516 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
517 |
+ |
assertTrue(p.getRejectedExecutionHandler() |
518 |
+ |
instanceof ThreadPoolExecutor.AbortPolicy); |
519 |
+ |
} |
520 |
+ |
} |
521 |
+ |
|
522 |
+ |
/** |
523 |
|
* isShutdown is false before shutdown, true after |
524 |
|
*/ |
525 |
|
public void testIsShutdown() { |
488 |
– |
|
526 |
|
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
527 |
< |
try { |
528 |
< |
assertFalse(p.isShutdown()); |
529 |
< |
} |
530 |
< |
finally { |
531 |
< |
try { p.shutdown(); } catch (SecurityException ok) { return; } |
527 |
> |
assertFalse(p.isShutdown()); |
528 |
> |
try (PoolCleaner cleaner = cleaner(p)) { |
529 |
> |
try { |
530 |
> |
p.shutdown(); |
531 |
> |
assertTrue(p.isShutdown()); |
532 |
> |
} catch (SecurityException ok) {} |
533 |
|
} |
496 |
– |
assertTrue(p.isShutdown()); |
534 |
|
} |
535 |
|
|
536 |
|
/** |
746 |
|
* - setContinueExistingPeriodicTasksAfterShutdownPolicy |
747 |
|
*/ |
748 |
|
public void testShutdown_cancellation() throws Exception { |
749 |
< |
Boolean[] allBooleans = { null, Boolean.FALSE, Boolean.TRUE }; |
713 |
< |
for (Boolean policy : allBooleans) |
714 |
< |
{ |
715 |
< |
final int poolSize = 2; |
749 |
> |
final int poolSize = 6; |
750 |
|
final ScheduledThreadPoolExecutor p |
751 |
|
= new ScheduledThreadPoolExecutor(poolSize); |
752 |
< |
final boolean effectiveDelayedPolicy = (policy != Boolean.FALSE); |
753 |
< |
final boolean effectivePeriodicPolicy = (policy == Boolean.TRUE); |
754 |
< |
final boolean effectiveRemovePolicy = (policy == Boolean.TRUE); |
755 |
< |
if (policy != null) { |
756 |
< |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy(policy); |
757 |
< |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy(policy); |
758 |
< |
p.setRemoveOnCancelPolicy(policy); |
759 |
< |
} |
752 |
> |
final BlockingQueue<Runnable> q = p.getQueue(); |
753 |
> |
final ThreadLocalRandom rnd = ThreadLocalRandom.current(); |
754 |
> |
final boolean effectiveDelayedPolicy; |
755 |
> |
final boolean effectivePeriodicPolicy; |
756 |
> |
final boolean effectiveRemovePolicy; |
757 |
> |
|
758 |
> |
if (rnd.nextBoolean()) |
759 |
> |
p.setExecuteExistingDelayedTasksAfterShutdownPolicy( |
760 |
> |
effectiveDelayedPolicy = rnd.nextBoolean()); |
761 |
> |
else |
762 |
> |
effectiveDelayedPolicy = true; |
763 |
|
assertEquals(effectiveDelayedPolicy, |
764 |
|
p.getExecuteExistingDelayedTasksAfterShutdownPolicy()); |
765 |
+ |
|
766 |
+ |
if (rnd.nextBoolean()) |
767 |
+ |
p.setContinueExistingPeriodicTasksAfterShutdownPolicy( |
768 |
+ |
effectivePeriodicPolicy = rnd.nextBoolean()); |
769 |
+ |
else |
770 |
+ |
effectivePeriodicPolicy = false; |
771 |
|
assertEquals(effectivePeriodicPolicy, |
772 |
|
p.getContinueExistingPeriodicTasksAfterShutdownPolicy()); |
773 |
+ |
|
774 |
+ |
if (rnd.nextBoolean()) |
775 |
+ |
p.setRemoveOnCancelPolicy( |
776 |
+ |
effectiveRemovePolicy = rnd.nextBoolean()); |
777 |
+ |
else |
778 |
+ |
effectiveRemovePolicy = false; |
779 |
|
assertEquals(effectiveRemovePolicy, |
780 |
|
p.getRemoveOnCancelPolicy()); |
781 |
< |
// Strategy: Wedge the pool with poolSize "blocker" threads |
781 |
> |
|
782 |
> |
// System.err.println("effectiveDelayedPolicy="+effectiveDelayedPolicy); |
783 |
> |
// System.err.println("effectivePeriodicPolicy="+effectivePeriodicPolicy); |
784 |
> |
// System.err.println("effectiveRemovePolicy="+effectiveRemovePolicy); |
785 |
> |
|
786 |
> |
// Strategy: Wedge the pool with one wave of "blocker" tasks, |
787 |
> |
// then add a second wave that waits in the queue. |
788 |
|
final AtomicInteger ran = new AtomicInteger(0); |
789 |
|
final CountDownLatch poolBlocked = new CountDownLatch(poolSize); |
790 |
|
final CountDownLatch unblock = new CountDownLatch(1); |
791 |
< |
final CountDownLatch periodicLatch1 = new CountDownLatch(2); |
792 |
< |
final CountDownLatch periodicLatch2 = new CountDownLatch(2); |
793 |
< |
Runnable task = new CheckedRunnable() { public void realRun() |
794 |
< |
throws InterruptedException { |
795 |
< |
poolBlocked.countDown(); |
796 |
< |
assertTrue(unblock.await(LONG_DELAY_MS, MILLISECONDS)); |
797 |
< |
ran.getAndIncrement(); |
798 |
< |
}}; |
799 |
< |
List<Future<?>> blockers = new ArrayList<>(); |
800 |
< |
List<Future<?>> periodics = new ArrayList<>(); |
801 |
< |
List<Future<?>> delayeds = new ArrayList<>(); |
802 |
< |
for (int i = 0; i < poolSize; i++) |
803 |
< |
blockers.add(p.submit(task)); |
804 |
< |
assertTrue(poolBlocked.await(LONG_DELAY_MS, MILLISECONDS)); |
805 |
< |
|
806 |
< |
periodics.add(p.scheduleAtFixedRate(countDowner(periodicLatch1), |
807 |
< |
1, 1, MILLISECONDS)); |
808 |
< |
periodics.add(p.scheduleWithFixedDelay(countDowner(periodicLatch2), |
809 |
< |
1, 1, MILLISECONDS)); |
791 |
> |
|
792 |
> |
class Task extends CheckedRunnable { |
793 |
> |
public void realRun() throws InterruptedException { |
794 |
> |
ran.getAndIncrement(); |
795 |
> |
poolBlocked.countDown(); |
796 |
> |
await(unblock); |
797 |
> |
} |
798 |
> |
} |
799 |
> |
|
800 |
> |
class PeriodicTask extends Task { |
801 |
> |
PeriodicTask(int rounds) { this.rounds = rounds; } |
802 |
> |
int rounds; |
803 |
> |
public void realRun() throws InterruptedException { |
804 |
> |
if (--rounds == 0) super.realRun(); |
805 |
> |
} |
806 |
> |
} |
807 |
> |
|
808 |
> |
Runnable task = new Task(); |
809 |
> |
|
810 |
> |
List<Future<?>> immediates = new ArrayList<>(); |
811 |
> |
List<Future<?>> delayeds = new ArrayList<>(); |
812 |
> |
List<Future<?>> periodics = new ArrayList<>(); |
813 |
> |
|
814 |
> |
immediates.add(p.submit(task)); |
815 |
|
delayeds.add(p.schedule(task, 1, MILLISECONDS)); |
816 |
+ |
for (int rounds : new int[] { 1, 2 }) { |
817 |
+ |
periodics.add(p.scheduleAtFixedRate( |
818 |
+ |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
819 |
+ |
periodics.add(p.scheduleWithFixedDelay( |
820 |
+ |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
821 |
+ |
} |
822 |
+ |
|
823 |
+ |
await(poolBlocked); |
824 |
+ |
|
825 |
+ |
assertEquals(poolSize, ran.get()); |
826 |
+ |
assertTrue(q.isEmpty()); |
827 |
+ |
|
828 |
+ |
// Add second wave of tasks. |
829 |
+ |
immediates.add(p.submit(task)); |
830 |
+ |
long delay_ms = effectiveDelayedPolicy ? 1 : LONG_DELAY_MS; |
831 |
+ |
delayeds.add(p.schedule(task, delay_ms, MILLISECONDS)); |
832 |
+ |
for (int rounds : new int[] { 1, 2 }) { |
833 |
+ |
periodics.add(p.scheduleAtFixedRate( |
834 |
+ |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
835 |
+ |
periodics.add(p.scheduleWithFixedDelay( |
836 |
+ |
new PeriodicTask(rounds), 1, 1, MILLISECONDS)); |
837 |
+ |
} |
838 |
+ |
|
839 |
+ |
assertEquals(poolSize, q.size()); |
840 |
+ |
assertEquals(poolSize, ran.get()); |
841 |
+ |
|
842 |
+ |
immediates.forEach( |
843 |
+ |
f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); |
844 |
+ |
|
845 |
+ |
Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) |
846 |
+ |
.forEach(f -> assertFalse(f.isDone())); |
847 |
|
|
757 |
– |
assertTrue(p.getQueue().containsAll(periodics)); |
758 |
– |
assertTrue(p.getQueue().containsAll(delayeds)); |
848 |
|
try { p.shutdown(); } catch (SecurityException ok) { return; } |
849 |
|
assertTrue(p.isShutdown()); |
850 |
+ |
assertTrue(p.isTerminating()); |
851 |
|
assertFalse(p.isTerminated()); |
852 |
< |
for (Future<?> periodic : periodics) { |
853 |
< |
assertTrue(effectivePeriodicPolicy ^ periodic.isCancelled()); |
854 |
< |
assertTrue(effectivePeriodicPolicy ^ periodic.isDone()); |
855 |
< |
} |
856 |
< |
for (Future<?> delayed : delayeds) { |
857 |
< |
assertTrue(effectiveDelayedPolicy ^ delayed.isCancelled()); |
858 |
< |
assertTrue(effectiveDelayedPolicy ^ delayed.isDone()); |
859 |
< |
} |
852 |
> |
|
853 |
> |
if (rnd.nextBoolean()) |
854 |
> |
assertThrows( |
855 |
> |
RejectedExecutionException.class, |
856 |
> |
() -> p.submit(task), |
857 |
> |
() -> p.schedule(task, 1, SECONDS), |
858 |
> |
() -> p.scheduleAtFixedRate( |
859 |
> |
new PeriodicTask(1), 1, 1, SECONDS), |
860 |
> |
() -> p.scheduleWithFixedDelay( |
861 |
> |
new PeriodicTask(2), 1, 1, SECONDS)); |
862 |
> |
|
863 |
> |
assertTrue(q.contains(immediates.get(1))); |
864 |
> |
assertTrue(!effectiveDelayedPolicy |
865 |
> |
^ q.contains(delayeds.get(1))); |
866 |
> |
assertTrue(!effectivePeriodicPolicy |
867 |
> |
^ q.containsAll(periodics.subList(4, 8))); |
868 |
> |
|
869 |
> |
immediates.forEach(f -> assertFalse(f.isDone())); |
870 |
> |
|
871 |
> |
assertFalse(delayeds.get(0).isDone()); |
872 |
> |
if (effectiveDelayedPolicy) |
873 |
> |
assertFalse(delayeds.get(1).isDone()); |
874 |
> |
else |
875 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
876 |
> |
|
877 |
|
if (testImplementationDetails) { |
878 |
< |
assertEquals(effectivePeriodicPolicy, |
879 |
< |
p.getQueue().containsAll(periodics)); |
880 |
< |
assertEquals(effectiveDelayedPolicy, |
881 |
< |
p.getQueue().containsAll(delayeds)); |
882 |
< |
} |
883 |
< |
// Release all pool threads |
884 |
< |
unblock.countDown(); |
885 |
< |
|
886 |
< |
for (Future<?> delayed : delayeds) { |
887 |
< |
if (effectiveDelayedPolicy) { |
781 |
< |
assertNull(delayed.get()); |
782 |
< |
} |
783 |
< |
} |
784 |
< |
if (effectivePeriodicPolicy) { |
785 |
< |
assertTrue(periodicLatch1.await(LONG_DELAY_MS, MILLISECONDS)); |
786 |
< |
assertTrue(periodicLatch2.await(LONG_DELAY_MS, MILLISECONDS)); |
787 |
< |
for (Future<?> periodic : periodics) { |
788 |
< |
assertTrue(periodic.cancel(false)); |
789 |
< |
assertTrue(periodic.isCancelled()); |
790 |
< |
assertTrue(periodic.isDone()); |
878 |
> |
if (effectivePeriodicPolicy) |
879 |
> |
// TODO: ensure periodic tasks continue executing |
880 |
> |
periodics.forEach( |
881 |
> |
f -> { |
882 |
> |
assertFalse(f.isDone()); |
883 |
> |
assertTrue(f.cancel(false)); |
884 |
> |
}); |
885 |
> |
else { |
886 |
> |
periodics.subList(0, 4).forEach(f -> assertFalse(f.isDone())); |
887 |
> |
periodics.subList(4, 8).forEach(f -> assertTrue(f.isCancelled())); |
888 |
|
} |
889 |
|
} |
890 |
+ |
|
891 |
+ |
unblock.countDown(); // Release all pool threads |
892 |
+ |
|
893 |
|
assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); |
894 |
+ |
assertFalse(p.isTerminating()); |
895 |
|
assertTrue(p.isTerminated()); |
896 |
< |
assertEquals(2 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
897 |
< |
}} |
896 |
> |
|
897 |
> |
assertTrue(q.isEmpty()); |
898 |
> |
|
899 |
> |
for (Future<?> f : immediates) assertNull(f.get()); |
900 |
> |
|
901 |
> |
assertNull(delayeds.get(0).get()); |
902 |
> |
if (effectiveDelayedPolicy) |
903 |
> |
assertNull(delayeds.get(1).get()); |
904 |
> |
else |
905 |
> |
assertTrue(delayeds.get(1).isCancelled()); |
906 |
> |
|
907 |
> |
periodics.forEach(f -> assertTrue(f.isDone())); |
908 |
> |
periodics.forEach(f -> assertTrue(f.isCancelled())); |
909 |
> |
|
910 |
> |
assertEquals(poolSize + 1 + (effectiveDelayedPolicy ? 1 : 0), ran.get()); |
911 |
> |
} |
912 |
|
|
913 |
|
/** |
914 |
|
* completed submit of callable returns result |
979 |
|
CountDownLatch latch = new CountDownLatch(1); |
980 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
981 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
982 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
982 |
> |
List<Callable<String>> l = new ArrayList<>(); |
983 |
|
l.add(latchAwaitingStringTask(latch)); |
984 |
|
l.add(null); |
985 |
|
try { |
996 |
|
public void testInvokeAny4() throws Exception { |
997 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
998 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
999 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
999 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1000 |
|
l.add(new NPETask()); |
1001 |
|
try { |
1002 |
|
e.invokeAny(l); |
1013 |
|
public void testInvokeAny5() throws Exception { |
1014 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1015 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1016 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1016 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1017 |
|
l.add(new StringTask()); |
1018 |
|
l.add(new StringTask()); |
1019 |
|
String result = e.invokeAny(l); |
1051 |
|
public void testInvokeAll3() throws Exception { |
1052 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1053 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1054 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1054 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1055 |
|
l.add(new StringTask()); |
1056 |
|
l.add(null); |
1057 |
|
try { |
1067 |
|
public void testInvokeAll4() throws Exception { |
1068 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1069 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1070 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1070 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1071 |
|
l.add(new NPETask()); |
1072 |
|
List<Future<String>> futures = e.invokeAll(l); |
1073 |
|
assertEquals(1, futures.size()); |
1086 |
|
public void testInvokeAll5() throws Exception { |
1087 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1088 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1089 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1089 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1090 |
|
l.add(new StringTask()); |
1091 |
|
l.add(new StringTask()); |
1092 |
|
List<Future<String>> futures = e.invokeAll(l); |
1115 |
|
public void testTimedInvokeAnyNullTimeUnit() throws Exception { |
1116 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1117 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1118 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1118 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1119 |
|
l.add(new StringTask()); |
1120 |
|
try { |
1121 |
|
e.invokeAny(l, MEDIUM_DELAY_MS, null); |
1144 |
|
CountDownLatch latch = new CountDownLatch(1); |
1145 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1146 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1147 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1147 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1148 |
|
l.add(latchAwaitingStringTask(latch)); |
1149 |
|
l.add(null); |
1150 |
|
try { |
1162 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1163 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1164 |
|
long startTime = System.nanoTime(); |
1165 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1165 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1166 |
|
l.add(new NPETask()); |
1167 |
|
try { |
1168 |
|
e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1181 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1182 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1183 |
|
long startTime = System.nanoTime(); |
1184 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1184 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1185 |
|
l.add(new StringTask()); |
1186 |
|
l.add(new StringTask()); |
1187 |
|
String result = e.invokeAny(l, LONG_DELAY_MS, MILLISECONDS); |
1209 |
|
public void testTimedInvokeAllNullTimeUnit() throws Exception { |
1210 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1211 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1212 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1212 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1213 |
|
l.add(new StringTask()); |
1214 |
|
try { |
1215 |
|
e.invokeAll(l, MEDIUM_DELAY_MS, null); |
1236 |
|
public void testTimedInvokeAll3() throws Exception { |
1237 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1238 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1239 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1239 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1240 |
|
l.add(new StringTask()); |
1241 |
|
l.add(null); |
1242 |
|
try { |
1252 |
|
public void testTimedInvokeAll4() throws Exception { |
1253 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1254 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1255 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1255 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1256 |
|
l.add(new NPETask()); |
1257 |
|
List<Future<String>> futures = |
1258 |
|
e.invokeAll(l, LONG_DELAY_MS, MILLISECONDS); |
1272 |
|
public void testTimedInvokeAll5() throws Exception { |
1273 |
|
final ExecutorService e = new ScheduledThreadPoolExecutor(2); |
1274 |
|
try (PoolCleaner cleaner = cleaner(e)) { |
1275 |
< |
List<Callable<String>> l = new ArrayList<Callable<String>>(); |
1275 |
> |
List<Callable<String>> l = new ArrayList<>(); |
1276 |
|
l.add(new StringTask()); |
1277 |
|
l.add(new StringTask()); |
1278 |
|
List<Future<String>> futures = |
1321 |
|
} |
1322 |
|
} |
1323 |
|
|
1324 |
+ |
/** |
1325 |
+ |
* A fixed delay task with overflowing period should not prevent a |
1326 |
+ |
* one-shot task from executing. |
1327 |
+ |
* https://bugs.openjdk.java.net/browse/JDK-8051859 |
1328 |
+ |
*/ |
1329 |
+ |
public void testScheduleWithFixedDelay_overflow() throws Exception { |
1330 |
+ |
final CountDownLatch delayedDone = new CountDownLatch(1); |
1331 |
+ |
final CountDownLatch immediateDone = new CountDownLatch(1); |
1332 |
+ |
final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); |
1333 |
+ |
try (PoolCleaner cleaner = cleaner(p)) { |
1334 |
+ |
final Runnable immediate = new Runnable() { public void run() { |
1335 |
+ |
immediateDone.countDown(); |
1336 |
+ |
}}; |
1337 |
+ |
final Runnable delayed = new Runnable() { public void run() { |
1338 |
+ |
delayedDone.countDown(); |
1339 |
+ |
p.submit(immediate); |
1340 |
+ |
}}; |
1341 |
+ |
p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); |
1342 |
+ |
await(delayedDone); |
1343 |
+ |
await(immediateDone); |
1344 |
+ |
} |
1345 |
+ |
} |
1346 |
+ |
|
1347 |
|
} |