blob: fe498371852ce469174ffa505d7feb77ed291cdf [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
ohair9deb3062009-05-18 10:36:38 -070027 * @ignore until hotspot 6776144 bug is resolved
chegard70e9be2008-08-29 17:46:45 +010028 * @summary (thread) Thread constructors throw undocumented NPE for null name
29 */
30
31/*
32 * Verify that threads constructed with a null thread name do not get added
33 * to the list of unstarted thread for a thread group. We can do this by
34 * checking that a daemon threadGroup is desroyed after its final valid thread
35 * has completed.
36 */
37
38import java.util.concurrent.CountDownLatch;
39import static java.lang.System.out;
40
41public class NullThreadName
42{
43 static CountDownLatch done = new CountDownLatch(1);
44
45 public static void main(String args[]) throws Exception {
46 ThreadGroup tg = new ThreadGroup("chegar-threads");
47 Thread goodThread = new Thread(tg, new GoodThread(), "goodThread");
48 try {
49 Thread badThread = new Thread(tg, new Runnable(){
50 @Override
51 public void run() {} }, null);
52 } catch (NullPointerException npe) {
53 out.println("OK, caught expected " + npe);
54 }
55 tg.setDaemon(true);
56 goodThread.start();
57
58 done.await();
59
60 int count = 0;
61 while (goodThread.isAlive()) {
62 /* Hold off a little to allow the thread to complete */
63 out.println("GoodThread still alive, sleeping...");
64 try { Thread.sleep(2000); }
65 catch (InterruptedException unused) {}
66
67 /* do not wait forever */
68 if (count++ > 5)
69 throw new AssertionError("GoodThread is still alive!");
70 }
71
72 if (!tg.isDestroyed()) {
73 throw new AssertionError("Failed: Thread group is not destroyed.");
74 }
75 }
76
77 static class GoodThread implements Runnable
78 {
79 @Override
80 public void run() {
81 out.println("Good Thread started...");
82 out.println("Good Thread finishing");
83 done.countDown();
84 }
85 }
86}