520N/A/*
2362N/A * Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.
520N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
520N/A *
520N/A * This code is free software; you can redistribute it and/or modify it
520N/A * under the terms of the GNU General Public License version 2 only, as
520N/A * published by the Free Software Foundation.
520N/A *
520N/A * This code is distributed in the hope that it will be useful, but WITHOUT
520N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
520N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
520N/A * version 2 for more details (a copy is included in the LICENSE file that
520N/A * accompanied this code).
520N/A *
520N/A * You should have received a copy of the GNU General Public License version
520N/A * 2 along with this work; if not, write to the Free Software Foundation,
520N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
520N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
520N/A */
520N/A
520N/A/*
520N/A * @test
520N/A * @bug 6576763
520N/A * @summary (thread) Thread constructors throw undocumented NPE for null name
520N/A */
520N/A
520N/A/*
520N/A * Verify that threads constructed with a null thread name do not get added
520N/A * to the list of unstarted thread for a thread group. We can do this by
520N/A * checking that a daemon threadGroup is desroyed after its final valid thread
520N/A * has completed.
520N/A */
520N/A
520N/Aimport java.util.concurrent.CountDownLatch;
520N/Aimport static java.lang.System.out;
520N/A
520N/Apublic class NullThreadName
520N/A{
520N/A static CountDownLatch done = new CountDownLatch(1);
520N/A
520N/A public static void main(String args[]) throws Exception {
520N/A ThreadGroup tg = new ThreadGroup("chegar-threads");
520N/A Thread goodThread = new Thread(tg, new GoodThread(), "goodThread");
520N/A try {
520N/A Thread badThread = new Thread(tg, new Runnable(){
520N/A @Override
520N/A public void run() {} }, null);
520N/A } catch (NullPointerException npe) {
520N/A out.println("OK, caught expected " + npe);
520N/A }
520N/A tg.setDaemon(true);
520N/A goodThread.start();
520N/A
520N/A done.await();
520N/A
520N/A int count = 0;
520N/A while (goodThread.isAlive()) {
520N/A /* Hold off a little to allow the thread to complete */
520N/A out.println("GoodThread still alive, sleeping...");
520N/A try { Thread.sleep(2000); }
520N/A catch (InterruptedException unused) {}
520N/A
5747N/A /* do not wait forever - allow 120 seconds same as jtreg default timeout. */
5747N/A if (count++ > 60)
520N/A throw new AssertionError("GoodThread is still alive!");
520N/A }
520N/A
520N/A if (!tg.isDestroyed()) {
520N/A throw new AssertionError("Failed: Thread group is not destroyed.");
520N/A }
520N/A }
520N/A
520N/A static class GoodThread implements Runnable
520N/A {
520N/A @Override
520N/A public void run() {
520N/A out.println("Good Thread started...");
520N/A out.println("Good Thread finishing");
520N/A done.countDown();
520N/A }
520N/A }
520N/A}