Java Code Examples for java.lang.management.ThreadInfo#getThreadState()
The following examples show how to use
java.lang.management.ThreadInfo#getThreadState() .
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ThreadLauncherDaemonTest.java From lutece-core with BSD 3-Clause "New" or "Revised" License | 6 votes |
private void dumpStateWhileWaiting( long lWait ) throws InterruptedException { // wait for the daemon to have a chance to try running Thread.sleep( lWait ); final StringBuilder dump = new StringBuilder( ); final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean( ); final ThreadInfo[ ] threadInfos = threadMXBean.getThreadInfo( threadMXBean.getAllThreadIds( ), 100 ); for ( ThreadInfo threadInfo : threadInfos ) { dump.append( '"' ); dump.append( threadInfo.getThreadName( ) ); dump.append( "\" " ); final Thread.State state = threadInfo.getThreadState( ); dump.append( "\n java.lang.Thread.State: " ); dump.append( state ); final StackTraceElement[ ] stackTraceElements = threadInfo.getStackTrace( ); for ( final StackTraceElement stackTraceElement : stackTraceElements ) { dump.append( "\n at " ); dump.append( stackTraceElement ); } dump.append( "\n\n" ); } AppLogService.info( "Current state : " + dump ); }
Example 2
Source File: JvmThreadInstanceEntryImpl.java From openjdk-jdk8u with GNU General Public License v2.0 | 6 votes |
public static Byte[] getState(ThreadInfo info) { byte[] bitmap = new byte[] {(byte)0x00, (byte)0x00}; try { final Thread.State state = info.getThreadState(); final boolean inNative = info.isInNative(); final boolean suspended = info.isSuspended(); log.debug("getJvmThreadInstState", "[State=" + state + ",isInNative=" + inNative + ",isSuspended=" + suspended + "]"); setState(bitmap,state); if (inNative) setNative(bitmap); if (suspended) setSuspended(bitmap); checkOther(bitmap); } catch (RuntimeException r) { bitmap[0]=(byte)0x00; bitmap[1]=Byte1.other; log.trace("getJvmThreadInstState", "Unexpected exception: " + r); log.debug("getJvmThreadInstState",r); } Byte[] result = { new Byte(bitmap[0]), new Byte(bitmap[1]) }; return result; }
Example 3
Source File: ThreadMXBeanStateTest.java From jdk8u-jdk with GNU General Public License v2.0 | 6 votes |
private static void checkSuspendedThreadState(ThreadStateController t, Thread.State state) { ThreadInfo info = getThreadInfo(t, state); if (info == null) { throw new RuntimeException(t.getName() + " expected to have ThreadInfo " + " but got null."); } if (info.getThreadState() != state) { throw new RuntimeException(t.getName() + " expected to be in " + state + " state but got " + info.getThreadState()); } if (!info.isSuspended()) { throw new RuntimeException(t.getName() + " expected to be suspended " + " but isSuspended() returns " + info.isSuspended()); } }
Example 4
Source File: ThreadMonitor.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
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()); } 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()); } }
Example 5
Source File: ThreadMonitor.java From TencentKona-8 with GNU General Public License v2.0 | 6 votes |
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()); } 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()); } }
Example 6
Source File: ThreadMXBeanStateTest.java From jdk8u60 with GNU General Public License v2.0 | 6 votes |
private static void checkSuspendedThreadState(ThreadStateController t, Thread.State state) { ThreadInfo info = getThreadInfo(t, state); if (info == null) { throw new RuntimeException(t.getName() + " expected to have ThreadInfo " + " but got null."); } if (info.getThreadState() != state) { throw new RuntimeException(t.getName() + " expected to be in " + state + " state but got " + info.getThreadState()); } if (!info.isSuspended()) { throw new RuntimeException(t.getName() + " expected to be suspended " + " but isSuspended() returns " + info.isSuspended()); } }
Example 7
Source File: ThreadMXBeanStateTest.java From openjdk-8-source with GNU General Public License v2.0 | 6 votes |
private static void checkSuspendedThreadState(ThreadStateController t, Thread.State state) { ThreadInfo info = getThreadInfo(t, state); if (info == null) { throw new RuntimeException(t.getName() + " expected to have ThreadInfo " + " but got null."); } if (info.getThreadState() != state) { throw new RuntimeException(t.getName() + " expected to be in " + state + " state but got " + info.getThreadState()); } if (!info.isSuspended()) { throw new RuntimeException(t.getName() + " expected to be suspended " + " but isSuspended() returns " + info.isSuspended()); } }
Example 8
Source File: DebugUtils.java From zstack with Apache License 2.0 | 6 votes |
public static void dumpAllThreads() { final StringBuilder dump = new StringBuilder(); final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean(); final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100); for (ThreadInfo threadInfo : threadInfos) { dump.append('"'); dump.append(threadInfo.getThreadName()); dump.append("\" "); final Thread.State state = threadInfo.getThreadState(); dump.append("\n java.lang.Thread.State: "); dump.append(state); final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace(); for (final StackTraceElement stackTraceElement : stackTraceElements) { dump.append("\n at "); dump.append(stackTraceElement); } dump.append("\n\n"); } logger.debug(dump.toString()); }
Example 9
Source File: SmallRyeMetricsRecorder.java From quarkus with Apache License 2.0 | 5 votes |
private long getThreadStateCount(ThreadMXBean threadBean, Thread.State state) { int count = 0; for (ThreadInfo threadInfo : threadBean.getThreadInfo(threadBean.getAllThreadIds())) { if (threadInfo != null && threadInfo.getThreadState() == state) { count++; } } return count; }
Example 10
Source File: ThreadMXBeanStateTest.java From dragonwell8_jdk with GNU General Public License v2.0 | 5 votes |
private static ThreadInfo getThreadInfo(ThreadStateController t, Thread.State expected) { // wait for the thread to transition to the expected state. // There is a small window between the thread checking the state // and the thread actual entering that state. int retryCount=0; ThreadInfo info = tm.getThreadInfo(t.getId()); while (info.getThreadState() != expected && retryCount < MAX_RETRY) { ThreadStateController.pause(10); retryCount++; info = tm.getThreadInfo(t.getId()); } return info; }
Example 11
Source File: ThreadMXBeanStateTest.java From jdk8u-jdk with GNU General Public License v2.0 | 5 votes |
private static ThreadInfo getThreadInfo(ThreadStateController t, Thread.State expected) { // wait for the thread to transition to the expected state. // There is a small window between the thread checking the state // and the thread actual entering that state. int retryCount=0; ThreadInfo info = tm.getThreadInfo(t.getId()); while (info.getThreadState() != expected && retryCount < MAX_RETRY) { ThreadStateController.pause(10); retryCount++; info = tm.getThreadInfo(t.getId()); } return info; }
Example 12
Source File: ThreadMonitoring.java From hbase with Apache License 2.0 | 5 votes |
/** * Print all of the thread's information and stack traces. * * @param sb * @param info * @param indent */ public static void appendThreadInfo(StringBuilder sb, ThreadInfo info, String indent) { boolean contention = threadBean.isThreadContentionMonitoringEnabled(); if (info == null) { sb.append(indent).append("Inactive (perhaps exited while monitoring was done)\n"); return; } String taskName = getTaskName(info.getThreadId(), info.getThreadName()); sb.append(indent).append("Thread ").append(taskName).append(":\n"); Thread.State state = info.getThreadState(); sb.append(indent).append(" State: ").append(state).append("\n"); sb.append(indent).append(" Blocked count: ").append(info.getBlockedCount()).append("\n"); sb.append(indent).append(" Waited count: ").append(info.getWaitedCount()).append("\n"); if (contention) { sb.append(indent).append(" Blocked time: " + info.getBlockedTime()).append("\n"); sb.append(indent).append(" Waited time: " + info.getWaitedTime()).append("\n"); } if (state == Thread.State.WAITING) { sb.append(indent).append(" Waiting on ").append(info.getLockName()).append("\n"); } else if (state == Thread.State.BLOCKED) { sb.append(indent).append(" Blocked on ").append(info.getLockName()).append("\n"); sb.append(indent).append(" Blocked by ").append( getTaskName(info.getLockOwnerId(), info.getLockOwnerName())).append("\n"); } sb.append(indent).append(" Stack:").append("\n"); for (StackTraceElement frame: info.getStackTrace()) { sb.append(indent).append(" ").append(frame.toString()).append("\n"); } }
Example 13
Source File: CurrentRequestsHandler.java From styx with Apache License 2.0 | 5 votes |
private String getThreadState(ThreadInfo t) { StringBuilder sb = new StringBuilder(); LockInfo lock = t.getLockInfo(); if (lock != null && t.getThreadState() != BLOCKED) { sb.append(format("%n - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName())); sb.append(format("%n - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName())); } else if (lock != null && t.getThreadState() == BLOCKED) { sb.append(format("%n - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName())); } return sb.toString(); }
Example 14
Source File: ThinClientPartitionAwarenessResourceReleaseTest.java From ignite with Apache License 2.0 | 5 votes |
/** * Gets threads count with a given name. */ private static int threadsCount(String name) { int cnt = 0; long[] threadIds = U.getThreadMx().getAllThreadIds(); for (long id : threadIds) { ThreadInfo info = U.getThreadMx().getThreadInfo(id); if (info != null && info.getThreadState() != Thread.State.TERMINATED && info.getThreadName().startsWith(name)) cnt++; } return cnt; }
Example 15
Source File: ThreadMXBeanStateTest.java From openjdk-8-source with GNU General Public License v2.0 | 5 votes |
private static ThreadInfo getThreadInfo(ThreadStateController t, Thread.State expected) { // wait for the thread to transition to the expected state. // There is a small window between the thread checking the state // and the thread actual entering that state. int retryCount=0; ThreadInfo info = tm.getThreadInfo(t.getId()); while (info.getThreadState() != expected && retryCount < MAX_RETRY) { ThreadStateController.pause(10); retryCount++; info = tm.getThreadInfo(t.getId()); } return info; }
Example 16
Source File: ThreadInfoCompositeData.java From jdk8u60 with GNU General Public License v2.0 | 4 votes |
static void checkThreadInfo(ThreadInfo info) throws Exception { if (info.getThreadId() != ((Long) values[THREAD_ID]).longValue()) { throw new RuntimeException("Thread Id = " + info.getThreadId() + " expected = " + values[THREAD_ID]); } if (!info.getThreadName().equals(values[THREAD_NAME])) { throw new RuntimeException("Thread Name = " + info.getThreadName() + " expected = " + values[THREAD_NAME]); } if (info.getThreadState() != Thread.State.RUNNABLE) { throw new RuntimeException("Thread Name = " + info.getThreadName() + " expected = " + Thread.State.RUNNABLE); } if (info.getBlockedTime() != ((Long) values[BLOCKED_TIME]).longValue()) { throw new RuntimeException("blocked time = " + info.getBlockedTime() + " expected = " + values[BLOCKED_TIME]); } if (info.getBlockedCount() != ((Long) values[BLOCKED_COUNT]).longValue()) { throw new RuntimeException("blocked count = " + info.getBlockedCount() + " expected = " + values[BLOCKED_COUNT]); } if (info.getWaitedTime() != ((Long) values[WAITED_TIME]).longValue()) { throw new RuntimeException("waited time = " + info.getWaitedTime() + " expected = " + values[WAITED_TIME]); } if (info.getWaitedCount() != ((Long) values[WAITED_COUNT]).longValue()) { throw new RuntimeException("waited count = " + info.getWaitedCount() + " expected = " + values[WAITED_COUNT]); } if (!info.getLockName().equals(values[LOCK_NAME])) { throw new RuntimeException("Lock Name = " + info.getLockName() + " expected = " + values[LOCK_NAME]); } if (info.getLockOwnerId() != ((Long) values[LOCK_OWNER_ID]).longValue()) { throw new RuntimeException( "LockOwner Id = " + info.getLockOwnerId() + " expected = " + values[LOCK_OWNER_ID]); } if (!info.getLockOwnerName().equals(values[LOCK_OWNER_NAME])) { throw new RuntimeException("LockOwner Name = " + info.getLockOwnerName() + " expected = " + values[LOCK_OWNER_NAME]); } checkStackTrace(info.getStackTrace()); checkLockInfo(info.getLockInfo()); }
Example 17
Source File: OWLHandler.java From owltools with BSD 3-Clause "New" or "Revised" License | 4 votes |
private static CharSequence toString(ThreadInfo threadInfo) { StringBuilder sb = new StringBuilder("\"" + threadInfo.getThreadName() + "\"" + " Id=" + threadInfo.getThreadId() + " " + threadInfo.getThreadState()); if (threadInfo.getLockName() != null) { sb.append(" on " + threadInfo.getLockName()); } 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'); StackTraceElement[] stackTrace = threadInfo.getStackTrace(); int i = 0; for (; i < stackTrace.length; 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'); } } } 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; }
Example 18
Source File: ThreadMXBeanStateTest.java From jdk8u-jdk with GNU General Public License v2.0 | 4 votes |
private static void checkLockInfo(ThreadStateController t, Thread.State state, Object lock, Thread owner) { ThreadInfo info = getThreadInfo(t, state); if (info == null) { throw new RuntimeException(t.getName() + " expected to have ThreadInfo " + " but got null."); } if (info.getThreadState() != state) { throw new RuntimeException(t.getName() + " expected to be in " + state + " state but got " + info.getThreadState()); } if (lock == null && info.getLockName() != null) { throw new RuntimeException(t.getName() + " expected not to be blocked on any lock" + " but got " + info.getLockName()); } String expectedLockName = getLockName(lock); if (lock != null && info.getLockName() == null) { throw new RuntimeException(t.getName() + " expected to be blocked on lock [" + expectedLockName + "] but got null."); } if (lock != null && !expectedLockName.equals(info.getLockName())) { throw new RuntimeException(t.getName() + " expected to be blocked on lock [" + expectedLockName + "] but got [" + info.getLockName() + "]."); } if (owner == null && info.getLockOwnerName() != null) { throw new RuntimeException("Lock owner is expected " + " to be null but got " + info.getLockOwnerName()); } if (owner != null && info.getLockOwnerName() == null) { throw new RuntimeException("Lock owner is expected to be " + owner.getName() + " but got null."); } if (owner != null && !info.getLockOwnerName().equals(owner.getName())) { throw new RuntimeException("Lock owner is expected to be " + owner.getName() + " but got " + owner.getName()); } if (owner == null && info.getLockOwnerId() != -1) { throw new RuntimeException("Lock owner is expected " + " to be -1 but got " + info.getLockOwnerId()); } if (owner != null && info.getLockOwnerId() <= 0) { throw new RuntimeException("Lock owner is expected to be " + owner.getName() + "(id = " + owner.getId() + ") but got " + info.getLockOwnerId()); } if (owner != null && info.getLockOwnerId() != owner.getId()) { throw new RuntimeException("Lock owner is expected to be " + owner.getName() + "(id = " + owner.getId() + ") but got " + info.getLockOwnerId()); } if (info.isSuspended()) { throw new RuntimeException(t.getName() + " isSuspended() returns " + info.isSuspended()); } }
Example 19
Source File: GameMonitor.java From jforgame with Apache License 2.0 | 4 votes |
@Override public String printServerState() { final long ONE_MB = 1024 * 1024; String newLine = "\n"; StringBuilder result = new StringBuilder(); try { // 空闲内存 long freeMemory = Runtime.getRuntime().freeMemory() / ONE_MB; // 当前内存 long totalMemory = Runtime.getRuntime().totalMemory() / ONE_MB; // 最大可使用内存 long maxMemory = Runtime.getRuntime().maxMemory() / ONE_MB; result.append(String.format("freeMemory: %s mb", freeMemory)).append(newLine); result.append(String.format(String.format("usedMemory: %s mb", (totalMemory - freeMemory)))).append(newLine); result.append(String.format("totalMemory: %s mb", totalMemory)).append(newLine); result.append(String.format("maxMemory: %s mb", maxMemory)).append(newLine); MemoryMXBean memoryMXBean = getMemoryMXBean(); result.append(String.format("heap memory used: %s mb", memoryMXBean.getHeapMemoryUsage().getUsed() / ONE_MB)).append(newLine); result.append(String.format("heap memory usage: %s", memoryMXBean.getHeapMemoryUsage())).append(newLine); result.append(String.format("nonHeap memory usage: %s", memoryMXBean.getNonHeapMemoryUsage())).append(newLine); List<BufferPoolMXBean> buffMXBeans = (List<BufferPoolMXBean>) getPlatformMXBeans(BufferPoolMXBean.class); for (BufferPoolMXBean buffMXBean : buffMXBeans) { result.append(String.format("buffer pool[%s]: used %s mb, total %s mb", buffMXBean.getName(), buffMXBean.getMemoryUsed() / ONE_MB, buffMXBean.getTotalCapacity() / ONE_MB)).append(newLine); } List<GarbageCollectorMXBean> gcMXBeans = getGarbageCollectorMXBeans(); for (GarbageCollectorMXBean gcBean : gcMXBeans) { result.append(String.format("%s 发生 %s 次 gc, gc 总共消耗 %s 毫秒", gcBean.getName(), gcBean.getCollectionCount(), gcBean.getCollectionTime())).append(newLine); } ThreadMXBean threadMXBean = getThreadMXBean(); int nThreadRun = 0; int nThreadBlocked = 0; int nThreadWaiting = 0; for (long threadId : threadMXBean.getAllThreadIds()) { ThreadInfo threadInfo = threadMXBean.getThreadInfo(threadId); if (threadInfo.getThreadState() == Thread.State.RUNNABLE) { nThreadRun++; } if (threadInfo.getThreadState() == Thread.State.BLOCKED) { nThreadBlocked++; } if (threadInfo.getThreadState() == Thread.State.WAITING) { nThreadWaiting++; } } result.append(String.format("活跃线程数 %s, 阻塞线程数 %s, 等待线程数 %s", nThreadRun, nThreadBlocked, nThreadWaiting)).append(newLine); } catch (Exception e) { LoggerUtils.error("", e); } return result.toString(); }
Example 20
Source File: ThreadInfoCompositeData.java From hottub with GNU General Public License v2.0 | 4 votes |
static void checkThreadInfo(ThreadInfo info) throws Exception { if (info.getThreadId() != ((Long) values[THREAD_ID]).longValue()) { throw new RuntimeException("Thread Id = " + info.getThreadId() + " expected = " + values[THREAD_ID]); } if (!info.getThreadName().equals(values[THREAD_NAME])) { throw new RuntimeException("Thread Name = " + info.getThreadName() + " expected = " + values[THREAD_NAME]); } if (info.getThreadState() != Thread.State.RUNNABLE) { throw new RuntimeException("Thread Name = " + info.getThreadName() + " expected = " + Thread.State.RUNNABLE); } if (info.getBlockedTime() != ((Long) values[BLOCKED_TIME]).longValue()) { throw new RuntimeException("blocked time = " + info.getBlockedTime() + " expected = " + values[BLOCKED_TIME]); } if (info.getBlockedCount() != ((Long) values[BLOCKED_COUNT]).longValue()) { throw new RuntimeException("blocked count = " + info.getBlockedCount() + " expected = " + values[BLOCKED_COUNT]); } if (info.getWaitedTime() != ((Long) values[WAITED_TIME]).longValue()) { throw new RuntimeException("waited time = " + info.getWaitedTime() + " expected = " + values[WAITED_TIME]); } if (info.getWaitedCount() != ((Long) values[WAITED_COUNT]).longValue()) { throw new RuntimeException("waited count = " + info.getWaitedCount() + " expected = " + values[WAITED_COUNT]); } if (!info.getLockName().equals(values[LOCK_NAME])) { throw new RuntimeException("Lock Name = " + info.getLockName() + " expected = " + values[LOCK_NAME]); } if (info.getLockOwnerId() != ((Long) values[LOCK_OWNER_ID]).longValue()) { throw new RuntimeException( "LockOwner Id = " + info.getLockOwnerId() + " expected = " + values[LOCK_OWNER_ID]); } if (!info.getLockOwnerName().equals(values[LOCK_OWNER_NAME])) { throw new RuntimeException("LockOwner Name = " + info.getLockOwnerName() + " expected = " + values[LOCK_OWNER_NAME]); } checkStackTrace(info.getStackTrace()); checkLockInfo(info.getLockInfo()); }