Example usage for java.lang.management ThreadInfo getThreadState

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

Introduction

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

Prototype

public Thread.State getThreadState() 

Source Link

Document

Returns the state of the thread associated with this ThreadInfo .

Usage

From source file:Main.java

/** Add the LockInfo data to the report. */
private static void addLockInfo(ThreadInfo info, StringBuilder sb) {
    Thread.State ts = info.getThreadState();
    switch (ts) {
    case BLOCKED:
        sb.append("\t-  blocked on " + info.getLockInfo() + "\n");
        break;//from   www .j a va 2s .  com
    case WAITING:
    case TIMED_WAITING:
        sb.append("\t-  waiting on " + info.getLockInfo() + "\n");
        break;
    default:
    }
}

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();
    }//from  w  ww. j  a  v a  2 s .co  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

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  .  jav a2s  .c  om

    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

/** 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;/*from w w w.j  a v a2s. c o 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

/**
 * Returns the names of the locks this {@link Thread} 
 * is blocking or waiting on.//from w  w w .j  a  v  a2  s .co m
 * 
 * @see ThreadInfo#getLockInfo()
 * @see ThreadInfo#getThreadState()
 */
private static String getLockState(ThreadInfo info) {
    StringBuilder buffer = new StringBuilder();

    LockInfo lockInfo = info.getLockInfo();
    Thread.State state = info.getThreadState();

    switch (state) {
    case BLOCKED:
        buffer.append("\t-  blocked on ").append(lockInfo).append("\n");
        break;
    case WAITING:
    case TIMED_WAITING:
        buffer.append("\t-  waiting on ").append(lockInfo).append("\n");
        break;
    default:
        break;
    }

    return buffer.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 av  a 2 s .  c  o m*/
    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

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());
    }//from  www .j ava  2 s.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;
    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:Main.java

public static String gerThreadDump(Thread thread) {

    Preconditions.checkNotNull(thread);/*from  w w  w  .  ja  v  a2 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();
}

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());
    }//www .  j a v a 2 s  . 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;
    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 formatState(final StringBuilder sb, final ThreadInfo info) {
    final Thread.State state = info.getThreadState();
    sb.append(state);//from   w  w w . jav a  2 s  .  co 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;
    }
}