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.Utils.java

/**
 * Gets the thread by name.//  w  w w.j  a  va2s  . co  m
 *
 * @param name the name
 * @return the thread by name
 */
public static Thread getThreadByName(String name) {
    if (name != null) {
        // get top ThreadGroup
        ThreadGroup masterGroup = Thread.currentThread().getThreadGroup();
        while (masterGroup.getParent() != null) {
            masterGroup = masterGroup.getParent();
        }

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

        for (int i = 0; i < numThreads; i++) {
            if (threads[i] != null && name.equals(threads[i].getName())) {
                return threads[i];
            }
        }
    }
    return null;
}

From source file:com.gigaspaces.internal.utils.ClassLoaderCleaner.java

private static Thread[] getThreads() {
    // Get the current thread group
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    // Find the root thread group
    while (tg.getParent() != null) {
        tg = tg.getParent();//  www . ja v a2s  . co  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:org.psikeds.common.threadlocal.ThreadLocalHelper.java

/**
 * Get all active Threads of our Root-ThreadGroup.
 * /*  ww w  . java  2 s . c o  m*/
 * @return Array of Threads
 */
private static Thread[] getThreads() {
    int threadCountActual = 0;
    try {
        LOGGER.trace("--> getThreads()");
        ThreadGroup rootgroup = Thread.currentThread().getThreadGroup();
        while (rootgroup.getParent() != null) {
            rootgroup = rootgroup.getParent();
        }
        // Note: ThreadGroup.enumerate(Thread[]) silently ignores any Thread
        // that won't fit into the Array. Therefore we must make sure that
        // we do not miss any Threads by continuously increasing the Size of
        // the Array.
        Thread[] threads = null;
        int threadCountGuess = rootgroup.activeCount();
        do {
            threadCountGuess *= 2;
            threads = new Thread[threadCountGuess];
            threadCountActual = rootgroup.enumerate(threads);
        } while (threadCountActual == threadCountGuess);
        return threads;
    } finally {
        LOGGER.trace("<-- getThreads(); #Threads = {}", threadCountActual);
    }
}

From source file:EnumerateMain.java

public void listCurrentThreads() {
    ThreadGroup currentGroup = Thread.currentThread().getThreadGroup();
    int numThreads = currentGroup.activeCount();
    Thread[] listOfThreads = new Thread[numThreads];

    currentGroup.enumerate(listOfThreads);
    for (int i = 0; i < numThreads; i++)
        System.out.println("Thread #" + i + " = " + listOfThreads[i].getName());
}

From source file:net.sourceforge.vulcan.jabber.SmackKeepAliveThreadInterrupter.java

public void interrupt() {
    final ThreadGroup group = Thread.currentThread().getThreadGroup();

    final Thread[] threads = new Thread[group.activeCount()];

    group.enumerate(threads);

    for (Thread thread : threads) {
        if (!thread.getName().startsWith("Smack Keep Alive")) {
            continue;
        }//w w  w .j a va  2  s  . c  o m

        if (!thread.getContextClassLoader().equals(getClass().getClassLoader())) {
            // only wake up threads from our own class loader
            LOG.info("Not waking up " + thread.getName() + " because it uses a different class loader.");
            continue;
        }

        LOG.info("Interrupting " + thread.getName());

        thread.interrupt();

        try {
            thread.join(1000);
        } catch (InterruptedException ignore) {
        }

        if (thread.isAlive()) {
            LOG.error("Smack Keep Alive thread still alive after interruption.");
        }
    }
}

From source file:com.inmobi.grill.server.IndexResource.java

@GET
@Path("/admin/stack")
@Produces(MediaType.TEXT_PLAIN)//from  w w w . j ava  2 s.  co  m
public String 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();
    builder.append("Total number of threads:").append(nr).append("\n");
    for (int i = 0; i < nr; i++) {
        builder.append(threads[i].getName()).append("\n\tState: ").append(threads[i].getState()).append("\n");
        String stackTrace = StringUtils.join(threads[i].getStackTrace(), "\n");
        builder.append(stackTrace);
        builder.append("\n----------------------\n\n");
    }
    return builder.toString();
}

From source file:Main.java

public ThreadGroupDemo() {

    ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");

    ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");

    Thread t1 = new Thread(pGroup, this);
    System.out.println("Starting " + t1.getName());
    t1.start();/*from   w  w  w  .  j a  va  2  s  . c  o m*/

    Thread t2 = new Thread(cGroup, this);
    System.out.println("Starting " + t2.getName());
    t2.start();

    Thread[] list = new Thread[pGroup.activeCount()];
    int count = pGroup.enumerate(list);
    for (int i = 0; i < count; i++) {
        System.out.println("Thread " + list[i].getName() + " found");
    }

}

From source file:Main.java

public ThreadGroupDemo() {
    ThreadGroup pGroup = new ThreadGroup("Parent ThreadGroup");
    ThreadGroup cGroup = new ThreadGroup(pGroup, "Child ThreadGroup");
    Thread t1 = new Thread(pGroup, this);
    System.out.println("Starting " + t1.getName());
    t1.start();// ww  w  .  ja va2s.  c o m

    Thread t2 = new Thread(cGroup, this);
    System.out.println("Starting " + t2.getName());
    t2.start();

    ThreadGroup[] grpList = new ThreadGroup[pGroup.activeGroupCount()];
    int count = pGroup.enumerate(grpList);
    for (int i = 0; i < count; i++) {
        System.out.println("ThreadGroup " + grpList[i].getName() + " found");
    }
}

From source file:org.apache.falcon.resource.admin.AdminResource.java

@GET
@Path("stack")
@Produces(MediaType.TEXT_PLAIN)//w w w .  j  av a  2s . co  m
public String 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);
    }
    return builder.toString();
}

From source file:com.googlecode.psiprobe.controllers.threads.ListThreadsController.java

private List enumerateThreads(final Map classLoaderMap) {

    ////from   ww w  .  j  av a2s  .co  m
    // get top ThreadGroup
    //
    ThreadGroup masterGroup = Thread.currentThread().getThreadGroup();
    while (masterGroup.getParent() != null) {
        masterGroup = masterGroup.getParent();
    }

    //
    // enumerate all Threads starting from top
    //
    List 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;
}