bronze man
bronze man

Reputation: 1627

Print all stacktrace of all threads currently running in Java

I want to print all stacktraces of all threads currently running in Java process to stdout, to debug a deadlock of my server.

Upvotes: 3

Views: 2060

Answers (1)

bronze man
bronze man

Reputation: 1627

Here is the code, you can copy and paste the function body and use it without any import.

class HelloWorld {
public static void main(String[] args) {
    java.util.Collection<java.lang.StackTraceElement[]> a1 = java.lang.Thread.getAllStackTraces().values();
    for (java.lang.StackTraceElement[] a2 : a1){
        System.out.println("==========");
        for (java.lang.StackTraceElement a3 : a2){
            System.out.println(a3.toString());
        }
    }
}
}

Above code has been tested on:

openjdk version "1.8.0_252"
OpenJDK Runtime Environment (build 1.8.0_252-8u252-b09-1~18.04-b09)
OpenJDK 64-Bit Server VM (build 25.252-b09, mixed mode)

Upvotes: 2

Related Questions