Java: 获得所有线程
关键字: java, thread我想在一个时刻查看java中还存在的所有线程, 于是google到了这里.
http://billharlan.com/pub/papers/javatips.html.
Getting a list of running Threads
public static String[] getThreadNames() { ThreadGroup group = Thread.currentThread().getThreadGroup(); ThreadGroup parent = null; while ( (parent = group.getParent()) != null ) { group = parent; } Thread[] threads = new Thread; group.enumerate(threads); java.util.HashSet set = new java.util.HashSet(); for (int i=0; i < threads.length; ++i) { if (threads != null && threads.isAlive()) { try { set.add(threads.getThreadGroup().getName()+"," +threads.getName()+"," +threads.getPriority()); } catch (Throwable e) {e.printStackTrace();} } } String[] result = (String[]) set.toArray(new String); java.util.Arrays.sort(result); return result;}
页:
[1]