Path: blob/master/test/jdk/java/lang/ThreadGroup/Destroy.java
41149 views
/*1* Copyright (c) 2019, 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* @summary enumerate(list,n,recurse) may return 0 if the group is being destroyed,26* whereas it should never return a value < n. This lead to inconsistent27* results if ThreadGroup::enumerate is called concurrently at the same28* time that a child group is being destroyed. This is a race condition,29* and this test will not always fail without the fix, but it does fail30* often enough.31* @bug 821919732*33*/34import java.util.concurrent.CountDownLatch;35import java.util.concurrent.Semaphore;36import java.util.concurrent.atomic.AtomicInteger;3738public class Destroy {3940static final class Task implements Runnable {41final Semaphore sem;42final CountDownLatch count;4344public Task(Semaphore sem, CountDownLatch count) {45this.sem = sem;46this.count = count;47}4849@Override50public void run() {51try {52count.countDown();53sem.acquire();54} catch (Throwable t) {55t.printStackTrace();56} finally {57System.out.println(Thread.currentThread().getName()58+ " exiting");59}60}61}6263public static void main(String[] args) throws Exception {64testDestroyChild();65}6667public static void testDestroyChild() throws Exception {68ThreadGroup root = new ThreadGroup("root");69ThreadGroup parent = new ThreadGroup(root,"parent");70ThreadGroup child1 = new ThreadGroup(parent, "child1");71CountDownLatch count = new CountDownLatch(2);72Semaphore sem1 = new Semaphore(1);73Semaphore sem2 = new Semaphore(1);74Thread t1 = new Thread(parent, new Task(sem1, count), "PT1");75Thread t2 = new Thread(parent, new Task(sem2, count), "PT2");76sem1.acquire();77sem2.acquire();78try {7980t1.start();81t2.start();8283System.out.println("\nAwaiting parent threads...");84count.await();85Thread[] threads = new Thread[2];86int nb = root.enumerate(threads, true);87if (nb != 2) {88throw new AssertionError("wrong number of threads: " + nb);89}9091Thread t3 = new Thread(child1::destroy, "destroy");92AtomicInteger nbr = new AtomicInteger();93Thread t4 = new Thread("enumerate") {94public void run() {95Thread[] threads = new Thread[42];96nbr.addAndGet(root.enumerate(threads, true));97}98};99t4.start();100t3.start();101t4.join();102t3.join();103if (nbr.get() != nb) {104throw new AssertionError("wrong number of threads: " + nbr.get());105}106107} finally {108sem1.release();109sem2.release();110}111t1.join();112t2.join();113}114}115116117