Monday 3 March 2014

getStackTrace : Get the Stack trace of a thread

public StackTraceElement[] getStackTrace()
Returns an array of stack trace elements representing the stack dump of this thread. The last element of the returned array represents the bottom of the stack, where as the top element represents the most recent method invocation call.

Example
class ThreadStackTrace implements Runnable{
 void print(){
  show();
 }

 void show(){
  display();
 }

 void display(){
  StackTraceElement[] trace = Thread.currentThread().getStackTrace();
  for(int i=0; i < trace.length; i++)
   System.out.println(trace[i]);
 }

 public void run(){
  print();
 }

 public static void main(String args[]){
  ThreadStackTrace task1 = new ThreadStackTrace();
  Thread t1 = new Thread(task1);
  t1.start();
 }
}
   
Output
java.lang.Exception: Stack trace
 at java.lang.Thread.dumpStack(Thread.java:1342)
 at ThreadStackTrace.display(ThreadStackTrace.java:22)
 at ThreadStackTrace.show(ThreadStackTrace.java:18)
 at ThreadStackTrace.print(ThreadStackTrace.java:14)
 at ThreadStackTrace.run(ThreadStackTrace.java:26)
 at java.lang.Thread.run(Thread.java:722)

Print current stack trace                                                 Get All Stack traces                                                 Home

No comments:

Post a Comment