Path: blob/master/test/jdk/java/lang/ThreadGroup/NullThreadName.java
41149 views
/*1* Copyright (c) 2008, 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 657676326* @summary (thread) Thread constructors throw undocumented NPE for null name27*/2829/*30* Verify that threads constructed with a null thread name do not get added31* to the list of unstarted thread for a thread group. We can do this by32* checking that a daemon threadGroup is desroyed after its final valid thread33* has completed.34*/3536import java.util.concurrent.CountDownLatch;37import static java.lang.System.out;3839public class NullThreadName40{41static CountDownLatch done = new CountDownLatch(1);4243public static void main(String args[]) throws Exception {44ThreadGroup tg = new ThreadGroup("chegar-threads");45Thread goodThread = new Thread(tg, new GoodThread(), "goodThread");46try {47Thread badThread = new Thread(tg, new Runnable(){48@Override49public void run() {} }, null);50} catch (NullPointerException npe) {51out.println("OK, caught expected " + npe);52}53tg.setDaemon(true);54goodThread.start();5556done.await();5758int count = 0;59while (goodThread.isAlive()) {60/* Hold off a little to allow the thread to complete */61out.println("GoodThread still alive, sleeping...");62try { Thread.sleep(2000); }63catch (InterruptedException unused) {}6465/* do not wait forever - allow 120 seconds same as jtreg default timeout. */66if (count++ > 60)67throw new AssertionError("GoodThread is still alive!");68}6970if (!tg.isDestroyed()) {71throw new AssertionError("Failed: Thread group is not destroyed.");72}73}7475static class GoodThread implements Runnable76{77@Override78public void run() {79out.println("Good Thread started...");80out.println("Good Thread finishing");81done.countDown();82}83}84}858687