Commit 6dfd8a77 by Bryce McKinlay Committed by Bryce McKinlay

ThreadGroup.java: Merged with classpath.

2000-06-20  Bryce McKinlay  <bryce@albatross.co.nz>

	* java/lang/ThreadGroup.java: Merged with classpath.
	* prims.cc (_Jv_RunMain): Don't use ain_group'.
	* gnu/gcj/runtime/FirstThread.java: Remove ThreadGroup constructor
	argument.
	* java/lang/Thread.java (Thread): Bootstrap initial thread from
	ThreadGroup.root if Thread.currentThread is null. Honour the
	ThreadGroup's max priority setting.

From-SVN: r34615
parent 83fb52d8
2000-06-20 Bryce McKinlay <bryce@albatross.co.nz>
* java/lang/ThreadGroup.java: Merged with classpath.
* prims.cc (_Jv_RunMain): Don't use `main_group'.
* gnu/gcj/runtime/FirstThread.java: Remove ThreadGroup constructor
argument.
* java/lang/Thread.java (Thread): Bootstrap initial thread from
ThreadGroup.root if Thread.currentThread is null. Honour the
ThreadGroup's max priority setting.
2000-06-18 Tom Tromey <tromey@cygnus.com> 2000-06-18 Tom Tromey <tromey@cygnus.com>
* java/lang/natClass.cc (forName): Removed dead code. Initialize * java/lang/natClass.cc (forName): Removed dead code. Initialize
......
...@@ -21,17 +21,17 @@ final class FirstThread extends Thread ...@@ -21,17 +21,17 @@ final class FirstThread extends Thread
{ {
public native void run (); public native void run ();
public FirstThread (ThreadGroup g, Class k, Object o) public FirstThread (Class k, Object o)
{ {
super (g, null, "main"); super (null, null, "main");
klass = k; klass = k;
klass_name = null; klass_name = null;
args = o; args = o;
} }
public FirstThread (ThreadGroup g, String class_name, Object o) public FirstThread (String class_name, Object o)
{ {
super (g, null, "main"); super (null, null, "main");
klass = null; klass = null;
klass_name = class_name; klass_name = class_name;
args = o; args = o;
......
...@@ -198,19 +198,21 @@ public class Thread implements Runnable ...@@ -198,19 +198,21 @@ public class Thread implements Runnable
public Thread (ThreadGroup g, Runnable r, String n) public Thread (ThreadGroup g, Runnable r, String n)
{ {
// Note that CURRENT can be null when we are creating the very
// first thread. That's why we check it below.
Thread current = currentThread (); Thread current = currentThread ();
if (g != null) if (g == null)
{ {
// If CURRENT is null, then we are creating the first thread. // If CURRENT is null, then we are bootstrapping the first thread.
// In this case we don't do the security check. // Use ThreadGroup.root, the main threadgroup.
if (current != null) if (current == null)
g.checkAccess(); group = ThreadGroup.root;
else
group = current.getThreadGroup();
} }
else else
g = current.getThreadGroup(); group = g;
group.checkAccess();
// The Class Libraries book says ``threadName cannot be null''. I // The Class Libraries book says ``threadName cannot be null''. I
// take this to mean NullPointerException. // take this to mean NullPointerException.
...@@ -218,8 +220,7 @@ public class Thread implements Runnable ...@@ -218,8 +220,7 @@ public class Thread implements Runnable
throw new NullPointerException (); throw new NullPointerException ();
name = n; name = n;
group = g; group.add(this);
g.add(this);
runnable = r; runnable = r;
data = null; data = null;
...@@ -230,7 +231,9 @@ public class Thread implements Runnable ...@@ -230,7 +231,9 @@ public class Thread implements Runnable
if (current != null) if (current != null)
{ {
daemon_flag = current.isDaemon(); daemon_flag = current.isDaemon();
priority = current.getPriority(); int gmax = group.getMaxPriority();
int pri = current.getPriority();
priority = (gmax < pri ? gmax : pri);
} }
else else
{ {
......
// ThreadGroup.java - ThreadGroup class. /* java.lang.ThreadGroup
Copyright (C) 1998, 2000 Free Software Foundation, Inc.
/* Copyright (C) 1998, 1999, 2000 Free Software Foundation This file is part of libgcj.
This file is part of libgcj.
This software is copyrighted work licensed under the terms of the This software is copyrighted work licensed under the terms of the
Libgcj License. Please consult the file "LIBGCJ_LICENSE" for Libgcj License. Please consult the file "LIBGCJ_LICENSE" for
...@@ -10,393 +9,554 @@ details. */ ...@@ -10,393 +9,554 @@ details. */
package java.lang; package java.lang;
import java.util.Enumeration;
import java.util.Vector; import java.util.Vector;
import java.util.Enumeration;
/**
* @author Tom Tromey <tromey@cygnus.com>
* @date August 25, 1998
*/
/* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3 /* Written using "Java Class Libraries", 2nd edition, ISBN 0-201-31002-3
* "The Java Language Specification", ISBN 0-201-63451-1 * "The Java Language Specification", ISBN 0-201-63451-1
* plus online API docs for JDK 1.2 beta from http://www.javasoft.com. * plus online API docs for JDK 1.2 beta from http://www.javasoft.com.
* Status: Complete for 1.1. Parts from the JDK 1.0 spec only are * Status: Complete for 1.2. Parts from the JDK 1.0 spec only are
* not implemented. Parts of the 1.2 spec are also not implemented. * not implemented.
*/
/**
* ThreadGroup allows you to group Threads together. There is a
* hierarchy of ThreadGroups, and only the initial ThreadGroup has
* no parent. A Thread may access information about its own
* ThreadGroup, but not its parents or others outside the tree.
*
* @author John Keiser
* @author Tom Tromey
* @version 1.2.0, June 20, 2000
* @since JDK1.0
*/ */
public class ThreadGroup public class ThreadGroup
{ {
public int activeCount () /* The Initial, top-level ThreadGroup. */
{ static ThreadGroup root = new ThreadGroup();
int ac = threads.size();
Enumeration e = groups.elements(); private ThreadGroup parent;
while (e.hasMoreElements()) private String name;
private Vector threads = new Vector();
private Vector groups = new Vector();
private boolean daemon_flag = false;
private boolean destroyed_flag = false;
int maxpri = Thread.MAX_PRIORITY;
private ThreadGroup()
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); name = "main";
ac += g.activeCount();
}
return ac;
} }
public int activeGroupCount () /** Create a new ThreadGroup using the given name and the
{ * current thread's ThreadGroup as a parent.
int ac = groups.size(); * @param name the name to use for the ThreadGroup.
Enumeration e = groups.elements(); */
while (e.hasMoreElements()) public ThreadGroup(String name)
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); this (Thread.currentThread().getThreadGroup(), name);
ac += g.activeGroupCount();
} }
return ac;
/** Create a new ThreadGroup using the given name and
* parent group.
* @param name the name to use for the ThreadGroup.
* @param parent the ThreadGroup to use as a parent.
* @exception NullPointerException if parent is null.
* @exception SecurityException if you cannot change
* the intended parent group.
*/
public ThreadGroup(ThreadGroup parent, String name)
{
parent.checkAccess();
this.parent = parent;
if (parent.destroyed_flag)
throw new IllegalArgumentException ();
this.name = name;
maxpri = parent.maxpri;
daemon_flag = parent.daemon_flag;
parent.add(this);
} }
// Deprecated in 1.2. /** Get the name of this ThreadGroup.
public boolean allowThreadSuspension (boolean allow) * @return the name of this ThreadGroup.
*/
public final String getName()
{ {
// There is no way for a Java program to determine whether this return name;
// has any effect whatsoever. We don't need it.
return true;
} }
public final void checkAccess () /** Get the parent of this ThreadGroup.
* @return the parent of this ThreadGroup.
*/
public final ThreadGroup getParent()
{ {
SecurityManager s = System.getSecurityManager(); return parent;
if (s != null)
s.checkAccess(this);
} }
// This is called to remove a ThreadGroup from our internal list. /** Set the maximum priority for Threads in this ThreadGroup. setMaxPriority
private final void remove (ThreadGroup g) * can only be used to reduce the current maximum. If maxpri
* is greater than the current Maximum, the current value is not changed.
* Calling this does not effect threads already in this ThreadGroup.
* @param maxpri the new maximum priority for this ThreadGroup.
* @exception SecurityException if you cannoy modify this ThreadGroup.
*/
public final void setMaxPriority(int maxpri)
{ {
groups.removeElement(g); checkAccess();
if (daemon_flag && groups.size() == 0 && threads.size() == 0) if (maxpri < this.maxpri
&& maxpri >= Thread.MIN_PRIORITY
&& maxpri <= Thread.MAX_PRIORITY)
{ {
// We inline destroy to avoid the access check. this.maxpri = maxpri;
destroyed_flag = true;
if (parent != null)
parent.remove(this);
} }
} }
// This is called by the Thread code to remove a Thread from our /** Get the maximum priority of Threads in this ThreadGroup.
// internal list. * @return the maximum priority of Threads in this ThreadGroup.
final void remove (Thread t) */
{ public final int getMaxPriority()
threads.removeElement(t);
if (daemon_flag && groups.size() == 0 && threads.size() == 0)
{ {
// We inline destroy to avoid the access check. return maxpri;
destroyed_flag = true;
if (parent != null)
parent.remove(this);
} }
/** Set whether this ThreadGroup is a daemon group. A daemon
* group will be destroyed when its last thread is stopped and
* its last thread group is destroyed.
* @specnote The Java API docs indicate that the group is destroyed
* when either of those happen, but that doesn't make
* sense.
* @param daemon whether this ThreadGroup should be a daemon group.
* @exception SecurityException if you cannoy modify this ThreadGroup.
*/
public final void setDaemon (boolean daemon)
{
checkAccess();
daemon_flag = daemon;
} }
// This is called by the Thread code to add a Thread to our internal /** Tell whether this ThreadGroup is a daemon group. A daemon
// list. * group will be destroyed when its last thread is stopped and
final void add (Thread t) * its last thread group is destroyed.
* @specnote The Java API docs indicate that the group is destroyed
* when either of those happen, but that doesn't make
* sense.
* @return whether this ThreadGroup is a daemon group.
*/
public final boolean isDaemon()
{ {
if (destroyed_flag) return daemon_flag;
throw new IllegalThreadStateException (); }
threads.addElement(t); /** Tell whether this ThreadGroup has been destroyed or not.
* @return whether this ThreadGroup has been destroyed or not.
*/
public boolean isDestroyed()
{
return destroyed_flag;
} }
// This is a helper that is used to implement the destroy method. /** Check whether this ThreadGroup is an ancestor of the
private final boolean canDestroy () * specified ThreadGroup, or if they are the same.
*
* @param g the group to test on.
* @return whether this ThreadGroup is a parent of the
* specified group.
*/
public final boolean parentOf(ThreadGroup tg)
{ {
if (! threads.isEmpty()) while (tg != null)
return false;
Enumeration e = groups.elements();
while (e.hasMoreElements())
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); if (tg == this)
if (! g.canDestroy())
return false;
}
return true; return true;
tg = tg.parent;
}
return false;
} }
public final void destroy () /** Return the total number of active threads in this ThreadGroup
* and all its descendants.<P>
*
* This cannot return an exact number, since the status of threads
* may change after they were counted. But it should be pretty
* close.<P>
*
* @return the number of active threads in this ThreadGroup and
* its descendants.
*/
public synchronized int activeCount()
{ {
checkAccess (); int total = threads.size();
if (! canDestroy ()) for (int i=0; i < groups.size(); i++)
throw new IllegalThreadStateException (); {
destroyed_flag = true; ThreadGroup g = (ThreadGroup) groups.elementAt(i);
if (parent != null) total += g.activeCount();
parent.remove(this); }
return total;
} }
// This actually implements enumerate. /** Get the number of active groups in this ThreadGroup. This group
private final int enumerate (Thread[] ts, int next_index, boolean recurse) * itself is not included in the count.
{ * @specnote it is unclear what exactly constitutes an
Enumeration e = threads.elements(); * active ThreadGroup. Currently we assume that
while (e.hasMoreElements() && next_index < ts.length) * all sub-groups are active.
ts[next_index++] = (Thread) e.nextElement(); * @return the number of active groups in this ThreadGroup.
if (recurse && next_index != ts.length) */
public int activeGroupCount()
{ {
e = groups.elements(); int total = groups.size();
while (e.hasMoreElements() && next_index < ts.length) for (int i=0; i < groups.size(); i++)
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); ThreadGroup g = (ThreadGroup) groups.elementAt(i);
next_index = g.enumerate(ts, next_index, true); total += g.activeGroupCount();
}
} }
return next_index; return total;
} }
public int enumerate (Thread[] ts) /** Copy all of the active Threads from this ThreadGroup and
* its descendants into the specified array. If the array is
* not big enough to hold all the Threads, extra Threads will
* simply not be copied.
*
* @param threads the array to put the threads into.
* @return the number of threads put into the array.
*/
public int enumerate(Thread[] threads)
{ {
return enumerate (ts, 0, true); return enumerate(threads, true);
} }
public int enumerate (Thread[] ts, boolean recurse) /** Copy all of the active Threads from this ThreadGroup and,
* if desired, from its descendants, into the specified array.
* If the array is not big enough to hold all the Threads,
* extra Threads will simply not be copied.
*
* @param threads the array to put the threads into.
* @param useDescendants whether to count Threads in this
* ThreadGroup's descendants or not.
* @return the number of threads put into the array.
*/
public int enumerate(Thread[] threads, boolean useDescendants)
{ {
return enumerate (ts, 0, recurse); return enumerate(threads, 0, useDescendants);
} }
// This actually implements enumerate. // This actually implements enumerate.
private final int enumerate (ThreadGroup[] ts, int next_index, private int enumerate (Thread[] list, int next_index, boolean recurse)
boolean recurse)
{ {
Enumeration e = groups.elements(); Enumeration e = threads.elements();
while (e.hasMoreElements() && next_index < ts.length) while (e.hasMoreElements() && next_index < list.length)
list[next_index++] = (Thread) e.nextElement();
if (recurse && next_index != list.length)
{
e = groups.elements();
while (e.hasMoreElements() && next_index < list.length)
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); ThreadGroup g = (ThreadGroup) e.nextElement();
ts[next_index++] = g; next_index = g.enumerate(list, next_index, true);
if (recurse && next_index != ts.length) }
next_index = g.enumerate(ts, next_index, true);
} }
return next_index; return next_index;
} }
public int enumerate (ThreadGroup[] gs) /** Copy all active ThreadGroups that are descendants of this
* ThreadGroup into the specified array. If the array is not
* large enough to hold all active ThreadGroups, extra
* ThreadGroups simply will not be copied.
*
* @param groups the array to put the ThreadGroups into.
* @return the number of ThreadGroups copied into the array.
*/
public int enumerate(ThreadGroup[] groups)
{
return enumerate(groups, false);
}
/** Copy all active ThreadGroups that are children of this
* ThreadGroup into the specified array, and if desired, also
* copy all active descendants into the array. If the array
* is not large enough to hold all active ThreadGroups, extra
* ThreadGroups simply will not be copied.
*
* @param groups the array to put the ThreadGroups into.
* @param useDescendants whether to include all descendants
* of this ThreadGroup's children in determining
* activeness.
* @return the number of ThreadGroups copied into the array.
*/
public int enumerate(ThreadGroup[] groups, boolean useDescendants)
{ {
return enumerate (gs, 0, true); return enumerate(groups, 0, useDescendants);
} }
public int enumerate (ThreadGroup[] gs, boolean recurse) // This actually implements enumerate.
private int enumerate (ThreadGroup[] list, int next_index, boolean recurse)
{ {
return enumerate (gs, 0, recurse); Enumeration e = groups.elements();
} while (e.hasMoreElements() && next_index < list.length)
public final int getMaxPriority ()
{ {
return maxpri; ThreadGroup g = (ThreadGroup) e.nextElement();
list[next_index++] = g;
if (recurse && next_index != list.length)
next_index = g.enumerate(list, next_index, true);
} }
return next_index;
public final String getName ()
{
return name;
} }
public final ThreadGroup getParent () /** Interrupt all Threads in this ThreadGroup and its sub-groups.
* @exception SecurityException if you cannot modify this
* ThreadGroup or any of its Threads or children
* ThreadGroups.
* @since JDK1.2
*/
public final void interrupt()
{ {
return parent; checkAccess();
} for (int i=0; i < threads.size(); i++)
// JDK 1.2.
// public void interrupt ();
public final boolean isDaemon ()
{ {
return daemon_flag; Thread t = (Thread) threads.elementAt(i);
t.interrupt();
} }
for (int i=0; i < groups.size(); i++)
public synchronized boolean isDestroyed ()
{ {
return destroyed_flag; ThreadGroup tg = (ThreadGroup) groups.elementAt(i);
tg.interrupt();
}
} }
private final void list (String indentation) /** Stop all Threads in this ThreadGroup and its descendants.
* @exception SecurityException if you cannot modify this
* ThreadGroup or any of its Threads or children
* ThreadGroups.
* @deprecated This method calls Thread.stop(), which is dangerous.
*/
public final void stop()
{ {
System.out.print(indentation); checkAccess();
System.out.println(toString ()); for (int i=0; i<threads.size(); i++)
String sub = indentation + " ";
Enumeration e = threads.elements();
while (e.hasMoreElements())
{ {
Thread t = (Thread) e.nextElement(); Thread t = (Thread) threads.elementAt(i);
System.out.print(sub); t.stop();
System.out.println(t.toString());
} }
e = groups.elements(); for (int i=0; i < groups.size(); i++)
while (e.hasMoreElements())
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); ThreadGroup tg = (ThreadGroup) groups.elementAt(i);
g.list(sub); tg.stop();
} }
} }
public void list () /** Suspend all Threads in this ThreadGroup and its descendants.
* @exception SecurityException if you cannot modify this
* ThreadGroup or any of its Threads or children
* ThreadGroups.
* @deprecated This method calls Thread.suspend(), which is dangerous.
*/
public final void suspend()
{ {
list (""); checkAccess();
} for (int i=0; i<threads.size(); i++)
public final boolean parentOf (ThreadGroup g)
{ {
while (g != null) Thread t = (Thread) threads.elementAt(i);
t.suspend();
}
for (int i=0; i < groups.size(); i++)
{ {
if (this == g) ThreadGroup tg = (ThreadGroup) groups.elementAt(i);
return true; tg.suspend();
g = g.parent;
} }
return false;
} }
// Deprecated in 1.2. /** Resume all Threads in this ThreadGroup and its descendants.
public final void resume () * @exception SecurityException if you cannot modify this
* ThreadGroup or any of its Threads or children
* ThreadGroups.
* @deprecated This method relies on Thread.suspend(), which is dangerous.
*/
public final void resume()
{ {
checkAccess (); checkAccess();
Enumeration e = threads.elements(); for (int i=0; i < threads.size(); i++)
while (e.hasMoreElements())
{ {
Thread t = (Thread) e.nextElement(); Thread t = (Thread) threads.elementAt(i);
t.resume(); t.resume();
} }
e = groups.elements(); for (int i=0; i < groups.size(); i++)
while (e.hasMoreElements())
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); ThreadGroup tg = (ThreadGroup) groups.elementAt(i);
g.resume(); tg.resume();
} }
} }
public final void setDaemon (boolean daemon) // This is a helper that is used to implement the destroy method.
private final void checkDestroy ()
{ {
checkAccess (); if (! threads.isEmpty())
daemon_flag = daemon; throw new IllegalThreadStateException ("ThreadGroup has threads");
// FIXME: the docs don't say you are supposed to do this. But for (int i=0; i < groups.size(); i++)
// they don't say you aren't, either.
if (groups.size() == 0 && threads.size() == 0)
destroy ();
}
public final void setMaxPriority (int pri)
{ {
checkAccess (); ThreadGroup tg = (ThreadGroup) groups.elementAt(i);
tg.checkDestroy();
}
}
// FIXME: JDK 1.2 behaviour is different: if the newMaxPriority /** Destroy this ThreadGroup. There can be no Threads in it,
// argument is < MIN_PRIORITY or > MAX_PRIORITY an * and none of its descendants (sub-groups) may have Threads in them.
// IllegalArgumentException should be thrown. * All its descendants will be destroyed as well.
if (pri >= Thread.MIN_PRIORITY && pri <= maxpri) * @exception IllegalThreadStateException if the ThreadGroup or
* its descendants have Threads remaining in them, or
* if the ThreadGroup in question is already destroyed.
* @exception SecurityException if you cannot modify this
* ThreadGroup or any of its descendants.
*/
public final void destroy()
{ {
maxpri = pri; checkAccess();
if (destroyed_flag)
throw new IllegalThreadStateException("Already destroyed.");
checkDestroy ();
if (parent != null)
parent.remove(this);
destroyed_flag = true;
parent = null;
Enumeration e = groups.elements(); for (int i=0; i < groups.size(); i++)
while (e.hasMoreElements())
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); ThreadGroup tg = (ThreadGroup) groups.elementAt(i);
g.setMaxPriority (maxpri); tg.destroy();
} }
} }
/** Print out information about this ThreadGroup to System.out.
*/
public void list()
{
list("");
} }
// Deprecated in 1.2. private final void list (String indentation)
public final void stop ()
{ {
checkAccess (); System.out.print(indentation);
Enumeration e = threads.elements(); System.out.println(toString ());
while (e.hasMoreElements()) String sub = indentation + " ";
for (int i=0; i < threads.size(); i++)
{ {
Thread t = (Thread) e.nextElement(); Thread t = (Thread) threads.elementAt(i);
t.stop(); System.out.print(sub);
System.out.println(t.toString());
} }
e = groups.elements(); for (int i=0; i < groups.size(); i++)
while (e.hasMoreElements())
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); ThreadGroup tg = (ThreadGroup) groups.elementAt(i);
g.stop(); tg.list(sub);
} }
} }
// Deprecated in 1.2. /** When a Thread in this ThreadGroup does not catch an exception,
public final void suspend () * this method of the ThreadGroup is called.<P>
{ *
checkAccess (); * ThreadGroup's implementation does the following:<BR>
Enumeration e = threads.elements(); * <OL>
while (e.hasMoreElements()) * <LI>If there is a parent ThreadGroup, call uncaughtException()
* in the parent.</LI>
* <LI>If the Throwable passed is a ThreadDeath, don't do
* anything.</LI>
* <LI>Otherwise, call <CODE>exception.printStackTrace().</CODE></LI>
* </OL>
*
* @param thread the thread that exited.
* @param exception the uncaught exception.
*/
public void uncaughtException(Thread thread, Throwable t)
{ {
Thread t = (Thread) e.nextElement(); if (parent != null)
t.suspend(); parent.uncaughtException (thread, t);
} else if (! (t instanceof ThreadDeath))
e = groups.elements(); t.printStackTrace();
while (e.hasMoreElements()) }
/** Tell the VM whether it may suspend Threads in low memory
* situations.
* @deprecated This method is unimplemented, because it would rely on
* suspend(), which is deprecated. There is no way for a Java
* program to determine whether this has any effect whatsoever,
* so we don't need it.
* @return false
*/
public boolean allowThreadSuspension(boolean allow)
{ {
ThreadGroup g = (ThreadGroup) e.nextElement(); return false;
g.suspend();
}
} }
// This constructor appears in the Class Libraries book but in /** Get a human-readable representation of this ThreadGroup.
// neither the Language Spec nor the 1.2 docs. * @return a String representing this ThreadGroup.
public ThreadGroup () * @specnote Language Spec and Class Libraries book disagree a bit here.
* We follow the Spec, but add "ThreadGroup" per the book. We
* include "java.lang" based on the list() example in the Class
* Libraries book.
*/
public String toString ()
{ {
this (Thread.currentThread().getThreadGroup(), null); return "java.lang.ThreadGroup[name=" + name +
",maxpri=" + maxpri + "]";
} }
public ThreadGroup (String n) /** Find out if the current Thread can modify this ThreadGroup.
* Calls the current SecurityManager's checkAccess() method to
* find out. If there is none, it assumes everything's OK.
* @exception SecurityException if the current Thread cannot
* modify this ThreadGroup.
*/
public final void checkAccess()
{ {
this (Thread.currentThread().getThreadGroup(), n); SecurityManager sm = System.getSecurityManager();
if (sm != null)
sm.checkAccess(this);
} }
public ThreadGroup (ThreadGroup p, String n) // This is called to add a Thread to our internal list.
final void add(Thread t)
{ {
checkAccess (); if (destroyed_flag)
if (p.destroyed_flag) throw new IllegalThreadStateException ("ThreadGroup is destroyed");
throw new IllegalArgumentException ();
parent = p; threads.addElement(t);
name = n;
maxpri = p.maxpri;
threads = new Vector ();
groups = new Vector ();
daemon_flag = p.daemon_flag;
destroyed_flag = false;
p.groups.addElement(this);
} }
// This is the constructor that is used when creating the very first // This is called to remove a Thread from our internal list.
// ThreadGroup. We have an arbitrary argument here just to final void remove(Thread t)
// differentiate this constructor from the others.
ThreadGroup (int dummy)
{ {
parent = null; if (destroyed_flag)
name = "main"; throw new IllegalThreadStateException ();
maxpri = Thread.MAX_PRIORITY;
threads = new Vector (); threads.removeElement(t);
groups = new Vector (); // Daemon groups are automatically destroyed when all their threads die.
daemon_flag = false; if (daemon_flag && groups.size() == 0 && threads.size() == 0)
destroyed_flag = false; {
// We inline destroy to avoid the access check.
if (parent != null)
parent.remove(this);
destroyed_flag = true;
}
} }
public String toString () // This is called to add a ThreadGroup to our internal list.
final void add(ThreadGroup g)
{ {
// Language Spec and Class Libraries book disagree a bit here. We groups.addElement(g);
// follow the Spec, but add "ThreadGroup" per the book. We
// include "java.lang" based on the list() example in the Class
// Libraries book.
return "java.lang.ThreadGroup[name=" + name + ",maxpri=" + maxpri + "]";
} }
public void uncaughtException (Thread thread, Throwable e) // This is called to remove a ThreadGroup from our internal list.
final void remove(ThreadGroup g)
{ {
// FIXME: in 1.2, this has different semantics. In particular if groups.removeElement(g);
// this group has a parent, the exception is passed upwards and // Daemon groups are automatically destroyed when all their threads die.
// not processed locally. if (daemon_flag && groups.size() == 0 && threads.size() == 0)
if (! (e instanceof ThreadDeath))
{ {
e.printStackTrace(); // We inline destroy to avoid the access check.
if (parent != null)
parent.remove(this);
destroyed_flag = true;
} }
} }
// Private data.
private ThreadGroup parent;
private String name;
private int maxpri;
private Vector threads;
private Vector groups;
private boolean daemon_flag;
private boolean destroyed_flag;
} }
...@@ -637,9 +637,6 @@ JvConvertArgv (int argc, const char **argv) ...@@ -637,9 +637,6 @@ JvConvertArgv (int argc, const char **argv)
// Command line arguments. // Command line arguments.
static jobject arg_vec; static jobject arg_vec;
// The primary threadgroup.
static java::lang::ThreadGroup *main_group;
// The primary thread. // The primary thread.
static java::lang::Thread *main_thread; static java::lang::Thread *main_thread;
...@@ -882,9 +879,7 @@ JvRunMain (jclass klass, int argc, const char **argv) ...@@ -882,9 +879,7 @@ JvRunMain (jclass klass, int argc, const char **argv)
#endif #endif
arg_vec = JvConvertArgv (argc - 1, argv + 1); arg_vec = JvConvertArgv (argc - 1, argv + 1);
main_group = new java::lang::ThreadGroup (23); main_thread = new gnu::gcj::runtime::FirstThread (klass, arg_vec);
main_thread = new gnu::gcj::runtime::FirstThread (main_group,
klass, arg_vec);
main_thread->start(); main_thread->start();
_Jv_ThreadWait (); _Jv_ThreadWait ();
...@@ -906,9 +901,7 @@ _Jv_RunMain (const char *class_name, int argc, const char **argv) ...@@ -906,9 +901,7 @@ _Jv_RunMain (const char *class_name, int argc, const char **argv)
#endif #endif
arg_vec = JvConvertArgv (argc - 1, argv + 1); arg_vec = JvConvertArgv (argc - 1, argv + 1);
main_group = new java::lang::ThreadGroup (23); main_thread = new gnu::gcj::runtime::FirstThread (JvNewStringLatin1 (class_name),
main_thread = new gnu::gcj::runtime::FirstThread (main_group,
JvNewStringLatin1 (class_name),
arg_vec); arg_vec);
main_thread->start(); main_thread->start();
_Jv_ThreadWait (); _Jv_ThreadWait ();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment