How can I see the number of threads in a Java process?
There is a static method on the Thread
Class that will return the number of active threads controlled by the JVM:
Thread.activeCount()
Returns the number of active threads in the current thread's thread group.
Additionally, external debuggers should list all active threads (and allow you to suspend any number of them) if you wish to monitor them in real-time.
Generic solution that doesn't require a GUI like jconsole (doesn't work on remote terminals), ps works for non-java processes, doesn't require a JVM installed.
ps -o nlwp <pid>
public class MainClass {
public static void main(String args[]) {
Thread t = Thread.currentThread();
t.setName("My Thread");
t.setPriority(1);
System.out.println("current thread: " + t);
int active = Thread.activeCount();
System.out.println("currently active threads: " + active);
Thread all[] = new Thread[active];
Thread.enumerate(all);
for (int i = 0; i < active; i++) {
System.out.println(i + ": " + all[i]);
}
}
}