--- jsr166/src/test/tck/ScheduledExecutorTest.java 2017/03/28 23:21:24 1.90 +++ jsr166/src/test/tck/ScheduledExecutorTest.java 2021/01/27 01:57:24 1.100 @@ -11,6 +11,8 @@ import static java.util.concurrent.TimeU import static java.util.concurrent.TimeUnit.SECONDS; import java.util.ArrayList; +import java.util.Collection; +import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.concurrent.BlockingQueue; @@ -64,16 +66,16 @@ public class ScheduledExecutorTest exten try (PoolCleaner cleaner = cleaner(p)) { final long startTime = System.nanoTime(); final CountDownLatch done = new CountDownLatch(1); - Callable task = new CheckedCallable() { + Callable task = new CheckedCallable<>() { public Boolean realCall() { done.countDown(); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); return Boolean.TRUE; }}; - Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); + Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); assertSame(Boolean.TRUE, f.get()); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); - assertTrue(done.await(0L, MILLISECONDS)); + assertEquals(0L, done.getCount()); } } @@ -90,7 +92,7 @@ public class ScheduledExecutorTest exten done.countDown(); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); }}; - Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); + Future f = p.schedule(task, timeoutMillis(), MILLISECONDS); await(done); assertNull(f.get(LONG_DELAY_MS, MILLISECONDS)); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); @@ -110,7 +112,7 @@ public class ScheduledExecutorTest exten done.countDown(); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); }}; - ScheduledFuture f = + ScheduledFuture f = p.scheduleAtFixedRate(task, timeoutMillis(), LONG_DELAY_MS, MILLISECONDS); await(done); @@ -132,7 +134,7 @@ public class ScheduledExecutorTest exten done.countDown(); assertTrue(millisElapsedSince(startTime) >= timeoutMillis()); }}; - ScheduledFuture f = + ScheduledFuture f = p.scheduleWithFixedDelay(task, timeoutMillis(), LONG_DELAY_MS, MILLISECONDS); await(done); @@ -160,7 +162,7 @@ public class ScheduledExecutorTest exten final CountDownLatch done = new CountDownLatch(cycles); final Runnable task = new CheckedRunnable() { public void realRun() { done.countDown(); }}; - final ScheduledFuture periodicTask = + final ScheduledFuture periodicTask = p.scheduleAtFixedRate(task, 0, delay, MILLISECONDS); final int totalDelayMillis = (cycles - 1) * delay; await(done, totalDelayMillis + LONG_DELAY_MS); @@ -206,7 +208,7 @@ public class ScheduledExecutorTest exten previous.set(now); done.countDown(); }}; - final ScheduledFuture periodicTask = + final ScheduledFuture periodicTask = p.scheduleWithFixedDelay(task, 0, delay, MILLISECONDS); final int totalDelayMillis = (cycles - 1) * delay; await(done, totalDelayMillis + cycles * LONG_DELAY_MS); @@ -222,110 +224,67 @@ public class ScheduledExecutorTest exten } /** - * execute(null) throws NPE + * Submitting null tasks throws NullPointerException */ - public void testExecuteNull() throws InterruptedException { + public void testNullTaskSubmission() { final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p)) { - try { - p.execute(null); - shouldThrow(); - } catch (NullPointerException success) {} + assertNullTaskSubmissionThrowsNullPointerException(p); } } /** - * schedule(null) throws NPE + * Submitted tasks are rejected when shutdown */ - public void testScheduleNull() throws InterruptedException { + public void testSubmittedTasksRejectedWhenShutdown() throws InterruptedException { final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - TrackedCallable callable = null; - Future f = p.schedule(callable, SHORT_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (NullPointerException success) {} - } - } + final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + final CountDownLatch threadsStarted = new CountDownLatch(p.getCorePoolSize()); + final CountDownLatch done = new CountDownLatch(1); + final Runnable r = () -> { + threadsStarted.countDown(); + for (;;) { + try { + done.await(); + return; + } catch (InterruptedException shutdownNowDeliberatelyIgnored) {} + }}; + final Callable c = () -> { + threadsStarted.countDown(); + for (;;) { + try { + done.await(); + return Boolean.TRUE; + } catch (InterruptedException shutdownNowDeliberatelyIgnored) {} + }}; - /** - * execute throws RejectedExecutionException if shutdown - */ - public void testSchedule1_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - p.shutdown(); - p.schedule(new NoOpRunnable(), - MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} - } - } + try (PoolCleaner cleaner = cleaner(p, done)) { + for (int i = p.getCorePoolSize(); i--> 0; ) { + switch (rnd.nextInt(4)) { + case 0: p.execute(r); break; + case 1: assertFalse(p.submit(r).isDone()); break; + case 2: assertFalse(p.submit(r, Boolean.TRUE).isDone()); break; + case 3: assertFalse(p.submit(c).isDone()); break; + } + } - /** - * schedule throws RejectedExecutionException if shutdown - */ - public void testSchedule2_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - p.shutdown(); - p.schedule(new NoOpCallable(), - MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} - } - } + // ScheduledThreadPoolExecutor has an unbounded queue, so never saturated. + await(threadsStarted); - /** - * schedule callable throws RejectedExecutionException if shutdown - */ - public void testSchedule3_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { + if (rnd.nextBoolean()) + p.shutdownNow(); + else p.shutdown(); - p.schedule(new NoOpCallable(), - MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} - } - } + // Pool is shutdown, but not yet terminated + assertTaskSubmissionsAreRejected(p); + assertFalse(p.isTerminated()); - /** - * scheduleAtFixedRate throws RejectedExecutionException if shutdown - */ - public void testScheduleAtFixedRate1_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - p.shutdown(); - p.scheduleAtFixedRate(new NoOpRunnable(), - MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} - } - } + done.countDown(); // release blocking tasks + assertTrue(p.awaitTermination(LONG_DELAY_MS, MILLISECONDS)); - /** - * scheduleWithFixedDelay throws RejectedExecutionException if shutdown - */ - public void testScheduleWithFixedDelay1_RejectedExecutionException() throws InterruptedException { - final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - try (PoolCleaner cleaner = cleaner(p)) { - try { - p.shutdown(); - p.scheduleWithFixedDelay(new NoOpRunnable(), - MEDIUM_DELAY_MS, MEDIUM_DELAY_MS, MILLISECONDS); - shouldThrow(); - } catch (RejectedExecutionException success) { - } catch (SecurityException ok) {} + assertTaskSubmissionsAreRejected(p); } + assertEquals(p.getCorePoolSize(), p.getCompletedTaskCount()); } /** @@ -590,7 +549,8 @@ public class ScheduledExecutorTest exten final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p, done)) { final CountDownLatch threadStarted = new CountDownLatch(1); - ScheduledFuture[] tasks = new ScheduledFuture[5]; + @SuppressWarnings("unchecked") + ScheduledFuture[] tasks = (ScheduledFuture[])new ScheduledFuture[5]; for (int i = 0; i < tasks.length; i++) { Runnable r = new CheckedRunnable() { public void realRun() throws InterruptedException { @@ -613,7 +573,8 @@ public class ScheduledExecutorTest exten final CountDownLatch done = new CountDownLatch(1); final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p, done)) { - ScheduledFuture[] tasks = new ScheduledFuture[5]; + @SuppressWarnings("unchecked") + ScheduledFuture[] tasks = (ScheduledFuture[])new ScheduledFuture[5]; final CountDownLatch threadStarted = new CountDownLatch(1); for (int i = 0; i < tasks.length; i++) { Runnable r = new CheckedRunnable() { @@ -641,14 +602,15 @@ public class ScheduledExecutorTest exten * purge eventually removes cancelled tasks from the queue */ public void testPurge() throws InterruptedException { - final ScheduledFuture[] tasks = new ScheduledFuture[5]; + @SuppressWarnings("unchecked") + ScheduledFuture[] tasks = (ScheduledFuture[])new ScheduledFuture[5]; final Runnable releaser = new Runnable() { public void run() { - for (ScheduledFuture task : tasks) + for (ScheduledFuture task : tasks) if (task != null) task.cancel(true); }}; final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p, releaser)) { for (int i = 0; i < tasks.length; i++) - tasks[i] = p.schedule(new SmallPossiblyInterruptedRunnable(), + tasks[i] = p.schedule(possiblyInterruptedRunnable(SMALL_DELAY_MS), LONG_DELAY_MS, MILLISECONDS); int max = tasks.length; if (tasks[4].cancel(true)) --max; @@ -680,7 +642,7 @@ public class ScheduledExecutorTest exten Runnable waiter = new CheckedRunnable() { public void realRun() { threadsStarted.countDown(); try { - MILLISECONDS.sleep(2 * LONG_DELAY_MS); + MILLISECONDS.sleep(LONGER_DELAY_MS); } catch (InterruptedException success) {} ran.getAndIncrement(); }}; @@ -710,7 +672,7 @@ public class ScheduledExecutorTest exten */ public void testShutdownNow_delayedTasks() throws InterruptedException { final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); - List tasks = new ArrayList<>(); + List> tasks = new ArrayList<>(); for (int i = 0; i < 3; i++) { Runnable r = new NoOpRunnable(); tasks.add(p.schedule(r, 9, SECONDS)); @@ -718,7 +680,7 @@ public class ScheduledExecutorTest exten tasks.add(p.scheduleWithFixedDelay(r, 9, 9, SECONDS)); } if (testImplementationDetails) - assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); + assertEquals(new HashSet(tasks), new HashSet(p.getQueue())); final List queuedTasks; try { queuedTasks = p.shutdownNow(); @@ -728,9 +690,9 @@ public class ScheduledExecutorTest exten assertTrue(p.isShutdown()); assertTrue(p.getQueue().isEmpty()); if (testImplementationDetails) - assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); + assertEquals(new HashSet(tasks), new HashSet(queuedTasks)); assertEquals(tasks.size(), queuedTasks.size()); - for (ScheduledFuture task : tasks) { + for (ScheduledFuture task : tasks) { assertFalse(task.isDone()); assertFalse(task.isCancelled()); } @@ -745,12 +707,15 @@ public class ScheduledExecutorTest exten * - setExecuteExistingDelayedTasksAfterShutdownPolicy * - setContinueExistingPeriodicTasksAfterShutdownPolicy */ + @SuppressWarnings("FutureReturnValueIgnored") public void testShutdown_cancellation() throws Exception { - final int poolSize = 6; + final int poolSize = 4; final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(poolSize); final BlockingQueue q = p.getQueue(); final ThreadLocalRandom rnd = ThreadLocalRandom.current(); + final long delay = rnd.nextInt(2); + final int rounds = rnd.nextInt(1, 3); final boolean effectiveDelayedPolicy; final boolean effectivePeriodicPolicy; final boolean effectiveRemovePolicy; @@ -816,29 +781,25 @@ public class ScheduledExecutorTest exten List> periodics = new ArrayList<>(); immediates.add(p.submit(task)); - delayeds.add(p.schedule(task, 1, MILLISECONDS)); - for (int rounds : new int[] { 1, 2 }) { - periodics.add(p.scheduleAtFixedRate( - new PeriodicTask(rounds), 1, 1, MILLISECONDS)); - periodics.add(p.scheduleWithFixedDelay( - new PeriodicTask(rounds), 1, 1, MILLISECONDS)); - } + delayeds.add(p.schedule(task, delay, MILLISECONDS)); + periodics.add(p.scheduleAtFixedRate( + new PeriodicTask(rounds), delay, 1, MILLISECONDS)); + periodics.add(p.scheduleWithFixedDelay( + new PeriodicTask(rounds), delay, 1, MILLISECONDS)); await(poolBlocked); assertEquals(poolSize, ran.get()); + assertEquals(poolSize, p.getActiveCount()); assertTrue(q.isEmpty()); // Add second wave of tasks. immediates.add(p.submit(task)); - long delay_ms = effectiveDelayedPolicy ? 1 : LONG_DELAY_MS; - delayeds.add(p.schedule(task, delay_ms, MILLISECONDS)); - for (int rounds : new int[] { 1, 2 }) { - periodics.add(p.scheduleAtFixedRate( - new PeriodicTask(rounds), 1, 1, MILLISECONDS)); - periodics.add(p.scheduleWithFixedDelay( - new PeriodicTask(rounds), 1, 1, MILLISECONDS)); - } + delayeds.add(p.schedule(task, effectiveDelayedPolicy ? delay : LONG_DELAY_MS, MILLISECONDS)); + periodics.add(p.scheduleAtFixedRate( + new PeriodicTask(rounds), delay, 1, MILLISECONDS)); + periodics.add(p.scheduleWithFixedDelay( + new PeriodicTask(rounds), delay, 1, MILLISECONDS)); assertEquals(poolSize, q.size()); assertEquals(poolSize, ran.get()); @@ -846,7 +807,7 @@ public class ScheduledExecutorTest exten immediates.forEach( f -> assertTrue(((ScheduledFuture)f).getDelay(NANOSECONDS) <= 0L)); - Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) + Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream) .forEach(f -> assertFalse(f.isDone())); try { p.shutdown(); } catch (SecurityException ok) { return; } @@ -868,7 +829,7 @@ public class ScheduledExecutorTest exten assertTrue(!effectiveDelayedPolicy ^ q.contains(delayeds.get(1))); assertTrue(!effectivePeriodicPolicy - ^ q.containsAll(periodics.subList(4, 8))); + ^ q.containsAll(periodics.subList(2, 4))); immediates.forEach(f -> assertFalse(f.isDone())); @@ -878,18 +839,18 @@ public class ScheduledExecutorTest exten else assertTrue(delayeds.get(1).isCancelled()); - if (testImplementationDetails) { - if (effectivePeriodicPolicy) - periodics.forEach( - f -> { - assertFalse(f.isDone()); - if (!periodicTasksContinue) - assertTrue(f.cancel(false)); - }); - else { - periodics.subList(0, 4).forEach(f -> assertFalse(f.isDone())); - periodics.subList(4, 8).forEach(f -> assertTrue(f.isCancelled())); - } + if (effectivePeriodicPolicy) + periodics.forEach( + f -> { + assertFalse(f.isDone()); + if (!periodicTasksContinue) { + assertTrue(f.cancel(false)); + assertTrue(f.isCancelled()); + } + }); + else { + periodics.subList(0, 2).forEach(f -> assertFalse(f.isDone())); + periodics.subList(2, 4).forEach(f -> assertTrue(f.isCancelled())); } unblock.countDown(); // Release all pool threads @@ -900,7 +861,7 @@ public class ScheduledExecutorTest exten assertTrue(q.isEmpty()); - Stream.of(immediates, delayeds, periodics).flatMap(c -> c.stream()) + Stream.of(immediates, delayeds, periodics).flatMap(Collection::stream) .forEach(f -> assertTrue(f.isDone())); for (Future f : immediates) assertNull(f.get()); @@ -925,7 +886,7 @@ public class ScheduledExecutorTest exten assertEquals(poolSize + 1 + (effectiveDelayedPolicy ? 1 : 0) - + (periodicTasksContinue ? 4 : 0), + + (periodicTasksContinue ? 2 : 0), ran.get()); } @@ -966,7 +927,7 @@ public class ScheduledExecutorTest exten } /** - * invokeAny(null) throws NPE + * invokeAny(null) throws NullPointerException */ public void testInvokeAny1() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); @@ -979,7 +940,7 @@ public class ScheduledExecutorTest exten } /** - * invokeAny(empty collection) throws IAE + * invokeAny(empty collection) throws IllegalArgumentException */ public void testInvokeAny2() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); @@ -992,7 +953,7 @@ public class ScheduledExecutorTest exten } /** - * invokeAny(c) throws NPE if c has null elements + * invokeAny(c) throws NullPointerException if c has null elements */ public void testInvokeAny3() throws Exception { CountDownLatch latch = new CountDownLatch(1); @@ -1054,12 +1015,14 @@ public class ScheduledExecutorTest exten } /** - * invokeAll(empty collection) returns empty collection + * invokeAll(empty collection) returns empty list */ public void testInvokeAll2() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); + final Collection> emptyCollection + = Collections.emptyList(); try (PoolCleaner cleaner = cleaner(e)) { - List> r = e.invokeAll(new ArrayList>()); + List> r = e.invokeAll(emptyCollection); assertTrue(r.isEmpty()); } } @@ -1122,14 +1085,14 @@ public class ScheduledExecutorTest exten final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { try { - e.invokeAny(null, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(null, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } } /** - * timed invokeAny(,,null) throws NPE + * timed invokeAny(,,null) throws NullPointerException */ public void testTimedInvokeAnyNullTimeUnit() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); @@ -1137,20 +1100,22 @@ public class ScheduledExecutorTest exten List> l = new ArrayList<>(); l.add(new StringTask()); try { - e.invokeAny(l, MEDIUM_DELAY_MS, null); + e.invokeAny(l, randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} } } /** - * timed invokeAny(empty collection) throws IAE + * timed invokeAny(empty collection) throws IllegalArgumentException */ public void testTimedInvokeAny2() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); + final Collection> emptyCollection + = Collections.emptyList(); try (PoolCleaner cleaner = cleaner(e)) { try { - e.invokeAny(new ArrayList>(), MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(emptyCollection, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (IllegalArgumentException success) {} } @@ -1167,7 +1132,7 @@ public class ScheduledExecutorTest exten l.add(latchAwaitingStringTask(latch)); l.add(null); try { - e.invokeAny(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAny(l, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} latch.countDown(); @@ -1216,7 +1181,7 @@ public class ScheduledExecutorTest exten final ExecutorService e = new ScheduledThreadPoolExecutor(2); try (PoolCleaner cleaner = cleaner(e)) { try { - e.invokeAll(null, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAll(null, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } @@ -1231,20 +1196,22 @@ public class ScheduledExecutorTest exten List> l = new ArrayList<>(); l.add(new StringTask()); try { - e.invokeAll(l, MEDIUM_DELAY_MS, null); + e.invokeAll(l, randomTimeout(), null); shouldThrow(); } catch (NullPointerException success) {} } } /** - * timed invokeAll(empty collection) returns empty collection + * timed invokeAll(empty collection) returns empty list */ public void testTimedInvokeAll2() throws Exception { final ExecutorService e = new ScheduledThreadPoolExecutor(2); + final Collection> emptyCollection + = Collections.emptyList(); try (PoolCleaner cleaner = cleaner(e)) { - List> r = e.invokeAll(new ArrayList>(), - MEDIUM_DELAY_MS, MILLISECONDS); + List> r = + e.invokeAll(emptyCollection, randomTimeout(), randomTimeUnit()); assertTrue(r.isEmpty()); } } @@ -1259,7 +1226,7 @@ public class ScheduledExecutorTest exten l.add(new StringTask()); l.add(null); try { - e.invokeAll(l, MEDIUM_DELAY_MS, MILLISECONDS); + e.invokeAll(l, randomTimeout(), randomTimeUnit()); shouldThrow(); } catch (NullPointerException success) {} } @@ -1308,7 +1275,7 @@ public class ScheduledExecutorTest exten public void testTimedInvokeAll6() throws Exception { for (long timeout = timeoutMillis();;) { final CountDownLatch done = new CountDownLatch(1); - final Callable waiter = new CheckedCallable() { + final Callable waiter = new CheckedCallable<>() { public String realCall() { try { done.await(LONG_DELAY_MS, MILLISECONDS); } catch (InterruptedException ok) {} @@ -1324,7 +1291,7 @@ public class ScheduledExecutorTest exten p.invokeAll(tasks, timeout, MILLISECONDS); assertEquals(tasks.size(), futures.size()); assertTrue(millisElapsedSince(startTime) >= timeout); - for (Future future : futures) + for (Future future : futures) assertTrue(future.isDone()); assertTrue(futures.get(1).isCancelled()); try { @@ -1345,18 +1312,16 @@ public class ScheduledExecutorTest exten * one-shot task from executing. * https://bugs.openjdk.java.net/browse/JDK-8051859 */ + @SuppressWarnings("FutureReturnValueIgnored") public void testScheduleWithFixedDelay_overflow() throws Exception { final CountDownLatch delayedDone = new CountDownLatch(1); final CountDownLatch immediateDone = new CountDownLatch(1); final ScheduledThreadPoolExecutor p = new ScheduledThreadPoolExecutor(1); try (PoolCleaner cleaner = cleaner(p)) { - final Runnable immediate = new Runnable() { public void run() { - immediateDone.countDown(); - }}; - final Runnable delayed = new Runnable() { public void run() { + final Runnable delayed = () -> { delayedDone.countDown(); - p.submit(immediate); - }}; + p.submit(() -> immediateDone.countDown()); + }; p.scheduleWithFixedDelay(delayed, 0L, Long.MAX_VALUE, SECONDS); await(delayedDone); await(immediateDone);