0N/A/*
2362N/A * Copyright (c) 1999, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/A/**
0N/A * @test
0N/A * @bug 4176355
0N/A * @summary Stopping a ThreadGroup that contains the current thread has
0N/A * unpredictable results.
0N/A */
0N/A
0N/Apublic class Stop implements Runnable {
5664N/A private static boolean groupStopped = false ;
5664N/A private static final Object lock = new Object();
0N/A
5664N/A private static final ThreadGroup group = new ThreadGroup("");
5664N/A private static final Thread first = new Thread(group, new Stop());
5664N/A private static final Thread second = new Thread(group, new Stop());
0N/A
0N/A public void run() {
0N/A while (true) {
5664N/A // Give the other thread a chance to start
0N/A try {
5664N/A Thread.sleep(1000);
5664N/A } catch (InterruptedException e) {
5664N/A }
5664N/A
5664N/A // When the first thread runs, it will stop the group.
5664N/A if (Thread.currentThread() == first) {
5664N/A synchronized (lock) {
5664N/A try {
5664N/A group.stop();
5664N/A } finally {
5664N/A // Signal the main thread it is time to check
5664N/A // that the stopped thread group was successful
5664N/A groupStopped = true;
5664N/A lock.notifyAll();
5664N/A }
5664N/A }
0N/A }
0N/A }
0N/A }
0N/A
0N/A public static void main(String[] args) throws Exception {
5664N/A // Launch two threads as part of the same thread group
5664N/A first.start();
5664N/A second.start();
5664N/A
5664N/A // Wait for the thread group stop to be issued
5664N/A synchronized(lock){
5664N/A while (!groupStopped) {
5664N/A lock.wait();
5664N/A // Give the other thread a chance to stop
5664N/A Thread.sleep(1000);
5664N/A }
5664N/A }
5664N/A
5664N/A // Check that the second thread is terminated when the
5664N/A // first thread terminates the thread group.
0N/A boolean failed = second.isAlive();
5664N/A
5664N/A // Clean up any threads that may have not been terminated
5664N/A first.stop();
5664N/A second.stop();
0N/A if (failed)
0N/A throw new RuntimeException("Failure.");
0N/A }
0N/A}