Example usage for java.lang.management ThreadInfo getThreadName

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

Introduction

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

Prototype

public String getThreadName() 

Source Link

Document

Returns the name of the thread associated with this ThreadInfo .

Usage

From source file:Main.java

private static List<ThreadInfo> sort(ThreadInfo[] dumpAllThreads) {
    List<ThreadInfo> allThreads = new ArrayList<ThreadInfo>(Arrays.asList(dumpAllThreads));
    Collections.sort(allThreads, new Comparator<ThreadInfo>() {
        @Override/*from   w  ww. j ava 2s  .  com*/
        public int compare(ThreadInfo o1, ThreadInfo o2) {
            return o1.getThreadName().compareTo(o2.getThreadName());
        }
    });
    return allThreads;
}

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   ww w  . j a v a2s .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

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

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 ww  w  .ja v  a  2 s.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

private static void threadHeader(StringBuilder sb, ThreadInfo threadInfo) {
    final String threadName = threadInfo.getThreadName();
    sb.append("\"");
    sb.append(threadName);/* w ww  . ja  v a  2  s .c om*/
    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

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");
        }// ww w  .  ja  va 2s .co m
        sb.append("\n\n");
    }
}

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 .  jav a2  s  . 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

/**
 * Turns the given {@link ThreadInfo} into a {@link String}.
 *//*  w  w w  .j a  v a  2s.  c  om*/
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());
    }//from   www.  j av  a  2  s  . co 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

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