一段打印堆栈的java代码

    import java.io.BufferedWriter;
    import java.io.File;
    import java.io.FileWriter;
    import java.io.IOException;
    import java.io.Writer;
    import java.lang.management.ManagementFactory;
    import java.lang.management.ThreadInfo;
    import java.lang.management.ThreadMXBean;
    import java.text.SimpleDateFormat;
    import java.util.Date;
    import java.util.HashMap;
    import java.util.Map;
     
    /**
     * Code which writes a stack dump for all threads to a file.
     */
    public class DumpStack {
     
            // directory where the stack files are written
            private static final String STACK_DUMP_DIR = "./";
     
            // here for testing
            public static void main(String[] args) throws Exception {
                    dumpStacks();
            }
     
            public static void dumpStacks() 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);
                    }
     
                    // choose our dump-file
                    File dumpFile = new File(STACK_DUMP_DIR, "stacks." + System.currentTimeMillis());
                    Writer writer = new BufferedWriter(new FileWriter(dumpFile));
                    try {
                            dumpTraces(mxBean, threadInfoMap, writer);
                    } finally {
                            writer.close();
                    }
            }
     
            private static void dumpTraces(ThreadMXBean mxBean, Map<Long, ThreadInfo> threadInfoMap, Writer writer)
                            throws IOException {
                    Map<Thread, StackTraceElement[]> stacks = Thread.getAllStackTraces();
                    writer.write("Dump of " + stacks.size() + " thread at "
                                    + new SimpleDateFormat("yyyy/MM/dd HH:mm:ss z").format(new Date(System.currentTimeMillis())) + "\n\n");
                    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") + "\n");
                            ThreadInfo threadInfo = threadInfoMap.get(thread.getId());
                            if (threadInfo != null) {
                                    writer.write("    native=" + threadInfo.isInNative() + ", suspended=" + threadInfo.isSuspended()
                                                    + ", block=" + threadInfo.getBlockedCount() + ", wait=" + threadInfo.getWaitedCount() + "\n");
                                    writer.write("    lock=" + threadInfo.getLockName() + " owned by " + threadInfo.getLockOwnerName()
                                                    + " (" + threadInfo.getLockOwnerId() + "), cpu="
                                                    + (mxBean.getThreadCpuTime(threadInfo.getThreadId()) / 1000000L) + ", user="
                                                    + (mxBean.getThreadUserTime(threadInfo.getThreadId()) / 1000000L) + "\n");
                            }
                            for (StackTraceElement element : entry.getValue()) {
                                    writer.write("        ");
                                    writer.write(element.toString());
                                    writer.write("\n");
                            }
                            writer.write("\n");
                    }
            }
    }

 来自 http://pastebin.com/zwcKC0hz

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值