If I want to generate a thread dump and log it within my application (for diagnostic purposes), are there any utilities for generating one, or am I stuck cobbling together my own?
Asked
Active
Viewed 260 times
0
-
Why not use visualvm? It's part of the jdk distribution – Jeff Storey Nov 05 '13 at 19:46
-
Because I want my application to do it itself based on certain conditions. – Jun-Dai Bates-Kobashigawa Nov 05 '13 at 19:46
-
you might want to check this answer: http://stackoverflow.com/a/240217/594406 – Katona Nov 05 '13 at 19:53
3 Answers
1
This is a semi-duplicate of this question. Check out my answer here:
This does an approximation of the output seen when you do a kill -QUIT <my-pid>
. Not sure how much that matches the JVisualVM output.
The full code for the Thread stack dump method is posted on pastebin. A sample of the code is in the question listed.
0
Did you try StackTraceElement[] cause = Thread.currentThread().getStackTrace();
as recommended in Determining Current Call Stack (For Diagnostic Purposes).
Related Q/A:
-
Note that you should post question as duplicate instead of using it as your own answer. – Luiggi Mendoza Nov 05 '13 at 19:48
-
Also, none of the links posted helps OP to get a thread dump from the running application. – Luiggi Mendoza Nov 05 '13 at 19:49
-
Yes, but that's the starting point for cobbling together my own solution. I wanted to know if there were any libraries that will generate the full dump with thread names, locks, etc., formatted in a way that most thread dump analysis tools will understand. – Jun-Dai Bates-Kobashigawa Nov 05 '13 at 19:50
-
@LuiggiMendoza : I agree, this is a duplicate question. I won't mind if its flagged for close. – Aleš Nov 05 '13 at 19:51
-
@Jun-DaiBates-Kobashigawa that's covered in the possible duplicate question link. – Luiggi Mendoza Nov 05 '13 at 19:52
0
I'm currently using this. Not sure if it's exactly what you want.
import java.io.PrintWriter;
import java.io.StringWriter;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadInfo;
import java.lang.management.ThreadMXBean;
public class ThreadDumper
{
public static String dumpThreads()
{
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
try
{
ThreadMXBean bean = ManagementFactory.getThreadMXBean();
ThreadInfo[] threadInfos = bean.getThreadInfo(bean.getAllThreadIds(), Integer.MAX_VALUE);
for (int i = 0; i < threadInfos.length; i++)
{
ThreadInfo threadInfo = threadInfos[i];
pw.print(threadInfo);
}
pw.println("----------------------------------------");
pw.println();
long[] deadlockedIds = bean.findMonitorDeadlockedThreads();
if(deadlockedIds == null || deadlockedIds.length == 0)
{
pw.println("Detected 0 deadlocked threads.");
}
else
{
pw.println("Detected " + deadlockedIds.length + " deadlocked threads:");
threadInfos = bean.getThreadInfo(deadlockedIds, 0);
for (int i = 0; i < threadInfos.length; i++)
{
ThreadInfo threadInfo = threadInfos[i];
pw.print(threadInfo);
}
}
}
catch(Exception e)
{
pw.println("----------------------------------------");
pw.println("Exception occurred while taking thread dump:");
e.printStackTrace(pw);
pw.println("----------------------------------------");
}
return sw.toString();
}
}

Mike Clark
- 10,027
- 3
- 40
- 54