/* * Copyright (c) 1999, 2013 Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. * * This code is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License version 2 only, as * published by the Free Software Foundation. * * This code is distributed in the hope that it will be useful, but WITHOUT * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License * version 2 for more details (a copy is included in the LICENSE file that * accompanied this code). * * You should have received a copy of the GNU General Public License version * 2 along with this work; if not, write to the Free Software Foundation, * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. * * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA * or visit www.oracle.com if you need additional information or have any * questions. */ /** * @test * @bug 4176355 7181748 * @summary Suspending a ThreadGroup that contains the current thread has * unpredictable results. */ import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; public class Suspend implements Runnable { private static final ThreadGroup group = new ThreadGroup(""); private static final Thread first = new Thread(group, new Suspend()); private static final Thread second = new Thread(group, new Suspend()); private static final CountDownLatch groupStarted = new CountDownLatch(2); private static final CountDownLatch runGroup = new CountDownLatch(1); private static final CountDownLatch secondContinue = new CountDownLatch(1); private static final CountDownLatch secondDone = new CountDownLatch(1); public void run() { groupStarted.countDown(); while (true) { try { runGroup.await(); if (Thread.currentThread() == first) { group.suspend(); } else { secondContinue.await(); secondDone.countDown(); // should never be executed } } catch (InterruptedException ex) { } } } public static void main(String[] args) throws Exception { // Launch two threads as part of the same thread group first.start(); second.start(); groupStarted.await(); runGroup.countDown(); Thread.sleep(1000); // Suppose, the thread group is now suspended secondContinue.countDown(); // Reaching zero count indicates that the second thread is still running boolean failed = secondDone.await(1L, TimeUnit.SECONDS); first.stop(); second.stop(); if (failed) throw new RuntimeException("Failure."); } }