jstack tool
The jstack
tool is used to generate Java thread stack traces. It helps you understand the thread states of your current Java application, aiding in diagnosing performance issues or thread deadlocks.
Before using jstack
, you need to know the PID (Process ID) of the target Java process. You can use the jps
command to view the currently running Java processes and their PIDs. Then, you can use the jstack
command to generate the thread stack trace.
The basic command format is as follows:
jstack <pid>
For example, if you want to generate a thread stack trace for a Java process with PID 12345, the command is:
jstack 12345
After executing the above command, jstack
will output the stack trace information of all threads in the current Java process.
Sample Output and Field Explanation
Here is a typical jstack
output example:
2024-12-03 17:50:17
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.181-b13 mixed mode):
"main" #1 prio=5 os_prio=0 tid=0x000000000276c000 nid=0x1f84 waiting on condition [0x0000000002bfe000]
java.lang.Thread.State: WAITING (parking)
at sun.misc.Unsafe.park(Native Method)
- parking to wait for <0x00000000d5cd8f18> (a java.util.concurrent.CountDownLatch$Sync)
at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:836)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:997)
at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1304)
at java.util.concurrent.CountDownLatch.await(CountDownLatch.java:231)
at com.example.MyClass.main(MyClass.java:15)
...
Field explanations for the sample output:
- Thread Name ("main"): Indicates the name of the thread, here it is "main".
- Thread ID (#1): The internal thread ID of the JVM.
- Priority (prio=5): The priority of the thread.
- OS Priority (os_prio=0): The thread's priority in the operating system.
- Thread Identifier (tid=0x000000000276c000): The identifier of the thread.
- Native ID (nid=0x1f84): The native ID of the thread in the operating system.
- Thread State: The current state of the thread, here it is WAITING (parking), indicating the thread is waiting for a condition.
- Stack Trace: The stack trace of the thread, showing the code being executed by the thread.