List of usage examples for java.lang.management ThreadMXBean dumpAllThreads
public ThreadInfo[] dumpAllThreads(boolean lockedMonitors, boolean lockedSynchronizers);
From source file:edu.usu.sdl.openstorefront.web.rest.service.Application.java
@GET @RequireAdmin//w ww . j a v a 2s . c o m @APIDescription("Gets the application system thread and status") @Produces({ MediaType.APPLICATION_JSON }) @DataType(ThreadStatus.class) @Path("/threads") public List<ThreadStatus> getThreads() { List<ThreadStatus> threadStatuses = new ArrayList<>(); ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadInfo threadInfos[] = threadMXBean.dumpAllThreads(false, false); for (ThreadInfo info : threadInfos) { ThreadStatus threadStatus = new ThreadStatus(); threadStatus.setId(info.getThreadId()); threadStatus.setName(info.getThreadName()); threadStatus.setStatus(info.getThreadState().name()); threadStatus .setDetails(info.toString().replace("\t", " ").replace("\n", "<br>")); threadStatuses.add(threadStatus); } return threadStatuses; }
From source file:org.ScripterRon.JavaBitcoin.RpcHandler.java
/** * Process 'getstacktraces' request/*from w w w . j a va2s .com*/ * * Response parameters: * locks - An array of lock objects for locks with waiters * threads - An array of thread objects * * Lock object: * name - Lock class name * hash - Lock identity hash code * thread - Identifier of thread holding the lock * * Monitor object: * name - Monitor class name * hash - Monitor identity hash * depth - Stack depth where monitor locked * trace - Stack element where monitor locked * * Thread object: * blocked - Lock object if thread is waiting on a lock * id - Thread identifier * locks - Array of monitor objects for locks held by this thread * name - Thread name * state - Thread state * trace - Array of stack trace elements * * @return Response as a JSONObject */ private JSONObject getStackTraces() { JSONArray threadsJSON = new JSONArray(); JSONArray locksJSON = new JSONArray(); ThreadMXBean tmxBean = ManagementFactory.getThreadMXBean(); boolean tmxMI = tmxBean.isObjectMonitorUsageSupported(); ThreadInfo[] tList = tmxBean.dumpAllThreads(tmxMI, false); // // Generate the response // for (ThreadInfo tInfo : tList) { JSONObject threadJSON = new JSONObject(); // // General thread information // threadJSON.put("id", tInfo.getThreadId()); threadJSON.put("name", tInfo.getThreadName()); threadJSON.put("state", tInfo.getThreadState().toString()); // // Gather lock usage // if (tmxMI) { MonitorInfo[] mList = tInfo.getLockedMonitors(); if (mList.length > 0) { JSONArray monitorsJSON = new JSONArray(); for (MonitorInfo mInfo : mList) { JSONObject lockJSON = new JSONObject(); lockJSON.put("name", mInfo.getClassName()); lockJSON.put("hash", mInfo.getIdentityHashCode()); lockJSON.put("depth", mInfo.getLockedStackDepth()); lockJSON.put("trace", mInfo.getLockedStackFrame().toString()); monitorsJSON.add(lockJSON); } threadJSON.put("locks", monitorsJSON); } if (tInfo.getThreadState() == Thread.State.BLOCKED) { LockInfo lInfo = tInfo.getLockInfo(); if (lInfo != null) { JSONObject lockJSON = new JSONObject(); lockJSON.put("name", lInfo.getClassName()); lockJSON.put("hash", lInfo.getIdentityHashCode()); lockJSON.put("thread", tInfo.getLockOwnerId()); threadJSON.put("blocked", lockJSON); boolean addLock = true; for (Object lock : locksJSON) { if (((String) ((JSONObject) lock).get("name")).equals(lInfo.getClassName())) { addLock = false; break; } } if (addLock) locksJSON.add(lockJSON); } } } // // Add the stack trace // StackTraceElement[] elements = tInfo.getStackTrace(); JSONArray traceJSON = new JSONArray(); for (StackTraceElement element : elements) traceJSON.add(element.toString()); threadJSON.put("trace", traceJSON); // // Add the thread to the response // threadsJSON.add(threadJSON); } // // Return the response // JSONObject response = new JSONObject(); response.put("threads", threadsJSON); response.put("locks", locksJSON); return response; }
From source file:org.kaaproject.kaa.server.common.thrift.cli.server.BaseCliThriftService.java
/** * Dump service threads information.//from ww w .j a v a 2 s .co m * * @param writer the writer to output threads information */ private void dumpThreads(PrintWriter writer) { writer.println("THREADS DUMP:"); writer.println(); ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean(); ThreadInfo[] threads = threadMxBean.dumpAllThreads(false, false); Map<Long, ThreadStruct> threadsMap = new HashMap<>(); for (ThreadInfo ti : threads) { ThreadStruct ts = new ThreadStruct(); ts.ti = ti; threadsMap.put(ti.getThreadId(), ts); } ThreadGroup root = Thread.currentThread().getThreadGroup(); ThreadGroup parent; parent = root.getParent(); while (parent != null) { root = parent; parent = parent.getParent(); } allThreadsFromGroup(root, threadsMap); Collection<ThreadStruct> threadValues = threadsMap.values(); List<ThreadStruct> threadList = new ArrayList<>(threadValues); Collections.sort(threadList); Map<State, Integer> threadStatistics = new LinkedHashMap<>(); threadStatistics.put(State.NEW, 0); threadStatistics.put(State.RUNNABLE, 0); threadStatistics.put(State.BLOCKED, 0); threadStatistics.put(State.WAITING, 0); threadStatistics.put(State.TIMED_WAITING, 0); threadStatistics.put(State.TERMINATED, 0); int maxGroup = 0; int maxName = 0; for (ThreadStruct thread : threadList) { maxName = Math.max(thread.ti.getThreadName().length(), maxName); maxGroup = Math.max(thread.getGroupName().length(), maxGroup); int count = threadStatistics.get(thread.ti.getThreadState()); count++; threadStatistics.put(thread.ti.getThreadState(), count); } StringBuffer header = new StringBuffer(); header.append("ID"); int idColumnLength = 4; int length = idColumnLength; header.append(createPadding(length - header.length())); header.append("GROUP"); int groupColumnLength = maxGroup + 1; length += groupColumnLength; header.append(createPadding(length - header.length())); header.append("NAME"); int nameColumnLength = maxName + 1; length += nameColumnLength; header.append(createPadding(length - header.length())); header.append("PRIORITY"); int priorityColumnLength = 10; length += priorityColumnLength; header.append(createPadding(length - header.length())); header.append("STATE"); int stateColumnLength = 14; length += stateColumnLength; header.append(createPadding(length - header.length())); header.append("DAEMON"); int daemonColumnLengh = 7; length += daemonColumnLengh; header.append(createPadding(length - header.length())); header.append("ALIVE"); int aliveColumnLengh = 6; length += aliveColumnLengh; header.append(createPadding(length - header.length())); header.append("CPU TIME (SEC)"); int cpuTimeColumnLengh = 14; length += cpuTimeColumnLengh; header.append(createPadding(length - header.length())); writer.println(header); int maxRowLength = header.length(); writer.println(createPadding(maxRowLength, '-')); NumberFormat format = new DecimalFormat("0.#"); for (ThreadStruct thread : threadList) { StringBuffer row = new StringBuffer(); row.append(thread.ti.getThreadId()); int rowLength = idColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.getGroupName()); rowLength += groupColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.ti.getThreadName()); rowLength += nameColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.getPriority()); rowLength += priorityColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.ti.getThreadState()); rowLength += stateColumnLength; row.append(createPadding(rowLength - row.length())); row.append(thread.isDaemon()); rowLength += daemonColumnLengh; row.append(createPadding(rowLength - row.length())); row.append(thread.isAlive()); rowLength += aliveColumnLengh; row.append(createPadding(rowLength - row.length())); double cpuTimeSec = (double) threadMxBean.getThreadCpuTime(thread.ti.getThreadId()) / (double) (1000 * 1000 * 1000); row.append(format.format(cpuTimeSec)); writer.println(row); } writer.println(createPadding(maxRowLength, '-')); writer.println("SUMMARY:"); writer.println(createPadding(maxRowLength, '-')); for (State state : threadStatistics.keySet()) { int count = threadStatistics.get(state); if (count > 0) { StringBuffer row = new StringBuffer(); row.append(state.toString()); row.append(createPadding(stateColumnLength - row.length())); row.append(count); writer.println(row); } } writer.println(createPadding(maxRowLength, '-')); StringBuffer row = new StringBuffer(); row.append("TOTAL"); row.append(createPadding(stateColumnLength - row.length())); row.append(threadList.size()); writer.println(row); writer.println(createPadding(maxRowLength, '-')); }
From source file:DeadlockDetector.java
@Override public void run() { boolean noDeadLocks = true; while (noDeadLocks) { try {//w ww . j a v a 2 s . 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(); } } }
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 ww .j av a 2 s . c o m 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"); } }
From source file:com.thoughtworks.go.server.service.support.ThreadInformationProvider.java
@Override public void appendInformation(InformationStringBuilder builder) { ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); 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 2 s.co m*/ 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"); } }