Home »
Java programming language
Java Thread Class static void dumpStack() method with Example
Java Thread Class static void dumpStack() method: Here, we are going to learn about the static void dumpStack() method of Thread class with its syntax and example.
Submitted by Preeti Jain, on July 18, 2019
Thread Class static void dumpStack()
- This method is available in package java.lang.Thread.dumpStack().
- This method is used to print or display stack tracing of the current thread to System.err (Standard error stream).
- The purpose of this method is basically for debugging (i.e. If we call multiple methods so it is difficult to find an error so with the help of this method we can find an error in stack trace or stack hierarchy).
- This method is static so this method is accessible with classname too like Thread.dumpStack().
- The return type of this method is void it does not return anything.
- This method does not raise any exception.
Syntax:
static void dumpStack(){
}
Parameter(s):
We don't pass any object as a parameter in the method of the File.
Return value:
The return type of this method is void, it does not return anything.
Java program to demonstrate example of dumpStack() method
/* 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;
public class PrintStackTraceOfCurrentThread {
public static void main(String[] args) {
// By using currentThread() of Thread class will return a
// reference of currently executing thread.
Thread th = Thread.currentThread();
// By using setName() method we are setting the name
// of current executing thread
th.setName("Main Thread");
// By using setPriority() method we are setting the
// priority of current executing thread
th.setPriority(2);
//Display Current Executing Thread
System.out.println("Currently Executing Thread is :" + th);
int active_thread = Thread.activeCount();
// Display the number of active threads in current threads thread group
System.out.println("The Current active threads is : " + active_thread);
// Display stack trace of current thread
// to the System.err (Standard error stream)
Thread.dumpStack();
}
}
Output
E:\Programs>javac PrintStackTraceOfCurrentThread.java
E:\Programs>java PrintStackTraceOfCurrentThread
Currently Executing Thread is :Thread[Main Thread,2,main]
The Current active threads is : 1
java.lang.Exception: Stack trace
at java.lang.Thread.dumpStack(Thread.java:1365)
at PrintStackTraceOfCurrentThread.main(PrintStackTraceOfCurrentThread.java:24)