use of java.lang.management.ThreadInfo in project geode by apache.
the class MemberMBeanBridge method fetchJvmThreads.
/**
* @return JVM thread list
*/
public String[] fetchJvmThreads() {
long[] threadIds = threadMXBean.getAllThreadIds();
ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadIds, 0);
if (threadInfos == null || threadInfos.length < 1) {
return ManagementConstants.NO_DATA_STRING;
}
ArrayList<String> thrdStr = new ArrayList<String>(threadInfos.length);
for (ThreadInfo thInfo : threadInfos) {
if (thInfo != null) {
thrdStr.add(thInfo.getThreadName());
}
}
String[] result = new String[thrdStr.size()];
return thrdStr.toArray(result);
}
use of java.lang.management.ThreadInfo in project metrics by dropwizard.
the class ThreadDump method dump.
/**
* Dumps all of the threads' current information to an output stream.
*
* @param out an output stream
*/
public void dump(OutputStream out) {
final ThreadInfo[] threads = this.threadMXBean.dumpAllThreads(true, true);
final PrintWriter writer = new PrintWriter(new OutputStreamWriter(out, UTF_8));
for (int ti = threads.length - 1; ti >= 0; ti--) {
final ThreadInfo t = threads[ti];
writer.printf("\"%s\" id=%d state=%s", t.getThreadName(), t.getThreadId(), t.getThreadState());
final LockInfo lock = t.getLockInfo();
if (lock != null && t.getThreadState() != Thread.State.BLOCKED) {
writer.printf("%n - waiting on <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
writer.printf("%n - locked <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
} else if (lock != null && t.getThreadState() == Thread.State.BLOCKED) {
writer.printf("%n - waiting to lock <0x%08x> (a %s)", lock.getIdentityHashCode(), lock.getClassName());
}
if (t.isSuspended()) {
writer.print(" (suspended)");
}
if (t.isInNative()) {
writer.print(" (running in native)");
}
writer.println();
if (t.getLockOwnerName() != null) {
writer.printf(" owned by %s id=%d%n", t.getLockOwnerName(), t.getLockOwnerId());
}
final StackTraceElement[] elements = t.getStackTrace();
final MonitorInfo[] monitors = t.getLockedMonitors();
for (int i = 0; i < elements.length; i++) {
final StackTraceElement element = elements[i];
writer.printf(" at %s%n", element);
for (int j = 1; j < monitors.length; j++) {
final MonitorInfo monitor = monitors[j];
if (monitor.getLockedStackDepth() == i) {
writer.printf(" - locked %s%n", monitor);
}
}
}
writer.println();
final LockInfo[] locks = t.getLockedSynchronizers();
if (locks.length > 0) {
writer.printf(" Locked synchronizers: count = %d%n", locks.length);
for (LockInfo l : locks) {
writer.printf(" - %s%n", l);
}
writer.println();
}
}
writer.println();
writer.flush();
}
use of java.lang.management.ThreadInfo in project metrics by dropwizard.
the class ThreadDeadlockDetectorTest method returnsASetOfThreadsIfAnyAreDeadlocked.
@Test
public void returnsASetOfThreadsIfAnyAreDeadlocked() {
final ThreadInfo thread1 = mock(ThreadInfo.class);
when(thread1.getThreadName()).thenReturn("thread1");
when(thread1.getLockName()).thenReturn("lock2");
when(thread1.getLockOwnerName()).thenReturn("thread2");
when(thread1.getStackTrace()).thenReturn(new StackTraceElement[] { new StackTraceElement("Blah", "bloo", "Blah.java", 150), new StackTraceElement("Blah", "blee", "Blah.java", 100) });
final ThreadInfo thread2 = mock(ThreadInfo.class);
when(thread2.getThreadName()).thenReturn("thread2");
when(thread2.getLockName()).thenReturn("lock1");
when(thread2.getLockOwnerName()).thenReturn("thread1");
when(thread2.getStackTrace()).thenReturn(new StackTraceElement[] { new StackTraceElement("Blah", "blee", "Blah.java", 100), new StackTraceElement("Blah", "bloo", "Blah.java", 150) });
final long[] ids = { 1, 2 };
when(threads.findDeadlockedThreads()).thenReturn(ids);
when(threads.getThreadInfo(eq(ids), anyInt())).thenReturn(new ThreadInfo[] { thread1, thread2 });
assertThat(detector.getDeadlockedThreads()).containsOnly(String.format(Locale.US, "thread1 locked on lock2 (owned by thread2):%n" + "\t at Blah.bloo(Blah.java:150)%n" + "\t at Blah.blee(Blah.java:100)%n"), String.format(Locale.US, "thread2 locked on lock1 (owned by thread1):%n" + "\t at Blah.blee(Blah.java:100)%n" + "\t at Blah.bloo(Blah.java:150)%n"));
}
use of java.lang.management.ThreadInfo in project suite by stupidsing.
the class Profiler method recordStats.
private void recordStats() {
long currentThreadId = Thread.currentThread().getId();
ThreadMXBean mx = ManagementFactory.getThreadMXBean();
long[] threadIds = mx.getAllThreadIds();
ThreadInfo[] threadInfos = mx.getThreadInfo(threadIds, stackTraceDepth);
count.getAndIncrement();
for (ThreadInfo threadInfo : threadInfos) if (//
threadInfo != null && //
threadInfo.getThreadId() != currentThreadId && //
threadInfo.getThreadState() == State.RUNNABLE && !String_.equals(threadInfo.getThreadName(), "ReaderThread")) {
StackTraceElement[] stackTrace = threadInfo.getStackTrace();
Set<String> elements = new HashSet<>();
int i = stackTrace.length;
Call call = callRoot;
// anonymous classes
while (0 < i) {
StackTraceElement elem = stackTrace[--i];
String fileName = elem.getFileName();
int lineNumber = elem.getLineNumber();
String mn = elem.getClassName() + "." + elem.getMethodName();
String fn = fileName != null ? " " + fileName + (1 < lineNumber ? ":" + lineNumber : "") : "<unknown>";
String name = mn + fn;
(call = call.callees.computeIfAbsent(mn, any -> new Call())).count++;
if (elements.add(name))
records.computeIfAbsent(name, any -> new Record()).count++;
}
}
}
use of java.lang.management.ThreadInfo in project suite by stupidsing.
the class ThreadMxBeanTest method test.
@Test
public void test() {
ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
System.out.println(threadBean.getThreadCount());
ThreadInfo[] threadInfos = threadBean.dumpAllThreads(false, false);
for (ThreadInfo threadInfo : threadInfos) if (threadInfo.getThreadState() == State.RUNNABLE)
System.out.println(threadInfo.toString());
}
Aggregations