How do I check whether a thread group has been destroyed?

You can use ThreadGroup.isDestroyed() method to check whether a thread group and its subgroups has been destroyed.

package org.kodejava.example.lang;

public class CheckGroupDestroy {
    public static void main(String[] args) {
        ThreadGroup grandParent = new ThreadGroup("GrandParent");
        ThreadGroup uncle = new ThreadGroup(grandParent, "Uncle");
        ThreadGroup parent = new ThreadGroup(grandParent, "Parent");
        ThreadGroup son = new ThreadGroup(parent, "Son");
        ThreadGroup daughter = new ThreadGroup(parent, "Daughter");
        ThreadGroup neighbour = new ThreadGroup("Neighbour");

        ThreadGroup[] groupArray = {
                grandParent, uncle, parent, son, daughter, neighbour

        // Destroy 'parent' group and all its subgroups

        // Check whether the group is destroyed. The result is,
        // GrandParent, Uncle, and Neighbour did not destroyed
        // because they are not Parent's subgroups
        for (ThreadGroup tg : groupArray) {
            if (tg.isDestroyed()) {
                System.out.println(tg.getName() + " is destroyed");
            } else {
                System.out.println(tg.getName() + " is not destroyed");

The result is:

GrandParent is not destroyed
Uncle is not destroyed
Parent is destroyed
Son is destroyed
Daughter is destroyed
Neighbour is not destroyed

How do I destroy a thread group?

You can destroy a thread group by using destroy() method of ThreadGroup class. It will cleans up the thread group and removes it from the thread group hierarchy. It’s not only destroy the thread group, but also all its subgroups.

The destroy() method is of limited use: it can only be called if there are no threads presently in the thread group.

package org.kodejava.example.lang;

public class ThreadGroupDestroy {
    public static void main(String[] args) {
        ThreadGroup root = new ThreadGroup("Root");
        ThreadGroup server = new ThreadGroup(root, "ServerGroup");
        ThreadGroup client = new ThreadGroup(root, "ClientGroup");

        // Destroy 'root' thread groups and all its subgroup
        // ('server' & 'client')

        // Check if 'root' group and its subgroups already destroyed
        if (root.isDestroyed()) {
            System.out.println("Root group is destroyed");

        if (server.isDestroyed()) {
            System.out.println("Server group is destroyed");

        if (client.isDestroyed()) {
            System.out.println("Client group is destroyed");

How do I count active thread in a thread group?

This example describe how to get the number of active threads in this thread group. The result might not reflect concurrent activity, and might be affected by the presence of certain system threads.

Due to the inherently imprecise nature of the result, it is recommended that this method only be used for informational purposes.

package org.kodejava.example.lang;

public class ThreadGroupActiveThread {
    public static void main(String[] args) {
        ThreadGroup threadGroup = new ThreadGroup("TestThread");

        Thread t1 = new Thread(threadGroup, new Server(), "Server1");
        Thread t2 = new Thread(threadGroup, new Server(), "Server2");
        Thread t3 = new Thread(threadGroup, new Server(), "Server3");
        Thread t4 = new Thread(threadGroup, new Server(), "Server4");


        // Get an estimate number of active thread of the thread
        // group.
        int activeThread = threadGroup.activeCount();
        System.out.format("Number of active threads of %s is %d %n",
                threadGroup.getName(), activeThread);

class Server implements Runnable {
    public void run() {