Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 718
bronze man Avatar asked Apr 13 '26 02:04

bronze man


1 Answers

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)
like image 198
bronze man Avatar answered Apr 15 '26 14:04

bronze man



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!