Example usage for java.lang ThreadGroup enumerate

List of usage examples for java.lang ThreadGroup enumerate

Introduction

In this page you can find the example usage for java.lang ThreadGroup enumerate.

Prototype

public int enumerate(ThreadGroup list[]) 

Source Link

Document

Copies into the specified array references to every active subgroup in this thread group and its subgroups.

Usage

From source file:com.meltmedia.cadmium.servlets.ClassLoaderLeakPreventor.java

/**
 * Destroy any ThreadGroups that are loaded by the application classloader
 *//*from w ww.j a  va2 s .  c o  m*/
public void destroyThreadGroups() {
    try {
        ThreadGroup systemThreadGroup = Thread.currentThread().getThreadGroup();
        while (systemThreadGroup.getParent() != null) {
            systemThreadGroup = systemThreadGroup.getParent();
        }
        // systemThreadGroup should now be the topmost ThreadGroup, "system"

        int enumeratedGroups;
        ThreadGroup[] allThreadGroups;
        int noOfGroups = systemThreadGroup.activeGroupCount(); // Estimate no of groups
        do {
            noOfGroups += 10; // Make room for 10 extra
            allThreadGroups = new ThreadGroup[noOfGroups];
            enumeratedGroups = systemThreadGroup.enumerate(allThreadGroups);
        } while (enumeratedGroups >= noOfGroups); // If there was not room for all groups, try again

        for (ThreadGroup threadGroup : allThreadGroups) {
            if (isLoadedInWebApplication(threadGroup) && !threadGroup.isDestroyed()) {
                warn("ThreadGroup '" + threadGroup + "' was loaded inside application, needs to be destroyed");

                int noOfThreads = threadGroup.activeCount();
                if (noOfThreads > 0) {
                    warn("There seems to be " + noOfThreads + " running in ThreadGroup '" + threadGroup
                            + "'; interrupting");
                    try {
                        threadGroup.interrupt();
                    } catch (Exception e) {
                        error(e);
                    }
                }

                try {
                    threadGroup.destroy();
                    info("ThreadGroup '" + threadGroup + "' successfully destroyed");
                } catch (Exception e) {
                    error(e);
                }
            }
        }
    } catch (Exception ex) {
        error(ex);
    }
}

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 w w .  ja v a  2 s  .  c o 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]);
        }
    }
}