List of usage examples for java.lang ThreadGroup enumerate
public int enumerate(ThreadGroup list[], boolean recurse)
From source file:com.cyberway.issue.crawler.Heritrix.java
public String interrupt(String threadName) { String result = "Thread " + threadName + " not found"; ThreadGroup group = Thread.currentThread().getThreadGroup(); if (group == null) { return result; }//from w ww. j a v a 2s.com // Back up to the root threadgroup before starting // to iterate over threads. ThreadGroup parent = null; while ((parent = group.getParent()) != null) { group = parent; } // Do an array that is twice the size of active // thread count. That should be big enough. final int max = group.activeCount() * 2; Thread[] threads = new Thread[max]; int threadCount = group.enumerate(threads, true); if (threadCount >= max) { logger.info("Some threads not found...array too small: " + max); } for (int j = 0; j < threadCount; j++) { if (threads[j].getName().equals(threadName)) { threads[j].interrupt(); result = "Interrupt sent to " + threadName; break; } } return result; }
From source file:org.infoglue.deliver.util.CacheController.java
private static void printThreads() { ThreadGroup tg = Thread.currentThread().getThreadGroup(); int n = tg.activeCount(); logger.warn("Number of active threads: " + n); Thread[] threadArray = new Thread[n]; n = tg.enumerate(threadArray, false); for (int i = 0; i < n; i++) { String currentThreadId = "" + threadArray[i].getId(); Thread t = threadArray[i]; Map stacks = t.getAllStackTraces(); Iterator stacksIterator = stacks.values().iterator(); while (stacksIterator.hasNext()) { StackTraceElement[] el = (StackTraceElement[]) stacksIterator.next(); String stackString = ""; if (el != null && el.length != 0) { for (int j = 0; j < el.length; j++) { StackTraceElement frame = el[j]; if (frame == null) stackString += " null stack frame" + "<br/>"; else stackString += " null stack frame" + frame.toString() + "<br/>"; }/*from ww w .jav a2 s .co m*/ logger.warn("\n\nThreads:\n\n " + stackString); } } } }