Thread[线程 1,5,main]get resource1 Thread[线程 2,5,main]get resource2 Thread[线程 1,5,main]waiting get resource2 Thread[线程 2,5,main]waiting get resource1
线程 A 通过 synchronized (resource1) 获得 resource1 的监视器锁,然后通过Thread.sleep(1000);让线程 A 休眠 1s 为的是让线程 B 得到执行然后获取到 resource2 的监视器锁。线程 A 和线程 B 休眠结束了都开始企图请求获取对方的资源,然后这两个线程就会陷入互相等待的状态,这也就产生了死锁。上面的例子符合产生死锁的四个必要条件。
D:\IDE_Project\JavaLearning\JavaThreadDemo>jstack 18456 2021-08-28 09:41:39 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.241-b07 mixed mode):
"ThreadBBB"#13 prio=5 os_prio=0 tid=0x000000001f24e800 nid=0x4f08 waiting for monitor entry [0x00000000201bf000] java.lang.Thread.State: BLOCKED (on object monitor) at com.marlowe.demos.HoldLockThread.run(DeadLockDemo.java:47) - waiting to lock <0x000000076b61d588> (a java.lang.String) - locked <0x000000076b61d5c0> (a java.lang.String) at java.lang.Thread.run(Thread.java:748)
"ThreadAAA"#12 prio=5 os_prio=0 tid=0x000000001f24d800 nid=0x4c68 waiting for monitor entry [0x00000000200be000] java.lang.Thread.State: BLOCKED (on object monitor) at com.marlowe.demos.HoldLockThread.run(DeadLockDemo.java:47) - waiting to lock <0x000000076b61d5c0> (a java.lang.String) - locked <0x000000076b61d588> (a java.lang.String) at java.lang.Thread.run(Thread.java:748)
"Monitor Ctrl-Break"#6 daemon prio=5 os_prio=0 tid=0x000000001f131800 nid=0x40a4 runnable [0x000000001f9be000] 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 <0x000000076b507680> (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 <0x000000076b507680> (a java.io.InputStreamReader) at java.io.BufferedReader.readLine(BufferedReader.java:389) at com.intellij.rt.execution.application.AppMainV2$1.run(AppMainV2.java:48)
"Finalizer"#3 daemon prio=8 os_prio=1 tid=0x000000001f081000 nid=0x5c1c in Object.wait() [0x000000001f65e000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b388ee0> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:144) - locked <0x000000076b388ee0> (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=0x000000001f080800 nid=0x5f5c in Object.wait() [0x000000001f55e000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x000000076b386c00> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x000000076b386c00> (a java.lang.ref.Reference$Lock) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Periodic Task Thread" os_prio=2 tid=0x000000001f1ea800 nid=0x28ec waiting on condition
JNI global references: 12
Found one Java-level deadlock: ============================= "ThreadBBB": waiting to lock monitor 0x000000001d290c28 (object 0x000000076b61d588, a java.lang.String), which is held by "ThreadAAA" "ThreadAAA": waiting to lock monitor 0x000000001f21fd88 (object 0x000000076b61d5c0, a java.lang.String), which is held by "ThreadBBB"
Java stack information for the threads listed above: =================================================== "ThreadBBB": at com.marlowe.demos.HoldLockThread.run(DeadLockDemo.java:47) - waiting to lock <0x000000076b61d588> (a java.lang.String) - locked <0x000000076b61d5c0> (a java.lang.String) at java.lang.Thread.run(Thread.java:748) "ThreadAAA": at com.marlowe.demos.HoldLockThread.run(DeadLockDemo.java:47) - waiting to lock <0x000000076b61d5c0> (a java.lang.String) - locked <0x000000076b61d588> (a java.lang.String) at java.lang.Thread.run(Thread.java:748)