Recently I was debugging some nasty deadlocks and as one does in this kind of situation I needed to look into thread dumps. Unfortunately for me, the only thing I had was heap dump. Do not get me wrong heap dumps contain much more information, including the needed thread info. But heap dumps are also very big (can be gigabytes) so they are not very practical if you want to share them with a co-worker or upload them to online analyzer tools like FastThread. All I wanted was a simple text file with the thread dump.
I started to google and my surprise I was not able to find easy to use tool that can do this. Most heap dump analysis tools can show you the thread data, but cannot export it in the format that is associated with thread dumps. I accepted my faith and continue. Until I noticed this nice button in VisualVM that did exactly what I wanted.
VisualVM is a JVM troubleshooting tool, it was bundled with JDK 6,7,8 and is a standalone free tool. It can help you with JMX, profiling, memory monitoring, taking and displaying thread and heap dumps and so much more. If you do not have it on your local machine, download it and learn how to use it. I will definitely improve your troubleshooting capabilities.
It's straightforward. Open the heap dump (hprof file) via the Load Snapshot button.
After the heap dump is loaded in the top left corner of the tab is a dropdown. Switch it to Threads.
This is named confusingly, but it will do exactly what you need.
Next to the dropdown is a couple of buttons to switch how the thread view is displayed. The first one is the fancy UI and the second one (confusingly named HTML) will convert the thread info into a thread dump text format. This operation will take some time as VisualVM is converting the thread info into the format.
Now you can copy the text and enjoy your thread dump.
For more tips like this, you can follow me on Twitter.