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:io.pcp.parfait.benchmark.StandardMetricThroughPutBenchmark.java

public static void main(String[] args) throws InterruptedException, UnknownHostException {
    int numThreads = args.length < 1 ? Runtime.getRuntime().availableProcessors() : Integer.valueOf(args[0]);
    int numCounters = 1000;
    int iterations = 10000;

    ReportHelper.environmentReportHeader();
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    System.out.printf("Thread Contention Supported: %s, Enabled by default: %s\n",
            threadMXBean.isThreadContentionMonitoringSupported(),
            threadMXBean.isThreadContentionMonitoringEnabled());
    System.out.printf("numThreads: %d, numCounters=%d, iterations=%d\n", numThreads, numCounters, iterations);

    new StandardMetricThroughPutBenchmark(numThreads, numCounters, iterations, false, false).runBenchmark();
    new StandardMetricThroughPutBenchmark(numThreads, numCounters, iterations, true, false).runBenchmark();
    new StandardMetricThroughPutBenchmark(numThreads, numCounters, iterations, true, true).runBenchmark();

    // now do it all again reverse, in case there's any JVM warmup issues unfairly treating the first/last runs
    new StandardMetricThroughPutBenchmark(numThreads, numCounters, iterations, true, true).runBenchmark();
    new StandardMetricThroughPutBenchmark(numThreads, numCounters, iterations, true, false).runBenchmark();
    new StandardMetricThroughPutBenchmark(numThreads, numCounters, iterations, false, false).runBenchmark();
}

From source file:Main.java

public static String dumpAllThreads() {
    ThreadInfo[] dumps = ManagementFactory.getThreadMXBean().dumpAllThreads(true, true);
    StringBuilder out = new StringBuilder("Thread Dump\n");
    for (ThreadInfo dump : dumps) {
        out.append("-------------------------\n").append(dump);
    }//from  w w  w. j av  a  2s  .  c  o m

    return out.toString();
}

From source file:Main.java

public static String getDeadlockedThreads() {
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    long[] deadlocked = bean.findMonitorDeadlockedThreads();
    if (deadlocked == null || deadlocked.length == 0) {
        return "";
    }//  w ww.  j a v  a 2s  . co  m

    String buf = "Deadlocked thread IDs: ";
    for (long id : deadlocked) {
        buf += id + " ";
    }

    return buf;
}

From source file:Main.java

public static String getThreadHeadline(long threadID) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadID, Integer.MAX_VALUE);
    return getThreadHeadline(threadInfo);
}

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 a  v a  2  s  . co  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

public static String dumpThreads() {
    StringBuilder sb = new StringBuilder();
    ThreadMXBean bean = ManagementFactory.getThreadMXBean();
    long[] ids = bean.getAllThreadIds();
    _printThreads(bean, ids, sb);/*  www  .  j  a  v a  2 s  .  c om*/
    long[] deadlocks = bean.findDeadlockedThreads();
    if (deadlocks != null && deadlocks.length > 0) {
        sb.append("deadlocked threads:\n");
        _printThreads(bean, deadlocks, sb);
    }

    deadlocks = bean.findMonitorDeadlockedThreads();
    if (deadlocks != null && deadlocks.length > 0) {
        sb.append("monitor deadlocked threads:\n");
        _printThreads(bean, deadlocks, sb);
    }
    return sb.toString();
}

From source file:Main.java

public static String getAllThreadDumps() {

    final StringBuilder dump = new StringBuilder();

    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

    final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    dump.append(lineSeperator);//from w w  w .j a  v a2s.c o m

    for (ThreadInfo threadInfo : threadInfos) {
        dump.append('"');
        dump.append(threadInfo.getThreadName());
        dump.append("\" ");
        final Thread.State state = threadInfo.getThreadState();
        dump.append(lineSeperator);
        dump.append("   State: ");
        dump.append(state);
        final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
        for (final StackTraceElement stackTraceElement : stackTraceElements) {
            dump.append(lineSeperator);
            dump.append("        at ");
            dump.append(stackTraceElement);
        }
        dump.append(lineSeperator);
        dump.append(lineSeperator);
    }
    return dump.toString();
}

From source file:Main.java

/** Returns all stack traces, including lock info. */
public static String getAllStackTraces() {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    boolean monitor = threadMXBean.isObjectMonitorUsageSupported();
    boolean sync = threadMXBean.isSynchronizerUsageSupported();
    ThreadInfo[] allThreadInfo = threadMXBean.dumpAllThreads(monitor, sync);
    StringBuilder sb = new StringBuilder("Stack Trace Report:\n");
    buildTrace(allThreadInfo, sb);/*from   w  w w  . jav a2  s  .  co  m*/
    return sb.toString();
}

From source file:Main.java

/** Builds the stack traces of all the given threadIDs in the buffer.  Returns the stack trace of the first thread. */
public static StackTraceElement[] buildStackTraces(long[] threadIds, StringBuilder buffer) {
    ThreadInfo[] allThreadInfo = ManagementFactory.getThreadMXBean().getThreadInfo(threadIds, true, true);
    return buildTrace(allThreadInfo, buffer);
}

From source file:Main.java

public static String gerThreadDump(Thread thread) {

    Preconditions.checkNotNull(thread);//from  w  w w. j  av a  2  s  .c o  m

    StringBuffer dump = new StringBuffer(200);

    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

    ThreadInfo threadInfo = threadMXBean.getThreadInfo(thread.getId(), 100);

    dump.append(lineSeperator);

    dump.append('"');
    dump.append(threadInfo.getThreadName());
    dump.append("\" ");
    final Thread.State state = threadInfo.getThreadState();
    dump.append(lineSeperator);
    dump.append("   State: ");
    dump.append(state);
    final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
    for (final StackTraceElement stackTraceElement : stackTraceElements) {
        dump.append(lineSeperator);
        dump.append("        at ");
        dump.append(stackTraceElement);
    }
    dump.append(lineSeperator);

    return dump.toString();
}