当前位置:网站首页>一个注解替换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
边栏推荐
- 字符串的排列
- js中常用的几种遍历处理数据的方法梳理
- 虚拟机CentOS7中无图形界面安装Oracle
- VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
- ThreadLocal
- Use nodejs switch version (no need to uninstall and reinstall)
- 《Greenplum构建实时数据仓库实践》简介
- 有没有jdbc 链接优炫数据库文档及示例?
- Web3 安全风险令人生畏?应该如何应对?
- Summary of GNSS Articles
猜你喜欢

虚拟机CentOS7中无图形界面安装Oracle

螺旋矩阵_数组 | leecode刷题笔记

LeetCode第三题(Longest Substring Without Repeating Characters)三部曲之三:两次优化

Getting started with MATLAB 3D drawing command plot3

Web3 安全风险令人生畏?应该如何应对?

nodejs+npm的安装与配置

【store商城项目01】环境准备以及测试

Observability:你所需要知道的关于 Syslog 的一些知识

Shell编程之循环语句(for、while)

GraphQL背后处理及执行过程是什么
随机推荐
VR panorama shooting online exhibition hall, 3D panorama brings you an immersive experience
GraphQL背后处理及执行过程是什么
快速入门EasyX图形编程
[store mall project 01] environment preparation and testing
jmeter跨平台运行csv等文件
【虚拟化生态平台】虚拟化平台esxi挂载USB硬盘
typescript53-泛型约束
取模运算(MOD)
KunlunBase 1.0 is released!
动态内存二
Quickly build a website with static files
【虚拟化生态平台】虚拟化平台搭建
螺旋矩阵_数组 | leecode刷题笔记
LDO investigation
MongoDB数据接入实践
【OpenCV】-重映射
C # WPF equipment monitoring software (classic) - the next
Please refer to dump files (if any exist) [date].dump, [date]-jvmRun[N].dump and [date].dumpstream.
GNSS文章汇总
vxe-table 从页面批量删除数据 (不动数据库里的数据)