ThreadGroup#activeCount()的文档说:返回这个线程组及其子组中活动线程数量的估计值。
该计数是否包括处于睡眠、等待和加入模式的线程,还是只包括正在执行运行方法的线程?
谢谢。
发布于 2015-05-23 09:34:45
您可以很容易地尝试以下操作:
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。
https://stackoverflow.com/questions/30411073
复制相似问题