dig090

Thread stack dump method

Oct 11th, 2012
1,411
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.91 KB | None | 0 0
  1. /*
  2.  * Method that dumps the stack trace for all of the threads to a file
  3.  */
  4. public static final String NEWLINE = System.getProperty("line.separator");
  5.  
  6. public void dumpStack(String message, Writer writer) throws IOException {
  7.     ThreadMXBean mxBean = ManagementFactory.getThreadMXBean();
  8.     ThreadInfo[] threadInfos = mxBean.getThreadInfo(mxBean.getAllThreadIds(), 0);
  9.     Map<Long, ThreadInfo> threadInfoMap = new HashMap<Long, ThreadInfo>();
  10.     for (ThreadInfo threadInfo : threadInfos) {
  11.         threadInfoMap.put(threadInfo.getThreadId(), threadInfo);
  12.     }
  13.  
  14.     try {
  15.         if (message != null) {
  16.             writer.write(message);
  17.             writer.write(NEWLINE);
  18.         }
  19.         Map<Thread, StackTraceElement[]> stacks = Thread.getAllStackTraces();
  20.         writer.write("Dump of " + stacks.size() + " threads at "
  21.                 + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z").format(new Date(System.currentTimeMillis()))
  22.                 + NEWLINE + NEWLINE);
  23.         for (Map.Entry<Thread, StackTraceElement[]> entry : stacks.entrySet()) {
  24.             Thread thread = entry.getKey();
  25.             writer.write("\"" + thread.getName() + "\" prio=" + thread.getPriority() + " tid=" + thread.getId()
  26.                     + " " + thread.getState() + " " + (thread.isDaemon() ? "deamon" : "worker") + NEWLINE);
  27.             ThreadInfo threadInfo = threadInfoMap.get(thread.getId());
  28.             if (threadInfo != null) {
  29.                 writer.write("    native=" + threadInfo.isInNative() + ", suspended=" + threadInfo.isSuspended()
  30.                         + ", block=" + threadInfo.getBlockedCount() + ", wait=" + threadInfo.getWaitedCount()
  31.                         + NEWLINE);
  32.                 writer.write("    lock="
  33.                         + threadInfo.getLockName()
  34.                         + " owned by "
  35.                         + threadInfo.getLockOwnerName()
  36.                         + " ("
  37.                         + threadInfo.getLockOwnerId()
  38.                         + "), cpu="
  39.                         + TimerHelper.durationMillisToString(mxBean.getThreadCpuTime(threadInfo.getThreadId()) / 1000000L)
  40.                         + ", user="
  41.                         + TimerHelper.durationMillisToString(mxBean.getThreadUserTime(threadInfo.getThreadId()) / 1000000L)
  42.                         + NEWLINE);
  43.             }
  44.             for (StackTraceElement element : entry.getValue()) {
  45.                 writer.write("    ");
  46.                 String eleStr = element.toString();
  47.                 if (eleStr.startsWith("com.mprew")) {
  48.                     writer.write(">>  ");
  49.                 } else {
  50.                     writer.write("    ");
  51.                 }
  52.                 writer.write(eleStr);
  53.                 writer.write(NEWLINE);
  54.             }
  55.             writer.write(NEWLINE);
  56.         }
  57.         writer.write("------------------------------------------------------");
  58.         writer.write(NEWLINE);
  59.         writer.write("Non-daemon threads: ");
  60.         for (Thread thread : stacks.keySet()) {
  61.             if (!thread.isDaemon()) {
  62.                 writer.write("\"" + thread.getName() + "\", ");
  63.             }
  64.         }
  65.         writer.write(NEWLINE);
  66.         writer.write("------------------------------------------------------");
  67.         writer.write(NEWLINE);
  68.         writer.write("Blocked threads: ");
  69.         for (Thread thread : stacks.keySet()) {
  70.             if (thread.getState() == State.BLOCKED) {
  71.                 writer.write("\"" + thread.getName() + "\", ");
  72.             }
  73.         }
  74.         writer.write(NEWLINE);
  75.     } finally {
  76.         writer.close();
  77.     }
  78. }
Add Comment
Please, Sign In to add comment