Method from java.lang.ThreadGroup Detail: |
public int activeCount() {
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 = Arrays.copyOf(groups, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
result += groupsSnapshot[i].activeCount();
}
return result;
}
Returns an estimate of the number of active threads in this thread
group and its subgroups. Recursively iterates over all subgroups in
this thread group.
The value returned is only an estimate because the number of
threads may change dynamically while this method traverses internal
data structures, and might be affected by the presence of certain
system threads. This method is intended primarily for debugging
and monitoring purposes. |
public int activeGroupCount() {
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
if (destroyed) {
return 0;
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
int n = ngroupsSnapshot;
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
n += groupsSnapshot[i].activeGroupCount();
}
return n;
}
Returns an estimate of the number of active groups in this
thread group and its subgroups. Recursively iterates over
all subgroups in this thread group.
The value returned is only an estimate because the number of
thread groups may change dynamically while this method traverses
internal data structures. This method is intended primarily for
debugging and monitoring purposes. |
void add(Thread t) {
synchronized (this) {
if (destroyed) {
throw new IllegalThreadStateException();
}
if (threads == null) {
threads = new Thread[4];
} else if (nthreads == threads.length) {
threads = Arrays.copyOf(threads, nthreads * 2);
}
threads[nthreads] = t;
// This is done last so it doesn't matter in case the
// thread is killed
nthreads++;
// The thread is now a fully fledged member of the group, even
// though it may, or may not, have been started yet. It will prevent
// the group from being destroyed so the unstarted Threads count is
// decremented.
nUnstartedThreads--;
}
}
Adds the specified thread to this thread group.
Note: This method is called from both library code
and the Virtual Machine. It is called from VM to add
certain system threads to the system thread group. |
void addUnstarted() {
synchronized(this) {
if (destroyed) {
throw new IllegalThreadStateException();
}
nUnstartedThreads++;
}
}
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. |
public boolean allowThreadSuspension(boolean b) {
this.vmAllowSuspension = b;
if (!b) {
VM.unsuspendSomeThreads();
}
return true;
} Deprecated! The - definition of this call depends on #suspend ,
which is deprecated. Further, the behavior of this call
was never specified.
Used by VM to control lowmem implicit suspension. |
public final void checkAccess() {
SecurityManager security = System.getSecurityManager();
if (security != null) {
security.checkAccess(this);
}
}
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 . |
public final void destroy() {
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
if (destroyed || (nthreads > 0)) {
throw new IllegalThreadStateException();
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = Arrays.copyOf(groups, 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);
}
}
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. |
public int enumerate(Thread[] list) {
checkAccess();
return enumerate(list, 0, true);
}
|
public int enumerate(ThreadGroup[] list) {
checkAccess();
return enumerate(list, 0, true);
}
|
public int enumerate(Thread[] list,
boolean recurse) {
checkAccess();
return enumerate(list, 0, recurse);
}
Copies into the specified array every active thread in this
thread group. If {@code recurse} is {@code true},
this method recursively enumerates all subgroups of this
thread group and references to every active thread in these
subgroups are also included. If the array is too short to
hold all the threads, the extra threads are silently ignored.
An application might use the {@linkplain #activeCount 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 {@code list}.
Due to the inherent race condition in this method, it is recommended
that the method only be used for debugging and monitoring purposes. |
public int enumerate(ThreadGroup[] list,
boolean recurse) {
checkAccess();
return enumerate(list, 0, recurse);
}
Copies into the specified array references to every active
subgroup in this thread group. If {@code recurse} is
{@code true}, this method recursively enumerates all subgroups of this
thread group and references to every active thread group in these
subgroups are also included.
An application might use the
{@linkplain #activeGroupCount 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
{@code list}.
Due to the inherent race condition in this method, it is recommended
that the method only be used for debugging and monitoring purposes. |
public final int getMaxPriority() {
return maxPriority;
}
Returns the maximum priority of this thread group. Threads that are
part of this group cannot have a higher priority than the maximum
priority. |
public final String getName() {
return name;
}
Returns the name of this thread group. |
public final ThreadGroup getParent() {
if (parent != null)
parent.checkAccess();
return parent;
}
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. |
public final void interrupt() {
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
for (int i = 0 ; i < nthreads ; i++) {
threads[i].interrupt();
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].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. |
public final boolean isDaemon() {
return daemon;
}
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. |
public synchronized boolean isDestroyed() {
return destroyed;
}
Tests if this thread group has been destroyed. |
public void list() {
list(System.out, 0);
}
Prints information about this thread group to the standard
output. This method is useful only for debugging. |
void list(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 = Arrays.copyOf(groups, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].list(out, indent);
}
}
|
public final boolean parentOf(ThreadGroup g) {
for (; g != null ; g = g.parent) {
if (g == this) {
return true;
}
}
return false;
}
Tests if this thread group is either the thread group
argument or one of its ancestor thread groups. |
public final void resume() {
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
for (int i = 0 ; i < nthreads ; i++) {
threads[i].resume();
}
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].resume();
}
} Deprecated! This - method is used solely in conjunction with
Thread.suspend and ThreadGroup.suspend,
both of which have been deprecated, as they are inherently
deadlock-prone. See Thread#suspend for details.
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. |
public final void setDaemon(boolean daemon) {
checkAccess();
this.daemon = 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. |
public final void setMaxPriority(int pri) {
int ngroupsSnapshot;
ThreadGroup[] groupsSnapshot;
synchronized (this) {
checkAccess();
if (pri < Thread.MIN_PRIORITY || pri > Thread.MAX_PRIORITY) {
return;
}
maxPriority = (parent != null) ? Math.min(pri, parent.maxPriority) : pri;
ngroupsSnapshot = ngroups;
if (groups != null) {
groupsSnapshot = Arrays.copyOf(groups, ngroupsSnapshot);
} else {
groupsSnapshot = null;
}
}
for (int i = 0 ; i < ngroupsSnapshot ; i++) {
groupsSnapshot[i].setMaxPriority(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
Thread#MIN_PRIORITY or greater than
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. |
public final void stop() {
if (stopOrSuspend(false))
Thread.currentThread().stop();
} Deprecated! This - method is inherently unsafe. See
Thread#stop for details.
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. |
public final void suspend() {
if (stopOrSuspend(true))
Thread.currentThread().suspend();
} Deprecated! This - method is inherently deadlock-prone. See
Thread#suspend for details.
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. |
void threadStartFailed(Thread t) {
synchronized(this) {
remove(t);
nUnstartedThreads++;
}
}
Notifies the group that the thread {@code t} has failed
an attempt to start.
The state of this thread group is rolled back as if the
attempt to start the thread has never occurred. The thread is again
considered an unstarted member of the thread group, and a subsequent
attempt to start the thread is permitted. |
void threadTerminated(Thread t) {
synchronized (this) {
remove(t);
if (nthreads == 0) {
notifyAll();
}
if (daemon && (nthreads == 0) &&
(nUnstartedThreads == 0) && (ngroups == 0))
{
destroy();
}
}
}
Notifies the group that the thread {@code t} has terminated.
Destroy the group if all of the following conditions are
true: this is a daemon thread group; there are no more alive
or unstarted threads in the group; there are no subgroups in
this thread group. |
public String toString() {
return getClass().getName() + "[name=" + getName() + ",maxpri=" + maxPriority + "]";
}
Returns a string representation of this Thread group. |
public void uncaughtException(Thread t,
Throwable e) {
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);
}
}
}
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 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 ThreadDeath . If so, nothing
special is done. Otherwise, a message containing the
thread's name, as returned from the thread's getName method, and a stack backtrace,
using the Throwable 's 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. |