Example usage for java.lang.management ThreadInfo getLockOwnerName

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

Introduction

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

Prototype

public String getLockOwnerName() 

Source Link

Document

Returns the name of the thread which owns the object for which the thread associated with this ThreadInfo is blocked waiting.

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();
    }//from w w w.  jav  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

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

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());
    }//www .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;
    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.  ja  va 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

/**
 * Turns the given {@link ThreadInfo} into a {@link String}.
 *///from  w  w  w. jav a 2 s . 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

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

private static void threadHeader(StringBuilder sb, ThreadInfo threadInfo) {
    final String threadName = threadInfo.getThreadName();
    sb.append("\"");
    sb.append(threadName);//from  ww w.  j  a  v  a2 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:EventDispatchThreadHangMonitor.java

private static void checkForDeadlock() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadBean.findMonitorDeadlockedThreads();
    if (threadIds == null) {
        return;// w  ww .  java 2  s  .co m
    }
    Log.warn("deadlock detected involving the following threads:");
    ThreadInfo[] threadInfos = threadBean.getThreadInfo(threadIds, Integer.MAX_VALUE);
    for (ThreadInfo info : threadInfos) {
        Log.warn("Thread #" + info.getThreadId() + " " + info.getThreadName() + " (" + info.getThreadState()
                + ") waiting on " + info.getLockName() + " held by " + info.getLockOwnerName()
                + stackTraceToString(info.getStackTrace()));
    }
}

From source file:Main.java

public static void printThreadInfo(ThreadInfo info, StringBuilder builder) {
    builder.append("Thread ").append(info.getThreadName()).append("\" (").append(info.getThreadId())
            .append(") -- ");
    formatState(builder, info);//from www.j  a v  a  2s. c  o  m
    if (info.isSuspended()) {
        builder.append(" (suspended)");
    }
    if (info.isInNative()) {
        builder.append(" (in native)");
    }
    builder.append("\n");

    // The waited lock.
    LockInfo li = info.getLockInfo();
    if (li != null) {
        builder.append("Waiting for ");
        formatLock(builder, li);
        if (info.getLockOwnerName() != null) {
            builder.append(" owned by ").append(info.getLockOwnerName()).append("(")
                    .append(info.getLockOwnerId()).append(")");
        }
        builder.append("\n");
    }

    // The stack
    builder.append("Stack:\n");
    printStack(info, builder, getStackForThread(info.getThreadId()));
}

From source file:ReflectionUtils.java

/**
 * Print all of the thread's information and stack traces.
 * //from w  w  w.  jav  a  2 s . co  m
 * @param stream
 *          the stream to
 * @param title
 *          a string title for the stack trace
 */
public static void printThreadInfo(PrintWriter 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();
}