I want to generate thread dump through java code, I tried using ThreadMXBean for this but I am not getting the thread dump in the proper format as we are getting using jstack command. Please can any one provide some help..is their any other way to get thread dump... using any other API.. The format of thread dump that i want is -
2015-08-06 05:51:15
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.51-b03 mixed mode):
"Attach Listener" daemon prio=10 tid=0x00007fdab805d800 nid=0x7a36 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"ajp-mco/127.168.121.120:8170-220" daemon prio=10 tid=0x00007fdaa056d000 nid=0x7961 runnable [0x00007fda8bb7a000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at org.apache.coyote.ajp.AjpProcessor.read(AjpProcessor.java:1114)
at org.apache.coyote.ajp.AjpProcessor.readMessage(AjpProcessor.java:1196)
at org.apache.coyote.ajp.AjpProcessor.process(AjpProcessor.java:436)
at org.apache.coyote.ajp.AjpProtocol$AjpConnectionHandler.process(AjpProtocol.java:420)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920)
at java.lang.Thread.run(Thread.java:744)
Try
ManagementFactory.getThreadMXBean().dumpAllThreads(true, true)
using something likeAlternatively you can hack your way around and actually call
jstack
throughRuntime.getRuntime.exec()
:Here's the output for a sample program using
ThreadMXBean
:jstack
(thorugh the console or throughRuntime.getRuntime().exec()
):