Example usage for java.lang.management ThreadInfo getThreadId

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

Introduction

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

Prototype

public long getThreadId() 

Source Link

Document

Returns the ID of the thread associated with this ThreadInfo .

Usage

From source file:Main.java

public static String getThreadHeadline(ThreadInfo threadInfo) {
    String buf = "\"" + threadInfo.getThreadName() + "\"" + " Id=" + threadInfo.getThreadId() + " "
            + threadInfo.getThreadState();

    if (threadInfo.getLockName() != null) {
        buf += " on " + threadInfo.getLockName();
    }/*ww  w .j  a v  a  2  s . c  o  m*/

    if (threadInfo.getLockOwnerName() != null) {
        buf += " owned by \"" + threadInfo.getLockOwnerName() + "\" Id=" + threadInfo.getLockOwnerId();
    }

    if (threadInfo.isSuspended()) {
        buf += " (suspended)";
    }

    if (threadInfo.isInNative()) {
        buf += " (in native)";
    }

    return buf;
}

From source file:Main.java

private static String getThreadHead(ThreadInfo f) {
    StringBuffer sb = new StringBuffer();
    sb.append('"').append(f.getThreadName()).append('"');
    sb.append(" tid=0x" + Long.toHexString(f.getThreadId()));
    sb.append(" native=" + f.isInNative());
    sb.append(" suspended=" + f.isSuspended());
    return sb.toString();
}

From source file:Main.java

/** Builds the stack traces of all the given ThreadInfos in the buffer.  Returns the stack trace of the first thread. */
private static StackTraceElement[] buildTrace(ThreadInfo[] allThreadInfo, StringBuilder buffer) {
    StackTraceElement[] firstStackTrace = null;
    for (ThreadInfo info : allThreadInfo) {
        buffer.append("\"" + info.getThreadName() + "\" (id=" + info.getThreadId() + ")");
        buffer.append(" " + info.getThreadState() + " on " + info.getLockName() + " owned by ");
        buffer.append("\"" + info.getLockOwnerName() + "\" (id=" + info.getLockOwnerId() + ")");
        if (info.isSuspended())
            buffer.append(" (suspended)");
        if (info.isInNative())
            buffer.append(" (in native)");
        buffer.append("\n");
        StackTraceElement[] trace = info.getStackTrace();
        if (firstStackTrace == null)
            firstStackTrace = trace;/*  w  w  w. j  av  a2 s.  co  m*/
        for (int i = 0; i < trace.length; i++) {
            buffer.append("\tat " + trace[i].toString() + "\n");
            if (i == 0)
                addLockInfo(info, buffer);
            addMonitorInfo(info, buffer, i);
        }

        addLockedSynchronizers(info, buffer);

        buffer.append("\n");
    }
    return firstStackTrace;
}

From source file:Main.java

/**
 * Turns the given {@link ThreadInfo} into a {@link String}.
 *//*from   w w  w  . j  a v a  2s.  co m*/
public static String toString(ThreadInfo[] threads) {
    StringBuilder buffer = new StringBuilder();

    for (ThreadInfo info : threads) {
        buffer.append("\"").append(info.getThreadName()).append("\" (id=").append(info.getThreadId())
                .append(")");

        buffer.append(" ").append(info.getThreadState()).append(" on ").append(info.getLockName())
                .append(" owned by ");

        buffer.append("\"").append(info.getLockOwnerName()).append("\" (id=").append(info.getLockOwnerId())
                .append(")");

        if (info.isSuspended()) {
            buffer.append(" (suspended)");
        }

        if (info.isInNative()) {
            buffer.append(" (is native)");
        }
        buffer.append("\n");

        StackTraceElement[] trace = info.getStackTrace();
        for (int i = 0; i < trace.length; i++) {
            buffer.append("\tat ").append(trace[i]).append("\n");

            if (i == 0) {
                buffer.append(getLockState(info));
            }

            buffer.append(getLockedMonitors(info, i));
        }

        buffer.append("\n");
    }

    return buffer.toString();
}

From source file:Main.java

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

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

    LockInfo[] locks = threadInfo.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();
}

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

private static void printThread(ThreadInfo ti, PrintWriter out) {
    out.print("\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " in " + ti.getThreadState());
    if (ti.getLockName() != null) {
        out.print(" on lock=" + ti.getLockName());
    }// w  w w.j a v a 2 s .  c  om
    if (ti.isSuspended()) {
        out.print(" (suspended)");
    }
    if (ti.isInNative()) {
        out.print(" (running in native)");
    }
    out.println();
    if (ti.getLockOwnerName() != null) {
        out.println(indent + " owned by " + ti.getLockOwnerName() + " Id=" + ti.getLockOwnerId());
    }
}

From source file:Main.java

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

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

    LockInfo[] locks = threadInfo.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();
}

From source file:Main.java

private static void threadHeader(StringBuilder sb, ThreadInfo threadInfo) {
    final String threadName = threadInfo.getThreadName();
    sb.append("\"");
    sb.append(threadName);/*from  w w  w  .j av  a 2s . c o  m*/
    sb.append("\" ");
    sb.append("Id=");
    sb.append(threadInfo.getThreadId());

    try {
        final Thread.State threadState = threadInfo.getThreadState();
        final String lockName = threadInfo.getLockName();
        final String lockOwnerName = threadInfo.getLockOwnerName();
        final Long lockOwnerId = threadInfo.getLockOwnerId();
        final Boolean isSuspended = threadInfo.isSuspended();
        final Boolean isInNative = threadInfo.isInNative();

        sb.append(" ");
        sb.append(threadState);
        if (lockName != null) {
            sb.append(" on ");
            sb.append(lockName);
        }
        if (lockOwnerName != null) {
            sb.append(" owned by \"");
            sb.append(lockOwnerName);
            sb.append("\" Id=");
            sb.append(lockOwnerId);
        }
        if (isSuspended) {
            sb.append(" (suspended)");
        }
        if (isInNative) {
            sb.append(" (in native)");
        }
    } catch (final Exception e) {
        sb.append(" ( Got exception : ").append(e.getMessage()).append(" :");
    }

    sb.append('\n');
}

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());
        }//from  w w w .  ja  v a  2 s. c  om
        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);
}

From source file:org.apache.tajo.master.querymaster.QueryMasterRunner.java

public static void printThreadInfo(PrintWriter stream, String title) {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    final int STACK_DEPTH = 60;
    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;
        }//w  ww  .j  a  v  a2s . 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();
}