Using Java code to trigger a build threaddump

Source: Internet
Author: User

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");
}
}
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.