ThreadGroup
Name
ThreadGroupSynopsis
- Class Name:
java.lang.ThreadGroup
- Superclass:
java.lang.Object
- Immediate Subclasses:
- None
- Interfaces Implemented:
- None
- Availability:
- JDK 1.0 or later
Description
The ThreadGroup
class implements a grouping scheme for threads. A ThreadGroup
object can own Thread
objects and other ThreadGroup
objects. The ThreadGroup
class provides methods that allow a ThreadGroup
object to control its Thread
and ThreadGroup
objects as a group. For example, suspend()
and resume()
methods of a ThreadGroup
object call the suspend()
and resume()
methods of each of the Thread
and ThreadGroup
objects that belong to the particular ThreadGroup
.
When a Java program starts, a ThreadGroup
object is created to own the first Thread
. Any additional ThreadGroup
objects are explicitly created by the program.
Class Summary
public class java.lang.ThreadGroup extends java.lang.Object { // Constructors public ThreadGroup(String name); public ThreadGroup(ThreadGroup parent, String name; // Instance Methods public int activeCount(); public int activeGroupCount(); public boolean allowThreadSuspension(boolean b); // New in 1.1 public final void checkAccess(); public final void destroy(); public int enumerate(Thread list[]); public int enumerate(Thread list[], boolean recurse); public int enumerate(ThreadGroup list[]); public int enumerate(ThreadGroup list[], boolean recurse); public final int getMaxPriority(); public final String getName(); public final ThreadGroup getParent(); public final boolean isDaemon(); public synchronized boolean isDestroyed(); // New in 1.1 public void list(); public final boolean parentOf(ThreadGroup g); public final void resume(); public final void setDaemon(boolean daemon); public final void setMaxPriority(int pri); public final void stop(); public final void suspend(); public String toString(); public void uncaughtException(Thread t, Throwable e); }
Constructors
ThreadGroup
public ThreadGroup(String name)
- Parameters
-
name
- The name of this
ThreadGroup
object.
- Throws
-
SecurityException
- If the
checkAccess()
method of theSecurityManager
throws aSecurityException
.
- Description
- Creates a
ThreadGroup
object that has the specified name and the same parentThreadGroup
as the current thread.
public ThreadGroup(ThreadGroup parent, String name)
- Parameters
-
parent
- The
ThreadGroup
object that thisThreadGroup
object is to be added to. name
- The name of this
ThreadGroup
object.
- Throws
-
SecurityException
- If the
checkAccess()
method of theSecurityManager
throws aSecurityException
.
- Description
- Creates a
ThreadGroup
object with the specified name and parentThreadGroup
object.
Instance Methods
activeCount
public int activeCount()
- Returns
- An approximation of the current number of threads in this
ThreadGroup
object and any childThreadGroup
objects. - Description
- This method returns an approximation of the number of threads that belong to this
ThreadGroup
object and any childThreadGroup
objects. The count is approximate because a thread can die after it is counted, but before the complete count is returned. Also, after a childThreadGroup
is counted but before the total count is returned, additionalThread
andThreadGroup
objects can be added to a childThreadGroup
.
activeGroupCount
public int activeGroupCount()
- Returns
- An approximation of the current number of child
ThreadGroup
objects in thisThreadGroup
object. - Description
- This method returns an approximation of the number of child
ThreadGroup
objects that belong to thisThreadGroup
object. The count is approximate because after a childThreadGroup
is counted but before the total count is returned, additionalThreadGroup
objects can be added to a childThreadGroup
.
allowThreadSuspension
public boolean allowThreadSuspension(boolean b)
- Availability
- New as of JDK 1.1
- Parameters
-
b
- A
boolean
value that specifies whether or not the run-time system is allowed to suspend threads due to low memory.
- Returns
- The
boolean
valuetrue
. - Description
- This method specifies whether or not the Java virtual machine is allowed to suspend threads due to low memory.
checkAccess
public final void checkAccess()
- Throws
-
SecurityException
- If the
checkAccess()
method of theSecurityManager
throws aSecurityException
.
- Description
- This method determines if the currently running thread has permission to modify this
ThreadGroup
object.
destroy
public final void destroy()
- Throws
-
IllegalThreadStateException
- If this
ThreadGroup
object is not empty, or if it has already been destroyed. SecurityException
- If the
checkAccess()
method of theSecurityManager
throws aSecurityException
.
- Description
- This method destroys this
ThreadGroup
object and any childThreadGroup
objects. TheThreadGroup
must not contain anyThread
objects. This method also removes theThreadGroup
object from its parentThreadGroup
object.
enumerate
public int enumerate(Thread list[])
- Parameters
- Returns
- The number of
Thread
objects stored in the array. - Description
- This method stores a reference in the array for each of the
Thread
objects that belongs to thisThreadGroup
or any of its childThreadGroup
objects.If the array is not big enough to contain references to all the
Thread
objects, only as many references as will fit are put into the array. No indication is given that someThread
objects were left out, so it is a good idea to callactiveCount()
before calling this method, to get an idea of how large to make the array.
public int enumerate(Thread list[], boolean recurse)
- Parameters
-
list
- A reference to an array of
Thread
objects. recurse
- A
boolean
value that specifies whether or not to includeThread
objects that belong to childThreadGroup
objects of thisThreadGroup
object.
- Returns
- The number of
Thread
objects stored in the array. - Description
- This method stores a reference in the array for each of the
Thread
objects that belongs to thisThreadGroup
object. Ifrecurse
istrue
, the method also stores a reference for each of theThread
objects that belongs to a childThreadGroup
object of thisThreadGroup
.If the array is not big enough to contain references to all the
Thread
objects, only as many references as will fit are put into the array. No indication is given that someThread
objects were left out, so it is a good idea to callactiveCount()
before calling this method, to get an idea of how large to make the array.
public int enumerate(ThreadGroup list[])
- Parameters
-
list
- A reference to an array of
ThreadGroup
objects.
- Returns
- The number of
ThreadGroup
objects stored in the array. - Description
- This method stores a reference in the array for each
ThreadGroup
object that belongs to thisThreadGroup
or any of its childThreadGroup
objects.If the array is not big enough to contain references to all the
ThreadGroup
objects, only as many references as will fit are put into the array. No indication is given that someThreadGroup
objects were left out, so it is a good idea to callactiveGroupCount()
before calling this method, to get an idea of how large to make the array.
public int enumerate(Thread list[], boolean recurse)
- Parameters
-
list
- A reference to an array of
ThreadGroup
objects. recurse
- A
boolean
value that specifies whether or not to includeThreadGroup
objects that belong to childThreadGroup
objects of thisThreadGroup
object.
- Returns
- The number of
ThreadGroup
objects stored in the array. - Description
- This method stores a reference in the array for each of the
ThreadGroup
objects that belongs to thisThreadGroup
object. Ifrecurse
istrue
, the method also stores a reference for each of theThreadGroup
objects that belongs to a childThreadGroup
object of thisThreadGroup
.If the array is not big enough to contain references to all the
ThreadGroup
objects, only as many references as will fit are put into the array. No indication is given that someThreadGroup
objects were left out, so it is a good idea to callactiveGroupCount()
before calling this method, to get an idea of how large to make the array.
getMaxPriority
public final int getMaxPriority()
- Returns
- The maximum priority that can be assigned to
Thread
objects that belong to thisThreadGroup
object. - Description
- This method returns the maximum priority that can be assigned to
Thread
objects that belong to thisThreadGroup
object.It is possible for a
ThreadGroup
to containThread
objects that have higher priorities than this maximum, if they were given that higher priority before the maximum was set to a lower value.
getName
public final String getName()
- Returns
- The name of this
ThreadGroup
object. - Description
- This method returns the name of this
ThreadGroup
object.
getParent
public final ThreadGroup getParent()
- Returns
- The parent
ThreadGroup
object of thisThreadGroup
, ornull
if thisThreadGroup
is the root of the thread group hierarchy. - Description
- This method returns the parent
ThreadGroup
object of thisThreadGroup
object. If thisThreadGroup
is at the root of the thread group hierarchy and has no parent, the method returnsnull
.
isDaemon
public final boolean isDaemon()
- Returns
true
if thisThreadGroup
is a daemon thread group; otherwisefalse
.- Description
- This method determines whether or not this
ThreadGroup
is a daemon thread group, based on the value of daemon attribute of thisThreadGroup
object. A daemon thread group is destroyed when the lastThread
in it is stopped, or the lastThreadGroup
in it is destroyed.
isDestroyed
public synchronized boolean isDestroyed()
- Availability
- New as of JDK 1.1
- Returns
true
if thisThreadGroup
has been destroyed; otherwisefalse
.- Description
- This method determines whether or not this
ThreadGroup
has been destroyed.
list
public void list()
- Description
- This method outputs a listing of the contents of this
ThreadGroup
object toSystem.out
.
parentOf
public final boolean parentOf(ThreadGroup g)
- Parameters
-
g
- A
ThreadGroup
object.
- Returns
true
if thisThreadGroup
object is the sameThreadGroup
, or a direct or indirect parent of the specifiedThreadGroup
; otherwisefalse
.- Description
- This method determines if this
ThreadGroup
object is the same as the specifiedThreadGroup
or one of its ancestors in the thread-group hierarchy.
resume
public final void resume()
- Throws
-
SecurityException
- If the
checkAccess()
method of theSecurityManager
throws aSecurityException
.
- Description
- This method resumes each
Thread
object that directly or indirectly belongs to thisThreadGroup
object by calling itsresume()
method.
setDaemon
public final void setDaemon(boolean daemon)
- Parameters
-
daemon
- The new value for this
ThreadGroup
object's daemon attribute.
- Throws
-
SecurityException
- If the
checkAccess()
method of theSecurityManager
throws aSecurityException
.
- Description
- This method sets the daemon attribute of this
ThreadGroup
object to the given value. A daemon thread group is destroyed when the lastThread
in it is stopped, or the lastThreadGroup
in it is destroyed.
setMaxPriority
public final void setMaxPriority(int pri)
- Parameters
-
pri
- The new maximum priority for
Thread
objects that belong to thisThreadGroup
object.
- Description
- This method sets the maximum priority that can be assigned to
Thread
objects that belong to thisThreadGroup
object.It is possible for a
ThreadGroup
to containThread
objects that have higher priorities than this maximum, if they were given that higher priority before the maximum was set to a lower value.
stop
public final void stop()
- Throws
-
SecurityException
- If the
checkAccess()
method of theSecurityManager
throws aSecurityException
.
- Description
- This method stops each
Thread
object that directly or indirectly belongs to thisThreadGroup
object by calling itsstop()
method.
suspend
public final void suspend()
- Throws
-
SecurityException
- If the
checkAccess()
method of theSecurityManager
throws aSecurityException
.
- Description
- This method suspends each
Thread
object that directly or indirectly belongs to thisThreadGroup
object by calling itssuspend()
method.
toString
public String toString()
- Returns
- A string representation of this
ThreadGroup
object. - Overrides
Object.toString()
- Description
- This method returns a string representation of this
ThreadGroup
object.
uncaughtException
public void uncaughtException(Thread t, Throwable e)
- Parameters
-
t
- A reference to a
Thread
that just died because of an uncaught exception. e
- The uncaught exception.
- Description
- This method is called when a
Thread
object that belongs to thisThreadGroup
object dies because of an uncaught exception. If thisThreadGroup
object has a parentThreadGroup
object, this method just calls the parent'suncaughtException()
method. Otherwise, this method must determine whether the uncaught exception is an instance ofThreadDeath
. If it is, nothing is done. If it is not, the method calls theprintStackTrace()
method of the exception object.If this method is overridden, the overriding method should end with a call to
super.uncaughtException()
.
Inherited Methods
Method | Inherited From | Method | Inherited From |
---|---|---|---|
clone()
| Object
| equals(Object)
| Object
|
finalize()
| Object
| getClass()
| Object
|
hashCode()
| Object
| notify()
| Object
|
notifyAll()
| Object
| wait()
| Object
|
wait(long)
| Object
| wait(long, int)
| Object |
See Also
Exceptions; Object; Runnable; SecurityManager; Thread; Threads 8; Throwable