6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems
authordl
Fri, 19 Nov 2010 10:43:24 +0000
changeset 7277 4c9b6e4143ea
parent 7276 0401d80abf4b
child 7279 32ac37aa9367
6712185: java/util/concurrent/Executors/AutoShutdown.java fails on slow or busy systems Reviewed-by: chegar, alanb
jdk/test/ProblemList.txt
jdk/test/java/util/concurrent/Executors/AutoShutdown.java
--- a/jdk/test/ProblemList.txt	Thu Nov 18 19:17:43 2010 +0000
+++ b/jdk/test/ProblemList.txt	Fri Nov 19 10:43:24 2010 +0000
@@ -734,10 +734,6 @@
 # Problems on windows, jmap.exe hangs? (these run jmap), fails on Solaris 10 x86
 java/util/concurrent/locks/Lock/TimedAcquireLeak.java		generic-all
 
-# Solaris sparc client, some failures, "1 not equal to 3"?
-#   also Linux problems with samevm mode, -server linux i586? 1 not equal to 3?
-java/util/concurrent/Executors/AutoShutdown.java		generic-all
-
 # Fails on solaris-sparc -server (Set not equal to copy. 1)
 java/util/EnumSet/EnumSetBash.java			 	solaris-sparc
 
--- a/jdk/test/java/util/concurrent/Executors/AutoShutdown.java	Thu Nov 18 19:17:43 2010 +0000
+++ b/jdk/test/java/util/concurrent/Executors/AutoShutdown.java	Fri Nov 19 10:43:24 2010 +0000
@@ -32,21 +32,40 @@
 import java.util.*;
 import java.util.concurrent.*;
 import static java.util.concurrent.Executors.*;
+import java.util.concurrent.Phaser;
 
 public class AutoShutdown {
-    private static void waitForFinalizersToRun() throws Throwable {
-        System.gc(); System.runFinalization(); Thread.sleep(10);
-        System.gc(); System.runFinalization(); Thread.sleep(10);
+    private static void waitForFinalizersToRun() {
+        for (int i = 0; i < 2; i++)
+            tryWaitForFinalizersToRun();
+    }
+
+    private static void tryWaitForFinalizersToRun() {
+        System.gc();
+        final CountDownLatch fin = new CountDownLatch(1);
+        new Object() { protected void finalize() { fin.countDown(); }};
+        System.gc();
+        try { fin.await(); }
+        catch (InterruptedException ie) { throw new Error(ie); }
     }
 
     private static void realMain(String[] args) throws Throwable {
-        Runnable trivialRunnable = new Runnable() { public void run() {}};
+        final Phaser phaser = new Phaser(3);
+        Runnable trivialRunnable = new Runnable() {
+            public void run() {
+                phaser.arriveAndAwaitAdvance();
+            }
+        };
         int count0 = Thread.activeCount();
-        newSingleThreadExecutor().execute(trivialRunnable);
-        newSingleThreadExecutor(defaultThreadFactory()).execute(trivialRunnable);
-        Thread.sleep(100);
+        Executor e1 = newSingleThreadExecutor();
+        Executor e2 = newSingleThreadExecutor(defaultThreadFactory());
+        e1.execute(trivialRunnable);
+        e2.execute(trivialRunnable);
+        phaser.arriveAndAwaitAdvance();
         equal(Thread.activeCount(), count0 + 2);
-        waitForFinalizersToRun();
+        e1 = e2 = null;
+        for (int i = 0; i < 10 && Thread.activeCount() > count0; i++)
+            tryWaitForFinalizersToRun();
         equal(Thread.activeCount(), count0);
     }