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 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短