关于ThreadGroup的混淆#activeCount()

时间:2015-05-23 09:29:18

标签: java multithreading threadgroup

ThreadGroup #activeCount()的文档说明:返回此线程组及其子组中活动线程数的估计值。
这是否包括睡眠,等待和加入模式中的线程或仅执行运行方法的线程?

感谢。

1 个答案:

答案 0 :(得分: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.