龙空技术网

java命令之jstack 使用实例

弈秋的美好生活 244

前言:

现时姐妹们对“destroyjava”大体比较讲究,朋友们都想要知道一些“destroyjava”的相关文章。那么小编在网络上搜集了一些关于“destroyjava””的相关文章,希望同学们能喜欢,兄弟们快快来学习一下吧!

三、使用实例

1、jstack pid

~$ jps -mlorg.apache.catalina.startup.Bootstrap ~$ jstack 56612013-04-16 21:09:27Full thread dump Java HotSpot(TM) Server VM (20.10-b01 mixed mode):"Attach Listener" daemon prio=10 tid=0x70e95400 nid=0x2265 waiting on condition [0x00000000]   java.lang.Thread.State: RUNNABLE"http-bio-8080-exec-20" daemon prio=10 tid=0x08a35800 nid=0x1d42 waiting on condition [0x70997000]   java.lang.Thread.State: WAITING (parking)    at sun.misc.Unsafe.park(Native Method)    - parking to wait for  <0x766a27b8> (a java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject)    at java.util.concurrent.locks.LockSupport.park(LockSupport.java:156)    at java.util.concurrent.locks.AbstractQueuedSynchronizer$ConditionObject.await(AbstractQueuedSynchronizer.java:1987)    at java.util.concurrent.LinkedBlockingQueue.take(LinkedBlockingQueue.java:399)    at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:104)    at org.apache.tomcat.util.threads.TaskQueue.take(TaskQueue.java:32)    at java.util.concurrent.ThreadPoolExecutor.getTask(ThreadPoolExecutor.java:947)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:907)    at java.lang.Thread.run(Thread.java:662)........

#jstack -l 4089 >1.txt,查看1.txt内容如下所示:

2014-03-14 10:47:04Full thread dump Java HotSpot(TM) Client VM (20.45-b01 mixed mode, sharing):"Attach Listener" daemon prio=10 tid=0x08251400 nid=0x11bd runnable [0x00000000]   java.lang.Thread.State: RUNNABLE   Locked ownable synchronizers:        - None"DestroyJavaVM" prio=10 tid=0xb3a0a800 nid=0xffa waiting on condition [0x00000000]   java.lang.Thread.State: RUNNABLE   Locked ownable synchronizers:        - None"Query Listener" prio=10 tid=0xb3a09800 nid=0x1023 runnable [0xb3b72000]   java.lang.Thread.State: RUNNABLE        at java.net.PlainSocketImpl.socketAccept(Native Method)        at java.net.PlainSocketImpl.accept(PlainSocketImpl.java:408)        - locked <0x70a84430> (a java.net.SocksSocketImpl)        at java.net.ServerSocket.implAccept(ServerSocket.java:462)        at java.net.ServerSocket.accept(ServerSocket.java:430)        at com.sun.tools.hat.internal.server.QueryListener.waitForRequests(QueryListener.java:76)        at com.sun.tools.hat.internal.server.QueryListener.run(QueryListener.java:65)        at java.lang.Thread.run(Thread.java:662)Locked ownable synchronizers:        - None"Low Memory Detector" daemon prio=10 tid=0x08220400 nid=0x1000 runnable [0x00000000]   java.lang.Thread.State: RUNNABLE   Locked ownable synchronizers:        - None"C1 CompilerThread0" daemon prio=10 tid=0x08214c00 nid=0xfff waiting on condition [0x00000000]   java.lang.Thread.State: RUNNABLE   Locked ownable synchronizers:        - None"Signal Dispatcher" daemon prio=10 tid=0x08213000 nid=0xffe runnable [0x00000000]   java.lang.Thread.State: RUNNABLE   Locked ownable synchronizers:        - None"Finalizer" daemon prio=10 tid=0x0820bc00 nid=0xffd in Object.wait() [0xb5075000]   java.lang.Thread.State: WAITING (on object monitor)        at java.lang.Object.wait(Native Method)- waiting on <0x7a2b6f50> (a java.lang.ref.ReferenceQueue$Lock)        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:118)        - locked <0x7a2b6f50> (a java.lang.ref.ReferenceQueue$Lock)        at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:134)        at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:171)   Locked ownable synchronizers:        - None"Reference Handler" daemon prio=10 tid=0x0820a400 nid=0xffc in Object.wait() [0xb50c7000]   java.lang.Thread.State: WAITING (on object monitor)        at java.lang.Object.wait(Native Method)        - waiting on <0x7a2b6fe0> (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 <0x7a2b6fe0> (a java.lang.ref.Reference$Lock)   Locked ownable synchronizers:        - None"VM Thread" prio=10 tid=0x08200000 nid=0xffb runnable"VM Periodic Task Thread" prio=10 tid=0x08222400 nid=0x1001 waiting on conditionJNI global references: 1317

一般情况下,通过jstack输出的线程信息主要包括:jvm自身线程、用户线程等。其中jvm线程会在jvm启动时就会存在。对于用户线程则是在用户访问时才会生成。

2、jstack 查看线程具体在做什么,可看出哪些线程在长时间占用CPU,尽快定位问题和解决问题

1.top查找出哪个进程消耗的cpu高。执行top命令,默认是进程视图,其中PID是进程号
21125 co_ad2    18   0 1817m 776m 9712 S  3.3  4.9  12:03.24 java                                                                                           
5284 co_ad     21   0 3028m 2.5g 9432 S  1.0 16.3   6629:44 ja
这里我们分析21125这个java进程
2.top中shift+h 或“H”查找出哪个线程消耗的cpu高 先输入top,然后再按shift+h 或“H”,此时打开的是线程视图,pid为线程号21233 co_ad2    15   0 1807m 630m 9492 S  1.3  4.0   0:05.12 java                                                                                           20503 co_ad2_s  15   0 1360m 560m 9176 S  0.3  3.6   0:46.72 java                                                                                           这里我们分析21233这个线程,并且注意的是,这个线程是属于21125这个进程的。 3.使用jstack命令输出这一时刻的线程栈,保存到文件,命名为jstack.log。注意:输出线程栈和保存top命令快照尽量同时进行。  由于jstack.log文件记录的线程ID是16进制,需要将top命令展示的线程号转换为16进制。4. jstack查找这个线程的信息 jstack [进程]|grep -A 10 [线程的16进制] 即: jstack 21125|grep -A 10 52f1  -A 10表示查找到所在行的后10行。21233用计算器转换为16进制52f1,注意字母是小写。 结果:  "http-8081-11" daemon prio=10 tid=0x00002aab049a1800 nid=0x52bb in Object.wait() [0x0000000042c75000]     java.lang.Thread.State: WAITING (on object monitor)       at java.lang.Object.wait(Native Method)       at java.lang.Object.wait(Object.java:485)       at org.apache.tomcat.util.net.JIoEndpoint$Worker.await(JIoEndpoint.java:416)  在结果中查找52f1,可看到当前线程在做什么。

3、代码示例

运行代码:

/** * @author hollis */public class JStackDemo1 {    public static void main(String[] args) {        while (true) {            //Do Nothing        }    }}

先是有jps查看进程号:

hollis@hos:~$ jps29788 JStackDemo129834 Jps22385 org.eclipse.equinox.launcher_1.3.0.v20130327-1440.jar

然后使用jstack 查看堆栈信息:

hollis@hos:~$ jstack 297882015-04-17 23:47:31...此处省略若干内容..."main" prio=10 tid=0x00007f197800a000 nid=0x7462 runnable [0x00007f197f7e1000]   java.lang.Thread.State: RUNNABLE    at javaCommand.JStackDemo1.main(JStackDemo1.java:7)

我们可以从这段堆栈信息中看出什么来呢?我们可以看到,当前一共有一条用户级别线程,线程处于runnable状态,执行到JStackDemo1.java的第七行。 看下面代码:

/** * @author hollis */public class JStackDemo1 {    public static void main(String[] args) {        Thread thread = new Thread(new Thread1());        thread.start();    }}class Thread1 implements Runnable{    @Override    public void run() {        while(true){            System.out.println(1);        }    }}

线程堆栈信息如下:

"Reference Handler" daemon prio=10 tid=0x00007fbbcc06e000 nid=0x286c in Object.wait() [0x00007fbbc8dfc000]   java.lang.Thread.State: WAITING (on object monitor)    at java.lang.Object.wait(Native Method)    - waiting on <0x0000000783e066e0> (a java.lang.ref.Reference$Lock)    at java.lang.Object.wait(Object.java:503)    at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:133)    - locked <0x0000000783e066e0> (a java.lang.ref.Reference$Lock)

我们能看到:

线程的状态: WAITING 线程的调用栈 线程的当前锁住的资源: <0x0000000783e066e0> 线程当前等待的资源:<0x0000000783e066e0>

为什么同时锁住的等待同一个资源:

线程的执行中,先获得了这个对象的 Monitor(对应于 locked <0x0000000783e066e0>)。当执行到 obj.wait(), 线程即放弃了 Monitor的所有权,进入 “wait set”队列(对应于 waiting on <0x0000000783e066e0> )。

标签: #destroyjava