写了一个死锁。使用jstack去查看。
package ztj;
/**
* Created by yp-tc-m-2777 on 2019/2/25.
*/
public class DEADLOCK {
public static void main(String[] args) {
new Thread(new Runnable() {
@Override
public void run() {
synchronized (Lock1.class){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (Lock2.class){
}
}
}
}).start();
new Thread(new Runnable() {
@Override
public void run() {
synchronized (Lock2.class){
try {
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (Lock1.class){
}
}
}
}).start();
}
}
class Lock1{
}
class Lock2{
}
localhost:~ yp-tc-m-2777$ jstack 84176
2019-02-25 19:11:35
Full thread dump Java HotSpot(TM) 64-Bit Server VM (24.75-b04 mixed mode):
"Attach Listener" daemon prio=5 tid=0x00007fe8cc801000 nid=0x1107 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"DestroyJavaVM" prio=5 tid=0x00007fe8cc13d000 nid=0x1603 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Thread-1" prio=5 tid=0x00007fe8cb0d1000 nid=0x4303 waiting for monitor entry [0x0000700010111000]
java.lang.Thread.State: BLOCKED (on object monitor)
at ztj.DEADLOCK$2.run(DEADLOCK.java:34)
- waiting to lock <0x00000007d584f3f0> (a java.lang.Class for ztj.Lock1)
- locked <0x00000007d58f8300> (a java.lang.Class for ztj.Lock2)
at java.lang.Thread.run(Thread.java:745)
"Thread-0" prio=5 tid=0x00007fe8cc13b000 nid=0x3b03 waiting for monitor entry [0x000070001000e000]
java.lang.Thread.State: BLOCKED (on object monitor)
at ztj.DEADLOCK$1.run(DEADLOCK.java:19)
- waiting to lock <0x00000007d58f8300> (a java.lang.Class for ztj.Lock2)
- locked <0x00000007d584f3f0> (a java.lang.Class for ztj.Lock1)
at java.lang.Thread.run(Thread.java:745)
"Service Thread" daemon prio=5 tid=0x00007fe8cb85a800 nid=0x3903 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" daemon prio=5 tid=0x00007fe8cb85a000 nid=0x3703 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" daemon prio=5 tid=0x00007fe8cc89f000 nid=0x3503 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Monitor Ctrl-Break" daemon prio=5 tid=0x00007fe8cb857000 nid=0x3403 runnable [0x000070000faff000]
java.lang.Thread.State: RUNNABLE
at java.net.SocketInputStream.socketRead0(Native Method)
at java.net.SocketInputStream.read(SocketInputStream.java:152)
at java.net.SocketInputStream.read(SocketInputStream.java:122)
at sun.nio.cs.StreamDecoder.readBytes(StreamDecoder.java:283)
at sun.nio.cs.StreamDecoder.implRead(StreamDecoder.java:325)
at sun.nio.cs.StreamDecoder.read(StreamDecoder.java:177)
- locked <0x00000007d5736120> (a java.io.InputStreamReader)
at java.io.InputStreamReader.read(InputStreamReader.java:184)
at java.io.BufferedReader.fill(BufferedReader.java:154)
at java.io.BufferedReader.readLine(BufferedReader.java:317)
- locked <0x00000007d5736120> (a java.io.InputStreamReader)
at java.io.BufferedReader.readLine(BufferedReader.java:382)
at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:64)
"Signal Dispatcher" daemon prio=5 tid=0x00007fe8cc800000 nid=0x320b runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" daemon prio=5 tid=0x00007fe8cb812000 nid=0x4f03 in Object.wait() [0x000070000f876000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d5504858> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:135)
- locked <0x00000007d5504858> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:151)
at java.lang.ref.Finalizer$FinalizerThread.run(Finalizer.java:209)
"Reference Handler" daemon prio=5 tid=0x00007fe8cb044000 nid=0x2c03 in Object.wait() [0x000070000f773000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x00000007d5504470> (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 <0x00000007d5504470> (a java.lang.ref.Reference$Lock)
"VM Thread" prio=5 tid=0x00007fe8cb00b800 nid=0x5203 runnable
"GC task thread#0 (ParallelGC)" prio=5 tid=0x00007fe8cc80d800 nid=0x1d07 runnable
"GC task thread#1 (ParallelGC)" prio=5 tid=0x00007fe8cc80e000 nid=0x1b03 runnable
"GC task thread#2 (ParallelGC)" prio=5 tid=0x00007fe8cc80e800 nid=0x2a03 runnable
"GC task thread#3 (ParallelGC)" prio=5 tid=0x00007fe8cc80f800 nid=0x5303 runnable
"VM Periodic Task Thread" prio=5 tid=0x00007fe8cb0b0000 nid=0x3a03 waiting on condition
JNI global references: 139
Found one Java-level deadlock:
=============================
"Thread-1":
waiting to lock monitor 0x00007fe8cc957a08 (object 0x00000007d584f3f0, a java.lang.Class),
which is held by "Thread-0"
"Thread-0":
waiting to lock monitor 0x00007fe8cb0460b8 (object 0x00000007d58f8300, a java.lang.Class),
which is held by "Thread-1"
Java stack information for the threads listed above:
===================================================
"Thread-1":
at ztj.DEADLOCK$2.run(DEADLOCK.java:34)
- waiting to lock <0x00000007d584f3f0> (a java.lang.Class for ztj.Lock1)
- locked <0x00000007d58f8300> (a java.lang.Class for ztj.Lock2)
at java.lang.Thread.run(Thread.java:745)
"Thread-0":
at ztj.DEADLOCK$1.run(DEADLOCK.java:19)
- waiting to lock <0x00000007d58f8300> (a java.lang.Class for ztj.Lock2)
- locked <0x00000007d584f3f0> (a java.lang.Class for ztj.Lock1)
at java.lang.Thread.run(Thread.java:745)
Found 1 deadlock.