1

ThreadGroup#activeCount()のドキュメントには、次のように記載されています。このスレッド グループとそのサブグループ内のアクティブなスレッドの推定数を返します。
その数には、sleep 、 wait および joinモードのスレッドが含まれますか、それとも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 に答える