Gary,

Did you test that this still fails on a JDK without the fix? AFAICS you must start the threads in the correct order so that in the original bad code the first thread in the ThreadGroup that would be stopped is "first". Hence

   64            // Launch two threads as part of the same thread group
   65            second.start();
   66            first.start();

needs to be reversed. It is the start() that adds the Thread to the ThreadGroup's internal array.

David

On 10/11/2011 5:09 AM, Gary Adams wrote:
Captured the latest round of comments
- more readable initialization
- allow sleep interruption to terminate main thread
- added current CR# to @bug tag

24 /**
25 * @test
26 * @bug 4176355 7084033
27 * @summary Stopping a ThreadGroup that contains the current thread has
28 * unpredictable results.
29 */
30
31 public class Stop implements Runnable {
32 private static boolean groupStopped = false ;
33 private static final Object lock = new Object();
34
35 private static final ThreadGroup group = new ThreadGroup("");
36 private static final Thread first = new Thread(group, new Stop());
37 private static final Thread second = new Thread(group, new Stop());
38
39 public void run() {
40 while (true) {
41 // Give the other thread a chance to start
42 try {
43 Thread.sleep(1000);
44 } catch (InterruptedException e) {
45 }
46
47 // When the first thread runs, it will stop the group.
48 if (Thread.currentThread() == first) {
49 synchronized (lock) {
50 try {
51 group.stop();
52 } finally {
53 // Signal the main thread it is time to check
54 // that the stopped thread group was successful
55 groupStopped = true;
56 lock.notifyAll();
57 }
58 }
59 }
60 }
61 }
62
63 public static void main(String[] args) throws Exception {
64 // Launch two threads as part of the same thread group
65 second.start();
66 first.start();
67
68 // Wait for the thread group stop to be issued
69 synchronized(lock){
70 while (!groupStopped) {
71 lock.wait();
72 // Give the other thread a chance to stop
73 Thread.sleep(1000);
74 }
75 }
76
77 // Check that the second thread is terminated when the
78 // first thread terminates the thread group.
79 boolean failed = second.isAlive();
80
81 // Clean up any threads that may have not been terminated
82 first.stop();
83 second.stop();
84 if (failed)
85 throw new RuntimeException("Failure.");
86 }
87 }

Reply via email to