Classic interview questions: talk about the understanding of deadlock

Definition of deadlock

Deadlock refers to a state in which two or more processes (or threads) cannot continue to execute because they are waiting for each other to release resources in a concurrent system. In simple terms , a deadlock can occur when multiple processes are waiting for resources held by other processes.

Three typical cases of deadlock:

One thread one lock situation

When a thread has a lock and locks it twice in a row, if the lock is a non-reentrant lock, it will deadlock

Supplement: C++, Python, and the native locking API of the operating system are not reentrant.

Two threads and two locks

When thread t1 and thread t2 perform lock operations on lock A and lock B respectively, and then try to acquire the lock of the other party, a deadlock will result. For
example:
classmate A and classmate B both use red pens and black pens, Student A uses the red pen first, which is equivalent to locking the red pen. Student B uses the black pen first, which is equivalent to locking the black pen. A deadlock situation is formed.

public class Thread03 {
    
    
    public static void main(String[] args) {
    
    

        Object red = new Object();
        Object black = new Object();

        Thread A= new Thread(() -> {
    
    
            synchronized (red) {
    
    
                try {
    
    
                    sleep(1000);
                    System.out.println("同学A拿到红笔");
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();

                }

                synchronized (black) {
    
    
                    System.out.println("同学A拿到黑笔");
                }
            }
        });
        Thread B= new Thread(() -> {
    
    
            synchronized (black) {
    
    

                try {
    
    
                    sleep(1000);
                    System.out.println("同学B拿到黑笔");
                } catch (InterruptedException e) {
    
    
                    e.printStackTrace();
                }

                synchronized (red) {
    
    
                    System.out.println("同学B拿到红笔");
                }
            }
        });
        A.start();
        B.start();
    }
}

The result of the operation is as follows:

"C:\Program Files\Java\jdk1.8.0_192\bin\java.exe" "-javaagent:D:\Program Files\IDEA\IntelliJ IDEA Community Edition 2021.3.2\lib\idea_rt.jar=56694:D:\Program Files\IDEA\IntelliJ IDEA Community Edition 2021.3.2\bin" -Dfile.encoding=UTF-8 -classpath "C:\Program Files\Java\jdk1.8.0_192\jre\lib\charsets.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\deploy.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\access-bridge-64.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\cldrdata.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\dnsns.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\jaccess.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\jfxrt.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\localedata.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\nashorn.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunec.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunjce_provider.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunmscapi.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\sunpkcs11.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\ext\zipfs.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\javaws.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jce.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jfr.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jfxswt.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\jsse.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\management-agent.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\plugin.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\resources.jar;C:\Program Files\Java\jdk1.8.0_192\jre\lib\rt.jar;D:\Java\java\untitled\out\production\untitled" Thread03
同学A拿到红笔
同学B拿到黑笔

Thread A and thread B first lock the lock red and the lock black, and then try to acquire each other's lock, forming a deadlock.

Multiple threads and multiple locks

Classic Case - Dining Philosophers Problem

Dining Philosophers Problem (Dining Philosophers Problem) is a classic concurrent programming problem, which is used to describe the situation where multiple philosophers eat at a shared round table. Each philosopher needs to alternately think (equivalent to the blocking state of the thread) and eat (equivalent to the thread acquiring the lock and then performing some calculations), and they share some resources (such as chopsticks), but each philosopher You must hold two chopsticks at the same time to eat.

The key to the problem is how to avoid deadlock and starvation. If every philosopher goes for the chopsticks in the same order, a deadlock may result. And if a philosopher can't get the chopsticks he needs, he will wait forever, leading to starvation.

Four necessary conditions for deadlock

  • Mutual exclusion: As soon as the thread gets the lock, the second thread has to wait. (basic characteristics of locks)
  • Non-preemption: After thread one acquires the lock, unless thread one actively releases the lock, thread two cannot forcibly occupy the lock.
  • Request and hold: After the thread acquires lock A, it then tries to acquire lock B, and the lock of A is still maintained. (Lock A will not be released just because lock B is acquired)
  • Circular waiting: thread one tries to acquire lock A and lock B, thread two tries to acquire lock B and lock A, thread one waits for thread two to release B when acquiring B; at the same time, thread two waits for thread one when acquiring A Release A.
    The first three conditions are the basic characteristics of locks, and when these four conditions are met at the same time, deadlock will occur.

How to break deadlock

The breakthrough iscycle wait

Solution : Add a number to the lock, and then specify a fixed order (such as from small to large) to add the lock. When any thread adds more locks, let the thread obey the above order, and the circular waiting will be broken naturally at this time! !

Guess you like

Origin blog.csdn.net/m0_63904107/article/details/131595076