List of usage examples for java.lang.management ThreadInfo getLockInfo
public LockInfo getLockInfo()
From source file:Main.java
/** * Returns the names of the locks this {@link Thread} * is blocking or waiting on./*from w w w . j a va 2s.c o m*/ * * @see ThreadInfo#getLockInfo() * @see ThreadInfo#getThreadState() */ private static String getLockState(ThreadInfo info) { StringBuilder buffer = new StringBuilder(); LockInfo lockInfo = info.getLockInfo(); Thread.State state = info.getThreadState(); switch (state) { case BLOCKED: buffer.append("\t- blocked on ").append(lockInfo).append("\n"); break; case WAITING: case TIMED_WAITING: buffer.append("\t- waiting on ").append(lockInfo).append("\n"); break; default: break; } return buffer.toString(); }
From source file:Main.java
/** Add the LockInfo data to the report. */ private static void addLockInfo(ThreadInfo info, StringBuilder sb) { Thread.State ts = info.getThreadState(); switch (ts) { case BLOCKED: sb.append("\t- blocked on " + info.getLockInfo() + "\n"); break;// w w w . j ava 2s .c o m case WAITING: case TIMED_WAITING: sb.append("\t- waiting on " + info.getLockInfo() + "\n"); break; default: } }
From source file:Main.java
public static void printStack(ThreadInfo info, final StringBuilder sb, final StackTraceElement[] stack) { int i = 0;//from ww w .j a va 2 s. c om 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
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 ww . j a v a2 s . com*/ 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
public static void printThreadInfo(ThreadInfo info, StringBuilder builder) { builder.append("Thread ").append(info.getThreadName()).append("\" (").append(info.getThreadId()) .append(") -- "); formatState(builder, info);/*w w w. j a v a 2 s.co 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: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 .j av a 2s . 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: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()); }/*from w w w .j a v a2 s . 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:com.workplacesystems.utilsj.ThreadDumperJdk16.java
@Override void outputWaitingInfo(ThreadInfo info, ExtraLockInfo exLockInfo, StringBuffer buffer) { final LockInfo lock_info = info.getLockInfo(); if (lock_info != null) { formatLock(lock_info, "waiting on", buffer); if (exLockInfo != null) { if (exLockInfo.isWaitingFor(ExtraLockInfo.WaitingFor.WRITE, lock_info)) buffer.append(" for write"); if (exLockInfo.isWaitingFor(ExtraLockInfo.WaitingFor.READ, lock_info)) buffer.append(" for read"); }/*from ww w. jav a 2 s . c o m*/ buffer.append("."); long lock_owner = info.getLockOwnerId(); if (lock_owner != -1) { buffer.append(" Locked by \""); buffer.append(info.getLockOwnerName()); buffer.append("\" tid="); buffer.append(lock_owner); } buffer.append("\n"); } }
From source file:DeadLockDetector.java
/** * Check if there is a DeadLock./* w w w . j ava 2s. co m*/ */ @Override public final void run() { boolean deadlock = false; while (!deadlock) { try { long[] ids = tmx.findDeadlockedThreads(); if (ids != null) { /** deadlock found :/ */ deadlock = true; ThreadInfo[] tis = tmx.getThreadInfo(ids, true, true); String info = "DeadLock Found!\n"; for (ThreadInfo ti : tis) { info += ti.toString(); } for (ThreadInfo ti : tis) { LockInfo[] locks = ti.getLockedSynchronizers(); MonitorInfo[] monitors = ti.getLockedMonitors(); if (locks.length == 0 && monitors.length == 0) { /** this thread is deadlocked but its not guilty */ continue; } ThreadInfo dl = ti; info += "Java-level deadlock:\n"; info += "\t" + dl.getThreadName() + " is waiting to lock " + dl.getLockInfo().toString() + " which is held by " + dl.getLockOwnerName() + "\n"; while ((dl = tmx.getThreadInfo(new long[] { dl.getLockOwnerId() }, true, true)[0]) .getThreadId() != ti.getThreadId()) { info += "\t" + dl.getThreadName() + " is waiting to lock " + dl.getLockInfo().toString() + " which is held by " + dl.getLockOwnerName() + "\n"; } } System.out.println(info); if (doWhenDL == RESTART) { System.exit(0); } } Thread.sleep(sleepTime); } catch (Exception e) { System.out.println(e); } } }
From source file:com.thoughtworks.go.server.service.support.ServerRuntimeInformationProvider.java
private void threadInfo(ThreadMXBean threadMXBean, InformationStringBuilder builder) { builder.addSection("Thread information"); builder.append(String.format("Current: %s, Total: %s, Daemon: %s, Peak: %s\n", threadMXBean.getThreadCount(), threadMXBean.getTotalStartedThreadCount(), threadMXBean.getDaemonThreadCount(), threadMXBean.getPeakThreadCount())); long[] deadlockedThreads = threadMXBean.findDeadlockedThreads(); if (deadlockedThreads != null && deadlockedThreads.length > 0) { builder.append(String.format("Found %s dead locked threads. Here is there information.\n", deadlockedThreads.length)); for (long deadlockedThread : deadlockedThreads) { ThreadInfo threadInfo = threadMXBean.getThreadInfo(deadlockedThread); LockInfo lockInfo = threadInfo.getLockInfo(); if (lockInfo != null) { builder.append(String.format("LockInfo: %s", lockInfo)); } else { builder.append("This thread is not waiting for any locks\n"); }//from w w w .ja v a 2s . c om builder.append(String.format("Monitor Info - Stack Frame where locks were taken.\n")); MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); for (MonitorInfo lockedMonitor : lockedMonitors) { builder.append(String.format("Monitor for class '%s' taken at stack frame '%s'.", lockedMonitor.getClassName(), lockedMonitor.getLockedStackFrame())); } builder.append("The stack trace of the deadlocked thread\n"); builder.append(Arrays.toString(threadInfo.getStackTrace())); } } builder.addSubSection("All thread stacktraces"); ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true); for (ThreadInfo threadInfo : threadInfos) { builder.append(String.format("%s, %s, %s\n", threadInfo.getThreadId(), threadInfo.getThreadName(), threadInfo.getThreadState())); MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); builder.append("Locked Monitors:\n"); for (MonitorInfo lockedMonitor : lockedMonitors) { builder.append(String.format("%s at %s", lockedMonitor, lockedMonitor.getLockedStackFrame())); } LockInfo[] lockedSynchronizers = threadInfo.getLockedSynchronizers(); builder.append("Locked Synchronizers:\n"); for (LockInfo lockedSynchronizer : lockedSynchronizers) { builder.append(lockedSynchronizer); } builder.append("Stacktrace:\n "); builder.append(StringUtils.join(threadInfo.getStackTrace(), "\n ")); builder.append("\n\n"); } }