java.lang.Object
java.lang.ThreadGroup
- All Implemented Interfaces:
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:
-
Constructor Summary
ConstructorsConstructorDescriptionThreadGroup
(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
Modifier and TypeMethodDescriptionint
Returns the number of Threads which are children of the receiver, directly or indirectly.int
Returns the number of ThreadGroups which are children of the receiver, directly or indirectly.boolean
allowThreadSuspension
(boolean b) Deprecated, for removal: This API element is subject to removal in a future version.Required deprecated method suspend().final void
Deprecated, for removal: This API element is subject to removal in a future version.final void
destroy()
Deprecated, for removal: This API element is subject to removal in a future version.int
Copies an array with all Threads which are children of the receiver (directly or indirectly) into the arraythreads
passed as parameters.int
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.final int
Answers the maximum allowed priority for a Thread in the receiver.final String
getName()
Answers the name of the receiver.final ThreadGroup
Answers the receiver's parent ThreadGroup.final void
Interrupts every Thread in the receiver and recursively in all its subgroups.final boolean
isDaemon()
Deprecated, for removal: This API element is subject to removal in a future version.boolean
Deprecated, for removal: This API element is subject to removal in a future version.void
list()
Outputs toSystem.out
a text representation of the hierarchy of Threads and ThreadGroups in the receiver (and recursively).final boolean
Answers true if the receiver is a direct or indirect parent group of ThreadGroupg
, false otherwise.final void
resume()
Deprecated, for removal: This API element is subject to removal in a future version.Requires deprecated method Thread.resume().final void
setDaemon
(boolean isDaemon) Deprecated, for removal: This API element is subject to removal in a future version.final void
setMaxPriority
(int newMax) Configures the maximum allowed priority for a Thread in the receiver and recursively in all its subgroups.final void
stop()
Deprecated, for removal: This API element is subject to removal in a future version.Requires deprecated method Thread.stop().final void
suspend()
Deprecated, for removal: This API element is subject to removal in a future version.Requires deprecated method Thread.suspend().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 Details
-
ThreadGroup
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:
-
ThreadGroup
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 Details
-
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, for removal: This API element is subject to removal in a future version.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
Deprecated, for removal: This API element is subject to removal in a future version.If there is a SecurityManager installed, callcheckAccess
in it passing the receiver as parameter, otherwise do nothing. -
destroy
Deprecated, for removal: This API element is subject to removal in a future version.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
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
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
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
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:
-
getName
Answers the name of the receiver.- Returns:
- the receiver's name (a java.lang.String)
-
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:
-
isDaemon
Deprecated, for removal: This API element is subject to removal in a future version.Answers true if the receiver is a daemon ThreadGroup, false otherwise.- Returns:
- if the receiver is a daemon ThreadGroup
- See Also:
-
isDestroyed
Deprecated, for removal: This API element is subject to removal in a future version.Answers true if the receiver has been destroyed already, false otherwise.- Returns:
- if the receiver has been destroyed already
- See Also:
-
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
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, for removal: This API element is subject to removal in a future version.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:
-
setDaemon
Deprecated, for removal: This API element is subject to removal in a future version.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:
-
stop
Deprecated, for removal: This API element is subject to removal in a future version.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:
-
suspend
Deprecated, for removal: This API element is subject to removal in a future version.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:
-
toString
Answers a string containing a concise, human-readable description of the receiver. -
uncaughtException
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:
-