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:psiprobe.controllers.threads.ListThreadsController.java

/**
 * Enumerate threads.//from  w  w  w  .  java2s.  com
 *
 * @param classLoaderMap the class loader map
 * @return the list
 */
private List<ThreadModel> enumerateThreads(final Map<String, String> classLoaderMap) {

    // get top ThreadGroup
    ThreadGroup masterGroup = Thread.currentThread().getThreadGroup();
    while (masterGroup.getParent() != null) {
        masterGroup = masterGroup.getParent();
    }

    // enumerate all Threads starting from top
    List<ThreadModel> threadList = new ArrayList<>();

    Thread[] threads = new Thread[masterGroup.activeCount()];
    int numThreads = masterGroup.enumerate(threads);

    for (int i = 0; i < numThreads; i++) {
        ThreadModel threadModel = new ThreadModel();
        threadModel.setThreadClass(threads[i].getClass().getName());
        threadModel.setName(threads[i].getName());
        threadModel.setPriority(threads[i].getPriority());
        threadModel.setDaemon(threads[i].isDaemon());
        threadModel.setInterrupted(threads[i].isInterrupted());
        if (threads[i].getThreadGroup() != null) {
            threadModel.setGroupName(threads[i].getThreadGroup().getName());
        }
        Object target = Instruments.getField(threads[i], "target");
        if (target != null) {
            threadModel.setRunnableClassName(target.getClass().getName());
        }

        ClassLoader cl = threads[i].getContextClassLoader();
        if (cl != null) {
            if (classLoaderMap != null) {
                threadModel.setAppName(classLoaderMap.get(toUid(cl)));
            }
            threadModel.setClassLoader(toUid(cl));
        }
        threadList.add(threadModel);
    }
    return threadList;
}

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("  ");
    }/*www. ja v  a2 s  . com*/
    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:net.testdriven.psiprobe.controllers.threads.ListThreadsController.java

private List enumerateThreads(final Map classLoaderMap) {

    ////w w w. j a va2 s  . c o m
    // get top ThreadGroup
    //
    ThreadGroup masterGroup = Thread.currentThread().getThreadGroup();
    while (masterGroup.getParent() != null) {
        masterGroup = masterGroup.getParent();
    }

    //
    // enumerate all Threads starting from top
    //
    List<ThreadModel> threadList = new ArrayList<>();

    Thread[] threads = new Thread[masterGroup.activeCount()];
    int numThreads = masterGroup.enumerate(threads);

    for (int i = 0; i < numThreads; i++) {
        ThreadModel threadModel = new ThreadModel();
        threadModel.setThreadClass(threads[i].getClass().getName());
        threadModel.setName(threads[i].getName());
        threadModel.setPriority(threads[i].getPriority());
        threadModel.setDaemon(threads[i].isDaemon());
        threadModel.setInterrupted(threads[i].isInterrupted());
        if (threads[i].getThreadGroup() != null) {
            threadModel.setGroupName(threads[i].getThreadGroup().getName());
        }
        Object target = Instruments.getField(threads[i], "target");
        if (target != null) {
            threadModel.setRunnableClassName(target.getClass().getName());
        }

        ClassLoader cl = threads[i].getContextClassLoader();
        if (cl != null) {
            if (classLoaderMap != null) {
                threadModel.setAppName((String) classLoaderMap.get(toUID(cl)));
            }
            threadModel.setClassLoader(toUID(cl));
        }
        threadList.add(threadModel);
    }
    return threadList;
}

From source file:org.codelibs.fess.servlet.Tomcat6ConfigServlet.java

private Thread[] getThreads() {
    // Get the current thread group
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    // Find the root thread group
    while (tg.getParent() != null) {
        tg = tg.getParent();/*w ww  . ja  va  2s .c o m*/
    }

    int threadCountGuess = tg.activeCount() + 50;
    Thread[] threads = new Thread[threadCountGuess];
    int threadCountActual = tg.enumerate(threads);
    // Make sure we don't miss any threads
    while (threadCountActual == threadCountGuess) {
        threadCountGuess *= 2;
        threads = new Thread[threadCountGuess];
        // Note tg.enumerate(Thread[]) silently ignores any threads that
        // can't fit into the array
        threadCountActual = tg.enumerate(threads);
    }

    return threads;
}

From source file:com.agiletec.apsadmin.category.CategoryAction.java

/**
 * provide the result for the progress bar
 * @return//from w ww  . j  a va2  s. c  o  m
 */
public Map<String, Integer> getUpdateReferencesStatus() {
    int total = 0;
    int done = 0;
    ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
    int numThreads = currentGroup.activeCount();
    Thread[] listOfThreads = new Thread[numThreads];
    currentGroup.enumerate(listOfThreads);
    for (int i = 0; i < numThreads; i++) {
        if (listOfThreads[i].getName()
                .startsWith(CategoryManager.RELOAD_CATEGORY_REFERENCES_THREAD_NAME_PREFIX)) {
            ReloadingCategoryReferencesThread thread = (ReloadingCategoryReferencesThread) listOfThreads[i];
            total = total + thread.getListSize();
            done = done + thread.getListIndex();
        }
    }
    Map<String, Integer> result = new HashMap<String, Integer>();
    result.put("total", total);
    result.put("done", done);
    return result;
}

From source file:org.apache.atlas.web.resources.AdminResource.java

/**
 * Fetches the thread stack dump for this application.
 *
 * @return json representing the thread stack dump.
 *//*from   w  w w .j a v  a  2s  .  c o  m*/
@GET
@Path("stack")
@Produces(MediaType.TEXT_PLAIN)
public String getThreadDump() {
    if (LOG.isDebugEnabled()) {
        LOG.debug("==> AdminResource.getThreadDump()");
    }

    ThreadGroup topThreadGroup = Thread.currentThread().getThreadGroup();

    while (topThreadGroup.getParent() != null) {
        topThreadGroup = topThreadGroup.getParent();
    }
    Thread[] threads = new Thread[topThreadGroup.activeCount()];

    int nr = topThreadGroup.enumerate(threads);
    StringBuilder builder = new StringBuilder();
    for (int i = 0; i < nr; i++) {
        builder.append(threads[i].getName()).append("\nState: ").append(threads[i].getState()).append("\n");
        String stackTrace = StringUtils.join(threads[i].getStackTrace(), "\n");
        builder.append(stackTrace);
    }

    if (LOG.isDebugEnabled()) {
        LOG.debug("<== AdminResource.getThreadDump()");
    }

    return builder.toString();
}

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

protected Thread[] findAllThreads() throws Exception {
    ThreadGroup group = findSystemThreadGroup();
    Thread[] threads = new Thread[group.activeCount()];
    group.enumerate(threads);

    return threads;
}

From source file:com.baidu.jprotobuf.mojo.PreCompileMojo.java

private Collection<Thread> getActiveThreads(ThreadGroup threadGroup) {
    Thread[] threads = new Thread[threadGroup.activeCount()];
    int numThreads = threadGroup.enumerate(threads);
    Collection<Thread> result = new ArrayList<Thread>(numThreads);
    for (int i = 0; i < threads.length && threads[i] != null; i++) {
        result.add(threads[i]);/* ww  w .jav  a2  s .com*/
    }
    return result; // note: result should be modifiable
}

From source file:org.zywx.wbpalmstar.engine.EBrowserActivity.java

public Thread[] findAllVMThreads() {
    ThreadGroup group = Thread.currentThread().getThreadGroup();
    ThreadGroup topGroup = group;
    while (group != null) {
        topGroup = group;//from  ww  w .  j  ava 2  s  .c  o m
        group = group.getParent();
    }
    int estimatedSize = topGroup.activeCount() * 2;
    Thread[] slackList = new Thread[estimatedSize];
    int actualSize = topGroup.enumerate(slackList);
    Thread[] list = new Thread[actualSize];
    System.arraycopy(slackList, 0, list, 0, actualSize);
    return list;
}

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

/**
 * Get a Collection with all Threads.//from  ww w. j a va 2 s  .  c  o m
 * This method is heavily inspired by org.apache.catalina.loader.WebappClassLoader.getThreads()
 */
protected Collection<Thread> getAllThreads() {
    // This is some orders of magnitude slower...
    // return Thread.getAllStackTraces().keySet();

    // Find root ThreadGroup
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    while (tg.getParent() != null)
        tg = tg.getParent();

    // Note that ThreadGroup.enumerate() silently ignores all threads that does not fit into array
    int guessThreadCount = tg.activeCount() + 50;
    Thread[] threads = new Thread[guessThreadCount];
    int actualThreadCount = tg.enumerate(threads);
    while (actualThreadCount == guessThreadCount) { // Map was filled, there may be more
        guessThreadCount *= 2;
        threads = new Thread[guessThreadCount];
        actualThreadCount = tg.enumerate(threads);
    }

    // Filter out nulls
    final List<Thread> output = new ArrayList<Thread>();
    for (Thread t : threads) {
        if (t != null) {
            output.add(t);
        }
    }
    return output;
}