Threaddump is a good thing for JVM diagnostics and tuning. In the past, we generate threaddump, generally need to be connected to the server on the process manager to do, whether it is ctrl+break or kill–3, more inconvenient. Sometimes, you might want to build and check the server's threaddump remotely, what about this? Luckily we have the following code to do:
Util.threaddump.ThreadDumpBuilder.java
Package util.threaddump;
Import Java.util.Map;
/**
* Generate Threaddump using Java remote code. Applies to JDK 1.5+.
* Reference: {@link thread#getstacktrace ()}
* {@link throwable#getstacktrace ()}
* @see Stacktraceelement
* @author beansoft@126.com
* Reprint Please indicate the source: beansoft.blogjava.net
*/
public class Threaddumpbuilder {
/**
* Generate and return Thread Dump.
* Reprint Please indicate the source: beansoft.blogjava.net
* @return
*/
Public String Build () {
StringBuilder output = new StringBuilder (1000);
For (Map.entry stackTrace:Thread.getAllStackTraces (). EntrySet ()) {
Appendthreadstacktrace (output, Thread) Stacktrace.getkey (),
(stacktraceelement[]) stacktrace.getvalue ());
}
return output.tostring ();
}
/**
* Process and output stack information.
* @param output
* Output Content
* @param thread
* Thread
* @param stack
* Thread Stack
*/
private void Appendthreadstacktrace (StringBuilder output, thread thread,
Stacktraceelement[] Stack) {
Ignore stack information for current thread
if (Thread.equals (Thread.CurrentThread ())) {
Return
}
Output.append (thread). Append ("\ n");
for (Stacktraceelement Element:stack) {
Output.append ("\ T"). Append (Element). Append ("\ n");
}
}
}