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:gate.Main.java

/** Main routine for GATE.
  * Command-line arguments://  w ww  . ja  va2 s .c o  m
  * <UL>
  * <LI>
  * <B>-h</B> display a short help message
  * <LI>
  * <B>-d URL</B> define URL to be a location for CREOLE resoures
  * <LI>
  * <B>-i file</B> additional initialisation file (probably called
  *   <TT>gate.xml</TT>). Used for site-wide initialisation by the
  *   start-up scripts
  * </UL>
  */
public static void main(String[] args) throws GateException {

    // check we have a useable JDK
    if (System.getProperty("java.version").compareTo(Gate.getMinJdkVersion()) < 0) {
        throw new GateException("GATE requires JDK " + Gate.getMinJdkVersion() + " or newer");
    }

    ThreadWarningSystem tws = new ThreadWarningSystem();
    tws.addListener(new ThreadWarningSystem.Listener() {

        final PrintStream out = System.out;

        @Override
        public void deadlockDetected(ThreadInfo inf) {
            out.println("Deadlocked Thread:");
            out.println("------------------");
            out.println(inf);
            for (StackTraceElement ste : inf.getStackTrace()) {
                out.println("\t" + ste);
            }
        }

        @Override
        public void thresholdExceeded(ThreadInfo[] threads) {
        }
    });

    // process command-line options
    processArgs(args);

    // GATE builtins should be loaded from the jar (or classes dir), not
    // from a web server (we load them over the web during testing to
    // make sure that users can load their own that way)
    Gate.setNetConnected(false);
    Gate.setLocalWebServer(false);

    runGui();
}

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:Main.java

protected static void _printThreads(ThreadMXBean bean, long[] ids, StringBuilder sb) {
    ThreadInfo[] threads = bean.getThreadInfo(ids, 20);
    for (ThreadInfo info : threads) {
        if (info == null)
            continue;
        sb.append(info.getThreadName()).append(":\n");
        StackTraceElement[] stack_trace = info.getStackTrace();
        for (StackTraceElement el : stack_trace) {
            sb.append("    at ").append(el.getClassName()).append(".").append(el.getMethodName());
            sb.append("(").append(el.getFileName()).append(":").append(el.getLineNumber()).append(")");
            sb.append("\n");
        }//from w ww  .ja v  a  2s .  c o m
        sb.append("\n\n");
    }
}

From source file:org.apache.hadoop.mapred.JettyBugMonitor.java

/**
 * @return true if the given thread ID appears to be a Jetty selector thread
 * based on its stack trace/*from   w  ww  .j av  a 2  s.c om*/
 */
private static boolean isJettySelectorThread(long tid) {
    ThreadInfo info = threadBean.getThreadInfo(tid, 20);
    for (StackTraceElement stack : info.getStackTrace()) {
        // compare class names instead of classses, since
        // jetty uses a different classloader
        if (SelectChannelConnector.class.getName().equals(stack.getClassName())) {
            LOG.debug("Thread #" + tid + " (" + info.getThreadName() + ") " + "is a Jetty selector thread.");
            return true;
        }
    }
    LOG.debug("Thread #" + tid + " (" + info.getThreadName() + ") " + "is not a jetty thread");
    return false;
}

From source file:org.apache.bookkeeper.common.testing.util.TimedOutTestsListener.java

private static void printThreadInfo(ThreadInfo ti, PrintWriter out) {
    // print thread information
    printThread(ti, out);/*from   w  w w .j ava2 s. co m*/

    // print stack trace with locks
    StackTraceElement[] stacktrace = ti.getStackTrace();
    MonitorInfo[] monitors = ti.getLockedMonitors();
    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        out.println(indent + "at " + ste.toString());
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                out.println(indent + "  - locked " + mi);
            }
        }
    }
    out.println();
}

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.  jav a 2s . c om*/

    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: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 ww.  ja  va  2s. 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

public static String gerThreadDump(Thread thread) {

    Preconditions.checkNotNull(thread);//from w  w  w  .  ja  va2  s  .  com

    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();
}

From source file:org.apache.metron.common.utils.ErrorUtils.java

public static String generateThreadDump() {
    final StringBuilder dump = new StringBuilder();
    final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
    for (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);//from  w  w  w .  j av  a 2 s. c om
        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:Main.java

public static String takeDumpJSON(ThreadMXBean threadMXBean) throws IOException {
    ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true);
    List<Map<String, Object>> threads = new ArrayList<>();

    for (ThreadInfo thread : threadInfos) {
        Map<String, Object> threadMap = new HashMap<>();
        threadMap.put("name", thread.getThreadName());
        threadMap.put("id", thread.getThreadId());
        threadMap.put("state", thread.getThreadState().name());
        List<String> stacktrace = new ArrayList<>();
        for (StackTraceElement element : thread.getStackTrace()) {
            stacktrace.add(element.toString());
        }//w  w w  .j  av a 2s .co  m
        threadMap.put("stack", stacktrace);

        if (thread.getLockName() != null) {
            threadMap.put("lock_name", thread.getLockName());
        }
        if (thread.getLockOwnerId() != -1) {
            threadMap.put("lock_owner_id", thread.getLockOwnerId());
        }
        if (thread.getBlockedTime() > 0) {
            threadMap.put("blocked_time", thread.getBlockedTime());
        }
        if (thread.getBlockedCount() > 0) {
            threadMap.put("blocked_count", thread.getBlockedCount());
        }
        if (thread.getLockedMonitors().length > 0) {
            threadMap.put("locked_monitors", thread.getLockedMonitors());
        }
        if (thread.getLockedSynchronizers().length > 0) {
            threadMap.put("locked_synchronizers", thread.getLockedSynchronizers());
        }
        threads.add(threadMap);
    }
    ObjectMapper om = new ObjectMapper();
    ObjectNode json = om.createObjectNode();
    json.put("date", new Date().toString());
    json.putPOJO("threads", threads);

    long[] deadlockedThreads = threadMXBean.findDeadlockedThreads();
    long[] monitorDeadlockedThreads = threadMXBean.findMonitorDeadlockedThreads();
    if (deadlockedThreads != null && deadlockedThreads.length > 0) {
        json.putPOJO("deadlocked_thread_ids", deadlockedThreads);
    }
    if (monitorDeadlockedThreads != null && monitorDeadlockedThreads.length > 0) {
        json.putPOJO("monitor_deadlocked_thread_ids", monitorDeadlockedThreads);
    }
    om.enable(SerializationFeature.INDENT_OUTPUT);
    return om.writerWithDefaultPrettyPrinter().writeValueAsString(json);
}