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:

TestDumpThread.java
public class TestDumpThread {
        //Dump the current thread stack trace.
	public static void dumpCurrentStackTrace() {
		StackTraceElement[] stes = Thread.currentThread().getStackTrace();
		for (StackTraceElement element : stes) {
			System.out.println(element);
		}
	}

	public static void main(String[] args) {
		dumpCurrentStackTrace();
	}
}

I executed this program and the output was:

java.lang.Thread.getStackTrace(Unknown Source)
TestDumpThread.dumpCurrentStackTrace(TestDumpThread.java:3)
TestDumpThread.main(TestDumpThread.java:10)