Origin:
Today I see a tool class with one sentence:
String msgtoprint = Thread.CurrentThread (). Getstacktrace () [1].getmethodname ();
The result of the output is simple, that is, the method name of the calling class.
Document:
Getstacktrace ()
- Returns an array of stack trace elements representing the stack dump of the thread. If the thread has not been started or has been terminated, the method returns a 0-length array. If the returned array is not 0-length, its first element represents the top of the stack, which is the most recent method call in the sequence. The last element represents the bottom of the stack, which is the oldest method call in the sequence.
If there is a security manager, and the thread is not the current thread, the checkpermission method of the security manager is invoked through runtimepermission ("Getstacktrace") permissions. See if you can get a stack trace.
Some virtual machines may omit one or more stack frames from the stack trace in some cases. In extreme cases, virtual machines that do not have the thread stack trace information can return a 0-length array from the method.
-
Return:
An
- array of stacktraceelement , each representing a stack frame.
The first method of the thread class that is used:
public static Thread CurrentThread ()
The second method: (read the documentation for Java SE:)
Public stacktraceelement[] Getstacktrace ()
Returns an array of stack trace elements that represent the stack of this thread.
if:1. This thread is not turned on; 2. This thread is turned on but has not been run by the system (because threads need to be rotated according to certain rules); 3. This thread is over.
In these three cases, the length of the array returned by Getstacktrace () is 0.
If the returned array length is not 0, then the first element of the array represents the top element of the stack, which is the recent method in the call sequence.
the last element of the array represents the bottom element of the stack, which is the furthest element in the call sequence.
Program Test--java Program
To test the class, the main method is as follows:
public static void Main (string[] args) {stacktraceelement[] Stacktraceelements=thread.currentthread (). Getstacktrace ( ); System.out.println ("The stacktraceelements Length:" +stacktraceelements.length); for (int i=0;i< stacktraceelements.length;i++) {System.out.println ("\ n---the " +i+ " element" + "---"); System.out.println ("toString:" +stacktraceelements[i].tostring ()); System.out.println ("ClassName:" +stacktraceelements[i].getclassname ()); System.out.println ("FileName:" +stacktraceelements[i].getfilename ()); System.out.println ("LineNumber:" +stacktraceelements[i].getlinenumber ()); System.out.println ("MethodName:" +stacktraceelements[i].getmethodname ());}}
Post-Execution output:
The stacktraceelements length:2---the 0 element---toString:java.lang.Thread.getStackTrace (thread.java : 1567) Classname:java.lang.threadfilename:thread.javalinenumber:1567methodname:getstacktrace---the 1 Element---toString:Exchange.main (exchange.java:10) ClassName:ExchangeFileName:Exchange.javaLineNumber : 10methodname:main
If you use a private method again , output the information of the call stack
public static void Main (string[] args) {stacktraceelement[] Stacktraceelements=thread.currentthread (). Getstacktrace ( ); System.out.println ("The stacktraceelements Length:" +stacktraceelements.length); for (int i=0;i< stacktraceelements.length;i++) {System.out.println ("\ n---the" +i+ "element" + "---"); System.out.println ("toString:" +stacktraceelements[i].tostring ()); System.out.println ("ClassName:" +stacktraceelements[i].getclassname ()); System.out.println ("FileName:" +stacktraceelements[i].getfilename ()); System.out.println ("LineNumber:" +stacktraceelements[i].getlinenumber ()); System.out.println ("MethodName:" +stacktraceelements[i].getmethodname ());} Printstackinfos ();} private static void Printstackinfos () {stacktraceelement[] stacktraceelements = Thread.CurrentThread (). Getstacktrace ( ); System.out.println ("\ncalled in Printstackinfos () method!!!"); System.out.println ("The stacktraceelements Length:" +stacktraceelements.length); for (int i =0;i< stacktraceelements.length;i++) {System.out.println ("\ n---thE "+i+" element "+"---"); System.out.println ("toString:" +stacktraceelements[i].tostring ()); System.out.println ("ClassName:" +stacktraceelements[i].getclassname ()); System.out.println ("FileName:" +stacktraceelements[i].getfilename ()); System.out.println ("LineNumber:" +stacktraceelements[i].getlinenumber ()); System.out.println ("MethodName:" +stacktraceelements[i].getmethodname ());}}
The output results are as follows:
The stacktraceelements length:2---the 0 element---toString:java.lang.Thread.getStackTrace (thread.java : 1567) Classname:java.lang.threadfilename:thread.javalinenumber:1567methodname:getstacktrace---the 1 Element---toString:Exchange.main (exchange.java:10) ClassName:ExchangeFileName:Exchange.javaLineNumber : 10methodname:maincalled in Printstackinfos () method!!! The stacktraceelements length:3---the 0 element---toString:java.lang.Thread.getStackTrace (thread.java : 1567) Classname:java.lang.threadfilename:thread.javalinenumber:1567methodname:getstacktrace---the 1 Element---toString:Exchange.printStackInfos (exchange.java:24) Classname:exchangefilename: Exchange.javalinenumber:24methodname:printstackinfos---The 2 element---toString:Exchange.main ( EXCHANGE.JAVA:20) Classname:exchangefilename:exchange.javalinenumber:20methodname:main
You can see that after adding a private method, the stack element returned is one more, and its location is between the method that called it and the method it called .
Talking about Getstacktrace () method (I.)