blob: 844fd93e2d815903b4994540027c76c95d0711cc [file] [log] [blame]
chegard70e9be2008-08-29 17:46:45 +01001/*
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 6576763
27 * @summary (thread) Thread constructors throw undocumented NPE for null name
28 */
29
30/*
31 * Verify that threads constructed with a null thread name do not get added
32 * to the list of unstarted thread for a thread group. We can do this by
33 * checking that a daemon threadGroup is desroyed after its final valid thread
34 * has completed.
35 */
36
37import java.util.concurrent.CountDownLatch;
38import static java.lang.System.out;
39
40public class NullThreadName
41{
42 static CountDownLatch done = new CountDownLatch(1);
43
44 public static void main(String args[]) throws Exception {
45 ThreadGroup tg = new ThreadGroup("chegar-threads");
46 Thread goodThread = new Thread(tg, new GoodThread(), "goodThread");
47 try {
48 Thread badThread = new Thread(tg, new Runnable(){
49 @Override
50 public void run() {} }, null);
51 } catch (NullPointerException npe) {
52 out.println("OK, caught expected " + npe);
53 }
54 tg.setDaemon(true);
55 goodThread.start();
56
57 done.await();
58
59 int count = 0;
60 while (goodThread.isAlive()) {
61 /* Hold off a little to allow the thread to complete */
62 out.println("GoodThread still alive, sleeping...");
63 try { Thread.sleep(2000); }
64 catch (InterruptedException unused) {}
65
66 /* do not wait forever */
67 if (count++ > 5)
68 throw new AssertionError("GoodThread is still alive!");
69 }
70
71 if (!tg.isDestroyed()) {
72 throw new AssertionError("Failed: Thread group is not destroyed.");
73 }
74 }
75
76 static class GoodThread implements Runnable
77 {
78 @Override
79 public void run() {
80 out.println("Good Thread started...");
81 out.println("Good Thread finishing");
82 done.countDown();
83 }
84 }
85}