Module java.base
Package java.lang

Class 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 ThreadGroup parent
    • 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, call checkAccess 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 array threads passed as parameters.
      int enumerate​(Thread[] threads, boolean recurse)
      Copies an array with all Threads which are children of the receiver into the array threads passed as parameter.
      int enumerate​(ThreadGroup[] groups)
      Copies an array with all ThreadGroups which are children of the receiver (directly or indirectly) into the array groups passed as parameters.
      int enumerate​(ThreadGroup[] groups, boolean recurse)
      Copies an array with all ThreadGroups which are children of the receiver into the array groups 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 to System.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 ThreadGroup g, 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 the Thread.currentThread() belongs.
        Parameters:
        name - Name for the ThreadGroup being created
        Throws:
        SecurityException - if checkAccess() 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 ThreadGroup parent
        Parameters:
        parent - Parent ThreadGroup
        name - Name for the ThreadGroup being created
        Throws:
        NullPointerException - if parent is null
        SecurityException - if checkAccess() for the parent group fails with a SecurityException
        IllegalThreadStateException - if parent 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 method suspend(). 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, call checkAccess 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 already
        SecurityException - if this.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 array threads 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 array threads passed as parameter. Children Threads of subgroups are recursively copied as well if parameter recurse is true. 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 copied
        recurse - 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 array groups 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 array groups passed as parameter. Children ThreadGroups of subgroups are recursively copied as well if parameter recurse is true. 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
        recurse - 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 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 - if this.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 to System.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 ThreadGroup g, false otherwise.
        Parameters:
        g - ThreadGroup to test
        Returns:
        if the receiver is parent of the ThreadGroup passed as parameter
      • 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 - if checkAccess() 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 - if checkAccess() fails with a SecurityException
        IllegalArgumentException - 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 - if this.checkAccess() fails with a SecurityException
        See Also:
        Thread.stop(), Thread#stop(Throwable), ThreadDeath
      • toString

        public String toString()
        Answers a string containing a concise, human-readable description of the receiver.
        Overrides:
        toString in class Object
        Returns:
        a printable representation for 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 interface Thread.UncaughtExceptionHandler
        Parameters:
        t - Thread with an uncaught exception
        e - The uncaught exception itself
        See Also:
        Thread.stop(), Thread#stop(Throwable), ThreadDeath