Thread.dumpStack()
Phương thức static void dumpStack() của lớp Thread in stack trace của thread hiện tại ra System.err. Dùng chủ yếu cho debugging. Nội bộ nó tạo một Exception và gọi printStackTrace().
Cú pháp
public static void dumpStack()
Ví dụ
public class ThreadDumpStackDemo {
public static void main(String[] args) {
Thread t = Thread.currentThread();
t.setName("ThreadDumpStackDemo");
t.setPriority(1);
System.out.println("Current Thread: " + t);
System.out.println("Active threads: " + Thread.activeCount());
Thread[] threads = new Thread[Thread.activeCount()];
Thread.enumerate(threads);
for (int i = 0; i < threads.length; i++) {
System.out.println(i + ": " + threads[i]);
}
// In stack trace tại thời điểm này
Thread.dumpStack();
}
}
Kết quả
Current Thread: Thread[ThreadDumpStackDemo,1,main]
Active threads: 1
0: Thread[ThreadDumpStackDemo,1,main]
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1336)
at ThreadDumpStackDemo.main(ThreadDumpStackDemo.java:14)
Khác với Thread.currentThread().getStackTrace(): dumpStack() in ra System.err ngay lập tức, trả về void. getStackTrace() trả về mảng StackTraceElement[] để xử lý lập trình.