List of usage examples for java.lang.management ThreadInfo getLockedMonitors
public MonitorInfo[] getLockedMonitors()
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"); }/*from w ww. j a v a 2 s .c om*/ } }
From source file:Main.java
/** * Returns the names of the locks this {@link Thread} has locked. * //from w ww .j av a2 s. co 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:org.apache.bookkeeper.common.testing.util.TimedOutTestsListener.java
private static void printThreadInfo(ThreadInfo ti, PrintWriter out) { // print thread information printThread(ti, out);// w w w . j a v a 2s . co 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 w w w . j av a2 s. co m*/ 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:Main.java
public static String takeDumpJSON(ThreadMXBean threadMXBean) throws IOException { ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true); List<Map<String, Object>> threads = new ArrayList<>(); for (ThreadInfo thread : threadInfos) { Map<String, Object> threadMap = new HashMap<>(); threadMap.put("name", thread.getThreadName()); threadMap.put("id", thread.getThreadId()); threadMap.put("state", thread.getThreadState().name()); List<String> stacktrace = new ArrayList<>(); for (StackTraceElement element : thread.getStackTrace()) { stacktrace.add(element.toString()); }//from w w w. j ava 2 s . c o m threadMap.put("stack", stacktrace); if (thread.getLockName() != null) { threadMap.put("lock_name", thread.getLockName()); } if (thread.getLockOwnerId() != -1) { threadMap.put("lock_owner_id", thread.getLockOwnerId()); } if (thread.getBlockedTime() > 0) { threadMap.put("blocked_time", thread.getBlockedTime()); } if (thread.getBlockedCount() > 0) { threadMap.put("blocked_count", thread.getBlockedCount()); } if (thread.getLockedMonitors().length > 0) { threadMap.put("locked_monitors", thread.getLockedMonitors()); } if (thread.getLockedSynchronizers().length > 0) { threadMap.put("locked_synchronizers", thread.getLockedSynchronizers()); } threads.add(threadMap); } ObjectMapper om = new ObjectMapper(); ObjectNode json = om.createObjectNode(); json.put("date", new Date().toString()); json.putPOJO("threads", threads); long[] deadlockedThreads = threadMXBean.findDeadlockedThreads(); long[] monitorDeadlockedThreads = threadMXBean.findMonitorDeadlockedThreads(); if (deadlockedThreads != null && deadlockedThreads.length > 0) { json.putPOJO("deadlocked_thread_ids", deadlockedThreads); } if (monitorDeadlockedThreads != null && monitorDeadlockedThreads.length > 0) { json.putPOJO("monitor_deadlocked_thread_ids", monitorDeadlockedThreads); } om.enable(SerializationFeature.INDENT_OUTPUT); return om.writerWithDefaultPrettyPrinter().writeValueAsString(json); }
From source file:Main.java
public static void printStack(ThreadInfo info, final StringBuilder sb, final StackTraceElement[] stack) { int i = 0;/*w w w . j a va 2s . com*/ 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())); }//from w w w . jav a2 s .co m 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:com.workplacesystems.utilsj.ThreadDumperJdk16.java
@Override int outputMonitors(ThreadInfo info, int stack_depth, int monitor_pointer, StringBuffer buffer) { MonitorInfo[] locked_monitors = info.getLockedMonitors(); boolean check_next_monitor; do {/*from www .jav a2 s . c o m*/ check_next_monitor = false; if (monitor_pointer < locked_monitors.length) { MonitorInfo monitor_info = locked_monitors[monitor_pointer]; if (monitor_info.getLockedStackDepth() == stack_depth) { formatLock(monitor_info, "locked", buffer); buffer.append("\n"); monitor_pointer++; check_next_monitor = true; } } } while (check_next_monitor); return monitor_pointer; }
From source file:DeadlockDetector.java
private void printThreadInfo(ThreadInfo threadInfo) { printThread(threadInfo);//from w w w.j a v a 2 s . c o 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"); } } } }
From source file:DeadlockDetector.java
@Override public void run() { boolean noDeadLocks = true; while (noDeadLocks) { try {// ww w.j a v a 2s . c om ThreadMXBean bean = ManagementFactory.getThreadMXBean(); long[] threadIds = bean.findDeadlockedThreads(); if (threadIds != null) { System.out.println("Deadlock detected!"); sb = new StringBuilder(); noDeadLocks = false; ThreadInfo[] infos = bean.getThreadInfo(threadIds); sb.append("\nTHREAD LOCK INFO: \n"); for (ThreadInfo threadInfo : infos) { printThreadInfo(threadInfo); LockInfo[] lockInfos = threadInfo.getLockedSynchronizers(); MonitorInfo[] monitorInfos = threadInfo.getLockedMonitors(); printLockInfo(lockInfos); printMonitorInfo(threadInfo, monitorInfos); } sb.append("\nTHREAD DUMPS: \n"); for (ThreadInfo ti : bean.dumpAllThreads(true, true)) { printThreadInfo(ti); } System.out.println(sb.toString()); } Thread.sleep(checkInterval); } catch (Exception ex) { ex.printStackTrace(); } } }