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
热门推荐
10 对患者生日祝福语简短
11 结婚祝福语简短装备
12 周岁祝福语学生文案简短
13 订婚领证祝福语简短精辟
14 导师获奖祝福语大全简短
15 新婚购房祝福语简短精辟
16 牛年祝福语简短的爱人
17 送芒果的祝福语简短
18 送给学长毕业祝福语简短