How to get the stack trace of current thread in Java

This tutorial demonstrates how to dump stack track of the current thread in Java, this is very useful troubleshoot or debug skill to identify the states of current running thread.

Use Thread.currentThread().getStackTrace() to retrieve an array of StackTraceElements that represent the current stack trace.

This is the example code:
public class TestDumpThread {
        //Dump the current thread stack trace.
	public static void dumpCurrentStackTrace() {
		StackTraceElement[] stes = Thread.currentThread().getStackTrace();
		for (StackTraceElement element : stes) {

	public static void main(String[] args) {

I executed this program and the output was:

java.lang.Thread.getStackTrace(Unknown Source)
Rate article
Add a comment

Your email address will not be published. Required fields are marked *

  1. Michael Vorburger

    So according to and its (much) faster to use new Throwable().getStackTrace() than the Thread.currentThread().getStackTrace(); proposed here.