List of usage examples for java.lang.management ManagementFactory getThreadMXBean
public static ThreadMXBean getThreadMXBean()
From source file:Main.java
/** * Get a list of all thread info objects. Since there is always at least one * thread running, there is always at least one thread info object. This * method never returns a null or empty array. * /*from w w w . ja va 2 s .c o m*/ * @return an array of thread infos */ public static ThreadInfo[] getAllThreadInfos() { final ThreadMXBean thbean = ManagementFactory.getThreadMXBean(); final long[] ids = thbean.getAllThreadIds(); // Get thread info with lock info, when available. ThreadInfo[] infos; if (!thbean.isObjectMonitorUsageSupported() || !thbean.isSynchronizerUsageSupported()) { infos = thbean.getThreadInfo(ids); } else { infos = thbean.getThreadInfo(ids, true, true); } // Clean nulls from array if threads have died. final ThreadInfo[] notNulls = new ThreadInfo[infos.length]; int nNotNulls = 0; for (ThreadInfo info : infos) { if (info != null) { notNulls[nNotNulls++] = info; } } if (nNotNulls == infos.length) return infos; // Original had no nulls return java.util.Arrays.copyOf(notNulls, nNotNulls); }
From source file:com.github.jinahya.codec.BossVsEngineerTestCpuTimeDecode.java
private static long decodeLikeAnEngineer(final byte[] encoded) { final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final long start = threadMXBean.getCurrentThreadCpuTime(); new HexDecoder().decodeLikeAnEngineer(encoded); return threadMXBean.getCurrentThreadCpuTime() - start; }
From source file:com.github.jinahya.codec.BossVsEngineerTestCpuTimeEncode.java
private static long encodeLikeAnEngineer(final byte[] decoded) { final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final long start = threadMXBean.getCurrentThreadCpuTime(); new HexEncoder().encodeLikeAnEngineer(decoded); return threadMXBean.getCurrentThreadCpuTime() - start; }
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 www.j av a2 s . 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"); } }
From source file:org.vbossica.springbox.metrics.ThreadDumpController.java
@Override public void afterPropertiesSet() throws Exception { this.threadDump = new ThreadDump(ManagementFactory.getThreadMXBean()); }
From source file:Main.java
/** * //from w w w . j av a 2 s . c om */ public static ThreadInfo[] getStackTraces() { ThreadMXBean bean = ManagementFactory.getThreadMXBean(); return bean.dumpAllThreads(true, true); }
From source file:DeadlockDetector.java
@Override public void run() { boolean noDeadLocks = true; while (noDeadLocks) { try {/*from ww w . j a va 2s . c o m*/ 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.alibaba.dragoon.client.jmx.Threading.java
public Threading() { Threading = ManagementFactory.getThreadMXBean(); OperatingSystem = (OperatingSystemMXBean) ManagementFactory.getOperatingSystemMXBean(); Runtime = ManagementFactory.getRuntimeMXBean(); try {//from ww w.ja va 2 s . c o m gc = new GC(); // ? lastGCTime = gc.getFullGCCollectionTime() + gc.getYoungGCCollectionTime(); lastCPUTime = getProcessCpuTime(); lastGCUpTime = lastCPUUpTime = getUpTime(); } catch (Exception e) { LOG.error(e.getMessage(), e); } }
From source file:org.talend.dataquality.statistics.datetime.PerformanceTest.java
@Test @Ignore/*from ww w . j av a 2s.c om*/ public void testIsDate() throws IOException { final InputStream stream = SystemDateTimePatternManager.class.getResourceAsStream("DateSampleTable.txt"); final List<String> lines = IOUtils.readLines(stream); SystemDateTimePatternManager.isDate("12/02/99");// init DateTimeFormatters final ThreadMXBean mxBean = ManagementFactory.getThreadMXBean(); final long cpuBefore = mxBean.getCurrentThreadCpuTime(); int count = 0; loop: for (int n = 0; n < REPLICATE; n++) { for (int i = 1; i < lines.size(); i++) { final String line = lines.get(i); if (!"".equals(line.trim())) { final String[] sampleLine = line.trim().split("\t"); final String sample = sampleLine[0]; CustomDateTimePatternManager.isDate(sample, Collections.emptyList()); count++; if (count > 100000) { break loop; } } } } final long cpuAfter = mxBean.getCurrentThreadCpuTime(); final long difference = cpuAfter - cpuBefore; assertTrue("The method isDate() is slower than expected. Actual CPU time spent: " + difference, difference < 20e8); }
From source file:Main.java
public static Thread[] getAllThreads() { final ThreadGroup root = getRootThreadGroup(); final ThreadMXBean thbean = ManagementFactory.getThreadMXBean(); int nAlloc = thbean.getThreadCount(); int n = 0;// w ww . j a va2 s .com Thread[] threads; do { nAlloc *= 2; threads = new Thread[nAlloc]; n = root.enumerate(threads, true); } while (n == nAlloc); return java.util.Arrays.copyOf(threads, n); }