Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;

public class Main {
    private static final String lineSeperator = System.getProperty("line.separator");

    public static String getAllThreadDumps() {

        final StringBuilder dump = new StringBuilder();

        final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();

        final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
        dump.append(lineSeperator);

        for (ThreadInfo threadInfo : threadInfos) {
            dump.append('"');
            dump.append(threadInfo.getThreadName());
            dump.append("\" ");
            final Thread.State state = threadInfo.getThreadState();
            dump.append(lineSeperator);
            dump.append("   State: ");
            dump.append(state);
            final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
            for (final StackTraceElement stackTraceElement : stackTraceElements) {
                dump.append(lineSeperator);
                dump.append("        at ");
                dump.append(stackTraceElement);
            }
            dump.append(lineSeperator);
            dump.append(lineSeperator);
        }
        return dump.toString();
    }
}