使用ThreadGroup类 activeGroupCount() 方法来获得活性基团的估计数的线程组中,并使用activeCount()获得线程组中活动线程的估计数。
package org.nhooo.example.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();
// 获取“根”线程组中的估计活动组
int activeGroup = root.activeGroupCount();
System.out.format("Estimated active group in %s is %d%n",
root.getName(), activeGroup);
// 获取“根”线程组中的估计活动线程
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..");
}
}上面的示例输出以下示例输出:
Running - Server Thread.. Running - Client Thread.. Estimated active group in RootGroup is 2 Estimated active thread in RootGroup is 0