- 运行可以产生死锁的程序
- 在cmd中输入jdk自带的
jps -l
命令(类似于linux中的ps -ef|grep xxx
),根据打印结果获取死锁进程编号
- 使用
jstack -进程号
就可以看到具体的原因。
demo
C:\Users\Limer>jps
1140 Jps
15140 Deadlock
8964
10344 jar
6680 Launcher
C:\Users\Limer>jstack 15140
2019-11-02 18:25:21
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.211-b12 mixed mode):
"DestroyJavaVM" #13 prio=5 os_prio=0 tid=0x0000000002802800 nid=0x3bc0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"threadB" #12 prio=5 os_prio=0 tid=0x0000000018899000 nid=0x3b80 waiting for monitor entry [0x000000001957f000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.lhp.MyHoldThread.run(Deadlock.java:29)
- waiting to lock <0x00000000d60c43d8> (a java.lang.String)
- locked <0x00000000d60c4410> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"threadA" #11 prio=5 os_prio=0 tid=0x0000000018896000 nid=0x3b40 waiting for monitor entry [0x000000001947e000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.lhp.MyHoldThread.run(Deadlock.java:29)
- waiting to lock <0x00000000d60c4410> (a java.lang.String)
- locked <0x00000000d60c43d8> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #10 daemon prio=9 os_prio=0 tid=0x0000000018857800 nid=0x31f8 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #9 daemon prio=9 os_prio=2 tid=0x00000000187c9800 nid=0x36d8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #8 daemon prio=9 os_prio=2 tid=0x00000000187c8800 nid=0x38bc waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #7 daemon prio=9 os_prio=2 tid=0x00000000187c5000 nid=0x1de8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" #6 daemon prio=5 os_prio=0 tid=0x00000000187c2800 nid=0x13d0 runnable [0x0000000018e7e000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.socketRead(SocketInputStream.java:116)
at java.net.SocketInputStream.read(SocketInputStream.java:171)
at java.net.SocketInputStream.read(SocketInputStream.java:141)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:284)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:326)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:178)
- locked <0x00000000d610d6e0> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:161)
at java.io.BufferedReader.readLine(BufferedReader.java:324)
- locked <0x00000000d610d6e0> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:389)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x0000000017476800 nid=0x3a94 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x0000000018790800 nid=0x13d4 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x00000000028f7000 nid=0x3594 in Object.wait() [0x000000001877f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d5f88ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144)
- locked <0x00000000d5f88ed0> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:165)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:216)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x000000001740a000 nid=0x3a8c in Object.wait() [0x000000001867f000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000000d5f86bf8> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x00000000d5f86bf8> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x00000000173e6800 nid=0x30b4 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000002818000 nid=0x3afc runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000002819800 nid=0x1148 runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x000000000281b800 nid=0x20a8 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x000000000281d000 nid=0x3978 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x0000000018893000 nid=0xcb0 waiting on condition
JNI global references: 12
Found one Java-level deadlock:
=============================
"threadB":
waiting to lock monitor 0x0000000017410708 (object 0x00000000d60c43d8, a java.lang.String),
which is held by "threadA"
"threadA":
waiting to lock monitor 0x000000001740dd18 (object 0x00000000d60c4410, a java.lang.String),
which is held by "threadB"
Java stack information for the threads listed above:
===================================================
"threadB":
at com.lhp.MyHoldThread.run(Deadlock.java:29)
- waiting to lock <0x00000000d60c43d8> (a java.lang.String)
- locked <0x00000000d60c4410> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
"threadA":
at com.lhp.MyHoldThread.run(Deadlock.java:29)
- waiting to lock <0x00000000d60c4410> (a java.lang.String)
- locked <0x00000000d60c43d8> (a java.lang.String)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.
网友评论