提交 b9a9e8f7 编写于 作者: A alanb

7084033: TEST_BUG: test/java/lang/ThreadGroup/Stop.java fails intermittently

Reviewed-by: forax, chegar, dholmes
Contributed-by: gary.adams@oracle.com
上级 d9671322
...@@ -29,37 +29,58 @@ ...@@ -29,37 +29,58 @@
*/ */
public class Stop implements Runnable { public class Stop implements Runnable {
private static Thread first=null; private static boolean groupStopped = false ;
private static Thread second=null; private static final Object lock = new Object();
private static ThreadGroup group = new ThreadGroup("");
Stop() { private static final ThreadGroup group = new ThreadGroup("");
Thread thread = new Thread(group, this); private static final Thread first = new Thread(group, new Stop());
if (first == null) private static final Thread second = new Thread(group, new Stop());
first = thread;
else
second = thread;
thread.start();
}
public void run() { public void run() {
while (true) { while (true) {
// Give the other thread a chance to start
try { try {
Thread.sleep(1000); // Give other thread a chance to start Thread.sleep(1000);
if (Thread.currentThread() == first) } catch (InterruptedException e) {
group.stop(); }
} 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 { public static void main(String[] args) throws Exception {
for (int i=0; i<2; i++) // Launch two threads as part of the same thread group
new Stop(); first.start();
Thread.sleep(3000); 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(); boolean failed = second.isAlive();
first.stop(); second.stop();
// Clean up any threads that may have not been terminated
first.stop();
second.stop();
if (failed) if (failed)
throw new RuntimeException("Failure."); throw new RuntimeException("Failure.");
} }
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册