Small case of deadlock

Some small concepts about deadlock

Deadlock is a phenomenon in which two or more processes are blocked due to competition for resources or due to communication with each other during the execution process. If there is no external force, they will not be able to advance. At this time, the system is said to be in a deadlock state or the system has a deadlock, and these processes that are always waiting for each other are called deadlock processes.
Personally, I like to call deadlocks the threading version of snipe and clam.

1. Cause (Copied)

(1) Because of insufficient system resources.  
(2). The order in which the process runs and advances is inappropriate.   
(3) Improper allocation of resources, etc.

  If the system resources are sufficient and the resource requests of the process can be satisfied, the possibility of deadlock occurrence is very low, otherwise it
  will fall into deadlock due to competition for limited resources. Second, processes run in different order and speed, which may also cause deadlocks.

2. Four major conditions generated (replicated)

(1) Mutual exclusion condition: A resource can only be used by one process at a time.

(2) Request and hold conditions: When a process is blocked due to requesting resources, it will hold on to the obtained resources.
(3) No deprivation condition: The resources that the process has obtained cannot be forcibly deprived before the end of the use.
(4) Circular waiting condition: A cyclic waiting resource relationship is formed between several processes.

  These four conditions are the necessary conditions for deadlock. As long as the system is deadlocked, these conditions must be established, and as long as
  one not satisfied, deadlock will not occur.

3. How to avoid

By understanding the causes of deadlocks, especially the four necessary conditions for deadlocks, it is possible to avoid, prevent and
remove . Therefore, in system design, process scheduling, etc., pay attention to how to prevent these four necessary conditions from being established, and how to
determine a reasonable allocation algorithm for resources to prevent processes from permanently occupying system resources. Also, prevent processes from consuming resources while they are in
a . Therefore, reasonable planning should be given to the allocation of resources.

A simple case written by a person
public class DeathThreadDemo {
    public static void main(String[] args) {
        DeadLock dt0=new DeadLock(0);
        DeadLock dt1=new DeadLock(1);
        new Thread(dt0).start();
        new Thread(dt1).start();
    }
}


class DeadLock implements Runnable{
    private int value;
    private static Object o1 = new Object(), o2 = new Object(); 
    public DeadLock(int value){
        this.value=value;
    }
    public void run() {
        if(value==0){
            synchronized(o1){
                try {
                    Thread.sleep(3000);
                    for (int i = 11; i < 20; i++) {
                        System.out.println("o1"+i);
                    }
                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                synchronized(o2){
                    System.out.println("o2"+value);
                }
            }

        }
        if(value==1){
            synchronized(o2){
                try {
                    Thread.sleep(3000);
                    for (int i = 1; i < 10; i++) {
                        System.out.println("o2"+i);
                    }

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                synchronized(o1){
                    System.out.println("o1"+value);
                }
            }

        }
    }
}

operation result:
write picture description here

Personal explanation:
write picture description here

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325907538&siteId=291194637