Getting a Thread Dump

We will introduce the three most commonly used methods. Note that there are many other ways to get a thread dump. A thread dump can only show the thread status at the time of measurement, so in order to see the change in thread status, it is recommended to extract them from 5 to 10 times with 5-second intervals.
Getting a Thread Dump Using jstack

In JDK 1.6 and higher, it is possible to get a thread dump on MS Windows using jstack.

Use PID via jps to check the PID of the currently running Java application process.

Shell代码 [转]Analyze Java Thread Dumps _Java [转]Analyze Java Thread Dumps _Java_02
  1. [user@linux ~]$ jps -v
  2.  
  3. 25780 RemoteTestRunner -Dfile.encoding=UTF-8
  4. 25590 sub.rmi.registry.RegistryImpl 2999 -Dapplication.home=/home1/user/java/jdk.1.6.0_24 -Xms8m
  5. 26300 sun.tools.jps.Jps -mlvV -Dapplication.home=/home1/user/java/jdk.1.6.0_24 -Xms8m


Use the extracted PID as the parameter of jstack to obtain a thread dump.

Shell代码 [转]Analyze Java Thread Dumps _Java [转]Analyze Java Thread Dumps _Java_02
  1. [user@linux ~]$ jstack -l 10023 > xxxfile.dump


Thread Information from the Thread Dump File

Java代码 [转]Analyze Java Thread Dumps _Java [转]Analyze Java Thread Dumps _Java_02
  1. "pool-1-thread-13" prio=6 tid=0x000000000729a000 nid=0x2fb4 runnable [0x0000000007f0f000] java.lang.Thread.State: RUNNABLE
  2. at java.net.SocketInputStream.socketRead0(Native Method)
  3.  
  4. at java.net.SocketInputStream.read(SocketInputStream.java:129)
  5.  
  6. at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:264)
  7.  
  8. at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:306)
  9.  
  10. at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:158)
  11.  
  12. - locked <0x0000000780b7e688> (a java.io.InputStreamReader)
  13.  
  14. at java.io.InputStreamReader.read(InputStreamReader.java:167)
  15.  
  16. at java.io.BufferedReader.fill(BufferedReader.java:136)
  17.  
  18. at java.io.BufferedReader.readLine(BufferedReader.java:299)
  19.  
  20. - locked <0x0000000780b7e688> (a java.io.InputStreamReader)
  21.  
  22. at java.io.BufferedReader.readLine(BufferedReader.java:362)


以上代码说明:

  • Thread name: When using Java.lang.Thread class to generate a thread, the thread will be named Thread-(Number), whereas when using java.util.concurrent.ThreadFactory class, it will be named pool-(number)-thread-(number).
  • Priority: Represents the priority of the threads.
  • Thread ID: Represents the unique ID for the threads. (Some useful information, including the CPU usage or memory usage of the thread, can be obtained by using thread ID.)
  • Thread status: Represents the status of the threads.
  • Thread callstack: Represents the call stack information of the threads.


Thread Dump Patterns by Type
When Unable to Obtain a Lock (BLOCKED)


This is when the overall performance of the application slows down because a thread is occupying the lock and prevents other threads from obtaining it. In the following example, BLOCKED_TEST pool-1-thread-1 thread is running with <0x0000000780a000b0> lock, while BLOCKED_TEST pool-1-thread-2 and BLOCKED_TEST pool-1-thread-3 threads are waiting to obtain <0x0000000780a000b0> lock.

Java代码 [转]Analyze Java Thread Dumps _Java [转]Analyze Java Thread Dumps _Java_02
  1. "BLOCKED_TEST pool-1-thread-1" prio=6 tid=0x0000000006904800 nid=0x28f4 runnable [0x000000000785f000]
  2. java.lang.Thread.State: RUNNABLE
  3. at java.io.FileOutputStream.writeBytes(Native Method)
  4. at java.io.FileOutputStream.write(FileOutputStream.java:282)
  5. at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:65)
  6. at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:123)
  7. - locked <0x0000000780a31778> (a java.io.BufferedOutputStream)
  8. at java.io.PrintStream.write(PrintStream.java:432)
  9. - locked <0x0000000780a04118> (a java.io.PrintStream)
  10. at sun.nio.cs.StreamEncoder.writeBytes(StreamEncoder.java:202)
  11. at sun.nio.cs.StreamEncoder.implFlushBuffer(StreamEncoder.java:272)
  12. at sun.nio.cs.StreamEncoder.flushBuffer(StreamEncoder.java:85)
  13. - locked <0x0000000780a040c0> (a java.io.OutputStreamWriter)
  14. at java.io.OutputStreamWriter.flushBuffer(OutputStreamWriter.java:168)
  15. at java.io.PrintStream.newLine(PrintStream.java:496)
  16. - locked <0x0000000780a04118> (a java.io.PrintStream)
  17. at java.io.PrintStream.println(PrintStream.java:687)
  18. - locked <0x0000000780a04118> (a java.io.PrintStream)
  19. at com.nbp.theplatform.threaddump.ThreadBlockedState.monitorLock(ThreadBlockedState.java:44)
  20. - locked <0x0000000780a000b0> (a com.nbp.theplatform.threaddump.ThreadBlockedState)
  21. at com.nbp.theplatform.threaddump.ThreadBlockedState$1.run(ThreadBlockedState.java:7)
  22. at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  23. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  24. at java.lang.Thread.run(Thread.java:662)
  25.  
  26. Locked ownable synchronizers:
  27. - <0x0000000780a31758> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
  28.  
  29. "BLOCKED_TEST pool-1-thread-2" prio=6 tid=0x0000000007673800 nid=0x260c waiting for monitor entry [0x0000000008abf000]
  30. java.lang.Thread.State: BLOCKED (on object monitor)
  31. at com.nbp.theplatform.threaddump.ThreadBlockedState.monitorLock(ThreadBlockedState.java:43)
  32. - waiting to lock <0x0000000780a000b0> (a com.nbp.theplatform.threaddump.ThreadBlockedState)
  33. at com.nbp.theplatform.threaddump.ThreadBlockedState$2.run(ThreadBlockedState.java:26)
  34. at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886)
  35. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  36. at java.lang.Thread.run(Thread.java:662)
  37.  
  38. Locked ownable synchronizers:
  39. - <0x0000000780b0c6a0> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)
  40.  
  41. "BLOCKED_TEST pool-1-thread-3" prio=6 tid=0x00000000074f5800 nid=0x1994 waiting for monitor entry [0x0000000008bbf000]
  42. java.lang.Thread.State: BLOCKED (on object monitor)
  43. at com.nbp.theplatform.threaddump.ThreadBlockedState.monitorLock(ThreadBlockedState.java:42)
  44. - waiting to lock <0x0000000780a000b0> (a com.nbp.theplatform.threaddump.ThreadBlockedState)
  45. at com.nbp.theplatform.threaddump.ThreadBlockedState$3.run(ThreadBlockedState.java:34)
  46. at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886
  47. at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908)
  48. at java.lang.Thread.run(Thread.java:662)
  49.  
  50. Locked ownable synchronizers:
  51. - <0x0000000780b0e1b8> (a java.util.concurrent.locks.ReentrantLock$NonfairSync)