Home »
Java programming language
Java ThreadGroup class activeGroupCount() method with example
ThreadGroup class activeGroupCount() method: Here, we are going to learn about the activeGroupCount() method of ThreadGroup class with its syntax and example.
Submitted by Preeti Jain, on September 13, 2019
ThreadGroup class activeGroupCount() method
- activeGroupCount() method is available in java.lang package.
- activeGroupCount() method is used to return all active thread groups in this thread group and its subgroup also. The returned result is estimated so that it returns the number of active thread group in this thread group and it may change at runtime.
- activeGroupCount() method is a static method so it is accessible with the class name too.
- activeGroupCount() method is not a final method, so it is overridable (i.e. this method is overridable in child class if we want).
Syntax:
public static int activeGroupCount();
Parameter(s):
- This method does not accept any parameter.
Return value:
The return type of this method is int, it returns all the active thread groups in this thread group.
Example:
// Java program to demonstrate the example of
// activeGroupCount() method of ThreadGroup Class.
import java.lang.*;
class ActiveGroupCount extends Thread {
// Override run() of Thread class
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name + " " + "finish executing");
}
}
public class Main {
public static void main(String[] args) {
ActiveGroupCount gac = new ActiveGroupCount();
try {
// We are creating an object of ThreadGroup class
ThreadGroup tg1 = new ThreadGroup("ThreadGroup 1");
ThreadGroup tg2 = new ThreadGroup("ThreadGroup 2");
// We are creating an object of Thread class, and
// we are assigning the ThreadGroup of both the thread
Thread th1 = new Thread(tg1, gac, "First Thread");
Thread th2 = new Thread(tg2, gac, "Second Thread");
// Calling start() method with Thread class object
// of Thread class
th1.start();
th2.start();
// Here we are counting active thread in ThreadGroup
System.out.print("Active Group in : " + tg1.getName() + "-");
System.out.println(tg1.activeGroupCount());
System.out.print("Active Group in : " + tg2.getName() + "-");
System.out.println(tg2.activeGroupCount());
th1.join();
th2.join();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
Output
E:\Programs>javac Main.java
E:\Programs>java Main
First Thread finish executing
Second Thread finish executing
Active Group in : ThreadGroup 1-0
Active Group in : ThreadGroup 2-0