Java虛擬機器工具之堆疊跟蹤工具jstack檢測物件wait方法
阿新 • • 發佈:2019-02-12
wait()方法源於Object類,當執行緒呼叫wait的時候,意思就是說執行緒會放棄物件鎖,進入等待此物件的等待鎖定池。
下面看看用jstack怎麼定位wait方法。
首先寫一段簡單的呼叫wait的程式碼:
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
class WaitTask implements Runnable{
@Override
public void run(){
synchronized (this) {
try {
wait();
}catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
public class ThreadWait{
public static void main(String[] args) {
WaitTask waitTask = new WaitTask();
ExecutorService executorService = Executors.newFixedThreadPool(1 );
executorService.execute(waitTask);
}
}
通過jps找到對應程序的id,然後用jstack跟蹤一下:
$ jstack -l 30394
2016-11-07 16:36:47
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.101-b13 mixed mode):
"Attach Listener" #12 daemon prio=9 os_prio=0 tid=0x00007fd2e4001000 nid=0x76ff runnable [0x0000000000000000]
java.lang.Thread.State : RUNNABLE
Locked ownable synchronizers:
- None
"DestroyJavaVM" #11 prio=5 os_prio=0 tid=0x00007fd32400a800 nid=0x76bb waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"pool-1-thread-1" #10 prio=5 os_prio=0 tid=0x00007fd324145000 nid=0x76d1 in Object.wait() [0x00007fd30a4da000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ec2174e8> (a WaitTask)
at java.lang.Object.wait(Object.java:502)
at WaitTask.run(ThreadWait.java:13)
- locked <0x00000000ec2174e8> (a WaitTask)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- <0x00000000ec2b3758> (a java.util.concurrent.ThreadPoolExecutor$Worker)
"Monitor Ctrl-Break" #9 daemon prio=5 os_prio=0 tid=0x00007fd324142000 nid=0x76c9 runnable [0x00007fd30aaf3000]
java.lang.Thread.State: RUNNABLE
at java.net.PlainSocketImpl.socketAccept(Native Method)
at java.net.AbstractPlainSocketImpl.accept(AbstractPlainSocketImpl.java:409)
at java.net.ServerSocket.implAccept(ServerSocket.java:545)
at java.net.ServerSocket.accept(ServerSocket.java:513)
at com.intellij.rt.execution.application.AppMain$1.run(AppMain.java:90)
at java.lang.Thread.run(Thread.java:745)
Locked ownable synchronizers:
- None
"Service Thread" #8 daemon prio=9 os_prio=0 tid=0x00007fd3240c8800 nid=0x76c7 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C1 CompilerThread2" #7 daemon prio=9 os_prio=0 tid=0x00007fd3240bd000 nid=0x76c6 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread1" #6 daemon prio=9 os_prio=0 tid=0x00007fd3240bb800 nid=0x76c5 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"C2 CompilerThread0" #5 daemon prio=9 os_prio=0 tid=0x00007fd3240b8800 nid=0x76c4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Signal Dispatcher" #4 daemon prio=9 os_prio=0 tid=0x00007fd3240b7000 nid=0x76c3 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
Locked ownable synchronizers:
- None
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fd324084000 nid=0x76c2 in Object.wait() [0x00007fd30bdfc000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ec188ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x00000000ec188ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
Locked ownable synchronizers:
- None
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fd32407f800 nid=0x76c1 in Object.wait() [0x00007fd30befd000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ec186b50> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000ec186b50> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
Locked ownable synchronizers:
- None
"VM Thread" os_prio=0 tid=0x00007fd324078000 nid=0x76c0 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x00007fd32401f800 nid=0x76bc runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x00007fd324021000 nid=0x76bd runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x00007fd324023000 nid=0x76be runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x00007fd324024800 nid=0x76bf runnable
"VM Periodic Task Thread" os_prio=0 tid=0x00007fd3240cb000 nid=0x76c8 waiting on condition
JNI global references: 15
這一段表示jstack已經檢測到了wait()正在執行。
"Finalizer" #3 daemon prio=8 os_prio=0 tid=0x00007fd324084000 nid=0x76c2 in Object.wait() [0x00007fd30bdfc000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ec188ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x00000000ec188ee0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:164)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
Locked ownable synchronizers:
- None
"Reference Handler" #2 daemon prio=10 os_prio=0 tid=0x00007fd32407f800 nid=0x76c1 in Object.wait() [0x00007fd30befd000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000ec186b50> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000ec186b50> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
Locked ownable synchronizers:
- None