1 死锁
当多条线程以不同的顺序抢占同步资源的时候,就有可能发生死锁。
2 示例
如下图所示,线程1持有锁对象A而希望获得锁对象B;另一方面,线程2持有锁对象B而希望
获得锁对象A。并且这两个线程的操作是交错执行的,因此它们会发生死锁。
参考以下示例代码:
class DeadLock {
private final Object lockA = new Object();
private final Object lockB = new Object();
public void funcA() {
synchronized (lockA) {
System.out.println("funcA in");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockB) {
System.out.println("funcA out");
}
}
}
public void funcB() {
synchronized (lockB) {
System.out.println("funcB in");
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
synchronized (lockA) {
System.out.println("funcB out");
}
}
}
}
public class DeadLockTest {
public static void main(String[] args) {
DeadLock deadLock = new DeadLock();
new Thread(new Runnable() {
@Override
public void run() {
deadLock.funcA();
}
}, "Thread-A").start();
new Thread(new Runnable() {
@Override
public void run() {
deadLock.funcB();
}
}, "Thread-B").start();
}
}
3 死锁发生
执行程序后一直没有停止,程序肯定进入了死锁
4 用jstack分析死锁
2018-10-17 13:31:12
Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.131-b11 mixed mode):
"DestroyJavaVM" #12 prio=5 os_prio=0 tid=0x0000000000fee000 nid=0x4e0c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Thread-B" #11 prio=5 os_prio=0 tid=0x000000001d6eb800 nid=0x1c24 waiting for monitor entry [0x000000001e2ef000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.study.deadlock.DeadLock.funcB(DeadLock.java:37)
- waiting to lock <0x000000076b4d5928> (a java.lang.Object)
- locked <0x000000076b4d5938> (a java.lang.Object)
at com.study.deadlock.DeadLockTest$2.run(DeadLockTest.java:22)
at java.lang.Thread.run(Thread.java:748)
"Thread-A" #10 prio=5 os_prio=0 tid=0x000000001d6f5000 nid=0x4780 waiting for monitor entry [0x000000001e1ef000]
java.lang.Thread.State: BLOCKED (on object monitor)
at com.study.deadlock.DeadLock.funcA(DeadLock.java:23)
- waiting to lock <0x000000076b4d5938> (a java.lang.Object)
- locked <0x000000076b4d5928> (a java.lang.Object)
at com.study.deadlock.DeadLockTest$1.run(DeadLockTest.java:16)
at java.lang.Thread.run(Thread.java:748)
"Service Thread" #9 daemon prio=9 os_prio=0 tid=0x000000001d673000 nid=0x96c runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C1 CompilerThread2" #8 daemon prio=9 os_prio=2 tid=0x000000001c31e800 nid=0x155c waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread1" #7 daemon prio=9 os_prio=2 tid=0x000000001c31c800 nid=0x50b8 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"C2 CompilerThread0" #6 daemon prio=9 os_prio=2 tid=0x000000001c31a000 nid=0x19f4 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000001c2ce800 nid=0x4cf0 waiting on condition [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x000000001d5f8800 nid=0x5478 runnable [0x0000000000000000]
java.lang.Thread.State: RUNNABLE
"Finalizer" #3 daemon prio=8 os_prio=1 tid=0x000000001c2a9000 nid=0x13a8 in Object.wait() [0x000000001d4ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076b408ec8> (a java.lang.ref.ReferenceQueue$Lock)
at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143)
- locked <0x000000076b408ec8> (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)
"Reference Handler" #2 daemon prio=10 os_prio=2 tid=0x0000000000f81000 nid=0x311c in Object.wait() [0x000000001d3ff000]
java.lang.Thread.State: WAITING (on object monitor)
at java.lang.Object.wait(Native Method)
- waiting on <0x000000076b406b68> (a java.lang.ref.Reference$Lock)
at java.lang.Object.wait(Object.java:502)
at java.lang.ref.Reference.tryHandlePending(Reference.java:191)
- locked <0x000000076b406b68> (a java.lang.ref.Reference$Lock)
at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153)
"VM Thread" os_prio=2 tid=0x000000001c287800 nid=0x3914 runnable
"GC task thread#0 (ParallelGC)" os_prio=0 tid=0x0000000000ea7000 nid=0x57b0 runnable
"GC task thread#1 (ParallelGC)" os_prio=0 tid=0x0000000000ea9000 nid=0x3eac runnable
"GC task thread#2 (ParallelGC)" os_prio=0 tid=0x0000000000eaa800 nid=0x4538 runnable
"GC task thread#3 (ParallelGC)" os_prio=0 tid=0x0000000000eac000 nid=0x2ad4 runnable
"VM Periodic Task Thread" os_prio=2 tid=0x000000001d6d9000 nid=0x19a0 waiting on condition
JNI global references: 6
Found one Java-level deadlock:
=============================
"Thread-B":
waiting to lock monitor 0x0000000000f872f8 (object 0x000000076b4d5928, a java.lang.Object),
which is held by "Thread-A"
"Thread-A":
waiting to lock monitor 0x0000000000f89c38 (object 0x000000076b4d5938, a java.lang.Object),
which is held by "Thread-B"
Java stack information for the threads listed above:
===================================================
"Thread-B":
at com.study.deadlock.DeadLock.funcB(DeadLock.java:37)
- waiting to lock <0x000000076b4d5928> (a java.lang.Object)
- locked <0x000000076b4d5938> (a java.lang.Object)
at com.study.deadlock.DeadLockTest$2.run(DeadLockTest.java:22)
at java.lang.Thread.run(Thread.java:748)
"Thread-A":
at com.study.deadlock.DeadLock.funcA(DeadLock.java:23)
- waiting to lock <0x000000076b4d5938> (a java.lang.Object)
- locked <0x000000076b4d5928> (a java.lang.Object)
at com.study.deadlock.DeadLockTest$1.run(DeadLockTest.java:16)
at java.lang.Thread.run(Thread.java:748)
Found 1 deadlock.