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




边栏推荐
- Multithreading--the usage of threads and thread pools
- idea2021+Activiti [the most complete note one (basic use)]
- Re21:读论文 MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
- Re15:读论文 LEVEN: A Large-Scale Chinese Legal Event Detection Dataset
- 软考 系统架构设计师 简明教程 | 案例分析 | 需求分析
- Mysterious APT Attack
- [100 Solidity Skills] 1. Contract reentrancy attack
- 快解析结合任我行crm
- 606. 根据二叉树创建字符串(视频讲解!!!)
- 这种叫什么手法
猜你喜欢
随机推荐
Multithreading--the usage of threads and thread pools
Re19: Read the paper Paragraph-level Rationale Extraction through Regularization: A case study on European Court
Redis Desktop Manager 2022.4.2 released
New in GNOME: Warn users when Secure Boot is disabled
(Text) Frameless button settings
Re21: Read the paper MSJudge Legal Judgment Prediction with Multi-Stage Case Representation Learning in the Real
Alibaba Cloud OSS Object Storage
hcip06 ospf special area comprehensive experiment
Re19:读论文 Paragraph-level Rationale Extraction through Regularization: A case study on European Court
软考 系统架构设计师 简明教程 | 案例分析 | 需求分析
Container Technology - A Simple Understanding of Kubernetes Objects
Meikle Studio - see the actual combat notes of Hongmeng equipment development five - drive subsystem development
线上靶机prompt.ml
学习笔记11--局部轨迹直接构造法
Multi-threading scheme to ensure that a single thread opens a transaction and takes effect
Online target drone prompt.ml
STM32CubeMX configuration to generate FreeRTOS project
In the robot industry professionals, Mr Robot industry current situation?
Shell系统学习之数组
MySQL |子查询









