< prev index next >

test/jdk/java/util/concurrent/ExecutorService/Invoke.java

Print this page
8234131: Miscellaneous changes imported from jsr166 CVS 2020-12
Reviewed-by: martin

@@ -82,26 +82,31 @@
         }
     }
 
     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)
             throw new Error(
                     String.format("Passed = %d, failed = %d", passed, failed));
     }
 
     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<Long> {
             public Long call() throws Exception {
                 return count.incrementAndGet();

@@ -134,21 +139,19 @@
         } finally {
             pool.shutdownNow();
         }
     }
 
-    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<Long> {
             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
                     check(invokeAnyDone.await(timeoutSeconds, SECONDS));
                 }

@@ -171,36 +174,34 @@
             else
                 val = pool.invokeAny(tasks);
             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();
         }
     }
 
     /**
      * 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<Long> {
             public Long call() throws Exception {
-                allStarted.await();
                 long x = count.incrementAndGet();
+                allStarted.await();
                 if (x > 1)
                     // main thread will interrupt us
                     awaitInterrupt(timeoutSeconds);
                 return x;
             }
< prev index next >