④. 死锁及排查
- ①. 什么是死锁?
死锁是指两个或两个以上的线程在执行过程中,因争夺资源而造成的一种互相等待
的现象,若无外力干涉那它们都将无法推进下去,如果资源充足,进程的资源请求都
能够得到满足,死锁出现的可能性就很低,否则就会因争夺有限的资源而陷入死锁
- ②. 产生死锁的原因
- 系统资源不足
- 进程运行推进的顺序不合适
- 资源分配不当
- ③. 代码展示
public class DeadLockDemo{ static Object lockA = new Object(); static Object lockB = new Object(); public static void main(String[] args){ Thread a = new Thread(() -> { synchronized (lockA) { System.out.println(Thread.currentThread().getName() + "\t" + " 自己持有A锁,期待获得B锁"); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockB) { System.out.println(Thread.currentThread().getName() + "\t 获得B锁成功"); } } }, "a"); a.start(); new Thread(() -> { synchronized (lockB){ System.out.println(Thread.currentThread().getName()+"\t"+" 自己持有B锁,期待获得A锁"); try { TimeUnit.SECONDS.sleep(1); } catch (InterruptedException e) { e.printStackTrace(); } synchronized (lockA){ System.out.println(Thread.currentThread().getName()+"\t 获得A锁成功"); } } },"b").start(); } }
④. 如何排除死锁方式一:纯命令
D:\studySoft\Idea201903\JavaSelfStudy>jps 10048 Launcher 6276 DeadLockDemo 6332 Jps 9356 D:\studySoft\Idea201903\JavaSelfStudy>jstack 6276 (最后面有一个发现了一个死锁) 2021-07-28 16:05:36 Full thread dump Java HotSpot(TM) 64-Bit Server VM (25.111-b14 mixed mode): "DestroyJavaVM" #16 prio=5 os_prio=0 tid=0x0000000003592800 nid=0x830 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "b" #15 prio=5 os_prio=0 tid=0x00000000253d5000 nid=0x1ba8 waiting for monitor entry [0x0000000025c8e000] java.lang.Thread.State: BLOCKED (on object monitor) at com.xiaozhi.juc.DeadLockDemo.lambda$main$1(DeadLockDemo.java:31) - waiting to lock <0x0000000741404050> (a java.lang.Object) - locked <0x0000000741404060> (a java.lang.Object) at com.xiaozhi.juc.DeadLockDemo$$Lambda$2/2101440631.run(Unknown Source) at java.lang.Thread.run(Thread.java:745) "a" #14 prio=5 os_prio=0 tid=0x00000000253d3800 nid=0xad8 waiting for monitor entry [0x0000000025b8e000] java.lang.Thread.State: BLOCKED (on object monitor) at com.xiaozhi.juc.DeadLockDemo.lambda$main$0(DeadLockDemo.java:20) - waiting to lock <0x0000000741404060> (a java.lang.Object) - locked <0x0000000741404050> (a java.lang.Object) at com.xiaozhi.juc.DeadLockDemo$$Lambda$1/1537358694.run(Unknown Source) at java.lang.Thread.run(Thread.java:745) "Service Thread" #13 daemon prio=9 os_prio=0 tid=0x000000002357b800 nid=0x1630 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C1 CompilerThread3" #12 daemon prio=9 os_prio=2 tid=0x00000000234f6000 nid=0x1fd4 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread2" #11 daemon prio=9 os_prio=2 tid=0x00000000234f3000 nid=0x5c0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread1" #10 daemon prio=9 os_prio=2 tid=0x00000000234ed800 nid=0x1afc waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "C2 CompilerThread0" #9 daemon prio=9 os_prio=2 tid=0x00000000234eb800 nid=0x2ae0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "JDWP Command Reader" #8 daemon prio=10 os_prio=0 tid=0x0000000023464800 nid=0xc50 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "JDWP Event Helper Thread" #7 daemon prio=10 os_prio=0 tid=0x000000002345f800 nid=0x1b0c runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "JDWP Transport Listener: dt_socket" #6 daemon prio=10 os_prio=0 tid=0x0000000023451000 nid=0x2028 runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Attach Listener" #5 daemon prio=5 os_prio=2 tid=0x000000002343f800 nid=0x1ea0 waiting on condition [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Signal Dispatcher" #4 daemon prio=9 os_prio=2 tid=0x00000000233eb800 nid=0x10dc runnable [0x0000000000000000] java.lang.Thread.State: RUNNABLE "Finalizer" #3 daemon prio=8 os_prio=1 tid=0x00000000233d3000 nid=0xafc in Object.wait() [0x000000002472f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x0000000741008e98> (a java.lang.ref.ReferenceQueue$Lock) at java.lang.ref.ReferenceQueue.remove(ReferenceQueue.java:143) - locked <0x0000000741008e98> (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=0x0000000021d0d000 nid=0x28ec in Object.wait() [0x000000002462f000] java.lang.Thread.State: WAITING (on object monitor) at java.lang.Object.wait(Native Method) - waiting on <0x0000000741006b40> (a java.lang.ref.Reference$Lock) at java.lang.Object.wait(Object.java:502) at java.lang.ref.Reference.tryHandlePending(Reference.java:191) - locked <0x0000000741006b40> (a java.lang.ref.Reference$Lock) at java.lang.ref.Reference$ReferenceHandler.run(Reference.java:153) JNI global references: 2504 Found one Java-level deadlock: ============================= "b": waiting to lock monitor 0x0000000021d10b58 (object 0x0000000741404050, a java.lang.Object), which is held by "a" "a": waiting to lock monitor 0x0000000021d13498 (object 0x0000000741404060, a java.lang.Object), which is held by "b" Java stack information for the threads listed above: =================================================== "b": at com.xiaozhi.juc.DeadLockDemo.lambda$main$1(DeadLockDemo.java:31) - waiting to lock <0x0000000741404050> (a java.lang.Object) - locked <0x0000000741404060> (a java.lang.Object) at com.xiaozhi.juc.DeadLockDemo$$Lambda$2/2101440631.run(Unknown Source) at java.lang.Thread.run(Thread.java:745) "a": at com.xiaozhi.juc.DeadLockDemo.lambda$main$0(DeadLockDemo.java:20) - waiting to lock <0x0000000741404060> (a java.lang.Object) - locked <0x0000000741404050> (a java.lang.Object) at com.xiaozhi.juc.DeadLockDemo$$Lambda$1/1537358694.run(Unknown Source) at java.lang.Thread.run(Thread.java:745) Found 1 deadlock.
⑤. 如何排除死锁方式二:jconsole(输入cmd,输入jconsole,点击检测死锁按钮)