use of java.lang.management.ThreadMXBean in project karaf by apache.
the class EnvironmentDumpProvider method dumpThreadsInformation.
private void dumpThreadsInformation(final PrintWriter outPW) {
final ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
if (null == mxBean) {
return;
}
outPW.println("Threads:");
outPW.printf(INDENT_KEY_VALUE_FORMAT, "live", formatLong(mxBean.getThreadCount())).println();
outPW.printf(INDENT_KEY_VALUE_FORMAT, "daemon", formatLong(mxBean.getDaemonThreadCount())).println();
outPW.printf(INDENT_KEY_VALUE_FORMAT, "peak", formatLong(mxBean.getPeakThreadCount())).println();
outPW.printf(INDENT_KEY_VALUE_FORMAT, "total", formatLong(mxBean.getTotalStartedThreadCount())).println();
}
use of java.lang.management.ThreadMXBean in project sling by apache.
the class ControlListener method dumpThreads.
// ---------- socket support
private void dumpThreads(final Socket socket) throws IOException {
final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean();
final ThreadInfo[] threadInfos = threadBean.dumpAllThreads(true, true);
for (ThreadInfo thread : threadInfos) {
printThread(socket, thread);
// add locked synchronizers
final LockInfo[] locks = thread.getLockedSynchronizers();
writeLine(socket, "-");
writeLine(socket, "- Locked ownable synchronizers:");
if (locks.length > 0) {
for (LockInfo li : locks) {
writeLine(socket, String.format("- - locked %s", formatLockInfo(li.getClassName(), li.getIdentityHashCode())));
}
} else {
writeLine(socket, "- - None");
}
// empty separator line
writeLine(socket, "-");
}
final long[] deadLocked;
if (threadBean.isSynchronizerUsageSupported()) {
deadLocked = threadBean.findDeadlockedThreads();
} else {
deadLocked = threadBean.findMonitorDeadlockedThreads();
}
if (deadLocked != null) {
final ThreadInfo[] dl = threadBean.getThreadInfo(deadLocked, true, true);
final Set<ThreadInfo> dlSet = new HashSet<ThreadInfo>(Arrays.asList(dl));
int deadlockCount = 0;
for (ThreadInfo current : dl) {
if (dlSet.remove(current)) {
// find and record a single deadlock
ArrayList<ThreadInfo> loop = new ArrayList<ThreadInfo>();
do {
loop.add(current);
for (ThreadInfo cand : dl) {
if (cand.getThreadId() == current.getLockOwnerId()) {
current = (dlSet.remove(cand)) ? cand : null;
break;
}
}
} while (current != null);
deadlockCount++;
// print the deadlock
writeLine(socket, "-Found one Java-level deadlock:");
writeLine(socket, "-=============================");
for (ThreadInfo thread : loop) {
writeLine(socket, String.format("-\"%s\" #%d", thread.getThreadName(), thread.getThreadId()));
writeLine(socket, String.format("- waiting on %s", formatLockInfo(thread.getLockInfo().getClassName(), thread.getLockInfo().getIdentityHashCode())));
writeLine(socket, String.format("- which is held by \"%s\" #%d", thread.getLockOwnerName(), thread.getLockOwnerId()));
}
writeLine(socket, "-");
writeLine(socket, "-Java stack information for the threads listed above:");
writeLine(socket, "-===================================================");
for (ThreadInfo thread : loop) {
printThread(socket, thread);
}
writeLine(socket, "-");
}
}
// "Thread-8":
// waiting to lock monitor 7f89fb80da08 (object 7f37a0968, a java.lang.Object),
// which is held by "Thread-7"
// "Thread-7":
// waiting to lock monitor 7f89fb80b0b0 (object 7f37a0958, a java.lang.Object),
// which is held by "Thread-8"
writeLine(socket, String.format("-Found %d deadlocks.", deadlockCount));
}
writeLine(socket, RESPONSE_OK);
}
Aggregations