This class lists all the currently active threads in a JVM. Note how the number of JVM-internal threads increases once GUI objects are used. If the code is part of a server application (like a web app), there will be even more threads.
public class ThreadLister {
public static void main (String[] args)
{
listThreads();
new javax.swing.JFrame();
listThreads();
System.exit(0);
}
private static void listThreads()
{
ThreadGroup adam = getAdam(Thread.currentThread().getThreadGroup());
Thread list[] = new Thread[adam.activeCount()];
int numThreads = adam.enumerate(list);
for (Thread th : list) {
if (th == null)
continue;
System.out.println("group:" + th.getThreadGroup().getName()
+ ", thread:" + th.getName()
+ ", priority:" + th.getPriority()
+ ", daemon:" + (th.isDaemon() ? "Yes" : "No")
+ ", alive:" + (th.isAlive() ? "Yes" : "No"));
}
System.out.println();
}
private static ThreadGroup getAdam (ThreadGroup tg)
{
ThreadGroup parent = tg.getParent();
if (parent == null)
return tg;
return getAdam(parent);
}
}
CodeBarn CategoryCodeSamples