Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/ThreadGroup/Stop.java
47182 views
/*1* Copyright (c) 1999, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 417635526* @summary Stopping a ThreadGroup that contains the current thread has27* unpredictable results.28*/2930public class Stop implements Runnable {31private static boolean groupStopped = false ;32private static final Object lock = new Object();3334private static final ThreadGroup group = new ThreadGroup("");35private static final Thread first = new Thread(group, new Stop());36private static final Thread second = new Thread(group, new Stop());3738public void run() {39while (true) {40// Give the other thread a chance to start41try {42Thread.sleep(1000);43} catch (InterruptedException e) {44}4546// When the first thread runs, it will stop the group.47if (Thread.currentThread() == first) {48synchronized (lock) {49try {50group.stop();51} finally {52// Signal the main thread it is time to check53// that the stopped thread group was successful54groupStopped = true;55lock.notifyAll();56}57}58}59}60}6162public static void main(String[] args) throws Exception {63// Launch two threads as part of the same thread group64first.start();65second.start();6667// Wait for the thread group stop to be issued68synchronized(lock){69while (!groupStopped) {70lock.wait();71// Give the other thread a chance to stop72Thread.sleep(1000);73}74}7576// Check that the second thread is terminated when the77// first thread terminates the thread group.78boolean failed = second.isAlive();7980// Clean up any threads that may have not been terminated81first.stop();82second.stop();83if (failed)84throw new RuntimeException("Failure.");85}86}878889