private static void showThreads(PrintStream pw, ThreadGroup g, Thread current) { int nthreads = g.activeCount(); pw.println("\nThread Group = " + g.getName() + " activeCount= " + nthreads); Thread[] tarray = new Thread[nthreads]; int n = g.enumerate(tarray, false); for (int i = 0; i < n; i++) { Thread thread = tarray[i]; ClassLoader loader = thread.getContextClassLoader(); String loaderName = (loader == null) ? "Default" : loader.getClass().getName(); Thread.State state = thread.getState(); long id = thread.getId(); pw.print(" " + id + " " + thread.getName() + " " + state + " " + loaderName); if (thread == current) pw.println(" **** CURRENT ***"); else pw.println(); } int ngroups = g.activeGroupCount(); ThreadGroup[] garray = new ThreadGroup[ngroups]; int ng = g.enumerate(garray, false); for (int i = 0; i < ng; i++) { ThreadGroup nested = garray[i]; showThreads(pw, nested, current); } }
public static void dumpThreads(ThreadGroup threadGroup, String indent) { Thread[] threadList = new Thread[threadGroup.activeCount()]; threadGroup.enumerate(threadList); for (int i = 0; i < threadList.length; i++) { Thread t = threadList[i]; if (t != null) { out( indent .concat("active thread = ") .concat(t.toString()) .concat(", daemon = ") .concat(String.valueOf(t.isDaemon()))); } } if (threadGroup.getParent() != null) { dumpThreads(threadGroup.getParent(), indent + "\t"); } }
public static void killAWTThreads(ThreadGroup threadGroup) { Thread[] threadList = new Thread[threadGroup.activeCount()]; threadGroup.enumerate(threadList); for (int i = 0; i < threadList.length; i++) { Thread t = threadList[i]; if (t != null) { String name = t.getName(); if (name.startsWith("AWT")) { out("Interrupting thread '".concat(t.toString()).concat("'")); t.interrupt(); } } } if (threadGroup.getParent() != null) { killAWTThreads(threadGroup.getParent()); } }
/** * Returns an array containing the existing client connections. This can be used by concrete * subclasses to implement messages that do something with each connection (e.g. kill it, send a * message to it etc.). Remember that after this array is obtained, some clients in this migth * disconnect. New clients can also connect, these later will not appear in the array. * * @return an array of <code>Thread</code> containing <code>ConnectionToClient</code> instances. */ public final synchronized Thread[] getClientConnections() { Thread[] clientThreadList = new Thread[clientThreadGroup.activeCount()]; clientThreadGroup.enumerate(clientThreadList); return clientThreadList; }
private static void dumptg(ThreadGroup tg, PrintWriter out, int indent) { for (int o = 0; o < indent; o++) out.print("\t"); out.println("G: \"" + tg.getName() + "\""); Thread[] ths = new Thread[tg.activeCount() * 2]; ThreadGroup[] tgs = new ThreadGroup[tg.activeGroupCount() * 2]; int nt = tg.enumerate(ths, false); int ng = tg.enumerate(tgs, false); for (int i = 0; i < nt; i++) { Thread ct = ths[i]; for (int o = 0; o < indent + 1; o++) out.print("\t"); out.println("T: \"" + ct.getName() + "\""); } for (int i = 0; i < ng; i++) { ThreadGroup cg = tgs[i]; dumptg(cg, out, indent + 1); } }
public static boolean kill() { int j = 0; Thread meMySelfI = Thread.currentThread(); do { /* iter across the thread group, killing all members. */ shutdown = true; Thread list[] = new Thread[tg.activeCount()]; // get all members of the group (including submembers) int i = tg.enumerate(list); // no members means that we have gracefully suceeded if (i == 0) return true; // if some of the threads do IO during the shut down they will // need time to accomplish the IO. So, I give it to 'em // after the first attempt. if (j > 0) try { meMySelfI.sleep(500); } catch (Exception e) { } ; // try to shudown each thread in the group while (i-- > 0) { FlickrFtpd tftp = (FlickrFtpd) list[i]; tftp.interrupt(); // first, do it politely meMySelfI.yield(); // give 'em time to respond tftp.forceClose(); // second, use a big hammer meMySelfI.yield(); // give 'em time to respond } } while (j++ <= 3); return false; }