--- old/test/jdk/java/util/concurrent/ExecutorService/Invoke.java 2021-01-09 11:36:02.468886255 -0800 +++ new/test/jdk/java/util/concurrent/ExecutorService/Invoke.java 2021-01-09 11:36:02.164888647 -0800 @@ -84,9 +84,16 @@ public static void main(String[] args) { try { - testInvokeAll(); - testInvokeAny(); - testInvokeAny_cancellationInterrupt(); + for (int nThreads = 1; nThreads <= 6; ++nThreads) { + // untimed + testInvokeAll(nThreads, false); + testInvokeAny(nThreads, false); + testInvokeAny_cancellationInterrupt(nThreads, false); + // timed + testInvokeAll(nThreads, true); + testInvokeAny(nThreads, true); + testInvokeAny_cancellationInterrupt(nThreads, true); + } } catch (Throwable t) { unexpected(t); } if (failed > 0) @@ -96,10 +103,8 @@ static final long timeoutSeconds = 10L; - static void testInvokeAll() throws Throwable { + static void testInvokeAll(int nThreads, boolean timed) throws Throwable { final ThreadLocalRandom rnd = ThreadLocalRandom.current(); - final int nThreads = rnd.nextInt(2, 7); - final boolean timed = rnd.nextBoolean(); final ExecutorService pool = Executors.newFixedThreadPool(nThreads); final AtomicLong count = new AtomicLong(0); class Task implements Callable { @@ -136,17 +141,15 @@ } } - static void testInvokeAny() throws Throwable { + static void testInvokeAny(int nThreads, boolean timed) throws Throwable { final ThreadLocalRandom rnd = ThreadLocalRandom.current(); - final boolean timed = rnd.nextBoolean(); - final ExecutorService pool = Executors.newSingleThreadExecutor(); + final ExecutorService pool = Executors.newFixedThreadPool(nThreads); 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) { + if (x > 1) { // wait for main thread to interrupt us ... awaitInterrupt(timeoutSeconds); // ... and then for invokeAny to return @@ -173,12 +176,12 @@ check(val == 1); invokeAnyDone.countDown(); - // inherent race between main thread interrupt and - // start of second task - check(count.get() == 1 || count.get() == 2); - pool.shutdown(); check(pool.awaitTermination(timeoutSeconds, SECONDS)); + + long c = count.get(); + check(c >= 1 && c <= tasks.size()); + } finally { pool.shutdownNow(); } @@ -187,18 +190,16 @@ /** * Every remaining running task is sent an interrupt for cancellation. */ - static void testInvokeAny_cancellationInterrupt() throws Throwable { + static void testInvokeAny_cancellationInterrupt(int nThreads, boolean timed) throws Throwable { final ThreadLocalRandom rnd = ThreadLocalRandom.current(); - final int nThreads = rnd.nextInt(2, 7); - final boolean timed = rnd.nextBoolean(); final ExecutorService pool = Executors.newFixedThreadPool(nThreads); final AtomicLong count = new AtomicLong(0); final AtomicLong interruptedCount = new AtomicLong(0); final CyclicBarrier allStarted = new CyclicBarrier(nThreads); class Task implements Callable { public Long call() throws Exception { - allStarted.await(); long x = count.incrementAndGet(); + allStarted.await(); if (x > 1) // main thread will interrupt us awaitInterrupt(timeoutSeconds);