当前位置:网站首页>Understanding of deadlock
Understanding of deadlock
2022-07-30 10:19:00 【@Luna】
Deadlock problem with bank transfers
I. Overview
1. Bank transfer problem (A to B transfers 1,000 yuan, involving the cash balance of two users)
The transfer business is a kind of transaction, so it is necessary to ensure the security of the data.Note is an atomic operation.
(1) If the balance of user A is less than the balance to be transferred, the balance of the transferor cannot be deducted.
(2) If the balance of user A is less than the balance to be transferred, the balance of the transferor cannot be deducted, and the transfer is stopped to remind the transferor that the transfer failed.
(3) If there is an error in the middle of the transfer (account A is successfully transferred out and deducted, account B fails to be added/A transfers to B and B transfers to A, the order of acquiring the locks is reversed, resulting in a deadlock), the data needs to be returned.roll.
2. For a transfer operation, 2 locks are required, one locks the balance of the transfer account, and the other locks the balance of the transfer account.But the sequence of locking will cause the program to deadlock.
3. The situation of deadlock: A transfers 100 to B (when the t1 thread executes the transfer from A to B, the account lock of A is successfully acquired + dormant + waiting to acquire the lock of B account), and B also transfers 100 to A (t2When the thread executes the transfer from B to A, the lock of the account B is successfully acquired + also waiting to acquire the lock of the A account), resulting in a deadlock
Second, the four elements of deadlock
1. Mutual exclusion, shared resources X and Y can only be occupied by one thread;
2. Occupy and wait, thread T1 has acquired shared resource X, and does not release shared resource X while waiting for shared resource Y;
3. No preemption, other threads cannot forcibly preempt the resources occupied by thread T1;
4. Circular waiting, thread T1 waits for the resources occupied by thread T2, and thread T2 waits for the resources occupied by thread T1, which is circular waiting;
III. Bank transfer code:
Deadlock version code:



Avoid deadlock version:
Original rule:
Get the sender first, and then acquire the lock of the payee, which will cause two senders to wait for the other party to release the lock.
Changed rules:
All transactions acquire smaller locks first, and then acquire larger locks before acquiring smaller locks, which avoids a circular deadlock.The size of the two locks is the same, and an additional lock is required to control the transaction process.
The principle I understand:
1. Because the two objects are transferred at the same time, the two objects are equals, so the one with the smaller hashcode value is fixed, and the value is locked first each timeSmall, it can ensure that each thread is locked sequentially to avoid deadlock.
2. Even if the two hashcodes are equal, you can first compete for the third lock to ensure that one person completes the business and releases the lock first, so that there will be no locks that each owns a lock and waits for the object to be released at the beginning.situation.




边栏推荐
- 实战演练 | 在 MySQL 中计算每日平均日期或时间间隔
- (BUG record) No module named PIL
- 你真的懂Redis的5种基本数据结构吗?
- Day113. Shangyitong: WeChat login QR code, login callback interface
- Meikle Studio-Look at the actual combat notes of Hongmeng device development six-wireless networking development
- Flask之路由(app.route)详解
- BERT预训练模型系列总结
- Re18:读论文 GCI Everything Has a Cause: Leveraging Causal Inference in Legal Text Analysis
- ESP32 入门篇(一)使用 VS Code 进行开发环境安装
- CVTE校招笔试题+知识点总结
猜你喜欢
随机推荐
JVM内存布局、类加载机制及垃圾回收机制详解
flowable工作流所有业务概念
The creation of a large root heap (video explanation)
Re20:读论文的先例:普通法的信息理论分析
Re19:读论文 Paragraph-level Rationale Extraction through Regularization: A case study on European Court
Re19: Read the paper Paragraph-level Rationale Extraction through Regularization: A case study on European Court
PyQt5 - draw sine curve with pixels
新一代开源免费的终端工具,太酷了
自适应控制——仿真实验一 用李雅普诺夫稳定性理论设计自适应规律
HR团队如何提升效率?人力资源RPA给你答案
Re16: Read the paper ILDC for CJPE: Indian Legal Documents Corpus for Court Judgment Prediction and Explanation
Paper reading: SegFormer: Simple and Efficient Design for Semantic Segmentation with Transformers
ospf2 two-point two-way republish (question 2)
Shell系统学习之函数
Security思想项目总结
CVTE school recruitment written test questions + summary of knowledge points
你真的懂Redis的5种基本数据结构吗?
线上靶机prompt.ml
105. 从前序与中序遍历序列构造二叉树(视频讲解!!)
mysql安装教程【安装版】








