Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
Y
yd-cloud-core
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
xingmin
yd-cloud-core
Commits
8fa54bc1
Commit
8fa54bc1
authored
Apr 23, 2026
by
zhangxingmin
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Redisson 分布式锁
parent
362b87c0
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
87 additions
and
0 deletions
+87
-0
yd-framework/src/main/java/com/yd/framework/config/LockExecutor.java
+87
-0
No files found.
yd-framework/src/main/java/com/yd/framework/config/LockExecutor.java
0 → 100644
View file @
8fa54bc1
package
com
.
yd
.
framework
.
config
;
import
org.redisson.api.RLock
;
import
org.redisson.api.RedissonClient
;
import
org.springframework.stereotype.Component
;
import
javax.annotation.Resource
;
import
java.util.concurrent.TimeUnit
;
import
java.util.function.Supplier
;
/**
* Redisson 分布式锁流式执行器
*/
@Component
public
class
LockExecutor
{
@Resource
private
RedissonClient
redissonClient
;
/**
* 在锁保护下执行无返回值的操作(阻塞直到获取锁)
*
* @param lockKey 锁的 key
* @param action 要执行的操作
*/
public
void
executeWithLock
(
String
lockKey
,
Runnable
action
)
{
RLock
lock
=
redissonClient
.
getLock
(
lockKey
);
lock
.
lock
();
try
{
action
.
run
();
}
finally
{
if
(
lock
.
isHeldByCurrentThread
())
{
lock
.
unlock
();
}
}
}
/**
* 在锁保护下执行有返回值的操作(阻塞直到获取锁)
*
* @param lockKey 锁的 key
* @param supplier 要执行的操作
* @param <T> 返回值类型
* @return 操作结果
*/
public
<
T
>
T
executeWithLock
(
String
lockKey
,
Supplier
<
T
>
supplier
)
{
RLock
lock
=
redissonClient
.
getLock
(
lockKey
);
lock
.
lock
();
try
{
return
supplier
.
get
();
}
finally
{
if
(
lock
.
isHeldByCurrentThread
())
{
lock
.
unlock
();
}
}
}
/**
* 尝试获取锁,支持等待超时,超时后返回 false 或抛异常(由调用方决定)
*
* @param lockKey 锁的 key
* @param waitTime 最大等待时间
* @param unit 时间单位
* @param action 要执行的操作
* @return 是否成功获取锁并执行操作
*/
public
boolean
tryExecuteWithLock
(
String
lockKey
,
long
waitTime
,
TimeUnit
unit
,
Runnable
action
)
{
RLock
lock
=
redissonClient
.
getLock
(
lockKey
);
boolean
locked
=
false
;
try
{
locked
=
lock
.
tryLock
(
waitTime
,
unit
);
if
(
locked
)
{
action
.
run
();
return
true
;
}
return
false
;
}
catch
(
InterruptedException
e
)
{
Thread
.
currentThread
().
interrupt
();
return
false
;
}
finally
{
if
(
locked
&&
lock
.
isHeldByCurrentThread
())
{
lock
.
unlock
();
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment