- java.lang.Object
-
- java.lang.ThreadGroup
-
- All Implemented Interfaces:
Thread.UncaughtExceptionHandler
public class ThreadGroup extends Object implements Thread.UncaughtExceptionHandler
ThreadGroups are containers of Threads and ThreadGroups, therefore providing a tree-like structure to organize Threads. The root ThreadGroup name is "system" and it has no parent ThreadGroup. All other ThreadGroups have exactly one parent ThreadGroup. All Threads belong to exactly one ThreadGroup.- See Also:
Thread
,SecurityManager
-
-
Constructor Summary
Constructors Constructor Description ThreadGroup(String name)
Constructs a new ThreadGroup with the name provided.ThreadGroup(ThreadGroup parent, String name)
Constructs a new ThreadGroup with the name provided, as child of the ThreadGroupparent
-
Method Summary
All Methods Instance Methods Concrete Methods Deprecated Methods Modifier and Type Method Description int
activeCount()
Returns the number of Threads which are children of the receiver, directly or indirectly.int
activeGroupCount()
Returns the number of ThreadGroups which are children of the receiver, directly or indirectly.boolean
allowThreadSuspension(boolean b)
Deprecated.Required deprecated method suspend().void
checkAccess()
If there is a SecurityManager installed, callcheckAccess
in it passing the receiver as parameter, otherwise do nothing.void
destroy()
Destroys the receiver and recursively all its subgroups.int
enumerate(Thread[] threads)
Copies an array with all Threads which are children of the receiver (directly or indirectly) into the arraythreads
passed as parameters.int
enumerate(Thread[] threads, boolean recurse)
Copies an array with all Threads which are children of the receiver into the arraythreads
passed as parameter.int
enumerate(ThreadGroup[] groups)
Copies an array with all ThreadGroups which are children of the receiver (directly or indirectly) into the arraygroups
passed as parameters.int
enumerate(ThreadGroup[] groups, boolean recurse)
Copies an array with all ThreadGroups which are children of the receiver into the arraygroups
passed as parameter.int
getMaxPriority()
Answers the maximum allowed priority for a Thread in the receiver.String
getName()
Answers the name of the receiver.ThreadGroup
getParent()
Answers the receiver's parent ThreadGroup.void
interrupt()
Interrupts every Thread in the receiver and recursively in all its subgroups.boolean
isDaemon()
Answers true if the receiver is a daemon ThreadGroup, false otherwise.boolean
isDestroyed()
Answers true if the receiver has been destroyed already, false otherwise.void
list()
Outputs toSystem.out
a text representation of the hierarchy of Threads and ThreadGroups in the receiver (and recursively).boolean
parentOf(ThreadGroup g)
Answers true if the receiver is a direct or indirect parent group of ThreadGroupg
, false otherwise.void
resume()
Deprecated.Requires deprecated method Thread.resume().void
setDaemon(boolean isDaemon)
Configures the receiver to be a daemon ThreadGroup or not.void
setMaxPriority(int newMax)
Configures the maximum allowed priority for a Thread in the receiver and recursively in all its subgroups.void
stop()
Deprecated.Requires deprecated method Thread.stop().void
suspend()
Deprecated.Requires deprecated method Thread.suspend().String
toString()
Answers a string containing a concise, human-readable description of the receiver.void
uncaughtException(Thread t, Throwable e)
Any uncaught exception in any Thread has to be forwarded (by the VM) to the Thread's ThreadGroup by sending this message (uncaughtException).
-
-
-
Constructor Detail
-
ThreadGroup
public ThreadGroup(String name)
Constructs a new ThreadGroup with the name provided. The new ThreadGroup will be child of the ThreadGroup to which theThread.currentThread()
belongs.- Parameters:
name
- Name for the ThreadGroup being created- Throws:
SecurityException
- ifcheckAccess()
for the parent group fails with a SecurityException- See Also:
Thread.currentThread()
-
ThreadGroup
public ThreadGroup(ThreadGroup parent, String name)
Constructs a new ThreadGroup with the name provided, as child of the ThreadGroupparent
- Parameters:
parent
- Parent ThreadGroupname
- Name for the ThreadGroup being created- Throws:
NullPointerException
- ifparent
isnull
SecurityException
- ifcheckAccess()
for the parent group fails with a SecurityExceptionIllegalThreadStateException
- ifparent
has been destroyed already
-
-
Method Detail
-
activeCount
public int activeCount()
Returns the number of Threads which are children of the receiver, directly or indirectly.- Returns:
- Number of children Threads
-
activeGroupCount
public int activeGroupCount()
Returns the number of ThreadGroups which are children of the receiver, directly or indirectly.- Returns:
- Number of children ThreadGroups
-
allowThreadSuspension
@Deprecated(forRemoval=false, since="1.2") public boolean allowThreadSuspension(boolean b)
Deprecated.Required deprecated method suspend().The definition of this method depends on the deprecated methodsuspend()
. The behavior of this call was never specified.- Parameters:
b
- Used to control low memory implicit suspension- Returns:
- always returns true
-
checkAccess
public final void checkAccess()
If there is a SecurityManager installed, callcheckAccess
in it passing the receiver as parameter, otherwise do nothing.
-
destroy
public final void destroy()
Destroys the receiver and recursively all its subgroups. It is only legal to destroy a ThreadGroup that has no Threads. Any daemon ThreadGroup is destroyed automatically when it becomes empty (no Threads and no ThreadGroups in it).- Throws:
IllegalThreadStateException
- if the receiver or any of its subgroups has been destroyed alreadySecurityException
- ifthis.checkAccess()
fails with a SecurityException
-
enumerate
public int enumerate(Thread[] threads)
Copies an array with all Threads which are children of the receiver (directly or indirectly) into the arraythreads
passed as parameters. If the array passed as parameter is too small no exception is thrown - the extra elements are simply not copied.- Parameters:
threads
- Thread array into which the Threads will be copied- Returns:
- How many Threads were copied over
-
enumerate
public int enumerate(Thread[] threads, boolean recurse)
Copies an array with all Threads which are children of the receiver into the arraythreads
passed as parameter. Children Threads of subgroups are recursively copied as well if parameterrecurse
istrue
. If the array passed as parameter is too small no exception is thrown - the extra elements are simply not copied.- Parameters:
threads
- array into which the Threads will be copiedrecurse
- Indicates whether Threads in subgroups should be recursively copied as well or not- Returns:
- How many Threads were copied over
-
enumerate
public int enumerate(ThreadGroup[] groups)
Copies an array with all ThreadGroups which are children of the receiver (directly or indirectly) into the arraygroups
passed as parameters. If the array passed as parameter is too small no exception is thrown - the extra elements are simply not copied.- Parameters:
groups
- array into which the ThreadGroups will be copied- Returns:
- How many ThreadGroups were copied over
-
enumerate
public int enumerate(ThreadGroup[] groups, boolean recurse)
Copies an array with all ThreadGroups which are children of the receiver into the arraygroups
passed as parameter. Children ThreadGroups of subgroups are recursively copied as well if parameterrecurse
istrue
. If the array passed as parameter is too small no exception is thrown - the extra elements are simply not copied.- Parameters:
groups
- array into which the ThreadGroups will be copiedrecurse
- Indicates whether ThreadGroups in subgroups should be recursively copied as well or not- Returns:
- How many ThreadGroups were copied over
-
getMaxPriority
public final int getMaxPriority()
Answers the maximum allowed priority for a Thread in the receiver.- Returns:
- the maximum priority (an
int
) - See Also:
setMaxPriority(int)
-
getName
public final String getName()
Answers the name of the receiver.- Returns:
- the receiver's name (a java.lang.String)
-
getParent
public final ThreadGroup getParent()
Answers the receiver's parent ThreadGroup. It can be null if the receiver is the root ThreadGroup.- Returns:
- the parent ThreadGroup
-
interrupt
public final void interrupt()
Interrupts every Thread in the receiver and recursively in all its subgroups.- Throws:
SecurityException
- ifthis.checkAccess()
fails with a SecurityException- See Also:
Thread.interrupt()
-
isDaemon
public final boolean isDaemon()
Answers true if the receiver is a daemon ThreadGroup, false otherwise.- Returns:
- if the receiver is a daemon ThreadGroup
- See Also:
setDaemon(boolean)
,destroy()
-
isDestroyed
public boolean isDestroyed()
Answers true if the receiver has been destroyed already, false otherwise.- Returns:
- if the receiver has been destroyed already
- See Also:
destroy()
-
list
public void list()
Outputs toSystem.out
a text representation of the hierarchy of Threads and ThreadGroups in the receiver (and recursively). Proper indentation is done to suggest the nesting of groups inside groups and threads inside groups.
-
parentOf
public final boolean parentOf(ThreadGroup g)
Answers true if the receiver is a direct or indirect parent group of ThreadGroupg
, false otherwise.- Parameters:
g
- ThreadGroup to test- Returns:
- if the receiver is parent of the ThreadGroup passed as parameter
-
resume
@Deprecated(forRemoval=false, since="1.2") public final void resume()
Deprecated.Requires deprecated method Thread.resume().Resumes every Thread in the receiver and recursively in all its subgroups.- Throws:
SecurityException
- ifthis.checkAccess()
fails with a SecurityException- See Also:
Thread.resume()
,suspend()
-
setDaemon
public final void setDaemon(boolean isDaemon)
Configures the receiver to be a daemon ThreadGroup or not. Daemon ThreadGroups are automatically destroyed when they become empty.- Parameters:
isDaemon
- new value defining if receiver should be daemon or not- Throws:
SecurityException
- ifcheckAccess()
for the parent group fails with a SecurityException- See Also:
isDaemon
,destroy()
-
setMaxPriority
public final void setMaxPriority(int newMax)
Configures the maximum allowed priority for a Thread in the receiver and recursively in all its subgroups. One can never change the maximum priority of a ThreadGroup to be higher than it was. Such an attempt will not result in an exception, it will simply leave the ThreadGroup with its current maximum priority.- Parameters:
newMax
- the new maximum priority to be set- Throws:
SecurityException
- ifcheckAccess()
fails with a SecurityExceptionIllegalArgumentException
- if the new priority is greater than Thread.MAX_PRIORITY or less than Thread.MIN_PRIORITY- See Also:
getMaxPriority()
-
stop
@Deprecated(forRemoval=false, since="1.2") public final void stop()
Deprecated.Requires deprecated method Thread.stop().Stops every Thread in the receiver and recursively in all its subgroups.- Throws:
SecurityException
- ifthis.checkAccess()
fails with a SecurityException- See Also:
Thread.stop()
,ThreadDeath
-
suspend
@Deprecated(forRemoval=false, since="1.2") public final void suspend()
Deprecated.Requires deprecated method Thread.suspend().Suspends every Thread in the receiver and recursively in all its subgroups.- Throws:
SecurityException
- ifthis.checkAccess()
fails with a SecurityException- See Also:
Thread.suspend()
,resume()
-
toString
public String toString()
Answers a string containing a concise, human-readable description of the receiver.
-
uncaughtException
public void uncaughtException(Thread t, Throwable e)
Any uncaught exception in any Thread has to be forwarded (by the VM) to the Thread's ThreadGroup by sending this message (uncaughtException). This allows users to define custom ThreadGroup classes and custom behavior for when a Thread has an uncaughtException or when it does (ThreadDeath).- Specified by:
uncaughtException
in interfaceThread.UncaughtExceptionHandler
- Parameters:
t
- Thread with an uncaught exceptione
- The uncaught exception itself- See Also:
Thread.stop()
,ThreadDeath
-
-