简介
jstack用于打印出给定的java进程ID的Java堆栈信息,一般用于检查应用的线程问题,死锁问题
常用命令
jstack
输出
$ jstack 11376 2014-01-21 20:36:54 Full thread dump Java HotSpot(TM) Server VM (17.0-b16 mixed mode): "_jprofiler_control_sampler" daemon prio=10 tid=0x6e69bc00 nid=0x4e72 waiting on condition [0x6ddfe000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at com.jprofiler.agent.probe.AnnotationHandler$_A.run(ejt:3492)"_jprofiler_native_sampler" daemon prio=10 tid=0x6e696c00 nid=0x4e71 runnable [0x00000000] java.lang.Thread.State: RUNNABLE "_jprofiler_sampler" daemon prio=10 tid=0x6f16ac00 nid=0x4e64 waiting on condition [0x6dfdd000] java.lang.Thread.State: TIMED_WAITING (sleeping) at java.lang.Thread.sleep(Native Method) at com.jprofiler.agent.Sampler.run(ejt:1336) "_jprofiler_comm_java" daemon prio=10 tid=0x6f168400 nid=0x4e63 runnable [0x6e02e000] java.lang.Thread.State: RUNNABLE at com.jprofiler.agent.Agent.enterCommunication(Native Method) at com.jprofiler.agent.Agent.access$000(ejt:2185) at com.jprofiler.agent.Agent$2.run(ejt:2187) "Attach Listener" daemon prio=10 tid=0xb68ac400 nid=0x2cab waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE "DestroyJavaVM" prio=10 tid=0xb6806000 nid=0x2c75 waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE "pool-1-thread-2" prio=10 tid=0x6e68a800 nid=0x2c82 waiting for monitor entry [0x6e5ad000] java.lang.Thread.State: BLOCKED (on object monitor) at Deadlock.run(Deadlock.java:28) - waiting to lock <0x9e355378> (a java.lang.Object) - locked <0x9e355380> (a java.lang.Object) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) "pool-1-thread-1" prio=10 tid=0x6e68b400 nid=0x2c81 waiting for monitor entry [0x6e5fe000] java.lang.Thread.State: BLOCKED (on object monitor) at Deadlock.run(Deadlock.java:28) - waiting to lock <0x9e355380> (a java.lang.Object) - locked <0x9e355378> (a java.lang.Object) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) "Low Memory Detector" daemon prio=10 tid=0xb689f800 nid=0x2c7f runnable [0x00000000] java.lang.Thread.State: RUNNABLE"CompilerThread1" daemon prio=10 tid=0xb689dc00 nid=0x2c7d waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE "CompilerThread0" daemon prio=10 tid=0xb689bc00 nid=0x2c7c waiting on condition [0x00000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" daemon prio=10 tid=0xb689a400 nid=0x2c7b runnable [0x00000000] java.lang.Thread.State: RUNNABLE "Finalizer" daemon prio=10 tid=0xb6885400 nid=0x2c7a in Object.wait() [0x6ed67000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x9e180b10> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118) - locked <0x9e180b10> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134) at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:159) "Reference Handler" daemon prio=10 tid=0xb6883c00 nid=0x2c79 in Object.wait() [0x6f07d000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x9e180a18> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:485) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:116) - locked <0x9e180a18> (a java.lang.ref.Reference$Lock) "VM Thread" prio=10 tid=0xb6881400 nid=0x2c78 runnable"GC task thread#0 (ParallelGC)" prio=10 tid=0xb680d000 nid=0x2c76 runnable "GC task thread#1 (ParallelGC)" prio=10 tid=0xb680e800 nid=0x2c77 runnable "VM Periodic Task Thread" prio=10 tid=0xb68a1400 nid=0x2c80 waiting on condition JNI global references: 12920 Found one Java-level deadlock: ============================= "pool-1-thread-2": waiting to lock monitor 0x6ef037e0 (object 0x9e355378, a java.lang.Object), which is held by "pool-1-thread-1" "pool-1-thread-1": waiting to lock monitor 0x6ef02c8c (object 0x9e355380, a java.lang.Object), which is held by "pool-1-thread-2" Java stack information for the threads listed above: =================================================== "pool-1-thread-2": at Deadlock.run(Deadlock.java:28) - waiting to lock <0x9e355378> (a java.lang.Object) - locked <0x9e355380> (a java.lang.Object) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) "pool-1-thread-1": at Deadlock.run(Deadlock.java:28) - waiting to lock <0x9e355380> (a java.lang.Object) - locked <0x9e355378> (a java.lang.Object) at java.util.concurrent.ThreadPoolExecutor$Worker.runTask(ThreadPoolExecutor.java:886) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:908) at java.lang.Thread.run(Thread.java:619) Found 1 deadlock
线程的状态分析:
- runnable
该状态表示线程具备所有运行条件,在运行队列中准备操作系统的调度,或者正在运行。
- waiting on condition
该状态出现在线程等待某个条件的发生。具体是什么原因,可以结合stacktrace来分析。最常见的情况是线程在等待网络的读写,比如当网络数据没有准备好读时,线程处于这种等待状态,而一旦有数据准备好读之后,线程会重新激活,读取并处理数据。在 Java引入 NIO之前,对于每个网络连接,都有一个对应的线程来处理网络的读写操作,即使没有可读写的数据,线程仍然阻塞在读写操作上,这样有可能造成资源浪费,而且给操作系统的线程调度也带来压力。在 NIO里采用了新的机制,编写的服务器程序的性能和可扩展性都得到提高。
如果发现有大量的线程都在处在 Wait on condition,从线程 stack看, 正等待网络读写,这可能是一个网络瓶颈的征兆。因为网络阻塞导致线程无法执行。一种情况是网络非常忙,几乎消耗了所有的带宽,仍然有大量数据等待网络读写;另一种情况也可能是网络空闲,但由于路由等问题,导致包无法正常的到达。所以要结合系统的一些性能观察工具来综合分析,比如 netstat统计单位时间的发送包的数目,如果很明显超过了所在网络带宽的限制 ; 观察 cpu的利用率,如果系统态的 CPU时间,相对于用户态的 CPU时间比例较高;如果程序运行在 Solaris 10平台上,可以用 dtrace工具看系统调用的情况,如果观察到 read/write的系统调用的次数或者运行时间遥遥领先;这些都指向由于网络带宽所限导致的网络瓶颈。
另外一种出现 Wait on condition的常见情况是该线程在 sleep,等待 sleep的时间到了时候,将被唤醒。
- waiting for monitor entry 和 in Object.wait()
在多线程的 JAVA程序中,实现线程之间的同步,就要说说Monitor。Monitor是Java中用以实现线程之间的互斥与协作的主要手段,它可以看成是对象或者 Class的锁。每一个对象都有,也仅有一个 monitor。下面这个图,描述了线程和 Monitor之间关系,以及线程的状态转换图: