ThreadGrouppublic class ThreadGroup extends Object implements Thread$UncaughtExceptionHandlerA thread group represents a set of threads. In addition, a thread
group can also include other thread groups. The thread groups form
a tree in which every thread group except the initial thread group
has a parent.
A thread is allowed to access information about its own thread
group, but not to access information about its thread group's
parent thread group or any other thread groups. |
Fields Summary |
---|
ThreadGroup | parent | String | name | int | maxPriority | boolean | destroyed | boolean | daemon | boolean | vmAllowSuspension | int | nUnstartedThreads | int | nthreads | Thread[] | threads | int | ngroups | ThreadGroup[] | groups |
Constructors Summary |
---|
private ThreadGroup()Creates an empty Thread group that is not in any Thread group.
This method is used to create the system Thread group.
// called from C code
this.name = "system";
this.maxPriority = Thread.MAX_PRIORITY;
| public ThreadGroup(String name)Constructs a new thread group. The parent of this new group is
the thread group of the currently running thread.
The checkAccess method of the parent thread group is
called with no arguments; this may result in a security exception.
this(Thread.currentThread().getThreadGroup(), name);
| public ThreadGroup(ThreadGroup parent, String name)Creates a new thread group. The parent of this new group is the
specified thread group.
The checkAccess method of the parent thread group is
called with no arguments; this may result in a security exception.
if (parent == null) {
throw new NullPointerException();
}
parent.checkAccess();
this.name = name;
this.maxPriority = parent.maxPriority;
this.daemon = parent.daemon;
this.vmAllowSuspension = parent.vmAllowSuspension;
this.parent = parent;
parent.add(this);
|
Methods Summary |
---|
public int | activeCount()Returns an estimate of the number of active threads in this
thread group. The result might not reflect concurrent activity,
and might be affected by the presence of certain system threads.
Due to the inherently imprecise nature of the result, it is
recommended that this method only be used for informational purposes.
int result;
// Snapshot sub-group data so we don't hold this lock
// while our children are computing.
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
if (destroyed) {
return 0;
}
result = nthreads;
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
result += groupsSnapshot[i].activeCount();
}
return result;
| public int | activeGroupCount()Returns an estimate of the number of active groups in this
thread group. The result might not reflect concurrent activity.
Due to the inherently imprecise nature of the result, it is
recommended that this method only be used for informational purposes.
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
if (destroyed) {
return 0;
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
int n = ngroupsSnapshot;
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
n += groupsSnapshot[i].activeGroupCount();
}
return n;
| private final void | add(java.lang.ThreadGroup g)Adds the specified Thread group to this group.
synchronized (this) {
if (destroyed) {
throw new IllegalThreadStateException();
}
if (groups == null) {
groups = new ThreadGroup[4];
} else if (ngroups == groups.length) {
ThreadGroup newgroups[] = new ThreadGroup[ngroups * 2];
System.arraycopy(groups, 0, newgroups, 0, ngroups);
groups = newgroups;
}
groups[ngroups] = g;
// This is done last so it doesn't matter in case the
// thread is killed
ngroups++;
}
| void | add(java.lang.Thread t)Adds the specified Thread to this group.
synchronized (this) {
if (destroyed) {
throw new IllegalThreadStateException();
}
if (threads == null) {
threads = new Thread[4];
} else if (nthreads == threads.length) {
Thread newthreads[] = new Thread[nthreads * 2];
System.arraycopy(threads, 0, newthreads, 0, nthreads);
threads = newthreads;
}
threads[nthreads] = t;
// This is done last so it doesn't matter in case the
// thread is killed
nthreads++;
nUnstartedThreads--;
}
| void | addUnstarted()Increments the count of unstarted threads in the thread group.
Unstarted threads are not added to the thread group so that they
can be collected if they are never started, but they must be
counted so that daemon thread groups with unstarted threads in
them are not destroyed.
synchronized(this) {
if (destroyed) {
throw new IllegalThreadStateException();
}
nUnstartedThreads++;
}
| public boolean | allowThreadSuspension(boolean b)Used by VM to control lowmem implicit suspension.
this.vmAllowSuspension = b;
if (!b) {
VM.unsuspendSomeThreads();
}
return true;
| public final void | checkAccess()Determines if the currently running thread has permission to
modify this thread group.
If there is a security manager, its checkAccess method
is called with this thread group as its argument. This may result
in throwing a SecurityException .
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkAccess(this);
}
| public final void | destroy()Destroys this thread group and all of its subgroups. This thread
group must be empty, indicating that all threads that had been in
this thread group have since stopped.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
if (destroyed || (nthreads > 0)) {
throw new IllegalThreadStateException();
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
if (parent != null) {
destroyed = true;
ngroups = 0;
groups = null;
nthreads = 0;
threads = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i += 1) {
groupsSnapshot[i].destroy();
}
if (parent != null) {
parent.remove(this);
}
| public int | enumerate(java.lang.Thread[] list)Copies into the specified array every active thread in this
thread group and its subgroups.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
An application might use the activeCount method to
get an estimate of how big the array should be, however if the
array is too short to hold all the threads, the extra threads are
silently ignored. If it is critical to obtain every active
thread in this thread group and its subgroups, the caller should
verify that the returned int value is strictly less than the length
of list.
Due to the inherent race condition in this method, it is recommended
that the method only be used for informational purposes.
checkAccess();
return enumerate(list, 0, true);
| public int | enumerate(java.lang.Thread[] list, boolean recurse)Copies into the specified array every active thread in this
thread group. If the recurse flag is
true , references to every active thread in this
thread's subgroups are also included. If the array is too short to
hold all the threads, the extra threads are silently ignored.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
An application might use the activeCount method to
get an estimate of how big the array should be, however if the
array is too short to hold all the threads, the extra threads are
silently ignored. If it is critical to obtain every active thread
in this thread group, the caller should verify that the returned int
value is strictly less than the length of list.
Due to the inherent race condition in this method, it is recommended
that the method only be used for informational purposes.
checkAccess();
return enumerate(list, 0, recurse);
| private int | enumerate(java.lang.Thread[] list, int n, boolean recurse)
int ngroupsSnapshot = 0;
ThreadGroup[] groupsSnapshot = null;
synchronized (this) {
if (destroyed) {
return 0;
}
int nt = nthreads;
if (nt > list.length - n) {
nt = list.length - n;
}
for (int i = 0; i < nt; i++) {
if (threads[i].isAlive()) {
list[n++] = threads[i];
}
}
if (recurse) {
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
}
if (recurse) {
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
n = groupsSnapshot[i].enumerate(list, n, true);
}
}
return n;
| public int | enumerate(java.lang.ThreadGroup[] list)Copies into the specified array references to every active
subgroup in this thread group.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
An application might use the activeGroupCount method to
get an estimate of how big the array should be, however if the
array is too short to hold all the thread groups, the extra thread
groups are silently ignored. If it is critical to obtain every
active subgroup in this thread group, the caller should verify that
the returned int value is strictly less than the length of
list.
Due to the inherent race condition in this method, it is recommended
that the method only be used for informational purposes.
checkAccess();
return enumerate(list, 0, true);
| public int | enumerate(java.lang.ThreadGroup[] list, boolean recurse)Copies into the specified array references to every active
subgroup in this thread group. If the recurse flag is
true , references to all active subgroups of the
subgroups and so forth are also included.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
An application might use the activeGroupCount method to
get an estimate of how big the array should be, however if the
array is too short to hold all the thread groups, the extra thread
groups are silently ignored. If it is critical to obtain every
active subgroup in this thread group, the caller should verify that
the returned int value is strictly less than the length of
list.
Due to the inherent race condition in this method, it is recommended
that the method only be used for informational purposes.
checkAccess();
return enumerate(list, 0, recurse);
| private int | enumerate(java.lang.ThreadGroup[] list, int n, boolean recurse)
int ngroupsSnapshot = 0;
ThreadGroup[] groupsSnapshot = null;
synchronized (this) {
if (destroyed) {
return 0;
}
int ng = ngroups;
if (ng > list.length - n) {
ng = list.length - n;
}
if (ng > 0) {
System.arraycopy(groups, 0, list, n, ng);
n += ng;
}
if (recurse) {
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
}
if (recurse) {
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
n = groupsSnapshot[i].enumerate(list, n, true);
}
}
return n;
| public final int | getMaxPriority()Returns the maximum priority of this thread group. Threads that are
part of this group cannot have a higher priority than the maximum
priority.
return maxPriority;
| public final java.lang.String | getName()Returns the name of this thread group.
return name;
| public final java.lang.ThreadGroup | getParent()Returns the parent of this thread group.
First, if the parent is not null , the
checkAccess method of the parent thread group is
called with no arguments; this may result in a security exception.
if (parent != null)
parent.checkAccess();
return parent;
| public final void | interrupt()Interrupts all threads in this thread group.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
This method then calls the interrupt method on all the
threads in this thread group and in all of its subgroups.
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
for (int i = 0 ; i < nthreads ; i++) {
threads[i].interrupt();
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].interrupt();
}
| public final boolean | isDaemon()Tests if this thread group is a daemon thread group. A
daemon thread group is automatically destroyed when its last
thread is stopped or its last thread group is destroyed.
return daemon;
| public synchronized boolean | isDestroyed()Tests if this thread group has been destroyed.
return destroyed;
| public void | list()Prints information about this thread group to the standard
output. This method is useful only for debugging.
list(System.out, 0);
| void | list(java.io.PrintStream out, int indent)
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
for (int j = 0 ; j < indent ; j++) {
out.print(" ");
}
out.println(this);
indent += 4;
for (int i = 0 ; i < nthreads ; i++) {
for (int j = 0 ; j < indent ; j++) {
out.print(" ");
}
out.println(threads[i]);
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].list(out, indent);
}
| public final boolean | parentOf(java.lang.ThreadGroup g)Tests if this thread group is either the thread group
argument or one of its ancestor thread groups.
for (; g != null ; g = g.parent) {
if (g == this) {
return true;
}
}
return false;
| private void | remove(java.lang.ThreadGroup g)Removes the specified Thread group from this group.
synchronized (this) {
if (destroyed) {
return;
}
for (int i = 0 ; i < ngroups ; i++) {
if (groups[i] == g) {
ngroups -= 1;
System.arraycopy(groups, i + 1, groups, i, ngroups - i);
// Zap dangling reference to the dead group so that
// the garbage collector will collect it.
groups[ngroups] = null;
break;
}
}
if (nthreads == 0) {
notifyAll();
}
if (daemon && (nthreads == 0) &&
(nUnstartedThreads == 0) && (ngroups == 0))
{
destroy();
}
}
| void | remove(java.lang.Thread t)Removes the specified Thread from this group.
synchronized (this) {
if (destroyed) {
return;
}
for (int i = 0 ; i < nthreads ; i++) {
if (threads[i] == t) {
System.arraycopy(threads, i + 1, threads, i, --nthreads - i);
// Zap dangling reference to the dead thread so that
// the garbage collector will collect it.
threads[nthreads] = null;
break;
}
}
if (nthreads == 0) {
notifyAll();
}
if (daemon && (nthreads == 0) &&
(nUnstartedThreads == 0) && (ngroups == 0))
{
destroy();
}
}
| public final void | resume()Resumes all threads in this thread group.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
This method then calls the resume method on all the
threads in this thread group and in all of its sub groups.
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
for (int i = 0 ; i < nthreads ; i++) {
threads[i].resume();
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].resume();
}
| public final void | setDaemon(boolean daemon)Changes the daemon status of this thread group.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
A daemon thread group is automatically destroyed when its last
thread is stopped or its last thread group is destroyed.
checkAccess();
this.daemon = daemon;
| public final void | setMaxPriority(int pri)Sets the maximum priority of the group. Threads in the thread
group that already have a higher priority are not affected.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
If the pri argument is less than
{@link Thread#MIN_PRIORITY} or greater than
{@link Thread#MAX_PRIORITY}, the maximum priority of the group
remains unchanged.
Otherwise, the priority of this ThreadGroup object is set to the
smaller of the specified pri and the maximum permitted
priority of the parent of this thread group. (If this thread group
is the system thread group, which has no parent, then its maximum
priority is simply set to pri .) Then this method is
called recursively, with pri as its argument, for
every thread group that belongs to this thread group.
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
if (pri < Thread.MIN_PRIORITY) {
maxPriority = Thread.MIN_PRIORITY;
} else if (pri < maxPriority) {
maxPriority = pri;
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].setMaxPriority(pri);
}
| public final void | stop()Stops all threads in this thread group.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
This method then calls the stop method on all the
threads in this thread group and in all of its subgroups.
if (stopOrSuspend(false))
Thread.currentThread().stop();
| private boolean | stopOrSuspend(boolean suspend)Helper method: recursively stops or suspends (as directed by the
boolean argument) all of the threads in this thread group and its
subgroups, except the current thread. This method returns true
if (and only if) the current thread is found to be in this thread
group or one of its subgroups.
boolean suicide = false;
Thread us = Thread.currentThread();
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot = null;
synchronized (this) {
checkAccess();
for (int i = 0 ; i < nthreads ; i++) {
if (threads[i]==us)
suicide = true;
else if (suspend)
threads[i].suspend();
else
threads[i].stop();
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = new ThreadGroup[ngroupsSnapshot];
System.arraycopy(groups, 0, groupsSnapshot, 0, ngroupsSnapshot);
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++)
suicide = groupsSnapshot[i].stopOrSuspend(suspend) || suicide;
return suicide;
| public final void | suspend()Suspends all threads in this thread group.
First, the checkAccess method of this thread group is
called with no arguments; this may result in a security exception.
This method then calls the suspend method on all the
threads in this thread group and in all of its subgroups.
if (stopOrSuspend(true))
Thread.currentThread().suspend();
| public java.lang.String | toString()Returns a string representation of this Thread group.
return getClass().getName() + "[name=" + getName() + ",maxpri=" + maxPriority + "]";
| public void | uncaughtException(java.lang.Thread t, java.lang.Throwable e)Called by the Java Virtual Machine when a thread in this
thread group stops because of an uncaught exception, and the thread
does not have a specific {@link Thread.UncaughtExceptionHandler}
installed.
The uncaughtException method of
ThreadGroup does the following:
- If this thread group has a parent thread group, the
uncaughtException method of that parent is called
with the same two arguments.
- Otherwise, this method checks to see if there is a
{@linkplain Thread#getDefaultUncaughtExceptionHandler default
uncaught exception handler} installed, and if so, its
uncaughtException method is called with the same
two arguments.
- Otherwise, this method determines if the
Throwable
argument is an instance of {@link ThreadDeath}. If so, nothing
special is done. Otherwise, a message containing the
thread's name, as returned from the thread's {@link
Thread#getName getName} method, and a stack backtrace,
using the Throwable 's {@link
Throwable#printStackTrace printStackTrace} method, is
printed to the {@linkplain System#err standard error stream}.
Applications can override this method in subclasses of
ThreadGroup to provide alternative handling of
uncaught exceptions.
if (parent != null) {
parent.uncaughtException(t, e);
} else {
Thread.UncaughtExceptionHandler ueh =
Thread.getDefaultUncaughtExceptionHandler();
if (ueh != null) {
ueh.uncaughtException(t, e);
} else if (!(e instanceof ThreadDeath)) {
System.err.print("Exception in thread \""
+ t.getName() + "\" ");
e.printStackTrace(System.err);
}
}
|
|