Example usage for java.lang.management ThreadMXBean getAllThreadIds

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

Introduction

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

Prototype

public long[] getAllThreadIds();

Source Link

Document

Returns all live thread IDs.

Usage

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);/*  ww  w. j ava 2 s. com*/
    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

private static boolean threadExists(String namepart) {
    ThreadMXBean threadmx = ManagementFactory.getThreadMXBean();
    ThreadInfo[] infos = threadmx.getThreadInfo(threadmx.getAllThreadIds());
    for (ThreadInfo info : infos) {
        if (info != null) { // see javadoc getThreadInfo (thread can have disappeared between the two calls)
            if (info.getThreadName().contains(namepart)) {
                return true;
            }//  w  w w . jav  a  2s .  c o  m
        }
    }
    return false;
}

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);/*  www .  j  a  v a 2s  .com*/

    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

private static void printDeadlockInfo(StringBuilder builder, ThreadMXBean bean) {
    long[] ids = bean.getAllThreadIds();
    long[] dds = bean.findDeadlockedThreads();
    if (dds != null) {
        ThreadInfo[] info = bean.getThreadInfo(ids, true, true);
        builder.append("The following threads are deadlocked:");
        for (ThreadInfo ti : info) {
            builder.append("\t").append(ti);
        }/*  ww  w.j  a va2s .  com*/
    }
}

From source file:Main.java

/**
 * Get a list of all thread info objects.  Since there is
 * always at least one thread running, there is always at
 * least one thread info object.  This method never returns
 * a null or empty array.//from w  w  w .  j a  v  a 2s.com
 *
 * @return      an array of thread infos
 */
public static ThreadInfo[] getAllThreadInfos() {
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    final long[] ids = thbean.getAllThreadIds();

    // Get thread info with lock info, when available.
    ThreadInfo[] infos;
    if (!thbean.isObjectMonitorUsageSupported() || !thbean.isSynchronizerUsageSupported())
        infos = thbean.getThreadInfo(ids);
    else
        infos = thbean.getThreadInfo(ids, true, true);

    // Clean nulls from array if threads have died.
    final ThreadInfo[] notNulls = new ThreadInfo[infos.length];
    int nNotNulls = 0;
    for (ThreadInfo info : infos)
        if (info != null)
            notNulls[nNotNulls++] = info;
    if (nNotNulls == infos.length)
        return infos; // Original had no nulls
    return java.util.Arrays.copyOf(notNulls, nNotNulls);
}

From source file:Main.java

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

    printTitle(builder);/*w  ww .ja  va 2 s  .c o  m*/

    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

/**
 * Get a list of all thread info objects. Since there is always at least one
 * thread running, there is always at least one thread info object. This
 * method never returns a null or empty array.
 * //from  w ww  . ja  v a 2s.c o m
 * @return an array of thread infos
 */
public static ThreadInfo[] getAllThreadInfos() {
    final ThreadMXBean thbean = ManagementFactory.getThreadMXBean();
    final long[] ids = thbean.getAllThreadIds();

    // Get thread info with lock info, when available.
    ThreadInfo[] infos;
    if (!thbean.isObjectMonitorUsageSupported() || !thbean.isSynchronizerUsageSupported()) {
        infos = thbean.getThreadInfo(ids);
    } else {
        infos = thbean.getThreadInfo(ids, true, true);
    }

    // Clean nulls from array if threads have died.
    final ThreadInfo[] notNulls = new ThreadInfo[infos.length];
    int nNotNulls = 0;
    for (ThreadInfo info : infos) {
        if (info != null) {
            notNulls[nNotNulls++] = info;
        }
    }
    if (nNotNulls == infos.length)
        return infos; // Original had no nulls
    return java.util.Arrays.copyOf(notNulls, nNotNulls);
}

From source file:backtype.storm.utils.Utils.java

public static String threadDump() {
    final StringBuilder dump = new StringBuilder();
    final java.lang.management.ThreadMXBean threadMXBean = java.lang.management.ManagementFactory
            .getThreadMXBean();//from   w  w  w  .ja  v  a2  s.c  om
    final java.lang.management.ThreadInfo[] threadInfos = threadMXBean
            .getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    for (java.lang.management.ThreadInfo threadInfo : threadInfos) {
        dump.append('"');
        dump.append(threadInfo.getThreadName());
        dump.append("\" ");
        final Thread.State state = threadInfo.getThreadState();
        dump.append("\n   java.lang.Thread.State: ");
        dump.append(state);
        final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
        for (final StackTraceElement stackTraceElement : stackTraceElements) {
            dump.append("\n        at ");
            dump.append(stackTraceElement);
        }
        dump.append("\n\n");
    }
    return dump.toString();
}

From source file:hudson.Functions.java

@IgnoreJRERequirement
public static ThreadInfo[] getThreadInfos() {
    ThreadMXBean mbean = ManagementFactory.getThreadMXBean();
    return mbean.getThreadInfo(mbean.getAllThreadIds(), mbean.isObjectMonitorUsageSupported(),
            mbean.isSynchronizerUsageSupported());
}

From source file:morphy.service.ThreadService.java

/**
 * Dumps stack traces of all threads to threaddump.txt.
 *///from   w ww. jav  a  2s. c o m
public void threadDump() {
    LOG.error("All threads are in use. Logging the thread stack trace to threaddump.txt and exiting.");
    final ThreadMXBean threads = ManagementFactory.getThreadMXBean();
    long[] threadIds = threads.getAllThreadIds();
    PrintWriter printWriter = null;
    try {
        printWriter = new PrintWriter(new FileWriter(THREAD_DUMP_FILE_PATH, false));
        printWriter.println("Morphy ThreadService initiated dump " + new Date());
        for (long threadId : threadIds) {
            ThreadInfo threadInfo = threads.getThreadInfo(threadId, 10);
            printWriter.println("Thread " + threadInfo.getThreadName() + " Block time:"
                    + threadInfo.getBlockedTime() + " Block count:" + threadInfo.getBlockedCount()
                    + " Lock name:" + threadInfo.getLockName() + " Waited Count:" + threadInfo.getWaitedCount()
                    + " Waited Time:" + threadInfo.getWaitedTime() + " Is Suspended:"
                    + threadInfo.isSuspended());
            StackTraceElement[] stackTrace = threadInfo.getStackTrace();
            for (StackTraceElement element : stackTrace) {
                printWriter.println(element);
            }

        }
    } catch (IOException ioe) {
        ioe.printStackTrace();
    } finally {
        if (printWriter != null) {
            try {
                printWriter.flush();
                printWriter.close();
            } catch (Exception e2) {
            }
        }
    }
}