当前位置:网站首页>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 |

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 |

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 .
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 .

边栏推荐
- leetcode 中的位运算
- How to view the installation date of the win system
- Glory earbud 3 Pro with three global first strong breakdowns flagship earphone Market
- The college entrance examination is over, and life has just begun. Suggestions from a 10-year veteran in the workplace
- Go OS module
- win10字体模糊怎么调节
- 科普 | NFT的类型有哪些(上)
- Go encoding package
- Non recursive writing of quick sort
- One question per day -- verifying palindrome string
猜你喜欢

leetcode 257. Binary Tree Paths 二叉树的所有路径(简单)

高考结束,人生才刚刚开始,10年职场老鸟给的建议

Simple example of logistic regression for machine learning

Tkinter学习笔记(三)

Latex combat notes 3- macro package and control commands

Inner join execution plan changed

详解异步任务:函数计算的任务触发去重

On the night of the joint commissioning, I beat up my colleagues

Learning bit segment (1)

Matlab: solution of folder locking problem
随机推荐
超标量处理器设计 姚永斌 第2章 Cache --2.2 小节摘录
[Yu Yue education] Yancheng Normal University Advanced Algebra reference
Analysis of the implementation principle of an open source markdown to rich text editor
[Yu Yue education] calculus of Zhejiang University in autumn and winter 2021 (I) reference materials
Huawei equipment configuration hovpn
Leetcode stack topic summary
C language to achieve eight sorts (2)
高考结束,人生才刚刚开始,10年职场老鸟给的建议
R7-1 列表或元组的数字元素求和
还在直接用 localStorage 么?该提升下逼格了
Use the securecrtportable script function to read data from network devices
inner join执行计划变了
Tkinter学习笔记(二)
Optimization of quick sort
6.项目上线
69. square root of X
STM32 development note 113:ads1258 drive design - reading temperature value
超标量处理器设计 姚永斌 第2章 Cache --2.4 小节摘录
Are you still using localstorage directly? It's time to raise the bar
Unity3d getlaunchintintforpackage getting package returned null