Ever needed to know which thread was blocking another from executing? Then take a look at the thread dump!
A thread dump is a snapshot containing information about currently running (and waiting) threads within the Java Virtual Machine. The thread dump is useful in determining what is causing a deadlock to occur – where one thread is preventing another from executing.
A thread dump can be obtained by sending a signal to the JVM. Alternatively, by using thread introspection methods introduced in JDK 1.5, it is possible to generate a thread dump from anywhere in your code.
The following code snippet generates a thread dump along with JVM memory usage. Often, it is useful to examine memory usage in these cases also, as OutOfMemory errors can lead to unexpected and inconsistent thread behaviour.

// Thread dump
Map stackTraces = Thread.getAllStackTraces();
long timeStamp = System.currentTimeMillis();
log.error("************* Start Thread Dump " + timeStamp + " *******************");
log.error(" -- Memory Details --");
log.error("Total Memory = " + (Runtime.getRuntime().totalMemory()/MEGABYTE) + "MB");
log.error("Used Memory = " + (Runtime.getRuntime().totalMemory()/MEGABYTE - Runtime.getRuntime().freeMemory()/MEGABYTE) + "MB");
log.error("Total Memory = " + (Runtime.getRuntime().freeMemory()/MEGABYTE) + "MB");
log.error(" --- --- --- ---");
for (Iterator iterator = stackTraces.keySet().iterator(); iterator.hasNext();)
{
Thread thread = (Thread) iterator.next();
StackTraceElement[] stackTraceElement = (StackTraceElement[]) stackTraces.get(thread);
log.error("Thread= " + thread.getName() + " " + (thread.isDaemon()?"daemon":"") + " prio=" + thread.getPriority + "id=" + thread.getId() + " " + thread.getState());
for(int i = 0; i <= (stackTraceElement.length -1); i++)
{
log.error("t" + stackTraceElement[i]);
}
log.error(" --- --- --- ---");
}
log.error("************* End Thread Dump " + timeStamp + " *******************");

Again – this code will only compile and run with JDK1.5.
Further information on examining and making sense of the output is available here!

To Catch A ThreadDump...