ThreadGroup#activeCount() 的文档说:返回此线程组及其子组中 Activity 线程数量的估计值。
该计数是否包括处于 sleep 、 wait 和 join 模式的线程,还是仅包括那些正在执行 run 方法的线程?
谢谢。
最佳答案
您可以轻松尝试此操作:
Thread t1 = new Thread(new Runnable() {
@Override
public void run() {
Scanner sc = new Scanner(System.in);
sc.nextInt();
}
});
Thread t2 = new Thread(new Runnable() {
@Override
public void run() {
try {
Thread.sleep(10000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
});
t1.start(); // this will be RUNNABLE
t2.start(); // this will be TIMED_WAITING
System.out.println(Thread.currentThread().getThreadGroup().activeCount());
打印 3. 注释行
t1.start();
t2.start();
导致打印1。
关于java - 关于 ThreadGroup#activeCount() 的困惑,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30411073/