Share on Google+Share on Google+

Java :Thread dumpStack

Advertisement
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");
		thread.setPriority(1);
		System.out.println("Current thread: " + thread);
		int count = Thread.activeCount();
		System.out.println("Active threads: " + count);
		Thread threads[] = new Thread[count];
		Thread.enumerate(threads);
		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
		 */
		Thread.dumpStack();
	}
}

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)

Advertisements

Advertisement

Posted on: November 6, 2012 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial 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.