Example usage for java.lang.management ManagementFactory getThreadMXBean

List of usage examples for java.lang.management ManagementFactory getThreadMXBean

Introduction

In this page you can find the example usage for java.lang.management ManagementFactory getThreadMXBean.

Prototype

public static ThreadMXBean getThreadMXBean() 

Source Link

Document

Returns the managed bean for the thread system of the Java virtual machine.

Usage

From source file:Main.java

public static String[] getStackTrace(long threadID) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadID, Integer.MAX_VALUE);

    List<String> buf = new ArrayList<String>();

    if (threadInfo == null) {
        return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
    }//from  w  w  w.j a va  2s  . co m

    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        buf.add("\t" + ste.toString());

        if (i == 0 && threadInfo.getLockInfo() != null) {
            Thread.State ts = threadInfo.getThreadState();
            switch (ts) {
            case BLOCKED:
                buf.add("\t-  blocked on " + threadInfo.getLockInfo());
                break;
            case WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            case TIMED_WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            default:
            }
        }

        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                buf.add("\t-  locked " + mi);
            }
        }
    }

    LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        buf.add("\n\tNumber of locked synchronizers = " + locks.length);
        for (LockInfo li : locks) {
            buf.add("\t- " + li);
        }
    }

    return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
}

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.j a  va  2 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

/**
 * //  w  w  w . j a  v a  2s  . c  o  m
 */
public static long[] getDeadlocks() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.findDeadlockedThreads();
}

From source file:Main.java

/**
 * /*from   ww w  .ja v a  2 s .  co m*/
 */
public static ThreadInfo[] getThreadInfo(long[] ids) {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    return bean.getThreadInfo(ids, true, true);
}

From source file:Main.java

public static void threadDumps() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    long[] ids = bean.getAllThreadIds();
    StringBuilder builder = new StringBuilder();

    printTitle(builder);//from   w  w  w.  j a  v  a 2  s.  com

    printDeadlockInfo(builder, bean);

    for (long id : ids) {
        ThreadInfo info = bean.getThreadInfo(id);
        printThreadInfo(info, builder);
        printThreadSeparator(builder);
    }

    System.out.println(builder.toString());
}

From source file:Main.java

public static String getThreadStack(long id) {
    ThreadMXBean tmb = ManagementFactory.getThreadMXBean();
    ThreadInfo f = tmb.getThreadInfo(id, 500);
    if (f == null)
        return null;
    return getStackTrace(f.getStackTrace());
}

From source file:com.espertech.esper.util.MetricUtil.java

/**
 * Initialize metrics mgmt./*from   www  .  j  a  v a2  s. com*/
 */
public static void initialize() {
    threadMXBean = ManagementFactory.getThreadMXBean();
    isCPUEnabled = threadMXBean.isCurrentThreadCpuTimeSupported();

    if (!isCPUEnabled) {
        log.warn("CPU metrics reporting is not enabled by Java VM");
    }
}

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;/*ww  w  .  jav  a  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:com.espertech.esper.regression.client.MyMetricFunctions.java

public static boolean takeCPUTime(long nanoSecTarget) {
    if (nanoSecTarget < 100) {
        throw new RuntimeException("CPU time wait nsec less then zero, was " + nanoSecTarget);
    }/*from  ww  w.j a v a2  s . com*/

    ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
    if (!mbean.isThreadCpuTimeEnabled()) {
        throw new RuntimeException("ThreadMXBean CPU time reporting not enabled");
    }

    long before = mbean.getCurrentThreadCpuTime();

    while (true) {
        long after = mbean.getCurrentThreadCpuTime();
        long spent = after - before;
        if (spent > nanoSecTarget) {
            break;
        }
    }

    return true;
}

From source file:ThreadTimer.java

public static long getThreadTime(long tid) {
    ThreadMXBean tb = ManagementFactory.getThreadMXBean();
    return tb.getThreadCpuTime(tid);
}