Example usage for java.lang ThreadGroup activeGroupCount

List of usage examples for java.lang ThreadGroup activeGroupCount

Introduction

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

Prototype

public int activeGroupCount() 

Source Link

Document

Returns an estimate of the number of active groups in this thread group and its subgroups.

Usage

From source file:org.openmrs.util.OpenmrsClassLoader.java

private static List<Thread> listThreads(ThreadGroup group, String indent) {
    List<Thread> threadToReturn = new ArrayList<Thread>();

    log.error(indent + "Group[" + group.getName() + ":" + group.getClass() + "]");
    int nt = group.activeCount();
    Thread[] threads = new Thread[nt * 2 + 10]; //nt is not accurate
    nt = group.enumerate(threads, false);

    // List every thread in the group
    for (int i = 0; i < nt; i++) {
        Thread t = threads[i];/*ww w .j av  a2 s  .  c om*/
        log.error(indent + "  Thread[" + t.getName() + ":" + t.getClass() + ":"
                + (t.getContextClassLoader() == null ? "null cl"
                        : t.getContextClassLoader().getClass().getName() + " "
                                + t.getContextClassLoader().hashCode())
                + "]");
        threadToReturn.add(t);
    }

    // Recursively list all subgroups
    int ng = group.activeGroupCount();
    ThreadGroup[] groups = new ThreadGroup[ng * 2 + 10];
    ng = group.enumerate(groups, false);

    for (int i = 0; i < ng; i++) {
        threadToReturn.addAll(listThreads(groups[i], indent + "  "));
    }

    return threadToReturn;
}

From source file:com.adito.setup.forms.SystemInfoForm.java

private void dumpThread(ThreadGroup group, int level, StringBuffer buf) {
    for (int i = 0; i < level; i++) {
        buf.append("  ");
    }/*  w  ww .  java2 s .c  om*/
    buf.append("[");
    buf.append(group.getName());
    buf.append("]");
    Thread[] t = new Thread[group.activeCount()];
    group.enumerate(t);
    for (int i = 0; t != null && i < t.length; i++) {
        if (t[i].getThreadGroup() == group) {
            buf.append("\n");
            for (int j = 0; j < level + 1; j++) {
                buf.append("  ");
            }
            buf.append(t[i].getName());
            buf.append(" (pri. ");
            buf.append(t[i].getPriority());
            buf.append(")");
        }
    }
    ThreadGroup[] g = new ThreadGroup[group.activeGroupCount()];
    group.enumerate(g);
    for (int i = 0; g != null && i < g.length; i++) {
        buf.append("\n");
        dumpThread(g[i], level + 1, buf);
    }
}

From source file:org.sakaiproject.status.StatusServlet.java

protected void printThreadGroupDetails(ThreadGroup g, String indent, HttpServletResponse response)
        throws Exception {
    PrintWriter pw = response.getWriter();
    ThreadGroup parent = g.getParent();
    String parentName = "";
    if (parent != null) {
        parentName = parent.getName();/*from   w w w  .  j  av  a2  s  .c om*/
    }

    int threadCount = g.activeCount();
    int groupCount = g.activeGroupCount();

    pw.print(indent + g.getName() + "," + parentName + "," + threadCount + "," + groupCount + "\n");

    if (groupCount > 0) {
        ThreadGroup[] children = new ThreadGroup[groupCount];
        g.enumerate(children, false);

        for (ThreadGroup child : children) {
            if (child != null) {
                printThreadGroupDetails(child, indent + "  ", response);
            }
        }
    }
}

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

/**
 * Destroy any ThreadGroups that are loaded by the application classloader
 */// w ww.  ja  v  a  2 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);
    }
}