Example usage for java.lang.management ThreadMXBean getThreadCount

List of usage examples for java.lang.management ThreadMXBean getThreadCount

Introduction

In this page you can find the example usage for java.lang.management ThreadMXBean getThreadCount.

Prototype

public int getThreadCount();

Source Link

Document

Returns the current number of live threads including both daemon and non-daemon threads.

Usage

From source file:MainClass.java

public static void premain(final Instrumentation inst) {
    Runtime.getRuntime().addShutdownHook(new Thread() {
        public void run() {
            try {
                PrintWriter out = new PrintWriter(System.err);

                ThreadMXBean tb = ManagementFactory.getThreadMXBean();
                out.printf("Current thread count: %d%n", tb.getThreadCount());
                out.printf("Peak thread count: %d%n", tb.getPeakThreadCount());

                List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
                for (MemoryPoolMXBean pool : pools) {
                    MemoryUsage peak = pool.getPeakUsage();
                    out.printf("Peak %s memory used: %,d%n", pool.getName(), peak.getUsed());
                    out.printf("Peak %s memory reserved: %,d%n", pool.getName(), peak.getCommitted());
                }//from   w w  w  .ja v  a2 s  .co  m

                Class[] loaded = inst.getAllLoadedClasses();
                out.println("Loaded classes:");
                for (Class c : loaded)
                    out.println(c.getName());
                out.close();
            } catch (Throwable t) {
                System.err.println("Exception in agent: " + t);
            }
        }
    });
}

From source file:Main.java

public static Thread[] getAllThreads() {

    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();

    int nAlloc = thbean.getThreadCount();
    int n = 0;//w w  w .j a  v  a  2 s .c om
    Thread[] threads;

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

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

From source file:Main.java

public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;//from   www  .j  av a 2  s  . c  o  m
    Thread[] threads;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return java.util.Arrays.copyOf(threads, n);
}

From source file:Main.java

/**
 * Gets all threads in the JVM.  This is really a snapshot of all threads
 * at the time this method is called.//w  w  w  .j a v a  2 s . c  o  m
 * @return An array of all threads currently running in the JVM.
 */
static public Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;
    Thread[] threads;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return java.util.Arrays.copyOf(threads, n);
}

From source file:Main.java

public static Thread[] getAllThreads() {
    final ThreadGroup rootThreadGroup = getRootThreadGroup();
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

    int nAlloc = threadMXBean.getThreadCount();
    int n = 0;/*from ww w  .  j a va  2 s . c o m*/
    Thread[] threads;

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

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

From source file:Main.java

/** Returns a list of all threads.
 *
 * @return a list of all threads//w w  w. j av a 2s. co m
 */
public static List<Thread> getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    @SuppressWarnings("UnusedAssignment")
    int n = 0;
    Thread[] threads;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    final List<Thread> threadList = new ArrayList<>();
    for (final Thread thread : threads) {
        if (thread != null) {
            threadList.add(thread);
        }
    }
    return threadList;
}

From source file:Main.java

public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();

    int nAlloc = thbean.getThreadCount();
    int n = 0;/*from ww  w. java 2s. c  o m*/

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

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

From source file:Main.java

/**
 * Get a list of all threads.  Since there is always at
 * least one thread, this method never returns null or
 * an empty array./*www.  java2 s . c  o m*/
 *
 * @return      an array of threads
 */
public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;
    Thread[] threads = null;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return threads.clone();
}

From source file:ro.nextreports.server.web.debug.InfoUtil.java

public static List<Info> getGeneralJVMInfo() {
    List<Info> infos = new ArrayList<Info>();

    RuntimeMXBean runtimeBean = ManagementFactory.getRuntimeMXBean();
    infos.add(new Info("uptime", "" + Duration.milliseconds(runtimeBean.getUptime()).toString()));
    infos.add(new Info("name", runtimeBean.getName()));
    infos.add(new Info("pid", runtimeBean.getName().split("@")[0]));

    OperatingSystemMXBean systemBean = ManagementFactory.getOperatingSystemMXBean();
    infos.add(new Info("os name", "" + systemBean.getName()));
    infos.add(new Info("os version", "" + systemBean.getVersion()));
    infos.add(new Info("system load average", "" + systemBean.getSystemLoadAverage()));
    infos.add(new Info("available processors", "" + systemBean.getAvailableProcessors()));

    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    infos.add(new Info("thread count", "" + threadBean.getThreadCount()));
    infos.add(new Info("peak thread count", "" + threadBean.getPeakThreadCount()));

    MemoryMXBean memoryBean = ManagementFactory.getMemoryMXBean();
    infos.add(new Info("heap memory used",
            FileUtils.byteCountToDisplaySize(memoryBean.getHeapMemoryUsage().getUsed())));
    infos.add(new Info("non-heap memory used",
            FileUtils.byteCountToDisplaySize(memoryBean.getNonHeapMemoryUsage().getUsed())));

    return infos;
}

From source file:Main.java

/**
 * Get a list of all threads. Since there is always at least one thread,
 * this method never returns null or an empty array.
 * //www .ja  v  a2 s. c  om
 * @return an array of threads
 */
public static Thread[] getAllThreads() {
    final ThreadGroup root = getRootThreadGroup();
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    int nAlloc = thbean.getThreadCount();
    int n = 0;
    Thread[] threads = null;
    do {
        nAlloc *= 2;
        threads = new Thread[nAlloc];
        n = root.enumerate(threads, true);
    } while (n == nAlloc);
    return java.util.Arrays.copyOf(threads, n);
}