What is the ThreadGroup.activeGroupCount() function in Java?

The activeGroupCount() function from the java.lang.ThreadGroup class helps calculate the approximate number of active thread groups in a thread group and its subgroups.

The returned number of active groups is an estimated value because of the dynamic change in the number of thread groups while the internal data structure is traversed by this function.

Syntax


int activeGroupCount()

Parameters

The activeGroupCount method does not take any parameters.

Return value

This method will return the total count of active threads.

Code

import java.lang.Thread;
import java.lang.ThreadGroup;
class thread_new extends Thread
{
thread_new(String thread_name, ThreadGroup Tg)
{
super(Tg, thread_name);
}
public void run()
{
System.out.println(Thread.currentThread().getName() +
" EXECUTION COMPLETED");
}
}
public class EdPresso
{
public static void main(String arg[])
{
//create the thread group
ThreadGroup thg1 = new ThreadGroup("thread group-Parent");
ThreadGroup thg2 = new ThreadGroup(thg1, "threadGroup-Child");
ThreadGroup thg3 = new ThreadGroup(thg1, "threadGroup-Child");
// create the threads
thread_new th1 = new thread_new("Thread#1", thg1);
System.out.println(th1.getName() + " begins");
// calling the run() method
th1.start();
// check the total number of active thread
System.out.println("# of active threads: "
+ thg1.activeGroupCount());
}
}

Free Resources