KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > core > execution > TaskThreadGroup


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.execution;
21
22
23 /**
24 *
25 * @author Ales Novak
26 * @version 0.10 Mar 19, 1998
27 */

28 final class TaskThreadGroup extends ThreadGroup JavaDoc {
29     /** lock for waitFor/wakeUpProcesses */
30     private Object JavaDoc TIMER = new Object JavaDoc();
31
32     /** true if RunClass thread entered waitFor - out of main method */
33     private boolean runClassThreadOut = false;
34
35     /** true iff the ThreadGroup can be finalized - that is after all threads die */
36     private boolean finalizable;
37
38     /** Is the process dead? The group can be marked as dead but still have running threads */
39     private boolean dead;
40     
41     /** reference to a RunClassThread */
42     private RunClassThread runClassThread;
43
44     TaskThreadGroup(ThreadGroup JavaDoc parent, String JavaDoc name) {
45         super(parent, name);
46
47         dead = false;
48     }
49
50     /**
51     * @return true iff there is not any window there is not more than two threads
52     * and first one if any must be waiting for next event in the queue (EventDispatchThread) and
53     * the second must be instanceof ExecutionEngine$RunClass thread and must wait
54     * external processes are never "dead"
55     */

56     private boolean isProcessDead(boolean inside) {
57
58         synchronized (this) {
59             if (dead) {
60                 return true;
61             }
62
63             // first System.out.println must be before sync block
64
if (! finalizable) {
65                 return false;
66             }
67
68             int count;
69             count = activeCount();
70
71             /* Following lines patch bug in implementaion of java.lang.-Thread/ThreadGroup
72                The bug - if new Thread(...) call is issued and the thread is not started.
73                The thread is added as a member to its thread group - such threads are counted in
74                calls activeCount() and enumerate(Thread[] ts, ...) although they do not live - e.g
75                call destroy() to threadgroup always throws an exception. Solution is to start suspicious
76                threads. I added try - catch block for already started threads.
77                This is patch for some code used inside Swing - code like this " ... = new Thread().getThreadGroup();"
78                see - javax.swing.SystemEventQueueUtilities$RunnableCanvas.maybeRegisterEventDispatchThread()
79                Original error was that TaskThreadGroups that had not any threads at thread dump were not removed.
80                !!! remove spagetti code if the bug is repaired in JDK.
81             */

82             if (count > 1) {
83                 int active = 0;
84                 Thread JavaDoc[] threads =
85                     new Thread JavaDoc[count];
86                 enumerate(threads);
87                 for (int i = threads.length; --i >= 0;) {
88                     // #33630 - ignore daemon threads
89
if ((threads[i] != null) &&
90                             (threads[i].isAlive() && !threads[i].isDaemon())
91                        ) {
92                         if (++active > 1) {
93                             return false;
94                         }
95                     }
96                 }
97                 count = active;
98             }
99
100             if (ExecutionEngine.hasWindows(this)) {
101                 return false;
102             }
103
104             if (count == 0) {
105                 return true; // no thread, no wins
106
}
107
108             // one thread remains - RunClass
109
if (inside) {
110                 return true;
111             }
112             return false;
113         }
114     }
115
116     /** blocks until this ThreadGroup die - isProcessDead = true
117     */

118     void waitFor() throws InterruptedException JavaDoc {
119         boolean inside = Thread.currentThread() instanceof RunClassThread;
120         synchronized (TIMER) {
121             try {
122                 while (! isProcessDead(inside)) {
123                     TIMER.wait(1000);
124                 }
125             } finally {
126                 TIMER.notifyAll();
127         synchronized(this) {
128                     dead = true;
129                 }
130             }
131         }
132     }
133     
134     /** Marks this group as finalizable. */
135     synchronized void setFinalizable() {
136         finalizable = true;
137     }
138
139     /** sets the group as dead - called from DefaultSysProcess */
140     void kill() {
141         synchronized (TIMER) {
142             if (! dead) {
143                 dead = true;
144                 TIMER.notifyAll();
145                 try {
146                     TIMER.wait(3000);
147                 } catch (InterruptedException JavaDoc e) {
148                 }
149             }
150         }
151     }
152     
153     public void setRunClassThread(RunClassThread t) {
154         runClassThread = t;
155     }
156     
157     public RunClassThread getRunClassThread() {
158         return runClassThread;
159     }
160 }
161
Popular Tags