Example usage for java.lang.management MonitorInfo getLockedStackDepth

List of usage examples for java.lang.management MonitorInfo getLockedStackDepth

Introduction

In this page you can find the example usage for java.lang.management MonitorInfo getLockedStackDepth.

Prototype

public int getLockedStackDepth() 

Source Link

Document

Returns the depth in the stack trace where the object monitor was locked.

Usage

From source file:Main.java

/** Add more specific locking details. */
private static void addMonitorInfo(ThreadInfo info, StringBuilder sb, int stackDepth) {
    MonitorInfo[] monitorInfos = info.getLockedMonitors();
    for (int i = 0; i < monitorInfos.length; i++) {
        MonitorInfo mi = monitorInfos[i];
        int depth = mi.getLockedStackDepth();
        if (depth == stackDepth) {
            sb.append("\t-  locked " + mi + "\n");
        }//  w  ww . java  2  s .  c o m
    }
}

From source file:Main.java

public static String[] getStackTrace(long threadID) {
    ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
    ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadID, Integer.MAX_VALUE);

    List<String> buf = new ArrayList<String>();

    if (threadInfo == null) {
        return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
    }//  w w  w  . j  ava 2s  . c  om

    StackTraceElement[] stackTrace = threadInfo.getStackTrace();
    for (int i = 0; i < stackTrace.length; i++) {
        StackTraceElement ste = stackTrace[i];
        buf.add("\t" + ste.toString());

        if (i == 0 && threadInfo.getLockInfo() != null) {
            Thread.State ts = threadInfo.getThreadState();
            switch (ts) {
            case BLOCKED:
                buf.add("\t-  blocked on " + threadInfo.getLockInfo());
                break;
            case WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            case TIMED_WAITING:
                buf.add("\t-  waiting on " + threadInfo.getLockInfo());
                break;
            default:
            }
        }

        for (MonitorInfo mi : threadInfo.getLockedMonitors()) {
            if (mi.getLockedStackDepth() == i) {
                buf.add("\t-  locked " + mi);
            }
        }
    }

    LockInfo[] locks = threadInfo.getLockedSynchronizers();
    if (locks.length > 0) {
        buf.add("\n\tNumber of locked synchronizers = " + locks.length);
        for (LockInfo li : locks) {
            buf.add("\t- " + li);
        }
    }

    return buf.toArray((String[]) Array.newInstance(String.class, buf.size()));
}

From source file:Main.java

/**
 * Returns the names of the locks this {@link Thread} has locked.
 * /*  w w  w. ja va 2  s.c  o m*/
 * @see ThreadInfo#getLockedMonitors()
 */
private static String getLockedMonitors(ThreadInfo info, int stackIndex) {
    StringBuilder buffer = new StringBuilder();

    MonitorInfo[] monitors = info.getLockedMonitors();
    for (MonitorInfo monitor : monitors) {
        int depth = monitor.getLockedStackDepth();
        if (depth == stackIndex) {
            buffer.append("\t-  locked ").append(monitor).append("\n");
        }
    }
    return buffer.toString();
}

From source file:Main.java

public static void printStack(ThreadInfo info, final StringBuilder sb, final StackTraceElement[] stack) {
    int i = 0;/*from  www .  ja  v  a  2  s . c o m*/
    for (final StackTraceElement element : stack) {
        sb.append("\tat ").append(element.toString());
        sb.append('\n');
        if (i == 0 && info.getLockInfo() != null) {
            final Thread.State ts = info.getThreadState();
            switch (ts) {
            case BLOCKED:
                sb.append("\t-  blocked on ");
                formatLock(sb, info.getLockInfo());
                sb.append('\n');
                break;
            case WAITING:
                sb.append("\t-  waiting on ");
                formatLock(sb, info.getLockInfo());
                sb.append('\n');
                break;
            case TIMED_WAITING:
                sb.append("\t-  waiting on ");
                formatLock(sb, info.getLockInfo());
                sb.append('\n');
                break;
            default:
            }
        }

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

    final LockInfo[] locks = info.getLockedSynchronizers();
    if (locks.length > 0) {
        sb.append("\n\tNumber of locked synchronizers = ").append(locks.length).append('\n');
        for (final LockInfo li : locks) {
            sb.append("\t- ");
            formatLock(sb, li);
            sb.append('\n');
        }
    }
}

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());
    }// ww w  .  j a v  a2 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;
    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  ww w  .j  ava  2 s.  com*/
    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:org.apache.bookkeeper.common.testing.util.TimedOutTestsListener.java

private static void printThreadInfo(ThreadInfo ti, PrintWriter out) {
    // print thread information
    printThread(ti, out);//w  ww.  j a va2  s.c  o m

    // print stack trace with locks
    StackTraceElement[] stacktrace = ti.getStackTrace();
    MonitorInfo[] monitors = ti.getLockedMonitors();
    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        out.println(indent + "at " + ste.toString());
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                out.println(indent + "  - locked " + mi);
            }
        }
    }
    out.println();
}

From source file:com.l2jfree.lang.L2Thread.java

public static List<String> getStats(Thread t) {
    List<String> list = new FastList<String>();

    list.add(t.toString() + " - ID: " + t.getId());
    list.add(" * State: " + t.getState());
    list.add(" * Alive: " + t.isAlive());
    list.add(" * Daemon: " + t.isDaemon());
    list.add(" * Interrupted: " + t.isInterrupted());
    for (ThreadInfo info : ManagementFactory.getThreadMXBean().getThreadInfo(new long[] { t.getId() }, true,
            true)) {/*from   ww w  . j  a  v  a  2s.  c  om*/
        for (MonitorInfo monitorInfo : info.getLockedMonitors()) {
            list.add("==========");
            list.add(" * Locked monitor: " + monitorInfo);
            list.add("\t[" + monitorInfo.getLockedStackDepth() + ".]: at " + monitorInfo.getLockedStackFrame());
        }

        for (LockInfo lockInfo : info.getLockedSynchronizers()) {
            list.add("==========");
            list.add(" * Locked synchronizer: " + lockInfo);
        }

        list.add("==========");
        for (StackTraceElement trace : info.getStackTrace())
            list.add("\tat " + trace);
    }

    return list;
}

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

/**
 * Copy of {@link ThreadInfo#toString()} but with the MAX_FRAMES changed from 8 to 256.
 *///  ww  w  .j ava  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();
}

From source file:DeadlockDetector.java

private void printThreadInfo(ThreadInfo threadInfo) {
    printThread(threadInfo);// w w  w . j av a 2  s  .  co m
    sb.append(INDENT + threadInfo.toString() + "\n");
    StackTraceElement[] stacktrace = threadInfo.getStackTrace();
    MonitorInfo[] monitors = threadInfo.getLockedMonitors();

    for (int i = 0; i < stacktrace.length; i++) {
        StackTraceElement ste = stacktrace[i];
        sb.append(INDENT + "at " + ste.toString() + "\n");
        for (MonitorInfo mi : monitors) {
            if (mi.getLockedStackDepth() == i) {
                sb.append(INDENT + "  - locked " + mi + "\n");
            }
        }
    }
}