java线程stack_Java线程类静态映射getAllStackTraces()方法(带示例)

线程类静态映射 getAllStackTraces()包java.lang.Thread.getAllStackTraces()中提供了此方法。

此方法用于返回所有现有或活动线程的堆栈跟踪图。

此方法返回一个Map,并且Map包含两个因素,一个是键,另一个是值。所以key表示线程,value是StackTrace元素的数组,表示相应线程的堆栈转储。

此方法是静态的,因此也可以使用类名访问此方法,例如Thread.getAllStackTraces()。

此方法的返回类型为Map,因此它返回所需或相应线程的堆栈跟踪元素数组。

如果检查访问权限拒绝获取线程的堆栈跟踪,则此方法将引发异常。

语法:static Map getAllStackTraces(){

}

参数:

在Thread方法中,我们不传递任何对象作为参数。

返回值:

此方法的返回类型是Map,其中包含线程和相应线程的堆栈跟踪元素数组。

Java程序演示getAllStackTraces()方法示例/*  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;

import java.util.Map;

class GetAllStackTraces extends Thread {

//我们正在重写run()Thread类的方法

//在GetAllStackTraces-

public void run() {

System.out.println("We are in GetAllStackTraces run() method");

}

public static void main(String[] args) {

//创建GetAllStackTraces的线程对象

GetAllStackTraces gast = new GetAllStackTraces();

//创建一个Thread类对象并传递

//GetAllStackTraces将对象插入其中。

Thread th = new Thread(gast);

/*  Call start() method of thread class and

then thread class start() will call run()             of GetAllStackTraces

*/

th.start();

//返回对应线程的堆栈跟踪图

Map m = Thread.getAllStackTraces();

}

}

输出结果E:\Programs>javac GetAllStackTraces.java

E:\Programs>java GetAllStackTraces

We are in GetAllStackTraces run() method

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值