List of usage examples for java.lang.management ThreadInfo getThreadState
public Thread.State getThreadState()
From source file:models.monitor.MonitorProvider.java
public ThreadUsage getThreadUsage() { ThreadMXBean threadMxBean = ManagementFactory.getThreadMXBean(); ThreadUsage threadUsage = new ThreadUsage(); long[] threadIds = threadMxBean.getAllThreadIds(); threadUsage.liveThreadCount = threadIds.length; for (long tId : threadIds) { ThreadInfo threadInfo = threadMxBean.getThreadInfo(tId); threadUsage.threadData.put(new Long(tId).toString(), new ThreadData(threadInfo.getThreadName(), threadInfo.getThreadState().name(), threadMxBean.getThreadCpuTime(tId))); }/*from ww w . j av a2 s.c om*/ return threadUsage; }
From source file:hudson.Functions.java
@IgnoreJRERequirement public static String dumpThreadInfo(ThreadInfo ti) { StringBuilder sb = new StringBuilder( "\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " " + ti.getThreadState()); if (ti.getLockName() != null) { sb.append(" on " + ti.getLockName()); }// ww w .j a v a 2 s.c o m if (ti.getLockOwnerName() != null) { sb.append(" owned by \"" + ti.getLockOwnerName() + "\" Id=" + ti.getLockOwnerId()); } if (ti.isSuspended()) { sb.append(" (suspended)"); } if (ti.isInNative()) { sb.append(" (in native)"); } sb.append('\n'); StackTraceElement[] stackTrace = ti.getStackTrace(); for (int i = 0; i < stackTrace.length; i++) { StackTraceElement ste = stackTrace[i]; sb.append("\tat " + ste.toString()); sb.append('\n'); if (i == 0 && ti.getLockInfo() != null) { Thread.State ts = ti.getThreadState(); switch (ts) { case BLOCKED: sb.append("\t- blocked on " + ti.getLockInfo()); sb.append('\n'); break; case WAITING: sb.append("\t- waiting on " + ti.getLockInfo()); sb.append('\n'); break; case TIMED_WAITING: sb.append("\t- waiting on " + ti.getLockInfo()); sb.append('\n'); break; default: } } for (MonitorInfo mi : ti.getLockedMonitors()) { if (mi.getLockedStackDepth() == i) { sb.append("\t- locked " + mi); sb.append('\n'); } } } LockInfo[] locks = ti.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:serposcope.controllers.admin.AdminController.java
public Result stackdump(Context context) { return Results.contentType("text/plain").render((ctx, res) -> { ResponseStreams responseStreams = context.finalizeHeaders(res); try (PrintWriter writer = new PrintWriter(responseStreams.getOutputStream());) { final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100); for (ThreadInfo threadInfo : threadInfos) { writer.append('"'); writer.append(threadInfo.getThreadName()); writer.append("\" "); final Thread.State state = threadInfo.getThreadState(); writer.append("\n java.lang.Thread.State: "); writer.append(state.toString()); final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); for (final StackTraceElement stackTraceElement : stackTraceElements) { writer.append("\n at "); writer.append(stackTraceElement.toString()); }// www .ja v a 2 s .co m writer.println("\n"); } } catch (IOException ex) { LOG.error("stackdump", ex); } }); }
From source file:org.apache.hadoop.metrics.jvm.JvmMetrics.java
private void doThreadUpdates() { ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); long threadIds[] = threadMXBean.getAllThreadIds(); ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadIds, 0); int threadsNew = 0; int threadsRunnable = 0; int threadsBlocked = 0; int threadsWaiting = 0; int threadsTimedWaiting = 0; int threadsTerminated = 0; for (ThreadInfo threadInfo : threadInfos) { // threadInfo is null if the thread is not alive or doesn't exist if (threadInfo == null) continue; Thread.State state = threadInfo.getThreadState(); if (state == NEW) { threadsNew++;/*from w w w .ja v a 2 s . c o m*/ } else if (state == RUNNABLE) { threadsRunnable++; } else if (state == BLOCKED) { threadsBlocked++; } else if (state == WAITING) { threadsWaiting++; } else if (state == TIMED_WAITING) { threadsTimedWaiting++; } else if (state == TERMINATED) { threadsTerminated++; } } metrics.setMetric("threadsNew", threadsNew); metrics.setMetric("threadsRunnable", threadsRunnable); metrics.setMetric("threadsBlocked", threadsBlocked); metrics.setMetric("threadsWaiting", threadsWaiting); metrics.setMetric("threadsTimedWaiting", threadsTimedWaiting); metrics.setMetric("threadsTerminated", threadsTerminated); }
From source file:org.apache.hama.monitor.plugin.JvmTask.java
private void threads(final MetricsRecord record) { int threadsNew = 0; int threadsRunnable = 0; int threadsBlocked = 0; int threadsWaiting = 0; int threadsTimedWaiting = 0; int threadsTerminated = 0; long threadIds[] = threadMXBean.getAllThreadIds(); for (ThreadInfo threadInfo : threadMXBean.getThreadInfo(threadIds, 0)) { if (threadInfo == null) continue; // race protection Thread.State state = threadInfo.getThreadState(); if (NEW.equals(state)) { threadsNew++;// www . j a v a 2 s . c o m break; } else if (RUNNABLE.equals(state)) { threadsRunnable++; break; } else if (BLOCKED.equals(state)) { threadsBlocked++; break; } else if (WAITING.equals(state)) { threadsWaiting++; break; } else if (TIMED_WAITING.equals(state)) { threadsTimedWaiting++; break; } else if (TERMINATED.equals(state)) { threadsTerminated++; break; } } record.add(new Metric(ThreadsNew, threadsNew)); record.add(new Metric(ThreadsRunnable, threadsRunnable)); record.add(new Metric(ThreadsBlocked, threadsBlocked)); record.add(new Metric(ThreadsWaiting, threadsWaiting)); record.add(new Metric(ThreadsTimedWaiting, threadsTimedWaiting)); record.add(new Metric(ThreadsTerminated, threadsTerminated)); if (LOG.isDebugEnabled()) { LOG.debug(ThreadsNew.description() + ": " + threadsNew); LOG.debug(ThreadsRunnable.description() + ": " + threadsRunnable); LOG.debug(ThreadsBlocked.description() + ": " + threadsBlocked); LOG.debug(ThreadsWaiting.description() + ": " + threadsWaiting); LOG.debug(ThreadsTimedWaiting.description() + ": " + threadsTimedWaiting); LOG.debug(ThreadsTerminated.description() + ": " + threadsTerminated); } }
From source file:FullThreadDump.java
private void printThread(ThreadInfo ti) { StringBuilder sb = new StringBuilder( "\"" + ti.getThreadName() + "\"" + " Id=" + ti.getThreadId() + " in " + ti.getThreadState()); if (ti.getLockName() != null) { sb.append(" on lock=" + ti.getLockName()); }/*w w w . j a va 2 s . c o m*/ if (ti.isSuspended()) { sb.append(" (suspended)"); } if (ti.isInNative()) { sb.append(" (running in native)"); } System.out.println(sb.toString()); if (ti.getLockOwnerName() != null) { System.out.println(INDENT + " owned by " + ti.getLockOwnerName() + " Id=" + ti.getLockOwnerId()); } }
From source file:com.streamsets.datacollector.bundles.content.SdcInfoContentGenerator.java
public void threadDump(BundleWriter writer) throws IOException { writer.markStartOfFile("runtime/threads.txt"); ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); ThreadInfo[] threads = threadMXBean.dumpAllThreads(true, true); // Sadly we can't easily do info.toString() as the implementation is hardcoded to cut the stack trace only to 8 // items which does not serve our purpose well. Hence we have custom implementation that prints entire stack trace // for all threads. for (ThreadInfo info : threads) { StringBuilder sb = new StringBuilder( "\"" + info.getThreadName() + "\"" + " Id=" + info.getThreadId() + " " + info.getThreadState()); if (info.getLockName() != null) { sb.append(" on " + info.getLockName()); }/*from w ww . j a v a2s . c o m*/ if (info.getLockOwnerName() != null) { sb.append(" owned by \"" + info.getLockOwnerName() + "\" Id=" + info.getLockOwnerId()); } if (info.isSuspended()) { sb.append(" (suspended)"); } if (info.isInNative()) { sb.append(" (in native)"); } sb.append('\n'); int i = 0; for (StackTraceElement ste : info.getStackTrace()) { if (i == 0 && info.getLockInfo() != null) { Thread.State ts = info.getThreadState(); switch (ts) { case BLOCKED: sb.append("\t- blocked on " + info.getLockInfo()); sb.append('\n'); break; case WAITING: sb.append("\t- waiting on " + info.getLockInfo()); sb.append('\n'); break; case TIMED_WAITING: sb.append("\t- waiting on " + info.getLockInfo()); sb.append('\n'); break; default: } } sb.append("\tat " + ste.toString()); sb.append('\n'); i++; for (MonitorInfo mi : info.getLockedMonitors()) { if (mi.getLockedStackDepth() == i) { sb.append("\t- locked " + mi); sb.append('\n'); } } } LockInfo[] locks = info.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'); writer.write(sb.toString()); } writer.markEndOfFile(); }
From source file:com.thoughtworks.go.server.service.support.ThreadInformationProvider.java
private TreeMap<Long, Map<String, Object>> getStackTraceInformation(ThreadMXBean threadMXBean) { TreeMap<Long, Map<String, Object>> traces = new TreeMap<>(); ThreadInfo[] threadInfos = threadMXBean.dumpAllThreads(true, true); for (ThreadInfo threadInfo : threadInfos) { LinkedHashMap<String, Object> threadStackTrace = new LinkedHashMap<>(); threadStackTrace.put("Id", threadInfo.getThreadId()); threadStackTrace.put("Name", threadInfo.getThreadName()); threadStackTrace.put("State", threadInfo.getThreadState()); LinkedHashMap<String, Object> lockMonitorInfo = new LinkedHashMap<>(); MonitorInfo[] lockedMonitors = threadInfo.getLockedMonitors(); ArrayList<Map<String, Object>> lockedMonitorsJson = new ArrayList<>(); for (MonitorInfo lockedMonitor : lockedMonitors) { LinkedHashMap<String, Object> lockedMonitorJson = new LinkedHashMap<>(); lockedMonitorJson.put("Class", lockedMonitor.getClassName()); lockedMonitorJson.put("IdentityHashCode", lockedMonitor.getIdentityHashCode()); lockedMonitorJson.put("LockedStackDepth", lockedMonitor.getLockedStackDepth()); lockedMonitorJson.put("StackFrame", lockedMonitor.getLockedStackFrame().toString()); lockedMonitorsJson.add(lockedMonitorJson); }// www . ja v a2 s. c o m lockMonitorInfo.put("Locked Monitors", lockedMonitorsJson); lockMonitorInfo.put("Locked Synchronizers", asJSON(threadInfo.getLockedSynchronizers())); threadStackTrace.put("Lock Monitor Info", lockMonitorInfo); LinkedHashMap<String, Object> blockedInfo = new LinkedHashMap<>(); blockedInfo.put("Blocked Time", threadInfo.getBlockedTime() == -1 ? null : threadInfo.getBlockedTime()); blockedInfo.put("Blocked Count", threadInfo.getBlockedCount()); threadStackTrace.put("Blocked Info", blockedInfo); LinkedHashMap<String, Object> timeInfo = new LinkedHashMap<>(); timeInfo.put("Waited Time", threadInfo.getWaitedTime() == -1 ? null : threadInfo.getWaitedTime()); timeInfo.put("Waited Count", threadInfo.getWaitedCount()); threadStackTrace.put("Time Info", timeInfo); LinkedHashMap<String, Object> lockInfoMap = new LinkedHashMap<>(); LockInfo lockInfo = threadInfo.getLockInfo(); lockInfoMap.put("Locked On", asJSON(lockInfo)); lockInfoMap.put("Lock Owner Thread Id", threadInfo.getLockOwnerId() == -1 ? null : threadInfo.getLockOwnerId()); lockInfoMap.put("Lock Owner Thread Name", threadInfo.getLockOwnerName()); threadStackTrace.put("Lock Info", lockInfoMap); LinkedHashMap<String, Object> stateInfo = new LinkedHashMap<>(); stateInfo.put("Suspended", threadInfo.isSuspended()); stateInfo.put("InNative", threadInfo.isInNative()); threadStackTrace.put("State Info", stateInfo); threadStackTrace.put("Stack Trace", asJSON(threadInfo.getStackTrace())); traces.put(threadInfo.getThreadId(), threadStackTrace); } return traces; }
From source file:org.apache.synapse.commons.jmx.ThreadingView.java
private boolean isBlocked(ThreadInfo threadInfo) { // A thread is considered "Blocked" if it is in the BLOCKED state // or if it is in the WAITING state due to some reason other than // 'parking'. Thread.State state = threadInfo.getThreadState(); if (state.equals(Thread.State.BLOCKED)) { return true; } else if (state.equals(Thread.State.WAITING) || state.equals(Thread.State.TIMED_WAITING)) { StackTraceElement[] stackTrace = threadInfo.getStackTrace(); if (stackTrace.length > 0 && !"park".equals(stackTrace[0].getMethodName())) { return true; }//from ww w. java2 s . c o m } return false; }
From source file:edu.usu.sdl.openstorefront.web.rest.service.Application.java
@GET @RequireAdmin/*from w w w .j a v a 2 s . 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; }