Java multithreading-7-deadlock

Seven, deadlock

1. The problem leads to

class DeadLock {
    
    
    private final Object LOCK_A = new Object();
    private final Object LOCK_B = new Object();

    public void methodA() {
    
    
        synchronized (LOCK_A) {
    
    
            try {
    
     Thread.sleep(1000); } catch (InterruptedException e) {
    
     e.printStackTrace(); }

            System.out.println(Thread.currentThread().getName() + "获取锁A,尝试获取锁B");
            synchronized (LOCK_B) {
    
    
            }
        }
    }

    public void methodB() {
    
    
        synchronized (LOCK_B) {
    
    
            try {
    
     Thread.sleep(1000); } catch (InterruptedException e) {
    
     e.printStackTrace(); }

            System.out.println(Thread.currentThread().getName() + "获取锁B,尝试获取锁A");
            synchronized (LOCK_A) {
    
    
            }
        }
    }

}

public class DeadLockTest {
    
    

    public static void main(String[] args) {
    
    
        DeadLock dl = new DeadLock();

        new Thread(dl::methodA, "线程A").start();
        new Thread(dl::methodB, "线程B").start();
    }

}

Execute the code, you can see that the program is blocked and cannot be executed

2. The cause of the problem

Multiple threads that have acquired locks, when trying to acquire locks already held by other threads, deadlock occurs

3. How to troubleshoot

Use the two commands that come with Java: jpsandjstack

First execute the command:, jps -lget the process ID of the Java program being executed

Then execute the command:, jstack -l 进程IDyou can see the following similar information

"线程B" #12 prio=5 os_prio=0 tid=0x000000001657a800 nid=0x65c waiting for monitor entry [0x0000000016d8f000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at DeadLock.methodB(DeadLockTest.java:21)
        - waiting to lock <0x00000000f57e2568> (a java.lang.Object)
        - locked <0x00000000f57e2578> (a java.lang.Object)
        at DeadLockTest$$Lambda$2/205797316.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None

"线程A" #11 prio=5 os_prio=0 tid=0x0000000016579800 nid=0x4f4 waiting for monitor entry [0x0000000016c8e000]
   java.lang.Thread.State: BLOCKED (on object monitor)
        at DeadLock.methodA(DeadLockTest.java:11)
        - waiting to lock <0x00000000f57e2578> (a java.lang.Object)
        - locked <0x00000000f57e2568> (a java.lang.Object)
        at DeadLockTest$$Lambda$1/1637070917.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

   Locked ownable synchronizers:
        - None



Found one Java-level deadlock:
=============================
"线程B":
  waiting to lock monitor 0x0000000002ffde38 (object 0x00000000f57e2568, a java.lang.Object),
  which is held by "线程A"
"线程A":
  waiting to lock monitor 0x0000000002ffb658 (object 0x00000000f57e2578, a java.lang.Object),
  which is held by "线程B"

Java stack information for the threads listed above:
===================================================
"线程B":
        at DeadLock.methodB(DeadLockTestjava:21)
        - waiting to lock <0x00000000f57e2568> (a java.lang.Object)
        - locked <0x00000000f57e2578> (a java.lang.Object)
        at DeadLockTest$$Lambda$2/205797316.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)
"线程A":
        at DeadLock.methodA(DeadLockTestjava:11)
        - waiting to lock <0x00000000f57e2578> (a java.lang.Object)
        - locked <0x00000000f57e2568> (a java.lang.Object)
        at DeadLockTest$$Lambda$1/1637070917.run(Unknown Source)
        at java.lang.Thread.run(Thread.java:748)

Found 1 deadlock.

The location of the deadlock is clearly marked

Guess you like

Origin blog.csdn.net/adsl624153/article/details/103865385