Example usage for java.lang.management ThreadMXBean getAllThreadIds

List of usage examples for java.lang.management ThreadMXBean getAllThreadIds

Introduction

In this page you can find the example usage for java.lang.management ThreadMXBean getAllThreadIds.

Prototype

public long[] getAllThreadIds();

Source Link

Document

Returns all live thread IDs.

Usage

From source file:serposcope.controllers.admin.AdminController.java

public Result stackdump(Context context) {

    return Results.contentType("text/plain").render((ctx, res) -> {
        ResponseStreams responseStreams = context.finalizeHeaders(res);
        try (PrintWriter writer = new PrintWriter(responseStreams.getOutputStream());) {
            final ThreadMXBean threadMXBean = ManagementFactory.getThreadMXBean();
            final ThreadInfo[] threadInfos = threadMXBean.getThreadInfo(threadMXBean.getAllThreadIds(), 100);
            for (ThreadInfo threadInfo : threadInfos) {
                writer.append('"');
                writer.append(threadInfo.getThreadName());
                writer.append("\" ");
                final Thread.State state = threadInfo.getThreadState();
                writer.append("\n   java.lang.Thread.State: ");
                writer.append(state.toString());
                final StackTraceElement[] stackTraceElements = threadInfo.getStackTrace();
                for (final StackTraceElement stackTraceElement : stackTraceElements) {
                    writer.append("\n        at ");
                    writer.append(stackTraceElement.toString());
                }//from   w ww .  j ava 2 s.  c  o  m
                writer.println("\n");
            }
        } catch (IOException ex) {
            LOG.error("stackdump", ex);
        }
    });
}