Java线程类StackTraceElement [] getStackTrace()方法(带示例)

本文详细介绍了Java中Thread类的getStackTrace()方法,该方法用于返回表示线程堆栈转储的StackTraceElement数组。文章解释了堆栈的顶部和底部的概念,以及如何通过示例程序展示堆栈跟踪元素。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

线程类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)


翻译自: https://www.includehelp.com/java/thread-class-stacktraceelement-getstacktrace-method-with-example.aspx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值