Java如何计算线程组中的活动线程?
本示例说明如何获取此线程组中活动线程的数量。结果可能无法反映并发活动,并且可能会受到某些系统线程的影响。
由于结果本身的不精确性,建议仅将此方法用于信息查看。
package org.nhooo.example.lang;
public class ThreadGroupActiveThread {
public static void main(String[] args) {
ThreadGroup threadGroup = new ThreadGroup("TestThread");
Thread t1 = new Thread(threadGroup, new Server(), "Server1");
Thread t2 = new Thread(threadGroup, new Server(), "Server2");
Thread t3 = new Thread(threadGroup, new Server(), "Server3");
Thread t4 = new Thread(threadGroup, new Server(), "Server4");
t1.start();
t2.start();
t3.start();
t4.start();
// 获取线程的活动线程的估计数量
// 组。
int activeThread = threadGroup.activeCount();
System.out.format("Number of active threads of %s is %d %n",
threadGroup.getName(), activeThread);
}
}
class Server implements Runnable {
public void run() {
System.out.println("Running..");
}
}
热门推荐
10 病人祝福语老师寄语简短
11 新娘生孩子祝福语简短
12 婆婆66岁祝福语简短
13 今天孩子高考祝福语简短
14 送彩票生日祝福语简短
15 分别祝福语简短情话英文
16 画室揭牌仪式祝福语简短
17 女友妈妈生日祝福语简短
18 离别赠言离职祝福语简短