Home Tutorial Java Core Java :Thread dumpStack


Share on Google+Share on Google+
Java :Thread dumpStack
Posted on: November 6, 2012 at 12:00 AM
In this tutorial you will learn about Java Thread dumpStack .

Java :Thread dumpStack

In this tutorial you will learn about Java Thread dumpStack .

Thread dumpStack :

JVM gives the concept of Thread Dump which provides you to represent a code level execution snapshot of all the threads which are created.
When you create any thread ,it doesn't mean that it is actually working. Some threads are running, some of them waiting for I/O operations, some of them going to die or in waiting stage. Functionality of Thread stack trace is to provide you with a snapshot of its current execution. Its first line show the native info about the thread as its name, state etc.

public static void dumpStack() : This method prints a stack trace of your current thread. In general, we use this method only for debugging.

Example : In this example we are using dumpStack() method.

class ThreadDumpStack {
	public static void main(String args[]) {
		Thread thread = Thread.currentThread();
		thread.setName("My ThreadDumpStack");
		System.out.println("Current thread: " + thread);
		int count = Thread.activeCount();
		System.out.println("Active threads: " + count);
		Thread threads[] = new Thread[count];
		for (int i = 0; i < count; i++) {
			System.out.println(i + ": " + threads[i]);
		 * prints a stack trace of the current thread to the standard error
		 * stream, used for debugging

Output :

Current thread: Thread[My ThreadDumpStack,1,main]
Active threads: 1
0: Thread[My ThreadDumpStack,1,main]
java.lang.Exception: Stack trace
	at java.lang.Thread.dumpStack(Unknown Source)
	at ThreadDumpStack.main(ThreadDumpStack.java:18)


Related Tags for Java :Thread dumpStack :

Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: November 6, 2012

Recommend the tutorial

Advertisements Advertisements