当前位置:网站首页>What is deadlock? (explain the deadlock to everyone and know what it is, why it is used and how to use it)

What is deadlock? (explain the deadlock to everyone and know what it is, why it is used and how to use it)

2022-06-11 22:13:00 if you never leave me, i will be with you till death do us apart

3 Answer in three aspects :

1. What is a deadlock ?

It provides several ways of understanding to help people better understand ; If you have any questions, you can ask ;

A deadlock is a group of threads competing for resources because they wait for each other “ permanent “ Blocking phenomenon ; ( wait for me I'll be waiting for you. If you don't let me go As a result “ permanent “ Blocking phenomenon )

Deadlock refers to the execution of two or more processes , A blocking phenomenon caused by competition for resources or communication with each other , If there is no external force , They will not be able to move forward . At this point, the system is said to be in a deadlock state or the system has produced a deadlock , These processes that are always waiting for each other are called deadlock processes

After knowing that you are a deadlock, we need to know why there is a deadlock , Please see the following ;

2. The cause of the deadlock ?

Divided into the following 4 Clock cause

mutual exclusion Shared resources X y It can only be occupied by one thread
Occupy and wait Threads T1 Shared resources occupied X He is waiting to share Y When you can't help yourself X
Do not take Other threads cannot preempt t1 Resources occupied by threads
Loop waiting for Threads t1 etc. t2 Resources in possession , Threads t2 etc. t1 Resources in possession Cycles and so on

 Insert picture description here

3. How to avoid deadlock ?

The reason for breaking the deadlock can be avoided

mutual exclusion It cannot be broken because the lock itself solves the problem of thread safety through mutual exclusion
Occupy and wait Apply for all resources at once and there will be no waiting
Do not take Threads that take up some resources , When further applying for other resources, if the application cannot be obtained, the resources he occupies can be released , This will destroy the non preemptive
Loop waiting for Apply for resources in order for prevention , The so-called sequential application means that resources have a linear order , When applying, you can first apply for a resource with a small serial number When applying for resources with large serial number , This linearizes the loop without waiting

 Insert picture description here

Let's use code to understand deadlock :

package com.example;

public class Deadlock {
    

       public static String str1 = "str1";
        public static String str2 = "str2";

        public static void main(String[] args){
    
            Thread a = new Thread(() -> {
    
                try{
    
                    while(true){
    
                        synchronized(Deadlock.str1){
    
                            System.out.println(Thread.currentThread().getName()+" Lock the  str1");
                            Thread.sleep(1000);
                            synchronized(Deadlock.str2){
    
                                System.out.println(Thread.currentThread().getName()+" Lock the  str2");
                            }
                        }
                    }
                }catch(Exception e){
    
                    e.printStackTrace();
                }
            });

            Thread b = new Thread(() -> {
    
                try{
    
                    while(true){
    
                        synchronized(Deadlock.str2){
    
                            System.out.println(Thread.currentThread().getName()+" Lock the  str2");
                            Thread.sleep(1000);
                            synchronized(Deadlock.str1){
    
                                System.out.println(Thread.currentThread().getName()+" Lock the  str1");
                            }
                        }
                    }
                }catch(Exception e){
    
                    e.printStackTrace();
                }
            });

            a.start();
            b.start();
        }
    }




The above code is a complete deadlock program , There are two threads in the program , Threads 1 lock str1, Sleep after obtaining the lock 1 Second , At this point the thread 2 lock str2, Also perform sleep operation .
Threads 1 Lock it after hibernation str2, however str2 Has been threaded 2 It's locked , We can only wait here , Same thing , Threads 2 After hibernation, you should also lock it str1, Will also wait , So a deadlock occurs .
 Insert picture description here Put the following 2 Change the value of the line code to the same , It won't exist

public static String str1 = "str1";
public static String str2 = "str1";

When declaring an object as a lock, you should pay attention to the string type lock object , Because strings have a constant pool , If the lock held by different threads is a string lock with the same character , Two locks are actually the same lock .

 Insert picture description here

原网站

版权声明
本文为[if you never leave me, i will be with you till death do us apart]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/162/202206112201113867.html