changeset 6099:aa88f22e90ab

7084033: TEST_BUG: test/java/lang/ThreadGroup/Stop.java fails intermittently Reviewed-by: forax, chegar, dholmes Contributed-by: gary.adams@oracle.com
author alanb
date Mon, 21 Nov 2011 12:51:30 +0000
parents a83eb49fa0a1
children 09860239dce3
files test/java/lang/ThreadGroup/Stop.java
diffstat 1 files changed, 41 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/test/java/lang/ThreadGroup/Stop.java	Sun Feb 24 16:53:31 2013 +0000
+++ b/test/java/lang/ThreadGroup/Stop.java	Mon Nov 21 12:51:30 2011 +0000
@@ -29,37 +29,58 @@
  */
 
 public class Stop implements Runnable {
-    private static Thread first=null;
-    private static Thread second=null;
-    private static ThreadGroup group = new ThreadGroup("");
+    private static boolean groupStopped = false ;
+    private static final Object lock = new Object();
 
-    Stop() {
-        Thread thread = new Thread(group, this);
-        if (first == null)
-            first = thread;
-        else
-            second = thread;
-
-        thread.start();
-    }
+    private static final ThreadGroup group = new ThreadGroup("");
+    private static final Thread first = new Thread(group, new Stop());
+    private static final Thread second = new Thread(group, new Stop());
 
     public void run() {
         while (true) {
+            // Give the other thread a chance to start
             try {
-                Thread.sleep(1000); // Give other thread a chance to start
-                if (Thread.currentThread() == first)
-                    group.stop();
-            } catch(InterruptedException e){
+                Thread.sleep(1000);
+            } catch (InterruptedException e) {
+            }
+
+            // When the first thread runs, it will stop the group.
+            if (Thread.currentThread() == first) {
+                synchronized (lock) {
+                    try {
+                        group.stop();
+                    } finally {
+                        // Signal the main thread it is time to check
+                        // that the stopped thread group was successful
+                        groupStopped = true;
+                        lock.notifyAll();
+                    }
+                }
             }
         }
     }
 
     public static void main(String[] args) throws Exception {
-        for (int i=0; i<2; i++)
-            new Stop();
-        Thread.sleep(3000);
+        // Launch two threads as part of the same thread group
+        first.start();
+        second.start();
+
+        // Wait for the thread group stop to be issued
+        synchronized(lock){
+            while (!groupStopped) {
+                lock.wait();
+                // Give the other thread a chance to stop
+                Thread.sleep(1000);
+            }
+        }
+
+        // Check that the second thread is terminated when the
+        // first thread terminates the thread group.
         boolean failed = second.isAlive();
-        first.stop(); second.stop();
+
+        // Clean up any threads that may have not been terminated
+        first.stop();
+        second.stop();
         if (failed)
             throw new RuntimeException("Failure.");
     }