What is thread getAllStackTraces() function in Java?

The static function getAllStackTraces() returns the map of stack traces regarding all the live threads. Threads are referred to as keys of maps. Every map value is an array of StackTraceElement. This array shows the stack dump of related threads.

Syntax


static Map<Thread , StackTraceElement[]> getAllStackTraces()

Parameters

It does not take any arguments.

Return Value

Map<Thread , StackTraceElement[]>: This function returns the map to an array of StackTraceElement. This array contains the stack traces of related threads.

Example

In the code snippet below, we use this method to extract stack traces.

// Load libraries
import java.lang.Thread;
import java.util.*;
// Main class
public class Main_Thread implements Runnable {
public void run() {
System.out.println("---- run() function called ----");
}
// main method
public static void main(String args[]) {
Main_Thread th_trace = new Main_Thread();
Thread th = new Thread(th_trace);
// calling run() function
th.start();
// returning the map that contains stack traces
Map mp = Thread.getAllStackTraces();
int i=0;
for(Object key : mp.keySet()){
System.out.print(i + "\t");
System.out.println("Key="+ key + "\t" +
"Value=" + mp.get(key));
i++;
}
}
}

Free Resources