- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
What is the purpose of using a dumpStack() method in Java?
The dumpStack() method is a static method of Thread class and it can be used to print or display stack tracing of the current thread to System.err. The purpose of the dumpStack() method is basically for debugging and Internally this method is calling the printStackTrace() method of Throwable class. This method does not raise any exceptions.
Syntax
public static void dumpStack()
Example
public class ThreadDumpStackTest { public static void main(String args[]) { Thread t = Thread.currentThread(); t.setName("ThreadDumpStackTest"); t.setPriority(1); System.out.println("Current Thread: " + t); int count = Thread.activeCount(); System.out.println("Active threads: " + count); Thread threads[] = new Thread[count]; Thread.enumerate(threads); for (int i = 0; i < count; i++) { System.out.println(i + ": " + threads[i]); } Thread.dumpStack(); } }
Output
Current Thread: Thread[#1,ThreadDumpStackTest,1,main] Active threads: 1 0: Thread[#1,ThreadDumpStackTest,1,main] java.lang.Exception: Stack trace at java.base/java.lang.Thread.dumpStack(Thread.java:2282) at ThreadDumpStackTest.main(ThreadDumpStackTest.java:14)
Advertisements