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 * @library /test/lib/share/classes 28 * @modules java.base/jdk.internal.misc 29 * @summary Stopping a ThreadGroup that contains the current thread has 30 * unpredictable results. 31 */ 32 33 import java.util.concurrent.CountDownLatch; 34 35 import jdk.test.lib.Utils; 36 37 public class Stop { 38 39 private static final long LONG_DELAY_MS = Utils.adjustTimeout(5000L); 40 private static final CountDownLatch ready = new CountDownLatch(1); 41 private static final CountDownLatch done = new CountDownLatch(2); 42 private static final ThreadGroup group = new ThreadGroup(""); 43 44 private static final Thread second = new Thread(group, () -> { 45 ready.countDown(); 46 try { 47 while (true) { 48 // keep it alive 49 } 50 } catch (final ThreadDeath td) { 51 done.countDown(); 52 throw td; 53 } 54 }); 55 56 private static final Thread first = new Thread(group, () -> { 57 // Wait until "second" is started 58 await(ready); 59 try { 60 // Now stop the group 61 // ThreadDeath then is thrown in the victim threads 62 group.stop(); 63 } catch (final ThreadDeath td) { 64 // Inform the main thead that the test thread has completed 65 done.countDown(); 66 // Rethrow the object so that the thread actually dies 67 throw td; 68 } 69 }); 70 71 private static void await(CountDownLatch latch) { 72 try { 73 latch.await(); 74 } catch (InterruptedException e) { 75 throw new RuntimeException(e); 76 } 77 } 78 79 public static void main(String[] args) throws Exception { 80 // Launch two threads as part of the same thread group 81 first.start(); 82 second.start(); 83 84 try { 85 // Wait for the thread group stop to be issued 86 await(done); 87 88 // Check that the second thread is terminated when the 89 // first thread terminates the thread group. 90 second.join(LONG_DELAY_MS); 91 boolean failed = second.isAlive(); 92 if (failed) { 93 throw new RuntimeException("Failure."); 94 } 95 } finally { 96 // Clean up any threads that may have not been terminated 97 first.stop(); 98 second.stop(); 99 } 100 } 101 }