Java jstack命令查看线程堆栈信息的方法

Java jstack命令查看线程堆栈信息

Java jstack命令可以用来查看Java应用程序中每个线程的堆栈信息,它可以帮助我们更好地了解程序的运行状态,从而更好地调试程序。

使用方法

使用Java jstack命令查看线程堆栈信息,需要使用以下命令:

jstack [option] <pid>

其中,option可以是以下参数:

  • -F:强制jstack命令在目标进程上执行,即使该进程处于挂起状态。
  • -l:显示锁信息,包括等待锁和拥有锁的线程。
  • -m:显示Java堆栈中的本地方法。
  • -h:显示jstack命令的帮助信息。

pid是要查看的进程号,可以使用jps命令查看。

示例

下面是一个使用Java jstack命令查看线程堆栈信息的例子:

$ jps
1480 Jps
1445 Main

$ jstack -l 1445
2015-01-01 12:00:00
Full thread dump Java HotSpot(TM) 64-Bit Server VM (14.0-b16 mixed mode):

"Thread-0" #11 prio=5 os_prio=0 tid=0x0000000000f5f000 nid=0x2c8 runnable [0x0000000000f8f000]
   java.lang.Thread.State: RUNNABLE
	at java.net.SocketInputStream.socketRead0(Native Method)
	at java.net.SocketInputStream.read(SocketInputStream.java:150)
	at java.net.SocketInputStream.read(SocketInputStream.java:121)
	at java.io.BufferedInputStream.fill(BufferedInputStream.java:246)
	at java.io.BufferedInputStream.read(BufferedInputStream.java:265)
	- locked <0x000000076b2f9f98> (a java.io.BufferedInputStream)
	at java.io.FilterInputStream.read(FilterInputStream.java:83)
	at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:547)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:812)
	at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:671)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)

"Thread-1" #10 prio=5 os_prio=0 tid=0x0000000000f5b800 nid=0x2b8 waiting on condition [0x0000000000e8f000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x000000076b2f9f68> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)

"Thread-2" #9 prio=5 os_prio=0 tid=0x0000000000f59000 nid=0x2a8 waiting on condition [0x0000000000d8f000]
   java.lang.Thread.State: WAITING (parking)
	at sun.misc.Unsafe.park(Native Method)
	- parking to wait for  <0x000000076b2f9f48> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)
	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:186)
	at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:2043)
	at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:442)
	at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:1068)
	at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1130)
	at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
	at java.lang.Thread.run(Thread.java:745)

...

从上面的例子可以看出,Java jstack命令可以查看Java应用程序中的每个线程的堆栈信息,从而帮助我们更好地调试程序。

本文链接:http://task.lmcjl.com/news/2562.html

展开阅读全文