public
static Map<Thread,StackTraceElement[]> getAllStackTraces()
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. The returned stack traces
are in the format specified for the getStackTrace method.
Example
import java.util.*; class ThreadStackTrace implements Runnable{ void print(){ show(); } void show(){ display(); } void display(){ try{ Thread.currentThread().sleep(1000); } catch(InterruptedException e){ } } public void run(){ print(); } public static void main(String args[]){ ThreadStackTrace task1 = new ThreadStackTrace(); Thread t1 = new Thread(task1); Thread t2 = new Thread(task1); Thread t3 = new Thread(task1); t1.start(); t2.start(); t3.start(); Map<Thread, StackTraceElement[]> obj = Thread.getAllStackTraces(); Set<Thread> s1 = obj.keySet(); Iterator<Thread> i1 = s1.iterator(); while(i1.hasNext()){ Thread key = i1.next(); System.out.println("Trace information for the thread " + key); StackTraceElement[] trace = obj.get(key); for(int i=0; i < trace.length; i++){ System.out.println(trace[i]); } System.out.println(); } } }
Output
Trace information for the thread Thread[Reference Handler,10,system] java.lang.Object.wait(Native Method) java.lang.Object.wait(Object.java:503) java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133) Trace information for the thread Thread[Thread-1,5,main] java.lang.Thread.sleep(Native Method) ThreadStackTrace.display(ThreadStackTrace.java:24) ThreadStackTrace.show(ThreadStackTrace.java:19) ThreadStackTrace.print(ThreadStackTrace.java:15) ThreadStackTrace.run(ThreadStackTrace.java:31) java.lang.Thread.run(Thread.java:722) Trace information for the thread Thread[Attach Listener,5,system] Trace information for the thread Thread[main,5,main] java.lang.Thread.dumpThreads(Native Method) java.lang.Thread.getAllStackTraces(Thread.java:1618) ThreadStackTrace.main(ThreadStackTrace.java:44) Trace information for the thread Thread[Thread-2,5,main] Trace information for the thread Thread[Finalizer,8,system] java.lang.Object.wait(Native Method) java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135) java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151) java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:177) Trace information for the thread Thread[Thread-0,5,main] java.lang.Thread.sleep(Native Method) ThreadStackTrace.display(ThreadStackTrace.java:24) ThreadStackTrace.show(ThreadStackTrace.java:19) ThreadStackTrace.print(ThreadStackTrace.java:15) ThreadStackTrace.run(ThreadStackTrace.java:31) java.lang.Thread.run(Thread.java:722) Trace information for the thread Thread[Signal Dispatcher,9,system]
As
you observe the output, It includes the trace information of all the
thread like user define, main thread and Background processing
threads.
You
can read the below posts for more information about Stack Trace.
No comments:
Post a Comment