Share
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.
int activeGroupCount()
The activeGroupCount
method does not take any parameters.
This method will return the total count of active threads.
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 groupThreadGroup thg1 = new ThreadGroup("thread group-Parent");ThreadGroup thg2 = new ThreadGroup(thg1, "threadGroup-Child");ThreadGroup thg3 = new ThreadGroup(thg1, "threadGroup-Child");// create the threadsthread_new th1 = new thread_new("Thread#1", thg1);System.out.println(th1.getName() + " begins");// calling the run() methodth1.start();// check the total number of active threadSystem.out.println("# of active threads: "+ thg1.activeGroupCount());}}