
write a java program to find out all the current running thread in a java program

public class RunningThreads{
public static void main(String[] args) {
ThreadGroup group = Thread.currentThread().getThreadGroup();
ThreadGroup gp;
while ((gp = group.getParent()) != null) {
group = gp;
}
findThreads(group, "");
}
public static void findThreads(ThreadGroup group, String str) {
System.out.println(str + "Group[" + group.getName() + ":" + group.getClass()+"]");
int count = group.activeCount();
Thread[] threads = new Thread[count*2 + 10];
count = group.enumerate(threads, false);
for (int i=0; i<count; i++) {
Thread t = threads[i];
System.out.println(str + " Thread[" + t.getName() + ":" + t.getClass() + "]");
}
int ct = group.activeGroupCount();
ThreadGroup[] groups = new ThreadGroup[ct*2 + 10];
ct = group.enumerate(groups, false);
for (int i=0; i<ct; i++) {
findThreads(groups[i], str + " ");
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.