redisson重入锁上锁失败5金钱.anslist{background:url(source/plugin/keke_
技术问答
244 人阅读
|
0 人回复
|
2023-09-11
|
! i+ J0 i; ?5 U& a: w
- public boolean acquire(String lockName) {
R: J# Z5 S. D" }% d) Y6 D8 h - //声明key对象
U9 v3 w0 u( B p0 w; S( k7 f - String key = LOCK_TITLE + lockName;
8 {3 F0 ~$ A, U - //获取锁对象
$ a0 N) t5 L8 Y& s$ T. c - RLock mylock = redissonClient.getLock(key);( ?+ O- }7 U2 |
- //加锁,并且设置锁过期时间3秒,防止死锁的产生 uuid+threadId
6 j& Y" S. ?: S4 x( G - mylock.lock(3, TimeUnit.SECONDS);2 N, h+ D2 E1 T5 T6 X# v" D# \- N. e
- //加锁成功
! v$ j% Q L. K( g" w, M5 H) { - return true;
' {" {5 w/ h& m; d3 x - }/ m* Y% ^' p$ l/ F2 u* y
- % k2 x; q" V/ X, d8 {
- public void release(String lockName) {
' F' B) Q+ k* d# S% L - String key = LOCK_TITLE + lockName;+ V2 x) u. b7 i) `6 V) G# F7 i
- RLock mylock = redissonClient.getLock(key);
% @) F) {% U2 |# U( _- H - mylock.unlock();5 y0 g- t: {3 q
- }! H* x! ?9 N3 M9 v Y4 K
调用如下- R5 k- z* s+ p& A7 Z
8 _, N2 R' m4 R- boolean acquire = distributedRedisLock.acquire(CacheKey.REGISTER_SMS_RECALL);$ G2 @( F s' [# e* h. g
- try {
; D/ q" _9 J1 m% m4 ?, f - if (acquire) {( s) @" f4 ?8 }2 C& A/ u; L
- taskExecutor.execute(() -> {/ ]0 D- A4 T9 g/ D( ~
- log.info("多线程测试");8 b& [6 b5 C1 t0 c
- });
# }7 F' x7 n2 S; }8 q4 e8 m - }
# B2 f9 D$ Q% e9 B - }catch (Exception e) {
" N: Q3 B2 Q9 X& S1 g: C - log.info("{}", ExceptionUtils.getFullStackTrace(e));) j2 T7 ?4 R n
- }finally {
* h+ c* X; @' q+ O; A0 J' h - distributedRedisLock.release(CacheKey.REGISTER_SMS_RECALL); }9 z; F$ S* k$ `2 Q7 P- h6 [
上锁方法如上,但是使用多线程调用的时候,上锁失败了
2 ? e- R) h+ B2 Q- Application_Stdout [2021-12-29 11:06:52] [traceid=yPGwkuNz,uid=,uri=/mid/test/test,host=192.168.126.47,ref=,ua=python-requests/2.26.0] INFO [http-nio-8089-exec-6] o.n.mid.task.SmsRecallRegisterTask[48] - 多线程测试- \3 x+ t* M5 {, P$ Q
- Application_Stdout [2021-12-29 11:06:52] [traceid=krfsFsWv,uid=,uri=/mid/test/test,host=192.168.126.47,ref=,ua=python-requests/2.26.0] INFO [http-nio-8089-exec-8] o.n.mid.task.SmsRecallRegisterTask[48] - 多线程测试Application_Stdout [2021-12-29 11:06:52] [traceid=8c9RU1WK,uid=,uri=/mid/test/test,host=192.168.126.47,ref=,ua=python-requests/2.26.0] INFO [http-nio-8089-exec-10] o.n.mid.task.SmsRecallRegisterTask[48] - 多线程测试' X% w$ H8 b2 T3 H! m
- t, j8 G0 ^2 B" ?8 O* @8 B
2 Z0 q0 G6 Q8 s2 ]) B
我知道答案 回答被采纳将会获得5 金钱 已有0人回答 |
|
|
|
|
|