From 0a416094b606ab2b02e245a4a35aed9bea32060c Mon Sep 17 00:00:00 2001 From: Gary Adams Date: Mon, 21 Nov 2011 12:51:30 +0000 Subject: [PATCH] 7084033: TEST_BUG: test/java/lang/ThreadGroup/Stop.java fails intermittently Reviewed-by: forax, chegar, dholmes --- jdk/test/java/lang/ThreadGroup/Stop.java | 61 ++++++++++++++++-------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/jdk/test/java/lang/ThreadGroup/Stop.java b/jdk/test/java/lang/ThreadGroup/Stop.java index 9c663c31241..4763acc8036 100644 --- a/jdk/test/java/lang/ThreadGroup/Stop.java +++ b/jdk/test/java/lang/ThreadGroup/Stop.java @@ -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."); }