Index (Frames) | Index (No Frames) | Package | Package Tree | Tree
java.lang

Class ThreadGroup

java.lang.Object
|
+--java.lang.ThreadGroup


public class ThreadGroup

extends Object

ThreadGroup allows you to group Threads together. There is a hierarchy of ThreadGroups, and only the initial ThreadGroup has no parent. A Thread may access information about its own ThreadGroup, but not its parents or others outside the tree.

Since:Authors:See Also:

Constructor Summary

ThreadGroup(java.lang.String name)

Create a new ThreadGroup using the given name and the current thread's ThreadGroup as a parent.
ThreadGroup(java.lang.ThreadGroup parent, java.lang.String name)

Create a new ThreadGroup using the given name and parent group.

Method Summary

intactiveCount()

Return an estimate of the total number of active threads in this ThreadGroup and all its descendants.
intactiveGroupCount()

Get the number of active groups in this ThreadGroup.
booleanallowThreadSuspension(boolean allow)

Originally intended to tell the VM whether it may suspend Threads in low memory situations, this method was never implemented by Sun, and is hence a no-op.
voidcheckAccess()

Find out if the current Thread can modify this ThreadGroup.
synchronized voiddestroy()

Destroy this ThreadGroup.
intenumerate(java.lang.Thread array)

Copy all of the active Threads from this ThreadGroup and its descendants into the specified array.
intenumerate(java.lang.Thread array, boolean recurse)

Copy all of the active Threads from this ThreadGroup and, if desired, from its descendants, into the specified array.
intenumerate(java.lang.ThreadGroup array)

Copy all active ThreadGroups that are descendants of this ThreadGroup into the specified array.
intenumerate(java.lang.ThreadGroup array, boolean recurse)

Copy all active ThreadGroups that are children of this ThreadGroup into the specified array, and if desired, also all descendents.
intgetMaxPriority()

Get the maximum priority of Threads in this ThreadGroup.
java.lang.StringgetName()

Get the name of this ThreadGroup.
java.lang.ThreadGroupgetParent()

Get the parent of this ThreadGroup.
synchronized voidinterrupt()

Interrupt all Threads in this ThreadGroup and its sub-groups.
booleanisDaemon()

Tell whether this ThreadGroup is a daemon group.
synchronized booleanisDestroyed()

Tell whether this ThreadGroup has been destroyed or not.
voidlist()

Print out information about this ThreadGroup to System.out.
booleanparentOf(java.lang.ThreadGroup tg)

Check whether this ThreadGroup is an ancestor of the specified ThreadGroup, or if they are the same.
synchronized voidresume()

Resume all suspended Threads in this ThreadGroup and its descendants.
voidsetDaemon(boolean daemon)

Set whether this ThreadGroup is a daemon group.
synchronized voidsetMaxPriority(int maxpri)

Set the maximum priority for Threads in this ThreadGroup.
synchronized voidstop()

Stop all Threads in this ThreadGroup and its descendants.
synchronized voidsuspend()

Suspend all Threads in this ThreadGroup and its descendants.
java.lang.StringtoString()

Return a human-readable String representing this ThreadGroup.
voiduncaughtException(java.lang.Thread thread, java.lang.Throwable t)

When a Thread in this ThreadGroup does not catch an exception, the virtual machine calls this method.

Constructor Details

ThreadGroup

public ThreadGroup(java.lang.String name)

Create a new ThreadGroup using the given name and the current thread's ThreadGroup as a parent. There may be a security check, checkAccess.

Parameters:

Throws:

See Also:


ThreadGroup

public ThreadGroup(java.lang.ThreadGroup parent, java.lang.String name)

Create a new ThreadGroup using the given name and parent group. The new group inherits the maximum priority and daemon status of its parent group. There may be a security check, checkAccess.

Parameters:

Throws:

See Also:


Method Details

activeCount

public int activeCount()

Return an estimate of the total number of active threads in this ThreadGroup and all its descendants. This cannot return an exact number, since the status of threads may change after they were counted; but it should be pretty close. Based on a JDC bug, 4089701, we take active to mean isAlive().

Returns:


activeGroupCount

public int activeGroupCount()

Get the number of active groups in this ThreadGroup. This group itself is not included in the count. A sub-group is active if it has not been destroyed. This cannot return an exact number, since the status of threads may change after they were counted; but it should be pretty close.

Returns:


allowThreadSuspension

public boolean allowThreadSuspension(boolean allow)

Originally intended to tell the VM whether it may suspend Threads in low memory situations, this method was never implemented by Sun, and is hence a no-op.

Since:Parameters:

Returns:


checkAccess

public final void checkAccess()

Find out if the current Thread can modify this ThreadGroup. This passes the check on to SecurityManager.checkAccess(this).

Throws:

See Also:


destroy

public final synchronized void destroy()

Destroy this ThreadGroup. The group must be empty, meaning that all threads and sub-groups have completed execution. Daemon groups are destroyed automatically. There may be a security check, checkAccess.

Throws:

See Also:


enumerate

public int enumerate(java.lang.Thread array)

Copy all of the active Threads from this ThreadGroup and its descendants into the specified array. If the array is not big enough to hold all the Threads, extra Threads will simply not be copied. There may be a security check, checkAccess.

Parameters:

Returns:

Throws:

See Also:


enumerate

public int enumerate(java.lang.Thread array, boolean recurse)

Copy all of the active Threads from this ThreadGroup and, if desired, from its descendants, into the specified array. If the array is not big enough to hold all the Threads, extra Threads will simply not be copied. There may be a security check, checkAccess.

Parameters:

Returns:

Throws:

See Also:


enumerate

public int enumerate(java.lang.ThreadGroup array)

Copy all active ThreadGroups that are descendants of this ThreadGroup into the specified array. If the array is not large enough to hold all active ThreadGroups, extra ThreadGroups simply will not be copied. There may be a security check, checkAccess.

Parameters:

Returns:

Throws:

See Also:


enumerate

public int enumerate(java.lang.ThreadGroup array, boolean recurse)

Copy all active ThreadGroups that are children of this ThreadGroup into the specified array, and if desired, also all descendents. If the array is not large enough to hold all active ThreadGroups, extra ThreadGroups simply will not be copied. There may be a security check, checkAccess.

Parameters:

Returns:

Throws:

See Also:


getMaxPriority

public final int getMaxPriority()

Get the maximum priority of Threads in this ThreadGroup. Threads created after this call in this group may not exceed this priority.

Returns:


getName

public final String getName()

Get the name of this ThreadGroup.

Returns:


getParent

public final ThreadGroup getParent()

Get the parent of this ThreadGroup. If the parent is not null, there may be a security check, checkAccess.

Returns:

Throws:


interrupt

public final synchronized void interrupt()

Interrupt all Threads in this ThreadGroup and its sub-groups. There may be a security check, checkAccess.

Since:Throws:

See Also:


isDaemon

public final boolean isDaemon()

Tell whether this ThreadGroup is a daemon group. A daemon group will be automatically destroyed when its last thread is stopped and its last thread group is destroyed.

Returns:


isDestroyed

public synchronized boolean isDestroyed()

Tell whether this ThreadGroup has been destroyed or not.

Since:Returns:


list

public void list()

Print out information about this ThreadGroup to System.out. This is meant for debugging purposes. WARNING: This method is not secure, and can print the name of threads to standard out even when you cannot otherwise get at such threads.


parentOf

public final boolean parentOf(java.lang.ThreadGroup tg)

Check whether this ThreadGroup is an ancestor of the specified ThreadGroup, or if they are the same.

Parameters:

Returns:


resume

public final synchronized void resume()

Resume all suspended Threads in this ThreadGroup and its descendants. To mirror suspend(), there is a security check: checkAccess(), followed by further checks on each thread being resumed.

Throws:

See Also:


setDaemon

public final void setDaemon(boolean daemon)

Set whether this ThreadGroup is a daemon group. A daemon group will be destroyed when its last thread is stopped and its last thread group is destroyed. There may be a security check, checkAccess.

Parameters:

Throws:

See Also:


setMaxPriority

public final synchronized void setMaxPriority(int maxpri)

Set the maximum priority for Threads in this ThreadGroup. setMaxPriority can only be used to reduce the current maximum. If maxpri is greater than the current Maximum of the parent group, the current value is not changed. Otherwise, all groups which belong to this have their priority adjusted as well. Calling this does not affect threads already in this ThreadGroup. There may be a security check, checkAccess.

Parameters:

Throws:

See Also:


stop

public final synchronized void stop()

Stop all Threads in this ThreadGroup and its descendants.

This is inherently unsafe, as it can interrupt synchronized blocks and leave data in bad states. Hence, there is a security check: checkAccess(), followed by further checks on each thread being stopped.

Throws:

See Also:


suspend

public final synchronized void suspend()

Suspend all Threads in this ThreadGroup and its descendants.

This is inherently unsafe, as suspended threads still hold locks, which can lead to deadlock. Hence, there is a security check: checkAccess(), followed by further checks on each thread being suspended.

Throws:

See Also:


toString

public String toString()

Return a human-readable String representing this ThreadGroup. The format of the string is:
getClass().getName() + "[name=" + getName() + ",maxpri=" + getMaxPriority() + ']'.

Returns:


uncaughtException

public void uncaughtException(java.lang.Thread thread, java.lang.Throwable t)

When a Thread in this ThreadGroup does not catch an exception, the virtual machine calls this method. The default implementation simply passes the call to the parent; then in top ThreadGroup, it will ignore ThreadDeath and print the stack trace of any other throwable. Override this method if you want to handle the exception in a different manner.

Parameters:

Throws:

See Also: