public class TestThread extends Thread {
public static void main(String[] args) {
TestThread p = new TestThread();
Thread t1 = new Thread(p);
Thread t2 = new Thread(p);
t1.start();
t2. start();
System.out.println(t1.activeCount());
System.out.println(t2.activeCount());
}
}
The activeCount() method returns the number of active threads in the thread group of the current thread. The result is 3. Why is it 3? Because when the program executes the main method, it is equivalent to starting a thread. Also, you must call this method and print after the thread is started, otherwise it cannot be compiled. If the program is relatively long, the result may be 1 if it is placed at the end, because your thread has died and ended by then, and the rest is main thread.