- Java.lang 包类
- Java.lang - 首页
- Java.lang - Boolean
- Java.lang - Byte
- Java.lang - Character
- Java.lang - Character.Subset
- Java.lang - Character.UnicodeBlock
- Java.lang - Class
- Java.lang - ClassLoader
- Java.lang - Compiler
- Java.lang - Double
- Java.lang - Enum
- Java.lang - Float
- Java.lang - InheritableThreadLocal
- Java.lang - Integer
- Java.lang - Long
- Java.lang - Math
- Java.lang - Number
- Java.lang - Object
- Java.lang - Package
- Java.lang - Process
- Java.lang - ProcessBuilder
- Java.lang - Runtime
- Java.lang - RuntimePermission
- Java.lang - SecurityManager
- Java.lang - Short
- Java.lang - StackTraceElement
- Java.lang - StrictMath
- Java.lang - String
- Java.lang - StringBuffer
- Java.lang - StringBuilder
- Java.lang - System
- Java.lang - Thread
- Java.lang - ThreadGroup
- Java.lang - ThreadLocal
- Java.lang - Throwable
- Java.lang - Void
- Java.lang 包额外内容
- Java.lang - 接口
- Java.lang - 错误
- Java.lang - 异常
- Java.lang 包有用资源
- Java.lang - 有用资源
- Java.lang - 讨论
Java Thread getAllStackTraces() 方法
描述
Java Thread getAllStackTraces() 方法返回所有活动线程的堆栈跟踪映射。映射键是线程,每个映射值都是一个StackTraceElement数组,表示相应Thread的堆栈转储。
声明
以下是java.lang.Thread.getAllStackTraces()方法的声明
public static Map<Thread,StackTraceElement[]> getAllStackTraces()
参数
无
返回值
此方法返回一个从 Thread 到 StackTraceElement 数组的映射,该数组表示相应线程的堆栈跟踪。
异常
SecurityException − 如果存在安全管理器并且其 checkPermission 方法不允许获取线程的堆栈跟踪。
示例:在多线程环境中获取堆栈跟踪映射
以下示例演示了 Java Thread getAllStackTraces() 方法的用法。在此程序中,我们通过实现 Runnable 接口创建了一个线程类 ThreadDemo。在主方法中,创建 ThreadDemo 对象,并使用该对象创建一个线程并启动它。现在,使用 getAllStackTraces() 方法检索并打印堆栈跟踪映射。
package com.tutorialspoint; import java.util.*; public class ThreadDemo implements Runnable { public void run() { System.out.println("This is run() method"); } public static void main(String args[]) { ThreadDemo trace = new ThreadDemo(); Thread t = new Thread(trace); // this will call run() method t.start(); // returns a map of stack traces Map m = Thread.getAllStackTraces(); System.out.println(m); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
This is run() method {Thread[#1,main,5,main]=[Ljava.lang.StackTraceElement;@5caf905d, Thread[#20,Common-Cleaner,8,InnocuousThreadGroup]=[Ljava.lang.StackTraceElement;@27716f4, Thread[#9,Reference Handler,10,system]=[Ljava.lang.StackTraceElement;@8efb846, Thread[#12,Attach Listener,5,system]=[Ljava.lang.StackTraceElement;@2a84aee7, Thread[#10,Finalizer,8,system]=[Ljava.lang.StackTraceElement;@a09ee92, Thread[#19,Notification Thread,9,system]=[Ljava.lang.StackTraceElement;@30f39991, Thread[#21,Thread-0,5,]=[Ljava.lang.StackTraceElement;@452b3a41, Thread[#11,Signal Dispatcher,9,system]=[Ljava.lang.StackTraceElement;@4a574795}
示例:在单线程程序中获取堆栈跟踪映射
以下示例演示了 Java Thread getAllStackTraces() 方法的用法。在此程序中,我们创建了一个类 ThreadDemo。在主方法中,使用 currentThread() 方法检索当前线程并打印它。使用 activeCount() 检索并打印活动线程的数量。现在,使用 getAllStackTraces() 方法检索并打印堆栈跟踪映射。
package com.tutorialspoint; import java.util.Map; public class ThreadDemo { public static void main(String[] args) { Thread t = Thread.currentThread(); t.setName("Admin Thread"); // set thread priority to 1 t.setPriority(1); // prints the current thread System.out.println("Thread = " + t); int count = Thread.activeCount(); System.out.println("currently active threads = " + count); // returns a map of stack traces Map m = Thread.getAllStackTraces(); System.out.println(m); } }
输出
让我们编译并运行上述程序,这将产生以下结果:
Thread = Thread[#1,Admin Thread,1,main] currently active threads = 1 {Thread[#1,Admin Thread,1,main]=[Ljava.lang.StackTraceElement;@2f2c9b19, Thread[#19,Notification Thread,9,system]=[Ljava.lang.StackTraceElement;@31befd9f, Thread[#20,Common-Cleaner,8,InnocuousThreadGroup]=[Ljava.lang.StackTraceElement;@1c20c684, Thread[#12,Attach Listener,5,system]=[Ljava.lang.StackTraceElement;@1fb3ebeb, Thread[#9,Reference Handler,10,system]=[Ljava.lang.StackTraceElement;@548c4f57, Thread[#10,Finalizer,8,system]=[Ljava.lang.StackTraceElement;@1218025c, Thread[#11,Signal Dispatcher,9,system]=[Ljava.lang.StackTraceElement;@816f27d}
java_lang_thread.htm
广告