线程类StackTraceElement [] getStackTrace() (Thread Class StackTraceElement[] getStackTrace())
This method is available in package java.lang.Thread.getStackTrace().
软件包java.lang.Thread.getStackTrace()中提供了此方法。
This method is used to return an array of stack trace elements that denote the stack dump of the thread.
此方法用于返回表示线程的堆栈转储的堆栈跟踪元素数组。
This method is not static so this method is accessible with Thread class object it is not accessible with the class name.
此方法不是静态的,因此该方法可通过Thread类对象访问,而无法通过类名称访问。
This method returns an array of stack trace element and stack has two terms top of the stack and bottom of the stack. Top of the stack contains the method which is called in the last of the hierarchy or sequence and Bottom of the stack contains the method which is called in the first of the hierarchy or sequence.
此方法返回一个堆栈跟踪元素数组,并且堆栈在堆栈的顶部和底部都有两个术语。 堆栈的顶部包含在层次结构或序列中的最后一个调用的方法,堆栈的底部包含在层次结构或序列中的第一个调用的方法。
The return type of this method is StackTraceElement[], so it returns an array of stack trace elements.
该方法的返回类型为StackTraceElement [] ,因此它返回一个堆栈跟踪元素数组。
This method raises an exception if the checkPermission method is not allowed to get the stack trace element.
如果不允许checkPermission方法获取堆栈跟踪元素,则此方法引发异常。
Syntax:
句法:
StackTraceElement[] getStackTrace(){
}
Parameter(s):
参数:
We don't pass any object as a parameter in the method of the Thread.
我们不会在Thread方法中将任何对象作为参数传递。
Return value:
返回值:
The return type of this method is StackTraceElement[], it returns an array of stack trace elements and each stack element denotes one stack frame.
此方法的返回类型为StackTraceElement [] ,它返回一个堆栈跟踪元素数组,每个堆栈元素表示一个堆栈帧。
Java程序演示getStackTrace()方法的示例 (Java program to demonstrate example of getStackTrace() method)
/* We will use Thread class methods so we are importing
the package but it is not mandate because
it is imported by default
*/
import java.lang.Thread;
class GetStackTrace {
public static void main(String[] args) {
// We are calling main1() method from main() method
// and this method will call first
main1();
}
public static void main1() {
// We are calling main2() method from main1() method and
// this method will call after first method main1() called
// [i.e This method main2() at the second position
main2();
}
public static void main2() {
// We are calling main3() method from main2() method and
// this method will call after second method main2() called
// [i.e This method main3() will call at the third position
main3();
}
public static void main3() {
// Creating a reference for Stack Trace Element
StackTraceElement[] ste = Thread.currentThread().getStackTrace();
System.out.println("We are using for loop to display stack trace elements");
for (StackTraceElement stack_tra_ele: ste)
System.out.println(stack_tra_ele);
}
}
Output
输出量
E:\Programs>javac GetStackTrace.java
E:\Programs>java GetStackTrace
We are using for loop to display stack trace elements
java.lang.Thread.getStackTrace(Thread.java:1589)
GetStackTrace.main3(GetStackTrace.java:29)
GetStackTrace.main2(GetStackTrace.java:22)
GetStackTrace.main1(GetStackTrace.java:15)
GetStackTrace.main(GetStackTrace.java:7)