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:Main.java

public static void dumpThreads() {

    ThreadGroup group = Thread.currentThread().getThreadGroup();
    int activeCount = group.activeCount();
    Thread[] threads = new Thread[activeCount];
    group.enumerate(threads);

    System.out.println("Thread-Group " + group + " contains " + activeCount + " threads");

    for (Thread thread : threads) {
        System.out.println("Thread " + thread);
    }//from w w w  . java 2  s.  c  o  m
}

From source file:Main.java

public static List<Thread> getAllThreads() {
    ThreadGroup rootThreadGroup = getRootThreadGroup();
    Thread[] threads = new Thread[256];
    int threadCount;
    while (true) {
        threadCount = rootThreadGroup.enumerate(threads);
        if (threadCount == threads.length) {
            // We probably missed threads; double the size of the array
            threads = new Thread[threads.length * 2];
        } else {//from  w  w  w .  ja  v a  2s  .c  o  m
            break;
        }
    }
    return Arrays.asList(threads).subList(0, threadCount);
}

From source file:Main.java

public static List<Thread> getGroupThreads(ThreadGroup threadGroup) {
    if (threadGroup == null) {
        return Collections.emptyList();
    }/*from   w  ww  . j a v a 2s. c om*/

    Thread[] threadArray = new Thread[threadGroup.activeCount() * 2];
    threadGroup.enumerate(threadArray);
    return Arrays.asList(threadArray);
}

From source file:Main.java

/**
 * Get all threads//from   w  ww.j  a  va2  s .c om
 * 
 * @return
 */
public static String[] getThreadNames() {
    ThreadGroup group = Thread.currentThread().getThreadGroup();
    ThreadGroup parent = null;
    while ((parent = group.getParent()) != null) {
        group = parent;
    }
    Thread[] threads = new Thread[group.activeCount()];
    group.enumerate(threads);
    HashSet<String> set = new HashSet<String>();

    for (int i = 0; i < threads.length; ++i) {
        if (threads[i] != null && threads[i].isAlive()) {
            try {
                set.add(threads[i].getThreadGroup().getName() + ", " + threads[i].getName() + ", "
                        + threads[i].getPriority() + ", " + threads[i].getState());
            } catch (Throwable e) {
                e.printStackTrace();
            }
        }
    }
    String[] result = (String[]) set.toArray(new String[0]);
    Arrays.sort(result);

    for (int i = 0; i < result.length; i++) {
        //         logger.debug(result[i]);
    }

    return result;
}

From source file:Main.java

public static Thread[] getAllThreadsInGroup(ThreadGroup group) {
    if (group != null) {
        int nAlloc = group.activeCount();
        int n = 0;
        Thread[] threads;//from   w ww  .j a  v a 2s  .co m

        do {
            nAlloc *= 2;
            threads = new Thread[nAlloc];
            n = group.enumerate(threads);
        } while (n == nAlloc);

        return java.util.Arrays.copyOf(threads, n);
    }

    return new Thread[0];
}

From source file:Main.java

public static Thread[] getThreads() {
    Thread currentThread = Thread.currentThread();

    ThreadGroup threadGroup = currentThread.getThreadGroup();

    while (threadGroup.getParent() != null) {
        threadGroup = threadGroup.getParent();
    }/*www  .j a  v a 2s .  c o  m*/

    int threadCountGuess = threadGroup.activeCount();

    Thread[] threads = new Thread[threadCountGuess];

    int threadCountActual = threadGroup.enumerate(threads);

    while (threadCountActual == threadCountGuess) {
        threadCountGuess *= 2;

        threads = new Thread[threadCountGuess];

        threadCountActual = threadGroup.enumerate(threads);
    }

    return threads;
}

From source file:org.nuxeo.runtime.management.jvm.ThreadDeadlocksDetector.java

protected static Map<Long, Thread> getThreads() {
    ThreadGroup root = rootGroup(Thread.currentThread().getThreadGroup());
    int nThreads = root.activeCount();
    Thread[] threads = new Thread[2 * nThreads];
    root.enumerate(threads);
    Map<Long, Thread> map = new HashMap<Long, Thread>(threads.length);
    for (Thread thread : threads) {
        if (thread == null) {
            continue;
        }//from w w  w .  j  a  va2s  .  c o  m
        map.put(thread.getId(), thread);
    }
    return map;
}

From source file:ThreadViewer.java

public static Thread[] findAllThreads() {
    ThreadGroup group = Thread.currentThread().getThreadGroup();

    ThreadGroup topGroup = group;

    while (group != null) {
        topGroup = group;/*w  w  w.  j a  v  a2s. c om*/
        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.googlecode.psiprobe.Utils.java

public static Thread getThreadByName(String name) {
    if (name != null) {
        ////  w w w .  j a  v a  2  s  .  c  o m
        // 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:org.springframework.data.hadoop.mapreduce.ExecutionUtils.java

/**
 * Returns the threads running inside the current JVM.
 * //from w  w  w .j  a  v a2 s . co  m
 * @return running threads
 */
static Thread[] threads() {
    // Could have used the code below but it tends to be somewhat ineffective and slow 
    // Set<Thread> threadSet = Thread.getAllStackTraces().keySet();

    // Get the current thread group 
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    // Find the root thread group
    while (tg.getParent() != null) {
        tg = tg.getParent();
    }

    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;
}