简单了解Java中的可重入锁
作者:syrdbt 时间:2023-12-18 12:29:19
这篇文章主要介绍了简单了解Java中的可重入锁,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
本文里面讲的是广义上的可重入锁,而不是单指JAVA下的ReentrantLock。
可重入锁,也叫做递归锁,指的是同一线程 外层函数获得锁之后 ,内层递归函数仍然有获取该锁的代码,但不受影响。
在JAVA环境下 ReentrantLock 和synchronized 都是 可重入锁。
下面是使用实例:
package reentrantLock;
public class Test implements Runnable{
public synchronized void get(){
System.out.println(Thread.currentThread().getId());
set();
}
public synchronized void set(){
System.out.println(Thread.currentThread().getId());
}
@Override
public void run() {
get();
}
public static void main(String[] args) {
Test ss=new Test();
new Thread(ss).start();
new Thread(ss).start();
new Thread(ss).start();
}
}
运行截图:
package reentrantLock;
import java.util.concurrent.locks.ReentrantLock;
public class Test implements Runnable {
ReentrantLock lock = new ReentrantLock();
public void get() {
lock.lock();
System.out.println(Thread.currentThread().getId());
set();
lock.unlock();
}
public void set() {
lock.lock();
System.out.println(Thread.currentThread().getId());
lock.unlock();
}
@Override
public void run() {
get();
}
public static void main(String[] args) {
Test ss = new Test();
new Thread(ss).start();
new Thread(ss).start();
new Thread(ss).start();
}
}
可重入锁最大的作用是避免死锁
我们以自旋锁作为例子,
public class SpinLock {
private AtomicReference<Thread> owner =new AtomicReference<>();
public void lock(){
Thread current = Thread.currentThread();
while(!owner.compareAndSet(null, current)){
}
}
public void unlock (){
Thread current = Thread.currentThread();
owner.compareAndSet(current, null);
}
}
对于自旋锁来说,
1、若有同一线程两调用lock() ,会导致第二次调用lock位置进行自旋,产生了死锁说明这个锁并不是可重入的。(在lock函数内,应验证线程是否为已经获得锁的线程)
2、若1问题已经解决,当unlock()第一次调用时,就已经将锁释放了。实际上不应释放锁。(采用计数次进行统计)修改之后,如下:
public class SpinLock1 {
private AtomicReference<Thread> owner =new AtomicReference<>();
private int count =0;
public void lock(){
Thread current = Thread.currentThread();
if(current==owner.get()) {
count++;
return ;
}
while(!owner.compareAndSet(null, current)){
}
}
public void unlock (){
Thread current = Thread.currentThread();
if(current==owner.get()){
if(count!=0){
count--;
}else{
owner.compareAndSet(current, null);
}
}
}
}
该自旋锁即为可重入锁。
来源:https://blog.csdn.net/qq_38737992/article/details/90613821
标签:java,可重入,锁
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java Socket编程服务器响应客户端实例代码
2023-08-09 03:25:51
C#中Dictionary泛型集合7种常见的用法
2021-08-07 04:47:17
Java十分钟精通类 封装 继承
2023-11-25 10:55:58
Java synchronized偏向锁的核心原理详解
2022-12-26 12:11:58
![](https://img.aspxhome.com/file/2023/1/101811_0s.png)
Spring Boot分离配置文件的多种方式总结
2021-08-31 14:43:37
![](https://img.aspxhome.com/file/2023/7/77457_0s.jpg)
Unity 点击UI与点击屏幕冲突的解决方案
2022-02-22 15:50:49
![](https://img.aspxhome.com/file/2023/7/92967_0s.jpg)
Android SDK在线更新镜像服务器大全
2023-03-14 05:44:27
![](https://img.aspxhome.com/file/2023/8/103878_0s.jpg)
Android自定义VIew实现卫星菜单效果浅析
2022-09-23 22:44:43
![](https://img.aspxhome.com/file/2023/4/115984_0s.png)
Android 调用系统照相机拍照和录像
2023-10-30 05:40:35
Java中的内部类你了解吗
2022-12-11 08:29:10
![](https://img.aspxhome.com/file/2023/0/62430_0s.png)
Android中复制图片的实例代码
2023-01-07 21:34:26
c#获取gridview的值代码分享
2023-06-27 22:23:50
Feign如何自定义注解翻译器
2022-11-09 07:05:04
SpringBoot访问外部文件及默认路由问题
2021-08-12 10:58:01
![](https://img.aspxhome.com/file/2023/7/65157_0s.png)
Android开发组件化架构设计原理到实战
2023-06-14 12:51:13
![](https://img.aspxhome.com/file/2023/5/98675_0s.jpg)
Android App中使用Gallery制作幻灯片播放效果
2022-04-03 21:53:37
![](https://img.aspxhome.com/file/2023/1/139461_0s.jpg)
Java调用windows系统的CMD命令并启动新程序
2021-11-27 17:09:13
基于Java HashMap的死循环的启示详解
2021-08-15 20:01:16
Android实现可拖动层叠卡片布局
2023-03-25 03:28:06
![](https://img.aspxhome.com/file/2023/9/126729_0s.jpg)
Java RateLimiter的限流详解
2023-01-06 17:14:12
![](https://img.aspxhome.com/file/2023/9/63669_0s.png)