The Java Thread getAllStackTraces() method returns a map of stack traces for all live threads. The map keys are threads and each map value is an array of StackTraceElement that represents the stack dump of the corresponding Thread.
DeclarationFollowing is the declaration for java.lang.Thread.getAllStackTraces() method
public static Map<Thread,StackTraceElement[]> getAllStackTraces()Parameters
NA
Return ValueThis method returns a Map from Thread to an array of StackTraceElement that represents the stack trace of the corresponding thread.
ExceptionSecurityException − if a security manager exists and its checkPermission method doesn't allow getting the stack trace of thread.
Example: Getting Map of Stacktraces in Multi-Threaded EnvironmentThe following example shows the usage of Java Thread getAllStackTraces() method. In this program, we've created a thread class ThreadDemo by implementing Runnable interface. In main method, ThreadDemo object is created and using that object, a thread is created and started. Now using getAllStackTraces() method, a map of stack trace is retrieved and printed.
package com.tutorialspoint; import java.util.*; public class ThreadDemo implements Runnable { public void run() { System.out.println("This is run() method"); } public static void main(String args[]) { ThreadDemo trace = new ThreadDemo(); Thread t = new Thread(trace); // this will call run() method t.start(); // returns a map of stack traces Map m = Thread.getAllStackTraces(); System.out.println(m); } }Output
Let us compile and run the above program, this will produce the following result −
This is run() method {Thread[#1,main,5,main]=[Ljava.lang.StackTraceElement;@5caf905d, Thread[#20,Common-Cleaner,8,InnocuousThreadGroup]=[Ljava.lang.StackTraceElement;@27716f4, Thread[#9,Reference Handler,10,system]=[Ljava.lang.StackTraceElement;@8efb846, Thread[#12,Attach Listener,5,system]=[Ljava.lang.StackTraceElement;@2a84aee7, Thread[#10,Finalizer,8,system]=[Ljava.lang.StackTraceElement;@a09ee92, Thread[#19,Notification Thread,9,system]=[Ljava.lang.StackTraceElement;@30f39991, Thread[#21,Thread-0,5,]=[Ljava.lang.StackTraceElement;@452b3a41, Thread[#11,Signal Dispatcher,9,system]=[Ljava.lang.StackTraceElement;@4a574795}Example: Getting Map of Stacktraces in Single Threaded Program
The following example shows the usage of Java Thread getAllStackTraces() method. In this program, we've created a class ThreadDemo. In main method, current thread is retrieved using currentThread() method and it is printed. Using activeCount(), the count of active thread is retrieved and printed. Now using getAllStackTraces() method, a map of stack trace is retrieved and printed.
package com.tutorialspoint; import java.util.Map; public class ThreadDemo { public static void main(String[] args) { Thread t = Thread.currentThread(); t.setName("Admin Thread"); // set thread priority to 1 t.setPriority(1); // prints the current thread System.out.println("Thread = " + t); int count = Thread.activeCount(); System.out.println("currently active threads = " + count); // returns a map of stack traces Map m = Thread.getAllStackTraces(); System.out.println(m); } }Output
Let us compile and run the above program, this will produce the following result −
Thread = Thread[#1,Admin Thread,1,main] currently active threads = 1 {Thread[#1,Admin Thread,1,main]=[Ljava.lang.StackTraceElement;@2f2c9b19, Thread[#19,Notification Thread,9,system]=[Ljava.lang.StackTraceElement;@31befd9f, Thread[#20,Common-Cleaner,8,InnocuousThreadGroup]=[Ljava.lang.StackTraceElement;@1c20c684, Thread[#12,Attach Listener,5,system]=[Ljava.lang.StackTraceElement;@1fb3ebeb, Thread[#9,Reference Handler,10,system]=[Ljava.lang.StackTraceElement;@548c4f57, Thread[#10,Finalizer,8,system]=[Ljava.lang.StackTraceElement;@1218025c, Thread[#11,Signal Dispatcher,9,system]=[Ljava.lang.StackTraceElement;@816f27d}
java_lang_thread.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4