# HG changeset patch # User dl # Date 1510272217 28800 # Node ID 53020d8cdf5be243d3746da9713fe97c4c2fa605 # Parent a85bb15efb57e77951c4b0e48415c7a51a7a1a4c 8190747: ExecutorService/Invoke.java fails intermittently Reviewed-by: martin, psandoz, dholmes diff -r a85bb15efb57 -r 53020d8cdf5b test/jdk/java/util/concurrent/ExecutorService/Invoke.java --- a/test/jdk/java/util/concurrent/ExecutorService/Invoke.java Thu Nov 09 13:30:03 2017 -0800 +++ b/test/jdk/java/util/concurrent/ExecutorService/Invoke.java Thu Nov 09 16:03:37 2017 -0800 @@ -35,6 +35,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; import java.util.concurrent.Callable; +import java.util.concurrent.CountDownLatch; import java.util.concurrent.CyclicBarrier; import java.util.concurrent.ExecutorService; import java.util.concurrent.Executors; @@ -140,13 +141,17 @@ final boolean timed = rnd.nextBoolean(); final ExecutorService pool = Executors.newSingleThreadExecutor(); final AtomicLong count = new AtomicLong(0); + final CountDownLatch invokeAnyDone = new CountDownLatch(1); class Task implements Callable { public Long call() throws Exception { long x = count.incrementAndGet(); check(x <= 2); - if (x == 2) - // wait for main thread to interrupt us + if (x == 2) { + // wait for main thread to interrupt us ... awaitInterrupt(timeoutSeconds); + // ... and then for invokeAny to return + check(invokeAnyDone.await(timeoutSeconds, SECONDS)); + } return x; } } @@ -166,6 +171,7 @@ else val = pool.invokeAny(tasks); check(val == 1); + invokeAnyDone.countDown(); // inherent race between main thread interrupt and // start of second task