Java如何获取活动线程组的数量?
使用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
热门推荐
6 保研的祝福语简短
10 年轻20岁祝福语简短
11 朋友结婚祝福语信息简短
12 女孩婚礼贺卡祝福语简短
13 30段点歌简短祝福语
14 虎年春节祝福语图文简短
15 写给后妈祝福语大全简短
16 简短回复生日祝福语
17 校长送毕业祝福语简短
18 毕业立体贺卡祝福语简短