Package java.lang

Class ThreadGroup

java.lang.Object
java.lang.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 new ThreadGroup with the given name.
    ThreadGroup​(ThreadGroup parent, String name)
    Constructs a new ThreadGroup with the given name, as a child of the given ThreadGroup.
  • Method Summary

    Modifier and Type Method Description
    int activeCount()
    Returns the number of running Threads which are children of this thread group, directly or indirectly.
    int activeGroupCount()
    Returns the number of ThreadGroups which are children of this group, directly or indirectly.
    boolean allowThreadSuspension​(boolean b)
    Deprecated.
    Required deprecated method suspend().
    void checkAccess()
    Does nothing.
    void destroy()
    Destroys this thread group and recursively all its subgroups.
    int enumerate​(Thread[] threads)
    Iterates over all active threads in this group (and its sub-groups) and stores the threads in the given array.
    int enumerate​(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.
    int enumerate​(ThreadGroup[] groups)
    Iterates over all thread groups in this group (and its sub-groups) and and stores the groups in the given array.
    int enumerate​(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.
    int getMaxPriority()
    Returns the maximum allowed priority for a Thread in this thread group.
    String getName()
    Returns the name of this thread group.
    ThreadGroup getParent()
    Returns this thread group's parent ThreadGroup.
    void interrupt()
    Interrupts every Thread in this group and recursively in all its subgroups.
    boolean isDaemon()
    Checks whether this thread group is a daemon ThreadGroup.
    boolean isDestroyed()
    Checks whether this thread group has already been destroyed.
    void list()
    Outputs to System.out a text representation of the hierarchy of Threads and ThreadGroups in this thread group (and recursively).
    boolean parentOf​(ThreadGroup g)
    Checks whether this thread group is a direct or indirect parent group of a given ThreadGroup.
    void resume()
    Deprecated.
    Requires deprecated method Thread.resume().
    void setDaemon​(boolean isDaemon)
    Sets whether this is a daemon ThreadGroup or not.
    void setMaxPriority​(int newMax)
    Configures the maximum allowed priority for a Thread in this group and recursively in all its subgroups.
    void stop()
    Deprecated.
    Requires deprecated method Thread.stop().
    void suspend()
    Deprecated.
    Requires deprecated method Thread.suspend().
    String toString()
    Returns a string containing a concise, human-readable description of this object.
    void uncaughtException​(Thread t, Throwable e)
    Handles uncaught exceptions.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
  • Constructor Details

    • ThreadGroup

      public ThreadGroup​(String name)
      Constructs a new ThreadGroup with the given name. The new ThreadGroup will be child of the ThreadGroup to which the calling thread belongs.
      Parameters:
      name - the name
      See Also:
      Thread.currentThread()
    • ThreadGroup

      public ThreadGroup​(ThreadGroup parent, String name)
      Constructs a new ThreadGroup with the given name, as a child of the given ThreadGroup.
      Parameters:
      parent - the parent
      name - the name
      Throws:
      NullPointerException - if parent == null
      IllegalThreadStateException - if parent has been destroyed already
  • Method Details

    • activeCount

      public int activeCount()
      Returns the number of running Threads which are children of this thread group, directly or indirectly.
      Returns:
      the number of children
    • activeGroupCount

      public int activeGroupCount()
      Returns the number of ThreadGroups which are children of this group, directly or indirectly.
      Returns:
      the number of children
    • allowThreadSuspension

      @Deprecated public boolean allowThreadSuspension​(boolean b)
      Deprecated.
      Required deprecated method suspend().
      Does nothing. The definition of this method depends on the deprecated method suspend(). 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 a ThreadGroup that has no threads in it. Any daemon ThreadGroup is 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

      public int enumerate​(Thread[] threads)
      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 the Threads will be copied
      Returns:
      the number of Threads that were copied
    • enumerate

      public int enumerate​(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. 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 the Threads will be copied
      recurse - indicates whether Threads in subgroups should be recursively copied as well
      Returns:
      the number of Threads that were copied
    • enumerate

      public int enumerate​(ThreadGroup[] groups)
      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 the ThreadGroups will be copied
      Returns:
      the number of ThreadGroups that were copied
    • enumerate

      public int enumerate​(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. 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 the ThreadGroups will be copied
      recurse - indicates whether ThreadGroups 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 a Thread in this thread group.
      Returns:
      the maximum priority
      See Also:
      setMaxPriority(int)
    • getName

      public final String getName()
      Returns the name of this thread group.
      Returns:
      the group's name
    • getParent

      public final ThreadGroup getParent()
      Returns this thread group's parent ThreadGroup. It can be null if this is the the root ThreadGroup.
      Returns:
      the parent
    • interrupt

      public final void interrupt()
      Interrupts every Thread in this group and recursively in all its subgroups.
      See Also:
      Thread.interrupt()
    • isDaemon

      public final boolean isDaemon()
      Checks whether this thread group is a daemon ThreadGroup.
      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 to System.out a text representation of the hierarchy of Threads and ThreadGroups in this thread group (and recursively). Proper indentation is used to show the nesting of groups inside groups and threads inside groups.
    • parentOf

      public final boolean parentOf​(ThreadGroup g)
      Checks whether this thread group is a direct or indirect parent group of a given ThreadGroup.
      Parameters:
      g - the potential child ThreadGroup
      Returns:
      true if this thread group is parent of g
    • resume

      @Deprecated public final void resume()
      Deprecated.
      Requires deprecated method Thread.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 daemon ThreadGroup or 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 a Thread in 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 public final void stop()
      Deprecated.
      Requires deprecated method Thread.stop().
      Stops every thread in this group and recursively in all its subgroups.
      See Also:
      Thread.stop(), Thread.stop(Throwable), ThreadDeath
    • suspend

      @Deprecated public final void suspend()
      Deprecated.
      Requires deprecated method Thread.suspend().
      Suspends every thread in this group and recursively in all its subgroups.
      See Also:
      Thread.suspend(), resume()
    • toString

      public String toString()
      Description copied from class: Object
      Returns 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 toString method if you intend implementing your own toString method.

      Overrides:
      toString in class Object
      Returns:
      a printable representation of this object.
    • uncaughtException

      public void uncaughtException​(Thread t, Throwable e)
      Handles uncaught exceptions. Any uncaught exception in any Thread is forwarded to the thread's ThreadGroup by invoking this method.

      New code should use Thread.setUncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler) instead of thread groups.

      Specified by:
      uncaughtException in interface Thread.UncaughtExceptionHandler
      Parameters:
      t - the Thread that terminated with an uncaught exception
      e - the uncaught exception itself