1

ThreadGroup#activeCount()的文档说:返回此线程组及其子组中活动线程数的估计值。
该计数是否包括处于睡眠、等待和加入的线程模式的线程,还是仅包括那些正在执行run方法的线程?

谢谢。

4

1 回答 1

2

你可以很容易地试试这个:

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。

于 2015-05-23T09:34:45.390 回答