当前位置:网站首页>A simple deadlock example

A simple deadlock example

2022-06-13 00:25:00 Survivors with dreams behind their backs

   Preface :
   When interviewing meituan in the front time , The interviewer came up with such a question : Write a deadlock ....

   I only remember that resources are mutually exclusive 、 The result of looting , But I haven't written it manually , As a result, it was not written .


   Deadlock generation conditions :

  1. Mutually exclusive ;

The requested resources are mutually exclusive , When used by a thread , Other threads are not available .

  1. Request and maintain ;

Threads own resources 1, To ask for resources 2. If the resource at this time 2 Can't get , Thread entry “ Request resources 2 And keep resources 1” The state of .

  1. Loop waiting for

Multiple threads request each other's resources at the same time , At the same time, it also holds the resources needed by the other party , Form a cycle .

  1. Not to deprive

A thread can only release its own resources , Can't be deprived of .

   Deadlock resolution :

Destroy one or more conditions for deadlock generation .

   Deadlock example :

package com.demo.test.demo.api;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/** *  Deadlock example  * * @author zhao.hualuo * Create at 2021/7/13 */
public class DeadLockDemo implements Runnable {
    
    /**  Conditions under which resource grabbing occurs  */
    public int flag = 1;

    /**  Robbed resources  */
    static final String STR1 = " resources 1";
    static final String STR2 = " resources 2";

    /**  Thread pool  */
    public static ExecutorService service = Executors.newFixedThreadPool(3);

    public static void main(String[] args) {
    
        // Threads 1
        DeadLockDemo demo1 = new DeadLockDemo();
        demo1.flag = 1;
        service.execute(demo1);

        /**  Threads 2 */
        DeadLockDemo demo2 = new DeadLockDemo();
        demo2.flag = 0;
        service.execute(demo2);
    }

    @Override
    public void run() {
    
        System.out.println(Thread.currentThread().getName() + ",flag=" + flag);
        if(flag == 0) {
    
            // Occupy resources 1, Grab resources 2
            synchronized(STR1) {
    
                try {
    
                    System.out.println(Thread.currentThread().getName() + " It's locked " + STR1 );
                    // sleep 3 Second is to wait for resources 2 Be occupied by the other party , Then you can't grab it , It's deadlocked 
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName() + " Get ready to get " + STR2);
                    synchronized(STR2) {
    
                        System.out.println(Thread.currentThread().getName() + " Acquired " + STR2);
                    }
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
                }
            }
        } else {
    
            // Occupy resources 2, Grab resources 1
            synchronized(STR2) {
    
                try {
    
                    System.out.println(Thread.currentThread().getName() + " It's locked " + STR2 );
                    Thread.sleep(3000);
                    System.out.println(Thread.currentThread().getName() + " Get ready to get " + STR1);
                    synchronized(STR1) {
    
                        System.out.println(Thread.currentThread().getName() + " Acquired " + STR1);
                    }
                } catch (InterruptedException e) {
    
                    e.printStackTrace();
                }
            }
        }
    }
}

   Execution results :
 Insert picture description here

   Code reference from :《 What is a deadlock ? Write deadlocks yourself . An example of a deadlock . Four conditions of deadlock , How to avoid deadlock .

原网站

版权声明
本文为[Survivors with dreams behind their backs]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202280600460214.html