Example usage for java.lang.management ThreadInfo getLockedSynchronizers

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

Introduction

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

Prototype

public LockInfo[] getLockedSynchronizers() 

Source Link

Document

Returns an array of LockInfo objects, each of which represents an ownable synchronizer currently locked by the thread associated with this ThreadInfo .

Usage

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

static String buildDeadlockInfo() {
    ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
    long[] threadIds = threadBean.findMonitorDeadlockedThreads();
    if (threadIds != null && threadIds.length > 0) {
        StringWriter stringWriter = new StringWriter();
        PrintWriter out = new PrintWriter(stringWriter);

        ThreadInfo[] infos = threadBean.getThreadInfo(threadIds, true, true);
        for (ThreadInfo ti : infos) {
            printThreadInfo(ti, out);//from  ww  w  .  ja  va2 s.  c  o m
            printLockInfo(ti.getLockedSynchronizers(), out);
            out.println();
        }

        out.close();
        return stringWriter.toString();
    } else {
        return null;
    }
}

From source file:password.pwm.util.java.JavaHelper.java

/**
 * Copy of {@link ThreadInfo#toString()} but with the MAX_FRAMES changed from 8 to 256.
 *///  w w w  . java  2 s. c om
public static String threadInfoToString(final ThreadInfo threadInfo) {
    final int MAX_FRAMES = 256;
    final StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id="
            + threadInfo.getThreadId() + " " + threadInfo.getThreadState());
    if (threadInfo.getLockName() != null) {
        sb.append(" on " + threadInfo.getLockName());
    }
    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 counter = 0;
    for (; counter < threadInfo.getStackTrace().length && counter < MAX_FRAMES; counter++) {
        final StackTraceElement ste = threadInfo.getStackTrace()[counter];
        sb.append("\tat ").append(ste.toString());
        sb.append('\n');
        if (counter == 0 && threadInfo.getLockInfo() != null) {
            final 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() == counter) {
                sb.append("\t-  locked " + mi);
                sb.append('\n');
            }
        }
    }
    if (counter < threadInfo.getStackTrace().length) {
        sb.append("\t...");
        sb.append('\n');
    }

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