线程类静态映射 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