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

边栏推荐
- R语言书籍学习03 《深入浅出R语言数据分析》-第十章 关联规则 第十一章 随机森林
- The device is in use when win10 ejects USB
- 【NodeJs】Electron安装
- 每日一题-1317. 将整数转换为两个无零整数的和
- C language implements eight sorts (3)
- Precision twist jitter
- Why is the printer unable to print the test page
- 被忽略的技巧:位运算
- [Yu Yue education] General English of Shenyang Institute of Engineering (4) reference materials
- Huawei equipment configuration h-vpn
猜你喜欢

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

Superscalar processor design yaoyongbin Chapter 2 cache -- Excerpt from subsection 2.4

Classes and objects (4)

R language book learning 03 "in simple terms R language data analysis" - Chapter 8 logistic regression model Chapter 9 clustering model

电脑强制关机 oracle登录不上

Latex combat notes 3- macro package and control commands

Basic operation of graph (C language)

图书管理系统

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

leetcode 中的位运算
随机推荐
详解异步任务:函数计算的任务触发去重
One question of the day - delete duplicates of the ordered array
Glory earbud 3 Pro with three global first strong breakdowns flagship earphone Market
go os模块
Take off efficiently! Can it be developed like this?
Use the securecrtportable script function to read data from network devices
Classes and objects (2)
[niuke.com] DP30 [template] 01 Backpack
Dynamic memory management (1)
Regular execution of shell scripts in crontab
3.3 naming rules of test modules
Leetcode stack topic summary
使用VBScript读取网络的日志数据进行处理
The shortcomings of the "big model" and the strengths of the "knowledge map"
The device is in use when win10 ejects USB
leetcode 257. Binary tree paths all paths to a binary tree (simple)
C language implements eight sorts (1)
Are you still using localstorage directly? It's time to raise the bar
[academic related] under the application review system, how difficult is it to study for a doctoral degree in a double first-class university?
剑指offer数组题型总结篇