title: Which Java thread is hogging the CPU?
body: <p>Let's say your Java program is taking 100% CPU. It has 50 threads. You need to find which thread is guilty. I have not found a tool that can help. Currently I use the following very time consuming routine:</p>
  
  <ol>
  <li>Run <code>jstack &lt;pid&gt;</code>, where pid is the process id of a Java process. The easy way to find it is to run another utility included in the JDK - <code>jps</code>. It is better to redirect jstack's output to a file.</li>
  <li>Search for "runnable" threads. Skip those that wait on a socket (for some reason they are still marked runnable).</li>
  <li>Repeat steps 1 and 2 a couple of times and see if you can locate a pattern.</li>
  </ol>
  
  <p>Alternatively, you could attach to a Java process in Eclipse and try to suspend threads one by one, until you hit the one that hogs CPU. On a one-CPU machine, you might need to first reduce the Java process's priority to be able to move around. Even then, Eclipse often isn't able to attach to a running process due to a timeout.</p>
  
  <p>I would have expected Sun's <code>visualvm</code> tool to do this.</p>
  
  <p>Does anybody know of a better way?</p>