Я использую приведенный ниже код для создания 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