Example usage for java.lang ThreadGroup getParent

List of usage examples for java.lang ThreadGroup getParent

Introduction

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

Prototype

public final ThreadGroup getParent() 

Source Link

Document

Returns the parent of this thread group.

Usage

From source file:ThreadLister.java

/** Find the root thread group and list it recursively */
public static void listAllThreads(PrintWriter out) {
    ThreadGroup current_thread_group;
    ThreadGroup root_thread_group;
    ThreadGroup parent;//ww  w.j  a va2 s  .  c  om

    // Get the current thread group
    current_thread_group = Thread.currentThread().getThreadGroup();

    // Now go find the root thread group
    root_thread_group = current_thread_group;
    parent = root_thread_group.getParent();
    while (parent != null) {
        root_thread_group = parent;
        parent = parent.getParent();
    }

    // And list it, recursively
    printGroupInfo(out, root_thread_group, "");
}

From source file:Main.java

/**
 * Get the root thread group in the thread group tree. Since there is always
 * a root thread group, this method never returns null.
 * //from w  w w  . j  av  a2 s. c om
 * @return the root thread group
 */
public static ThreadGroup getRootThreadGroup() {
    if (rootThreadGroup != null) {
        return rootThreadGroup;
    }
    ThreadGroup tg = Thread.currentThread().getThreadGroup();
    ThreadGroup ptg;
    while ((ptg = tg.getParent()) != null) {
        tg = ptg;
    }
    rootThreadGroup = tg;
    return tg;
}

From source file:ThreadViewer.java

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

    ThreadGroup topGroup = group;

    while (group != null) {
        topGroup = group;/*from   w  ww  .ja  va2  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:es.darkhogg.hazelnutt.Hazelnutt.java

/**
 * Terminates the application in at most <i>time</i> milliseconds for
 * every alive thread. /*w  w  w .j av  a  2s .c o  m*/
 * 
 * @param time Number of milliseconds to wait for each thread to terminate
 */
public static void terminate(long time) {
    Logger logger = getLogger();
    logger.info("Terminating application...");

    try {
        getFrame().dispose();

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

        // Declare some collections
        Queue<ThreadGroup> threadGroups = new LinkedList<ThreadGroup>();
        Queue<Thread> threads = new LinkedList<Thread>();

        // Get ALL groups
        threadGroups.add(rootThreadGroup);
        while (!threadGroups.isEmpty()) {
            ThreadGroup group = threadGroups.remove();

            Thread[] subThreads = new Thread[group.activeCount() * 2];
            //group.enumerate( subThreads );
            for (Thread subThread : subThreads) {
                if (subThread != null) {
                    threads.add(subThread);
                }
            }

            ThreadGroup[] subThreadGroups = new ThreadGroup[group.activeGroupCount() * 2];
            for (ThreadGroup subThreadGroup : subThreadGroups) {
                if (subThreadGroup != null) {
                    threadGroups.add(subThreadGroup);
                }
            }
        }

        // Join a maximum of time milliseconds for all non-daemon threads
        while (!threads.isEmpty()) {
            Thread thread = threads.remove();
            LOGGER.trace(thread);

            if (!thread.isDaemon() && thread != Thread.currentThread()) {
                logger.trace("Waiting for thread '" + thread.getName() + "'");
                thread.join(time);
                if (thread.isAlive()) {
                    logger.trace("Interrupting thread '" + thread.getName() + "'");
                    thread.interrupt();
                }
            }
        }

    } catch (Throwable e) {
        LOGGER.warn("Interrupted while terminating application", e);

    } finally {
        // Exit the program
        System.exit(0);
    }
}

From source file:org.psikeds.common.threadlocal.ThreadLocalHelper.java

/**
 * Get all active Threads of our Root-ThreadGroup.
 * /*from  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:com.googlecode.psiprobe.Utils.java

public static Thread getThreadByName(String name) {
    if (name != null) {
        ////from   w  w w.  j av  a  2 s.  co  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.
 * /* w w  w . ja va 2  s  . c o  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;
}

From source file:psiprobe.Utils.java

/**
 * Gets the thread by name./* w ww.jav a  2 s .c  o  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();/*from  www  .  java2 s.  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:ioheater.ui.IOHeaterUI.java

/**
 * Utility method for checking active threads.
 *///w  w  w  . j ava 2 s  .  c  o m
public static void checkThreads() {
    ThreadGroup mainThreadGroup = Thread.currentThread().getThreadGroup();
    ThreadGroup systemThreadGroup = mainThreadGroup.getParent();

    logger.log(Level.INFO, "Current thread: {0}", Thread.currentThread());
    systemThreadGroup.list();
}