Class ThreadGroup
- All Implemented Interfaces:
Thread.UncaughtExceptionHandler
public class ThreadGroup extends Object implements Thread.UncaughtExceptionHandler
ThreadGroup is a means of organizing threads into a hierarchical structure.
This class is obsolete. See Effective Java Item 73, "Avoid thread groups" for details.- See Also:
Thread
-
Constructor Summary
Constructors Constructor Description ThreadGroup(String name)Constructs a newThreadGroupwith the given name.ThreadGroup(ThreadGroup parent, String name)Constructs a newThreadGroupwith the given name, as a child of the givenThreadGroup. -
Method Summary
Modifier and Type Method Description intactiveCount()Returns the number of runningThreads which are children of this thread group, directly or indirectly.intactiveGroupCount()Returns the number ofThreadGroups which are children of this group, directly or indirectly.booleanallowThreadSuspension(boolean b)Deprecated.voidcheckAccess()Does nothing.voiddestroy()Destroys this thread group and recursively all its subgroups.intenumerate(Thread[] threads)Iterates over all active threads in this group (and its sub-groups) and stores the threads in the given array.intenumerate(Thread[] threads, boolean recurse)Iterates over all active threads in this group (and, optionally, its sub-groups) and stores the threads in the given array.intenumerate(ThreadGroup[] groups)Iterates over all thread groups in this group (and its sub-groups) and and stores the groups in the given array.intenumerate(ThreadGroup[] groups, boolean recurse)Iterates over all thread groups in this group (and, optionally, its sub-groups) and stores the groups in the given array.intgetMaxPriority()Returns the maximum allowed priority for aThreadin this thread group.StringgetName()Returns the name of this thread group.ThreadGroupgetParent()Returns this thread group's parentThreadGroup.voidinterrupt()Interrupts everyThreadin this group and recursively in all its subgroups.booleanisDaemon()Checks whether this thread group is a daemonThreadGroup.booleanisDestroyed()Checks whether this thread group has already been destroyed.voidlist()Outputs toSystem.outa text representation of the hierarchy ofThreads andThreadGroups in this thread group (and recursively).booleanparentOf(ThreadGroup g)Checks whether this thread group is a direct or indirect parent group of a givenThreadGroup.voidresume()Deprecated.Requires deprecated methodThread.resume().voidsetDaemon(boolean isDaemon)Sets whether this is a daemonThreadGroupor not.voidsetMaxPriority(int newMax)Configures the maximum allowed priority for aThreadin this group and recursively in all its subgroups.voidstop()Deprecated.Requires deprecated methodThread.stop().voidsuspend()Deprecated.Requires deprecated methodThread.suspend().StringtoString()Returns a string containing a concise, human-readable description of this object.voiduncaughtException(Thread t, Throwable e)Handles uncaught exceptions.
-
Constructor Details
-
ThreadGroup
Constructs a newThreadGroupwith the given name. The newThreadGroupwill be child of theThreadGroupto which the calling thread belongs.- Parameters:
name- the name- See Also:
Thread.currentThread()
-
ThreadGroup
Constructs a newThreadGroupwith the given name, as a child of the givenThreadGroup.- Parameters:
parent- the parentname- the name- Throws:
NullPointerException- ifparent == nullIllegalThreadStateException- ifparenthas been destroyed already
-
-
Method Details
-
activeCount
public int activeCount()Returns the number of runningThreads which are children of this thread group, directly or indirectly.- Returns:
- the number of children
-
activeGroupCount
public int activeGroupCount()Returns the number ofThreadGroups which are children of this group, directly or indirectly.- Returns:
- the number of children
-
allowThreadSuspension
Deprecated.Required deprecated methodsuspend().Does nothing. The definition of this method depends on the deprecated methodsuspend(). The exact behavior of this call was never specified.- Parameters:
b- Used to control low memory implicit suspension- Returns:
true(always)
-
checkAccess
public final void checkAccess()Does nothing. -
destroy
public final void destroy()Destroys this thread group and recursively all its subgroups. It is only legal to destroy aThreadGroupthat has no threads in it. Any daemonThreadGroupis destroyed automatically when it becomes empty (no threads or thread groups in it).- Throws:
IllegalThreadStateException- if this thread group or any of its subgroups has been destroyed already or if it still contains threads.
-
enumerate
Iterates over all active threads in this group (and its sub-groups) and stores the threads in the given array. Returns when the array is full or no more threads remain, whichever happens first.Note that this method will silently ignore any threads that don't fit in the supplied array.
- Parameters:
threads- the array into which theThreads will be copied- Returns:
- the number of
Threads that were copied
-
enumerate
Iterates over all active threads in this group (and, optionally, its sub-groups) and stores the threads in the given array. Returns when the array is full or no more threads remain, whichever happens first.Note that this method will silently ignore any threads that don't fit in the supplied array.
- Parameters:
threads- the array into which theThreads will be copiedrecurse- indicates whetherThreads in subgroups should be recursively copied as well- Returns:
- the number of
Threads that were copied
-
enumerate
Iterates over all thread groups in this group (and its sub-groups) and and stores the groups in the given array. Returns when the array is full or no more groups remain, whichever happens first.Note that this method will silently ignore any thread groups that don't fit in the supplied array.
- Parameters:
groups- the array into which theThreadGroups will be copied- Returns:
- the number of
ThreadGroups that were copied
-
enumerate
Iterates over all thread groups in this group (and, optionally, its sub-groups) and stores the groups in the given array. Returns when the array is full or no more groups remain, whichever happens first.Note that this method will silently ignore any thread groups that don't fit in the supplied array.
- Parameters:
groups- the array into which theThreadGroups will be copiedrecurse- indicates whetherThreadGroups in subgroups should be recursively copied as well or not- Returns:
- the number of
ThreadGroups that were copied
-
getMaxPriority
public final int getMaxPriority()Returns the maximum allowed priority for aThreadin this thread group.- Returns:
- the maximum priority
- See Also:
setMaxPriority(int)
-
getName
Returns the name of this thread group.- Returns:
- the group's name
-
getParent
Returns this thread group's parentThreadGroup. It can be null if this is the the root ThreadGroup.- Returns:
- the parent
-
interrupt
public final void interrupt()Interrupts everyThreadin this group and recursively in all its subgroups.- See Also:
Thread.interrupt()
-
isDaemon
public final boolean isDaemon()Checks whether this thread group is a daemonThreadGroup.- Returns:
- true if this thread group is a daemon
ThreadGroup - See Also:
setDaemon(boolean),destroy()
-
isDestroyed
public boolean isDestroyed()Checks whether this thread group has already been destroyed.- Returns:
- true if this thread group has already been destroyed
- See Also:
destroy()
-
list
public void list()Outputs toSystem.outa text representation of the hierarchy ofThreads andThreadGroups in this thread group (and recursively). Proper indentation is used to show the nesting of groups inside groups and threads inside groups. -
parentOf
Checks whether this thread group is a direct or indirect parent group of a givenThreadGroup.- Parameters:
g- the potential childThreadGroup- Returns:
- true if this thread group is parent of
g
-
resume
Deprecated.Requires deprecated methodThread.resume().Resumes every thread in this group and recursively in all its subgroups.- See Also:
Thread.resume(),suspend()
-
setDaemon
public final void setDaemon(boolean isDaemon)Sets whether this is a daemonThreadGroupor not. Daemon thread groups are automatically destroyed when they become empty.- Parameters:
isDaemon- the new value- See Also:
isDaemon,destroy()
-
setMaxPriority
public final void setMaxPriority(int newMax)Configures the maximum allowed priority for aThreadin this group and recursively in all its subgroups.A caller can never increase the maximum priority of a thread group. Such an attempt will not result in an exception, it will simply leave the thread group with its current maximum priority.
- Parameters:
newMax- the new maximum priority to be set- Throws:
IllegalArgumentException- if the new priority is greater than Thread.MAX_PRIORITY or less than Thread.MIN_PRIORITY- See Also:
getMaxPriority()
-
stop
Deprecated.Requires deprecated methodThread.stop().Stops every thread in this group and recursively in all its subgroups.- See Also:
Thread.stop(),Thread.stop(Throwable),ThreadDeath
-
suspend
Deprecated.Requires deprecated methodThread.suspend().Suspends every thread in this group and recursively in all its subgroups.- See Also:
Thread.suspend(),resume()
-
toString
Description copied from class:ObjectReturns a string containing a concise, human-readable description of this object. Subclasses are encouraged to override this method and provide an implementation that takes into account the object's type and data. The default implementation is equivalent to the following expression:getClass().getName() + '@' + Integer.toHexString(hashCode())
See Writing a useful
toStringmethod if you intend implementing your owntoStringmethod. -
uncaughtException
Handles uncaught exceptions. Any uncaught exception in anyThreadis forwarded to the thread'sThreadGroupby invoking this method.New code should use
Thread.setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)instead of thread groups.- Specified by:
uncaughtExceptionin interfaceThread.UncaughtExceptionHandler- Parameters:
t- the Thread that terminated with an uncaught exceptione- the uncaught exception itself
-
suspend().