List of usage examples for java.lang ThreadGroup activeCount
public int activeCount()
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; }// w ww . j a v a 2 s. c o m // 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:com.baidu.jprotobuf.mojo.PreCompileMojo.java
private void terminateThreads(ThreadGroup threadGroup) { long startTime = System.currentTimeMillis(); Set<Thread> uncooperativeThreads = new HashSet<Thread>(); // these were not responsive to interruption for (Collection<Thread> threads = getActiveThreads(threadGroup); !threads .isEmpty(); threads = getActiveThreads(threadGroup), threads.removeAll(uncooperativeThreads)) { // Interrupt all threads we know about as of this instant (harmless if spuriously went dead (! isAlive()) // or if something else interrupted it ( isInterrupted() ). for (Thread thread : threads) { getLog().debug("interrupting thread " + thread); thread.interrupt();/*from w ww .j ava2s. co m*/ } // Now join with a timeout and call stop() (assuming flags are set right) for (Thread thread : threads) { if (!thread.isAlive()) { continue; // and, presumably it won't show up in getActiveThreads() next iteration } if (daemonThreadJoinTimeout <= 0) { joinThread(thread, 0); // waits until not alive; no timeout continue; } long timeout = daemonThreadJoinTimeout - (System.currentTimeMillis() - startTime); if (timeout > 0) { joinThread(thread, timeout); } if (!thread.isAlive()) { continue; } uncooperativeThreads.add(thread); // ensure we don't process again if (stopUnresponsiveDaemonThreads) { getLog().warn("thread " + thread + " will be Thread.stop()'ed"); thread.stop(); } else { getLog().warn("thread " + thread + " will linger despite being asked to die via interruption"); } } } if (!uncooperativeThreads.isEmpty()) { getLog().warn("NOTE: " + uncooperativeThreads.size() + " thread(s) did not finish despite being asked to " + " via interruption. This is not a problem with exec:java, it is a problem with the running code." + " Although not serious, it should be remedied."); } else { int activeCount = threadGroup.activeCount(); if (activeCount != 0) { // TODO this may be nothing; continue on anyway; perhaps don't even log in future Thread[] threadsArray = new Thread[1]; threadGroup.enumerate(threadsArray); getLog().debug("strange; " + activeCount + " thread(s) still active in the group " + threadGroup + " such as " + threadsArray[0]); } } }
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 w w w . j ava2 s . c o m logger.warn("\n\nThreads:\n\n " + stackString); } } } }