使用ThreadMXBean进行线程转储时,没有得到“等待锁定”和“锁定”的详细信息

时间:2019-02-23 07:07:32

标签: java deadlock thread-dump jstack

我正在使用以下代码生成threaddump。生成的输出无需等待锁定和锁定的资源详细信息。 当我们使用jstack时,我们将获得锁定的资源以及它正在等待的资源的详细信息。

代码:

private StringBuilder appendThreadDumbs(final StringBuilder dumbs, final ThreadInfo[] threadInfos) {
    for (ThreadInfo threadInfo : threadInfos) {
        if (threadInfo.getThreadName().contains("Thread-")) {
            MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors();
            System.out.println(threadInfo.getThreadName() + " - locked monitors length = " + lockedMonitors.length);
            for (MonitorInfo monitorInfo : lockedMonitors) {
                System.out.println(threadInfo.getThreadName() + " - LockedMOnitorStackFrame :" + monitorInfo.getLockedStackFrame());
                System.out.println(threadInfo.getThreadName() + " - LockedMonitorepth " + monitorInfo.getLockedStackDepth());
            }
            LockInfo lockInfo = threadInfo.getLockInfo();
            if (lockInfo != null) {
                System.out.println(threadInfo.getThreadName() + " - LOCK info - " + lockInfo + " - " + lockInfo.toString() + " - " + lockInfo.getIdentityHashCode());
            } else {
                System.out.println(threadInfo.getThreadName() + " - LCOK INFO IS NULL");
            }
            LockInfo[] lockedSynchronizors = threadInfo.getLockedSynchronizers();
            System.out.println(threadInfo.getThreadName() + " - locked synchronizors length = " + lockedSynchronizors.length);
            for (LockInfo lockInfo2 : lockedSynchronizors) {
                System.out.println(threadInfo.getThreadName() + " - Locked synchronization " + lockInfo2);
            }
            System.out.println(threadInfo.getThreadName() + " - LOCKED SYNCHRONIZORS + " + threadInfo.getLockedSynchronizers());
            dumbs.append('"' + threadInfo.getThreadName() + " - " + threadInfo.getThreadId() + "\"");
            dumbs.append("\nLockName:" + threadInfo.getLockName() + " Lock Owner:" + threadInfo.getLockOwnerName() + " Lock Owner id:" + threadInfo.getLockOwnerId());
            dumbs.append("\nBlocked count:" + threadInfo.getBlockedCount() + " Blocked time:" + threadInfo.getBlockedTime());
            dumbs.append(" Waited count:" + threadInfo.getWaitedCount() + " Waited time:" + threadInfo.getWaitedTime());
            final Thread.State state = threadInfo.getThreadState();
            dumbs.append("\njava.lang.Thread.State: " + state);
            final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
            for (final StackTraceElement stackTraceElement : stackTraceElements) {
                dumbs.append("\n    at ");
                dumbs.append(stackTraceElement);
            }
            dumbs.append("\n\n");
        }
    }
    return dumbs;
}

输出:

"Thread-1 - 12"
LockName:java.lang.Object@288130de Lock Owner:Thread-0 Lock Owner id:11
Blocked count:1 Blocked time:-1 Waited count:1 Waited time:-1
java.lang.Thread.State: BLOCKED
    at DeadlockProgram$DeadlockRunnable.run(DeadlockProgram.java:38)
    at java.lang.Thread.run(Thread.java:748)

"Thread-0 - 11"
LockName:java.lang.Object@251340d3 Lock Owner:Thread-1 Lock Owner id:12
Blocked count:1 Blocked time:-1 Waited count:1 Waited time:-1
java.lang.Thread.State: BLOCKED
    at DeadlockProgram$DeadlockRunnable.run(DeadlockProgram.java:38)
    at java.lang.Thread.run(Thread.java:748)

出口:

Thread-0: locked resource -> java.lang.Object@76861e45
Thread-1: locked resource -> java.lang.Object@66b01c61
taking treadDump
Thread-1 - locked monitors length = 0
Thread-1 - LOCK info - java.lang.Object@76861e45 - java.lang.Object@76861e45 - 1988501061
Thread-1 - locked synchronizors length = 0
Thread-1 - LOCKED SYNCHRONIZORS + [Ljava.lang.management.LockInfo;@27ddd392
Thread-0 - locked monitors length = 0
Thread-0 - LOCK info - java.lang.Object@66b01c61 - java.lang.Object@66b01c61 - 1722817633
Thread-0 - locked synchronizors length = 0
Thread-0 - LOCKED SYNCHRONIZORS + [Ljava.lang.management.LockInfo;@27ddd392

0 个答案:

没有答案