Example usage for java.lang.management ThreadInfo getStackTrace

List of usage examples for java.lang.management ThreadInfo getStackTrace

Introduction

In this page you can find the example usage for java.lang.management ThreadInfo getStackTrace.

Prototype

public StackTraceElement[] getStackTrace() 

Source Link

Document

Returns the stack trace of the thread associated with this ThreadInfo .

Usage

From source file:org.apache.hadoop.util.ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 *
 * @param stream the stream to/*from w  w w. j a  va2  s .  c om*/
 * @param title a string title for the stack trace
 */
public static synchronized void printThreadInfo(PrintWriter stream, String title) {
    final int stackDepth = 20;
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid : threadIds) {
        ThreadInfo info = threadBean.getThreadInfo(tid, stackDepth);
        if (info == null) {
            stream.println("  Inactive");
            continue;
        }
        stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");
        Thread.State state = info.getThreadState();
        stream.println("  State: " + state);
        stream.println("  Blocked count: " + info.getBlockedCount());
        stream.println("  Waited count: " + info.getWaitedCount());
        if (contention) {
            stream.println("  Blocked time: " + info.getBlockedTime());
            stream.println("  Waited time: " + info.getWaitedTime());
        }
        if (state == Thread.State.WAITING) {
            stream.println("  Waiting on " + info.getLockName());
        } else if (state == Thread.State.BLOCKED) {
            stream.println("  Blocked on " + info.getLockName());
            stream.println("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
        }
        stream.println("  Stack:");
        for (StackTraceElement frame : info.getStackTrace()) {
            stream.println("    " + frame.toString());
        }
    }
    stream.flush();
}

From source file:com.buaa.cfs.utils.ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 *
 * @param stream the stream to/*from  w ww.  ja v a2 s.c  om*/
 * @param title  a string title for the stack trace
 */
public synchronized static void printThreadInfo(PrintStream stream, String title) {
    final int STACK_DEPTH = 20;
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid : threadIds) {
        ThreadInfo info = threadBean.getThreadInfo(tid, STACK_DEPTH);
        if (info == null) {
            stream.println("  Inactive");
            continue;
        }
        stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");
        Thread.State state = info.getThreadState();
        stream.println("  State: " + state);
        stream.println("  Blocked count: " + info.getBlockedCount());
        stream.println("  Waited count: " + info.getWaitedCount());
        if (contention) {
            stream.println("  Blocked time: " + info.getBlockedTime());
            stream.println("  Waited time: " + info.getWaitedTime());
        }
        if (state == Thread.State.WAITING) {
            stream.println("  Waiting on " + info.getLockName());
        } else if (state == Thread.State.BLOCKED) {
            stream.println("  Blocked on " + info.getLockName());
            stream.println("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
        }
        stream.println("  Stack:");
        for (StackTraceElement frame : info.getStackTrace()) {
            stream.println("    " + frame.toString());
        }
    }
    stream.flush();
}

From source file:com.alicloud.tablestore.adaptor.client.IntegratedTest.java

public static void printThreadInfo() {
    String title = "Automatic Stack Trace";
    PrintWriter stream = new PrintWriter(System.out);
    int STACK_DEPTH = 20;
    boolean contention = threadBean.isThreadContentionMonitoringEnabled();
    long[] threadIds = threadBean.getAllThreadIds();
    stream.println("Process Thread Dump: " + title);
    stream.println(threadIds.length + " active threads");
    for (long tid : threadIds) {
        ThreadInfo info = threadBean.getThreadInfo(tid, 20);
        if (info == null) {
            stream.println("  Inactive");
        } else {//from   w w w  .  j av  a 2 s  . c  o  m
            stream.println("Thread " + getTaskName(info.getThreadId(), info.getThreadName()) + ":");

            Thread.State state = info.getThreadState();
            stream.println("  State: " + state);
            stream.println("  Blocked count: " + info.getBlockedCount());
            stream.println("  Waited count: " + info.getWaitedCount());
            if (contention) {
                stream.println("  Blocked time: " + info.getBlockedTime());
                stream.println("  Waited time: " + info.getWaitedTime());
            }
            if (state == Thread.State.WAITING) {
                stream.println("  Waiting on " + info.getLockName());
            } else if (state == Thread.State.BLOCKED) {
                stream.println("  Blocked on " + info.getLockName());
                stream.println("  Blocked by " + getTaskName(info.getLockOwnerId(), info.getLockOwnerName()));
            }

            stream.println("  Stack:");
            for (StackTraceElement frame : info.getStackTrace())
                stream.println("    " + frame.toString());
        }
    }
    stream.flush();
}

From source file:serposcope.controllers.admin.AdminController.java

public Result stackdump(Context context) {

    return Results.contentType("text/plain").render((ctx, res) -> {
        ResponseStreams responseStreams = context.finalizeHeaders(res);
        try (PrintWriter writer = new PrintWriter(responseStreams.getOutputStream());) {
            final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
            for (ThreadInfo threadInfo : threadInfos) {
                writer.append('"');
                writer.append(threadInfo.getThreadName());
                writer.append("\" ");
                final Thread.State state = threadInfo.getThreadState();
                writer.append("\n   java.lang.Thread.State: ");
                writer.append(state.toString());
                final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
                for (final StackTraceElement stackTraceElement : stackTraceElements) {
                    writer.append("\n        at ");
                    writer.append(stackTraceElement.toString());
                }//  w w w.  j a  va2 s .c om
                writer.println("\n");
            }
        } catch (IOException ex) {
            LOG.error("stackdump", ex);
        }
    });
}

From source file:fullThreadDump.java

private String printThreadInfo(ThreadInfo ti) {
    // print stack trace with locks
    StackTraceElement[] stacktrace = ti.getStackTrace();
    MonitorInfo[] monitors = ti.getLockedMonitors();
    currentThreadInfo result = new currentThreadInfo(ti);
    StringBuilder threadOutput = new StringBuilder(result.getThreadName());
    threadOutput.append(result.getThreadStateDesc());
    if (stats == true) {
        threadOutput.append(result.getThreadStats());
    }//from   ww w  .  j  av a2s.co  m
    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        if (i == 0) {
            threadOutput.append("\n    java.lang.Thread.State: " + result.getThreadState());
            threadOutput.append("\n" + INDENT + "at " + ste.toString());
            if (ste.toString().contains("java.lang.Object.wait(Native Method)")
                    && result.getLockName() != null) {
                threadOutput.append("\n" + INDENT + "- waiting on " + result.getLockName());
            }
            if (ste.toString().contains("sun.misc.Unsafe.park(Native Method)")
                    && result.getLockName() != null) {
                threadOutput.append("\n" + INDENT + "- parking to wait for " + result.getLockName());
            }
            if (result.getThreadStateDesc().contains("BLOCKED") && result.getLockName() != null) {
                threadOutput.append("\n" + INDENT + "- waiting to lock " + result.getLockName());
            }
        } else {
            threadOutput.append("\n" + INDENT + "at " + ste.toString());
        }
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                threadOutput.append("\n" + INDENT + " - locked " + mi);
            }
        }
    }
    threadOutput.append("\n");
    //System.out.print(threadOutput.toString());
    //printMonitorInfo(ti, monitors);
    return threadOutput.toString();
}

From source file:org.jahia.tools.jvm.ThreadMonitor.java

private void printThreadInfo(ThreadInfo ti, StringBuilder dump) {
    // print thread information
    printThread(ti, dump);/*from www.  j a v  a  2 s .com*/

    // print stack trace with locks
    StackTraceElement[] stacktrace = ti.getStackTrace();
    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        dump.append(INDENT + "at " + ste.toString());
        dump.append("\n");
    }
    dump.append("\n");
}

From source file:morphy.service.ThreadService.java

/**
 * Dumps stack traces of all threads to threaddump.txt.
 *//* w  w  w .j a v  a2 s  .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) {
            }
        }
    }
}

From source file:Main.java

private static void formatState(final StringBuilder sb, final ThreadInfo info) {
    final Thread.State state = info.getThreadState();
    sb.append(state);/*from w  w w .  j  av  a  2s . c o m*/
    switch (state) {
    case BLOCKED: {
        sb.append(" (on object monitor owned by \"");
        sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId()).append(")");
        break;
    }
    case WAITING: {
        final StackTraceElement element = getStackForThread(info.getThreadId())[0];
        final String className = element.getClassName();
        final String method = element.getMethodName();
        if (className.equals("java.lang.Object") && method.equals("wait")) {
            sb.append(" (on object monitor");
            if (info.getLockOwnerName() != null) {
                sb.append(" owned by \"");
                sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId());
            }
            sb.append(")");
        } else if (className.equals("java.lang.Thread") && method.equals("join")) {
            sb.append(" (on completion of thread ").append(info.getLockOwnerId()).append(")");
        } else {
            sb.append(" (parking for lock");
            if (info.getLockOwnerName() != null) {
                sb.append(" owned by \"");
                sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId());
            }
            sb.append(")");
        }
        break;
    }
    case TIMED_WAITING: {
        final StackTraceElement element = info.getStackTrace()[0];
        final String className = element.getClassName();
        final String method = element.getMethodName();
        if (className.equals("java.lang.Object") && method.equals("wait")) {
            sb.append(" (on object monitor");
            if (info.getLockOwnerName() != null) {
                sb.append(" owned by \"");
                sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId());
            }
            sb.append(")");
        } else if (className.equals("java.lang.Thread") && method.equals("sleep")) {
            sb.append(" (sleeping)");
        } else if (className.equals("java.lang.Thread") && method.equals("join")) {
            sb.append(" (on completion of thread ").append(info.getLockOwnerId()).append(")");
        } else {
            sb.append(" (parking for lock");
            if (info.getLockOwnerName() != null) {
                sb.append(" owned by \"");
                sb.append(info.getLockOwnerName()).append("\" Id=").append(info.getLockOwnerId());
            }
            sb.append(")");
        }
        break;
    }
    default:
        break;
    }
}

From source file:org.apache.synapse.commons.jmx.ThreadingView.java

private boolean isBlocked(ThreadInfo threadInfo) {
    // A thread is considered "Blocked" if it is in the BLOCKED state
    // or if it is in the WAITING state due to some reason other than
    // 'parking'.
    Thread.State state = threadInfo.getThreadState();
    if (state.equals(Thread.State.BLOCKED)) {
        return true;
    } else if (state.equals(Thread.State.WAITING) || state.equals(Thread.State.TIMED_WAITING)) {
        StackTraceElement[] stackTrace = threadInfo.getStackTrace();
        if (stackTrace.length > 0 && !"park".equals(stackTrace[0].getMethodName())) {
            return true;
        }//from   w w w.j av  a 2 s .  c o  m
    }

    return false;
}

From source file:hudson.Functions.java

@IgnoreJRERequirement
public static String dumpThreadInfo(ThreadInfo ti) {
    StringBuilder sb = new StringBuilder(
            "\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " " + ti.getThreadState());
    if (ti.getLockName() != null) {
        sb.append(" on " + ti.getLockName());
    }//from ww w.ja v  a 2 s. c  om
    if (ti.getLockOwnerName() != null) {
        sb.append(" owned by \"" + ti.getLockOwnerName() + "\" Id=" + ti.getLockOwnerId());
    }
    if (ti.isSuspended()) {
        sb.append(" (suspended)");
    }
    if (ti.isInNative()) {
        sb.append(" (in native)");
    }
    sb.append('\n');
    StackTraceElement[] stackTrace = ti.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        sb.append("\tat " + ste.toString());
        sb.append('\n');
        if (i == 0 && ti.getLockInfo() != null) {
            Thread.State ts = ti.getThreadState();
            switch (ts) {
            case BLOCKED:
                sb.append("\t-  blocked on " + ti.getLockInfo());
                sb.append('\n');
                break;
            case WAITING:
                sb.append("\t-  waiting on " + ti.getLockInfo());
                sb.append('\n');
                break;
            case TIMED_WAITING:
                sb.append("\t-  waiting on " + ti.getLockInfo());
                sb.append('\n');
                break;
            default:
            }
        }

        for (MonitorInfo mi : ti.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }

    LockInfo[] locks = ti.getLockedSynchronizers();
    if (locks.length > 0) {
        sb.append("\n\tNumber of locked synchronizers = " + locks.length);
        sb.append('\n');
        for (LockInfo li : locks) {
            sb.append("\t- " + li);
            sb.append('\n');
        }
    }
    sb.append('\n');
    return sb.toString();
}