Java如何获取当前线程中活动线程的数量?

在此示例中,您将看到如何获取当前线程中活动线程的数量。您可以使用该Thread.activeCount()方法来获取此信息。

package org.nhooo.example.lang;

public class CountActiveThread {
    public static void main(String[] args) {
        Thread t = new Thread(new Runnable() {
            public void run() {
                System.out.println("Hello...");
            }
        });
        t.start();

        // 获取当前线程的活动线程数
        // 线程组。
        int activeThread = Thread.activeCount();
        System.out.format("Number of active threads of %s is %d %n",
                Thread.currentThread().getName(), activeThread);
    }
}