Use method activeGroupCount()
of ThreadGroup
class to get estimate number of active groups in the thread group and use activeCount()
to get estimate number of active threads in a thread group.
package org.kodejava.lang;
public class ActiveGroupCount {
public static void main(String[] args) {
ThreadGroup root = new ThreadGroup("RootGroup");
ThreadGroup server = new ThreadGroup(root, "ServerGroup");
ThreadGroup client = new ThreadGroup(root, "ClientGroup");
Thread t1 = new Thread(server, new ServerThread(), "ServerThread");
Thread t2 = new Thread(client, new ClientThread(), "ClientThread");
t1.start();
t2.start();
// Get estimate active groups in 'root' thread group
int activeGroup = root.activeGroupCount();
System.out.format("Estimated active group in %s is %d%n",
root.getName(), activeGroup);
// Get estimate active threads in 'root' thread group
int activeThread = root.activeCount();
System.out.format("Estimated active thread in %s is %d%n",
root.getName(), activeThread);
}
}
class ServerThread implements Runnable {
public void run() {
System.out.println("Running - Server Thread..");
}
}
class ClientThread implements Runnable {
public void run() {
System.out.println("Running - Client Thread..");
}
}
The example above print the following example output:
Estimated active group in RootGroup is 2
Estimated active thread in RootGroup is 2
Running - Client Thread..
Running - Server Thread..
Latest posts by Wayan (see all)
- How do I get number of each day for a certain month in Java? - September 8, 2024
- How do I get operating system process information using ProcessHandle? - July 22, 2024
- How do I sum a BigDecimal property of a list of objects using Java Stream API? - July 22, 2024