JDK 9 RFR of JDK-8132548: java/lang/ThreadGroup/Stop.java fails with "RuntimeException: Failure"

Martin Buchholz martinrb at google.com
Tue Jul 12 06:33:42 UTC 2016


And then somehow the unnecessary statics started to bother me:


import java.util.concurrent.CountDownLatch;
import java.util.concurrent.atomic.AtomicReference;

public class Stop {
    public static void main(String[] args) throws Exception {
        final CountDownLatch ready = new CountDownLatch(1);
        final ThreadGroup group = new ThreadGroup("");
        final AtomicReference<Throwable> firstThrowable
            = new AtomicReference<>();
        final AtomicReference<Throwable> secondThrowable
            = new AtomicReference<>();

        final Thread second = new Thread(group, () -> {
            ready.countDown();
            try {
                do {} while (true);
            } catch (Throwable ex) {
                secondThrowable.set(ex);
            }});

        final Thread first = new Thread(group, () -> {
            // Wait until "second" is started
            try {
                ready.await();
                group.stop();
            } catch (Throwable ex) {
                firstThrowable.set(ex);
            }});

        // Launch two threads as part of the same thread group
        first.start();
        second.start();

        // Both threads should terminate when the first thread
        // terminates the thread group.
        second.join();
        first.join();
        if (! (firstThrowable.get() instanceof ThreadDeath) ||
            ! (secondThrowable.get() instanceof ThreadDeath))
            throw new AssertionError("should have thrown ThreadDeath");
        // Test passed - if never get here the test times out and fails.
    }
}


More information about the core-libs-dev mailing list