What is the Thread.dumpStack() function in Java?

Thread.dumpStack() is a static method in Java used to debug the faulty code. This function prints the stack trace of a recent thread to the standard error stream.

Syntax


static void dumpStack()

Return value

No value is returned by this function.

Example

The code below shows the use of dumpStack().

// Load Thread class
import java.lang.Thread*;
// Main class
public class EdPresso {
// main method
public static void main(String[] args) {
Thread thread = Thread.currentThread();
th.setName("Main Thread");
// setting the priority of thread to 1
thread.setPriority(1);
// printing the recent thread
System.out.println("Thread = " + thread);
int threadCount = Thread.activeCount();
System.out.println("The threads that are active currently = " + threadCount);
/* printing current thread's stack trace to the standard
error stream used to debug*/
Thread.dumpStack();
}
}

Free Resources