Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Method that dumps the stack trace for all of the threads to a file
- */
- public static final String NEWLINE = System.getProperty("line.separator");
- public void dumpStack(String message, Writer writer) throws IOException {
- ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
- ThreadInfo[] threadInfos = mxBean.getThreadInfo(mxBean.getAllThreadIds(), 0);
- Map<Long, ThreadInfo> threadInfoMap = new HashMap<Long, ThreadInfo>();
- for (ThreadInfo threadInfo : threadInfos) {
- threadInfoMap.put(threadInfo.getThreadId(), threadInfo);
- }
- try {
- if (message != null) {
- writer.write(message);
- writer.write(NEWLINE);
- }
- Map<Thread, StackTraceElement[]> stacks = Thread.getAllStackTraces();
- writer.write("Dump of " + stacks.size() + " threads at "
- + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z").format(new Date(System.currentTimeMillis()))
- + NEWLINE + NEWLINE);
- for (Map.Entry<Thread, StackTraceElement[]> entry : stacks.entrySet()) {
- Thread thread = entry.getKey();
- writer.write("\"" + thread.getName() + "\" prio=" + thread.getPriority() + " tid=" + thread.getId()
- + " " + thread.getState() + " " + (thread.isDaemon() ? "deamon" : "worker") + NEWLINE);
- ThreadInfo threadInfo = threadInfoMap.get(thread.getId());
- if (threadInfo != null) {
- writer.write(" native=" + threadInfo.isInNative() + ", suspended=" + threadInfo.isSuspended()
- + ", block=" + threadInfo.getBlockedCount() + ", wait=" + threadInfo.getWaitedCount()
- + NEWLINE);
- writer.write(" lock="
- + threadInfo.getLockName()
- + " owned by "
- + threadInfo.getLockOwnerName()
- + " ("
- + threadInfo.getLockOwnerId()
- + "), cpu="
- + TimerHelper.durationMillisToString(mxBean.getThreadCpuTime(threadInfo.getThreadId()) / 1000000L)
- + ", user="
- + TimerHelper.durationMillisToString(mxBean.getThreadUserTime(threadInfo.getThreadId()) / 1000000L)
- + NEWLINE);
- }
- for (StackTraceElement element : entry.getValue()) {
- writer.write(" ");
- String eleStr = element.toString();
- if (eleStr.startsWith("com.mprew")) {
- writer.write(">> ");
- } else {
- writer.write(" ");
- }
- writer.write(eleStr);
- writer.write(NEWLINE);
- }
- writer.write(NEWLINE);
- }
- writer.write("------------------------------------------------------");
- writer.write(NEWLINE);
- writer.write("Non-daemon threads: ");
- for (Thread thread : stacks.keySet()) {
- if (!thread.isDaemon()) {
- writer.write("\"" + thread.getName() + "\", ");
- }
- }
- writer.write(NEWLINE);
- writer.write("------------------------------------------------------");
- writer.write(NEWLINE);
- writer.write("Blocked threads: ");
- for (Thread thread : stacks.keySet()) {
- if (thread.getState() == State.BLOCKED) {
- writer.write("\"" + thread.getName() + "\", ");
- }
- }
- writer.write(NEWLINE);
- } finally {
- writer.close();
- }
- }
Add Comment
Please, Sign In to add comment