当前位置:网站首页>Code examples explain the difference between [reentrant lock] and [non reentrant lock]?
Code examples explain the difference between [reentrant lock] and [non reentrant lock]?
2022-07-26 12:42:00 【User 9919783】
First of all, we need to understand the concept of lock , In a multithreaded scenario , Running different threads together will lead to dirty data , To ensure thread safety , At this time, the concept of lock appears , Locks can be applied to code blocks , resources , Object lock , After locking , Only one thread is allowed to operate on it , Other threads will enter the blocking state , This is the blocking and locking method , Until the lock is released , Only other threads can acquire locks and operate on them .
The previous article focused on introducing Synchronized and Reentrantlock, Students who don't understand can have a look first :
currentHashMap The fair lock of , Interruptible response , Restrict waiting instances
This article mainly introduces reentrant locks and non reentrant locks , stay jdk in synchronized and Reentrantlock, Are reentrant locks , For more efficient performance and deadlock prevention .
Reentrant lock can be understood as : Under the same thread , After the outer method is locked , The method called by the inner layer can also obtain the lock normally .
Now let's introduce the code to 【 Do not reenter the lock 】 The understanding of the .
When testA() Method to obtain the lock , Call... In the business testB(), At this time because A The lock of is not released , therefore B Cannot get lock , Now B Our business cannot go on normally , Lead to A Nor can the lock be released normally ,A and B Method is deadlocked . This is the non reentrant lock .
What is a reentrant lock ?
Jdk The middle band is basically reentrant lock , Let's use synchronized introduces , Locking the same object after , Console printing can see ,threadA call threadB Method ,B and A Method will not deadlock , Business can continue .
We can use synchronized + wait + notify To implement a non reentrant lock , The code is as follows :
边栏推荐
- C语言文件知识点
- .eslintrc.js配置说明
- Use of strjoin function in MATLAB
- HCIP-9.OSPF的各种拓展
- [MySQL constraint]
- 一文看懂GaitSet中的test.py
- [wechat applet] read the article, data request
- Can the Windows version of Oracle be used in Linux?
- In the digital era, what "golden treasure" is driving the development of pharmaceutical enterprises for a century?
- Understand test.py in gaitset
猜你喜欢

【Map】万能的Map使用方法 & 模糊查询的两种方式

笔记。。。。

Use and optimization of MySQL composite index (multi column index)

2022 年要了解的新兴安全供应商

Detailed explanation of Legendre transformation and conjugate function

什么是物联网?常见IoT协议最全讲解

Minesweeping games - easy to play addictive (C language version)

The programmed navigation route jumps to the current route (the parameters remain unchanged), and if it is executed multiple times, it will throw a navigationduplicated warning error?

Industry case | how does the index help the sustainable development of Inclusive Finance in the banking industry

食品安全 | 无菌蛋真的完全无菌吗?
随机推荐
Knowledge points of C language documents
Uniapp H5, APP references external online JS
二、容器_
PXE原理与配置
In the digital era, what "golden treasure" is driving the development of pharmaceutical enterprises for a century?
Food safety | are sterile eggs really completely sterile?
.eslintrc.js配置说明
Backtracking - 491. Incremental subsequence
酷早报:7月25日Web3加密行业新闻大汇总
If there is a declaration "int x=5, y=1;", Then the expression x < y? The result of x++: y++ is:
Backtracking - 46. Full arrangement
Backtracking - 131. Split palindrome string
LCD notes (6) LCD driver framework_ Configuration pin
Jsj-3/ac220v time relay
Overseas app push (Part 2): Channel Integration Guide for overseas manufacturers
Shell variables and references
How does the chain store cashier system help shoe stores manage their branches?
Minesweeping games - easy to play addictive (C language version)
字节流习题遇到的问题及解决方法
微软关闭了两种攻击途径:Office 宏、RDP 暴力破解