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:com.google.gdt.eclipse.designer.hosted.tdt.HostedModeSupport.java

/**
 * @return array of {@link Thread}s, may be with <code>null</code> on the end.
 *///from w w w . j a va 2 s.c  om
private static Thread[] getAllThreads() {
    // prepare root ThreadGroup
    ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
    ThreadGroup parentGroup;
    while ((parentGroup = rootGroup.getParent()) != null) {
        rootGroup = parentGroup;
    }
    // fill Thread array
    Thread[] threads = new Thread[rootGroup.activeCount()];
    while (rootGroup.enumerate(threads, true) == threads.length) {
        threads = new Thread[threads.length * 2];
    }
    return threads;
}

From source file:net.bull.javamelody.internal.model.JavaInformations.java

static List<Thread> getThreadsFromThreadGroups() {
    ThreadGroup group = Thread.currentThread().getThreadGroup(); // NOPMD
    while (group.getParent() != null) {
        group = group.getParent();/*from   www .jav  a  2  s . c o  m*/
    }
    final Thread[] threadsArray = new Thread[group.activeCount()];
    group.enumerate(threadsArray, true);
    return Arrays.asList(threadsArray);
}

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

/**
 * Destroy the current instance of the classloader. Note**: After calling this and after the new
 * service is set up, All classes using this instance should be flushed. This would allow all
 * java classes that were loaded by the old instance variable to be gc'd and modules to load in
 * new java classes//from  w  w  w .ja va 2s .co  m
 *
 * @see #flushInstance()
 */
public static void destroyInstance() {

    // remove all thread references to this class
    // Walk up all the way to the root thread group
    ThreadGroup rootGroup = Thread.currentThread().getThreadGroup();
    ThreadGroup parent;
    while ((parent = rootGroup.getParent()) != null) {
        rootGroup = parent;
    }

    log.info("this classloader hashcode: " + OpenmrsClassLoaderHolder.INSTANCE.hashCode());

    //      List<Thread> threads = listThreads(rootGroup, "");
    //      for (Thread thread : threads) {
    //         if (thread.getContextClassLoader() != null) {
    //            log.debug("context classloader on thread: " + thread.getName() + " is: "
    //                    + thread.getContextClassLoader().getClass().getName() + ":"
    //                    + thread.getContextClassLoader().hashCode());
    //            if (thread.getContextClassLoader() == OpenmrsClassLoaderHolder.INSTANCE) {
    //               thread.setContextClassLoader(OpenmrsClassLoaderHolder.INSTANCE.getParent());
    //               log.error("Cleared context classloader to save the world from memory leaks. thread: " + thread.getName()
    //                       + " ");
    //            }
    //         }
    //      }

    //Shut down and remove all cache managers.
    List<CacheManager> knownCacheManagers = CacheManager.ALL_CACHE_MANAGERS;
    while (!knownCacheManagers.isEmpty()) {
        CacheManager cacheManager = CacheManager.ALL_CACHE_MANAGERS.get(0);
        try {
            //This shuts down and removes the cache manager.
            cacheManager.shutdown();

            //Just in case the the timer does not stop, set the cacheManager 
            //timer to null because it references this class loader.
            Field field = cacheManager.getClass().getDeclaredField("cacheManagerTimer");
            field.setAccessible(true);
            field.set(cacheManager, null);
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
        }
    }

    OpenmrsClassScanner.destroyInstance();

    OpenmrsClassLoaderHolder.INSTANCE = null;
}

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

@GET
@Path("/admin/stack")
@Produces(MediaType.TEXT_PLAIN)//from w w w.  j  a v  a2  s  .c  o 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:com.googlecode.psiprobe.controllers.threads.ListThreadsController.java

private List enumerateThreads(final Map classLoaderMap) {

    ////from  w  w w. j  ava  2s .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;
}

From source file:net.testdriven.psiprobe.controllers.threads.ListThreadsController.java

private List enumerateThreads(final Map classLoaderMap) {

    ///*from  w  w w.j  a va2 s .com*/
    // 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:psiprobe.controllers.threads.ListThreadsController.java

/**
 * Enumerate threads.//from   ww w  .  j a va  2 s  .co  m
 *
 * @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.symbian.driver.core.processors.HardwarePostProcessor.java

/**
 * @see java.lang.Thread#run()/*from   ww w  .  ja  va  2  s .  c o m*/
 */
public void run() {
    LOGGER.info("Stopping and cleaning after TestDriver.");
    try {
        Runtime.getRuntime().removeShutdownHook(this);
    } catch (Exception lException) {
        LOGGER.fine("Didn't succefully deregister the shutdownhook.");
    }

    //uninstall tef before stop communication channel
    try {
        if (iTefDeps != null) {
            TDConfig CONFIG = TDConfig.getInstance();
            boolean lPlatSec = true;
            try {
                //lPlatSec = CONFIG.isPreference(TDConfig.PLATSEC) && !CONFIG.isPreference(TDConfig.SYS_BIN);
                lPlatSec = !CONFIG.isPreference(TDConfig.SYS_BIN);
            } catch (ParseException e) {
                LOGGER.log(Level.WARNING, "Could not get the configuration for PlatSec. Defaulting to ON");
            }
            if (lPlatSec) {
                iTefDeps.uninstall();
                LOGGER.fine("TEF dependencies package was successfully removed!");
            }
        }
    } catch (TimeLimitExceededException e) {
        LOGGER.log(Level.WARNING, " Warning: TEF dependencies package " + "was not successfully removed!");
    }

    try {
        DeviceCommsProxy.getInstance().stop(false);
    } catch (Exception lException) {
        LOGGER.log(Level.SEVERE, " Error ", lException);
    }

    // Move RDebug to the results folder
    if (iRDebugThread != null) {
        LOGGER.fine("Killing RDebug.");
        iRDebugThread.setM_Life(false);
    }

    // Stop all remaing JStat Threads
    ThreadGroup lRootThread = Thread.currentThread().getThreadGroup().getParent();
    while (lRootThread.getParent() != null) {
        lRootThread = lRootThread.getParent();
    }

    // Get all threads
    Thread[] lThreads = new Thread[50];
    int lNumThreads = lRootThread.enumerate(lThreads, true);

    LOGGER.fine(
            "The root thread is: " + lRootThread.getName() + "; and has " + lNumThreads + " children threads.");

    // Find any JStat threads remaining
    for (int lIter = 0; lIter < lNumThreads; lIter++) {
        LOGGER.fine("Looking at Thread: " + lThreads[lIter].getName());
        if (lThreads[lIter].getName().indexOf("JStat") >= 0) {
            //|| lThreads[lIter].getName().indexOf("Timer") >= 0) {
            LOGGER.log(Level.SEVERE,
                    "Could not stop all JStat Threads therefore killing TestDriver. Please check your Hardware or Emulator for failures.");
        }
    }

    LOGGER.exiting(HardwarePostProcessor.class.getName(), "run");
}

From source file:org.apache.pulsar.common.stats.JvmMetrics.java

private long getThreadCount() {
    // get top level thread group to track active thread count
    ThreadGroup parentThreadGroup = Thread.currentThread().getThreadGroup();

    while (parentThreadGroup.getParent() != null) {
        parentThreadGroup = parentThreadGroup.getParent();
    }/*from  w w w. ja  va 2  s  .  c  o  m*/

    return parentThreadGroup.activeCount();
}

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

@GET
@Path("stack")
@Produces(MediaType.TEXT_PLAIN)/* w  w  w.  j  a v  a 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();
    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();
}