jump to navigation

How to take Java Thread Dump? May 30, 2009

Posted by Anoop Somasundaran in Java, Performance Tuning, Servers, Tomcat.
Tags: , , ,

Full Thread Dump is a complete list of active threads. A java thread dump is a way of finding out what each thread in the JVM is doing at a particular point of time. This is especially useful when your java application seems to hang when running under load. Thread dump will help you to find out where the threads are stuck.

A sample thread dump is given below

2009-04-28 05:21:57
Full thread dump Java HotSpot(TM) 64-Bit Server VM (11.2-b01 mixed mode):

"Keep-Alive-Timer" daemon prio=10 tid=0x000000005459ec00 nid=0x78d8 waiting on condition [0x000000004aa2c000..0x000000004aa2cc10]
java.lang.Thread.State: TIMED_WAITING (sleeping)
at java.lang.Thread.sleep(Native Method)
at sun.net.www.http.KeepAliveCache.run(KeepAliveCache.java:149)
at java.lang.Thread.run(Thread.java:619)

"Thread-349" daemon prio=10 tid=0x0000000054ea4c00 nid=0x700b runnable [0x000000004ab2d000..0x000000004ab2db90]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:129)
at java.io.BufferedInputStream.fill(BufferedInputStream.java:218)
at java.io.BufferedInputStream.read1(BufferedInputStream.java:258)
at java.io.BufferedInputStream.read(BufferedInputStream.java:317)
- locked (a java.io.BufferedInputStream)
at com.sun.jndi.ldap.Connection.run(Connection.java:805)
at java.lang.Thread.run(Thread.java:619)

Each section in thread dump indicates what the thread was doing.

How to take thread dump on UNIX:

First, find the process id by looking in the process table. You can generally get the process numbers of all running Java processes with the command:

ps axf | grep java

Run the following command to take the thread dump.

Kill –QUIT process_id

The thread dump will be sent to where ever the standard output is redirected to. (In tomcat, normally the thread dump will be sent to TOMCAT_HOME/logs/Catalina.out)

QUIT signal does not actually kill the java process. The thread dump will be sent to the standard output and the process will continue.

How to take thread dump on Windows:

press CTRL+Break

The thread dump is printed in the command window, and you must cut / paste to a separate file in order to continue working on it.

What happens when you take thread dump?

1. The Java process is paused — all threads simply stop dead in their tracks
2. The Main java process asks each thread in turn to give a complete account of what they’re doing
3. The thread dump is sent to standard error, or somewhere else, depending on your Java vendor
4. The Java process is unpaused — all threads simply continue where they left off.
The Java process usually keeps on running, and the whole process only takes a few seconds. Any activity, even input/output is suspended. After the thread dump has completed, everything returns back to normal, just as if nothing had happened.


Accessing JSPs kept outside the servlet context May 23, 2009

Posted by Anoop Somasundaran in JSP, Tomcat.
Tags: ,

In one of my projects we had a requirement to access the JSPs/HTMLs which were kept outside the servlet context. The following is the trick that worked for me.

My application was located at /opt/TOMCAT_HOME/webapps/app1 and I had to access (include) the HTML files which were located at a different folder (/folder/files/).

Configuring tomcat to access the Files:

Add the following tag under the <Host name="localhost".. tag in server.xml

<Context path="/app1/files" docBase="/folder/files" debug="0" reloadable="true" crossContext="true"/>

The files for the request http://localhost:8080/app1/files/file.jsp will be fetched from the folder /folder/files.

This will work just fine if you call the JSPs directly. How ever if you want to include the files in a different context in your JSP neither action nor directive will work and will throw error saying that page cannot found.

In my project I had to include the files (HTMLs) in my JSP and the above tags didn’t work.

In this case the JSTL tag <c:import> will work perfectly. Remember that you will have to provide the complete url (http://localhost:8080/app1/files) for the parameter ‘url’ and relative path /app1/files will not work.

<c:import url="http://localhost:8080/app1/files" />

This will help to access the JSPs which are placed in a different context as well. However make sure that you are using crossContext=”true” in the context tag.