当前位置:网站首页>Thread deadlock and its solution
Thread deadlock and its solution
2022-06-12 09:15:00 【Java supernatural Road】
Four necessary conditions for thread deadlock
If the following four conditions are true in a system , Then it can cause a deadlock :
- Mutual exclusivity : The possession of resources by threads is exclusive , A resource can only be occupied by one thread , Until released .
- Request and hold conditions : When a thread blocks a resource that is requested to be occupied , Don't release the acquired resources .
- Not to deprive : Before a thread releases resources , Other threads cannot be deprived of occupation .
- Loop waiting for : When you have a life and death lock , Thread goes into a dead loop , Permanent blocking .
resolvent :
- Mutual exclusivity , You can use ThreadLocal
- Request and hold conditions , Kick one out ( End one )
- Not to deprive ,trylcok, Add a timeout , Unable to seize and release resources
Distributed lock , It can be used DB,redis Of nx Implement distributed locks
DB Distributed lock
1、 Generate... According to the business field transaction_id, And thread safe creation of lock resources
2、 according to transaction_id To apply for the lock
3、 Release the lock
Threads within a process can be based on obj To synchronize .obj In this case, it can be understood as a lock object . If the thread wants to enter synchronized In code block , Must hold first obj A lock on an object . This kind of lock is JAVA The built-in lock inside , The creation process is thread safe . Then with the help of DB, How to ensure that the process of creating a lock is thread safe ? You can use DB Medium UNIQUE
KEY characteristic , Once there's a repeat key, because UNIQUE
KEY Uniqueness , Will throw an exception . stay JAVA Inside , yes SQLIntegrityConstraintViolationException abnormal .
边栏推荐
- top命令含义
- 128. 最長連續序列-哈希錶
- Machine learning notes - circular neural network memo list
- day5-x
- Sword finger offer:[day 9 dynamic planning (medium)] --- > maximum sum of continuous subarrays
- Node sample background setup
- Several ways to restart kubernetes pod
- Permission modifiers and code blocks
- Sword finger offer II 016 Longest substring without repeating characters - sliding window
- Distributed transaction solution 2: message queue to achieve final consistency
猜你喜欢

Four steps for sending rockertmq producer messages
功能测试面试常见的技术问题,总结好了你不看?

网络层IP协议 ARP&ICMP&IGMP NAT
![Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock](/img/42/000a3e601ba1771a1ee07fcd800307.jpg)
Offer:[day 8 dynamic planning (simple)] --- > maximum profit of stock

Countdownlatch example

Swagger documentation details

Notes on data mining in Tsinghua University (1)

Unittest test framework

Flink CheckPoint : Exceeded checkpoint tolerable failure threshold

Minimum transfer times
随机推荐
128. 最长连续序列-哈希表
128. longest continuous sequence hash table
Black screen solution for computer boot
Sword finger offer:[day 8 dynamic planning (simple)] --- > frog jumping on steps
重启Kubernetes Pod的几种方式
MySQL learning record - II. MySQL create table command
Flink传入自定义的参数或配置文件
Basic SQL syntax i
软件测试面试题精选
软件测试面试官问这些问题的背后意义你知道吗?
RecyclerView的onBindViewHolder被同时调用两次解决方法
Chapter IV - first procedure
[computer use] how to change a computer disk into a mobile disk?
自动化测试学习路线,快来学吧
Flink CheckPoint : Exceeded checkpoint tolerable failure threshold
Visualization of two-dimensional feature logistic regression prediction results
APP测试面试题汇总,面试必考一定要看
Jenkins Pipeline 语法
(15) Tweenrunner
软件测试基础知识分享,写点你不知道的