ThreadGroup类activeGroupCount()方法
- activeGroupCount() 方法可在
java.lang
包。 - activeGroupCount() 方法用于返回此线程组及其子组中的所有活动线程组。估计返回的结果,以便它返回该线程组中活动线程组的数量,并且可能会在运行时发生变化。
- activeGroupCount() 方法是一个静态方法,因此它也可以通过类名访问。
- activeGroupCount() 方法不是最终方法,因此它是可覆盖的(即,如果我们愿意,此方法在子类中是可覆盖的)。
用法:
public static int activeGroupCount();
参数:
- 此方法不接受任何参数。
返回值:
这个方法的返回类型是int
,它返回该线程组中的所有活动线程组。
例:
// Java program to demonstrate the example of
// activeGroupCount() method of ThreadGroup Class.
import java.lang.*;
class ActiveGroupCount extends Thread {
// Override run() of Thread class
public void run() {
String name = Thread.currentThread().getName();
System.out.println(name + " " + "finish executing");
}
}
public class Main {
public static void main(String[] args) {
ActiveGroupCount gac = new ActiveGroupCount();
try {
// We are creating an object of ThreadGroup class
ThreadGroup tg1 = new ThreadGroup("ThreadGroup 1");
ThreadGroup tg2 = new ThreadGroup("ThreadGroup 2");
// We are creating an object of Thread class, and
// we are assigning the ThreadGroup of both the thread
Thread th1 = new Thread(tg1, gac, "First Thread");
Thread th2 = new Thread(tg2, gac, "Second Thread");
// Calling start() method with Thread class object
// of Thread class
th1.start();
th2.start();
// Here we are counting active thread in ThreadGroup
System.out.print("Active Group in:" + tg1.getName() + "-");
System.out.println(tg1.activeGroupCount());
System.out.print("Active Group in:" + tg2.getName() + "-");
System.out.println(tg2.activeGroupCount());
th1.join();
th2.join();
} catch (Exception ex) {
System.out.println(ex.getMessage());
}
}
}
输出
E:\Programs>javac Main.java E:\Programs>java Main First Thread finish executing Second Thread finish executing Active Group in:ThreadGroup 1-0 Active Group in:ThreadGroup 2-0
相关用法
- Java ThreadGroup activeCount()用法及代码示例
- Java ThreadGroup enumerate()用法及代码示例
- Java ThreadGroup getMaxPriority()用法及代码示例
- Java ThreadGroup getParent()用法及代码示例
- Java ThreadGroup getName()用法及代码示例
- Java ThreadGroup parentOf()用法及代码示例
- Java ThreadGroup interrupt()用法及代码示例
- Java ThreadGroup suspend()用法及代码示例
- Java ThreadGroup setDaemon()用法及代码示例
- Java ThreadGroup isDaemon()用法及代码示例
- Java ThreadGroup checkAccess()用法及代码示例
- Java ThreadGroup resume()用法及代码示例
- Java ThreadGroup isDestroyed()用法及代码示例
- Java ThreadGroup toString()用法及代码示例
- Java ThreadGroup destroy()用法及代码示例
- Java ThreadGroup list()用法及代码示例
- Java ThreadGroup stop()用法及代码示例
- Java ThreadGroup setMaxPriority()用法及代码示例
- Java Thread toString()用法及代码示例
- Java Thread interrupted()用法及代码示例
注:本文由纯净天空筛选整理自Preeti Jain大神的英文原创作品 Java ThreadGroup class activeGroupCount() method with example。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。