KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > startup > TopThreadGroup


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.core.startup;
21
22 import org.openide.util.Exceptions;
23
24 /** The ThreadGroup for catching uncaught exceptions in Corona.
25 *
26 * @author Ian Formanek
27 */

28 final class TopThreadGroup extends ThreadGroup JavaDoc implements Runnable JavaDoc {
29     /** The command line args */
30     private String JavaDoc[] args;
31     /** flag that indicates whether the main thread had finished or not */
32     private boolean finished;
33
34     /** Constructs a new thread group. The parent of this new group is
35     * the thread group of the currently running thread.
36     *
37     * @param name the name of the new thread group.
38     */

39     public TopThreadGroup(String JavaDoc name, String JavaDoc[] args) {
40         super(name);
41         this.args = args;
42     }
43
44     /** Creates a new thread group. The parent of this new group is the
45     * specified thread group.
46     * <p>
47     * The <code>checkAccess</code> method of the parent thread group is
48     * called with no arguments; this may result in a security exception.
49     *
50     * @param parent the parent thread group.
51     * @param name the name of the new thread group.
52     * @exception NullPointerException if the thread group argument is
53     * <code>null</code>.
54     * @exception SecurityException if the current thread cannot create a
55     * thread in the specified thread group.
56     * @see java.lang.SecurityException
57     * @see java.lang.ThreadGroup#checkAccess()
58     */

59     public TopThreadGroup(ThreadGroup JavaDoc parent, String JavaDoc name) {
60         super(parent, name);
61     }
62
63     public void uncaughtException(Thread JavaDoc t, Throwable JavaDoc e) {
64         if (!(e instanceof ThreadDeath JavaDoc)) {
65             if (e instanceof VirtualMachineError JavaDoc) {
66                 // Try as hard as possible to get a stack trace from e.g. StackOverflowError
67
e.printStackTrace();
68             }
69             System.err.flush();
70             Exceptions.printStackTrace(e);
71         }
72         else {
73             super.uncaughtException(t, e);
74         }
75     }
76     
77     public synchronized void start () throws InterruptedException JavaDoc {
78         Thread JavaDoc t = new Thread JavaDoc (this, this, "main"); // NOI18N
79
t.start ();
80         
81         while (!finished) {
82             wait ();
83         }
84     }
85
86     public void run() {
87         try {
88             Main.start (args);
89         } catch (Throwable JavaDoc t) {
90             // XXX is this not handled by uncaughtException?
91
Exceptions.printStackTrace(t);
92             // System is probably broken, so don't just sit there with the splash screen open.
93
try {
94                 Thread.sleep(10000);
95             } catch (InterruptedException JavaDoc x) {
96                 Exceptions.printStackTrace(x);
97             }
98             System.exit(2);
99         } finally {
100             synchronized (this) {
101                 finished = true;
102                 notify ();
103             }
104         }
105     }
106 }
107
Popular Tags