当前位置:网站首页>一个注解替换synchronized关键字:分布式场景下实现方法加锁
一个注解替换synchronized关键字:分布式场景下实现方法加锁
2022-08-04 01:08:00 【[email protected】
一个注解替换synchronized关键字:分布式场景下实现方法加锁
一、前言
单机部署场景下,当我们需要锁住少量代码块或者方法,通常会使用synchronized关键字进行加锁,如下所示
public synchronized void test(){
}
public void test2(){
Object o = new Object();
synchronized (o){
}
}
但是,在分布式场景下,由于jvm之间无法通信,一个服务是无法感知另外一个服务对当前代码快加锁。
该demo实现了分布式场景下,使用一个注解,对方法进行加锁。单机也可以使用。
二 、实现原理
1.原理
利用Aspect的Around方法,对需要加锁的方法进行动态代理
在方法执行前获取Redisson锁对象,获取成功,方法执行。执行成功后释放锁
获取规则根据注解的相关参数进行设置。
用Redisson进行加锁
2.注解参数描述
- value锁名称
- waitTime等待超时:默认5秒,当waitTime<0,表示一直等待获取锁
- leaseTime过期时长:默认10秒
- timeUnit时长单位:默认单位秒
三、公平锁注解(@FairLock)
@FairLock实现了对接口fairLock加锁,知道该接口当前请求结束后,才可以再次被请求
@FairLock("fairLock")
@RequestMapping("fairLock")
public Date fairLock() throws InterruptedException {
Thread.sleep(3000);
return new Date();
}
四、读写锁注解(@ReadWriteLock)
@ReadWriteLock(value = “readWrite”,lockType = ReadWriteLockType.WRITELOCK)
如下图所示,用@ReadWriteLock对方法进行加读写锁,锁名称为readWrite,类型为读锁

import com.mabo.redis.easycode.readWriteLock.ReadWriteLock;
import com.mabo.redis.easycode.readWriteLock.ReadWriteLockType;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.text.SimpleDateFormat;
import java.util.Date;
@RestController
public class ReadWriteLockController {
private static SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
@ReadWriteLock(value = "readWrite",lockType = ReadWriteLockType.READLOCK)
@RequestMapping("read")
public String read() throws InterruptedException {
System.out.println("开始执行read");
Thread.sleep(100000);
System.out.println("read执行结束");
return sdf.format(new Date());
}
/** * value锁名称 * waitTime等待超时:默认5秒,当waitTime<0,表示一直等待获取锁 * leaseTime过期时长:默认10秒 * timeUnit时长单位:默认秒 */
@ReadWriteLock(value = "readWrite",lockType = ReadWriteLockType.WRITELOCK)
@RequestMapping("write")
public String write() throws InterruptedException {
Thread.sleep(3000);
return sdf.format(new Date());
}
}
五、下载地址
github下载地址: github
https://github.com/MaBo2420935619/EasycodeRedissonLock
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/weixin_47053123/article/details/126138519
边栏推荐
- appium软件自动化测试框架
- KunlunBase 1.0 is released!
- 【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘
- LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化
- typescript53 - generic constraints
- Android interview questions and answer analysis of major factories in the first half of 2022 (continuously updated...)
- 取模运算(MOD)
- typescript56-泛型接口
- Analysis of usage scenarios of mutex, read-write lock, spin lock, and atomic operation instructions xaddl and cmpxchg
- nodejs切换版本使用(不需要卸载重装)
猜你喜欢
随机推荐
Slipper —— 虚点,最短路
Installation and configuration of nodejs+npm
XSS-绕过for循环过滤
即席查询——Presto
js中常用的几种遍历处理数据的方法梳理
outputBufferIndex = mDecode.dequeueOutputBuffer(bufferInfo, 0) 一直返回为-1
typescript50 - type specification between cross types and interfaces
网络带宽监控,带宽监控工具哪个好
VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
【QT小记】QT中信号和槽的基本使用
MySQL回表指的是什么
KunlunBase 1.0 发布了!
typescript56-泛型接口
typescript48-函数之间的类型兼容性
TensoFlow学习记录(二):基础操作
- heavy OpenCV 】 【 mapping
静态文件快速建站
C 学生管理系统_分析
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
LDO investigation








