1 /*
   2  * Copyright (c) 1999, 2016, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /**
  25  * @test
  26  * @bug 4176355
  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 volatile boolean groupStopped = false ;
  33     private static final int MAX_RETRY = 5;
  34     private static final Object lock = new Object();
  35 
  36     private static final ThreadGroup group = new ThreadGroup("");
  37     private static final Thread first = new Thread(group, new Stop());
  38     private static final Thread second = new Thread(group, new Stop());
  39 
  40     public void run() {
  41         while (true) {
  42             // Give the other thread a chance to start
  43             try {
  44                 Thread.sleep(1000);
  45             } catch (InterruptedException e) {
  46             }
  47 
  48             // When the first thread runs, it will stop the group.
  49             if (Thread.currentThread() == first) {
  50                 synchronized (lock) {
  51                     try {
  52                         group.stop();
  53                     } finally {
  54                         // Signal the main thread it is time to check
  55                         // that the stopped thread group was successful
  56                         groupStopped = true;
  57                         lock.notifyAll();
  58                     }
  59                 }
  60             }
  61         }
  62     }
  63 
  64     public static void main(String[] args) throws Exception {
  65         // Launch two threads as part of the same thread group
  66         first.start();
  67         second.start();
  68 
  69         // Wait for the thread group stop to be issued
  70         int numTries = 0;
  71         synchronized(lock){
  72             while (!groupStopped && numTries++ < MAX_RETRY) {
  73                 lock.wait();
  74                 // Give the other thread a chance to stop
  75                 Thread.sleep(1000);
  76             }
  77         }
  78 
  79         // Check that the second thread is terminated when the
  80         // first thread terminates the thread group.
  81         boolean failed = second.isAlive();
  82 
  83         // Clean up any threads that may have not been terminated
  84         first.stop();
  85         second.stop();
  86         if (failed)
  87             throw new RuntimeException("Failure.");
  88     }
  89 }
--- EOF ---