public static void lock(String lock, long wait, long leaseTime, TimeUnit unit, Consumer…
Jump to turn (6)
- 1public static void lock(String lock, long wait, long leaseTime, TimeUnit unit, C
- 2如何优化这段代码
- 3你这样优化是错误的,因为我需要通过Promise的完成来判断是否需要释放锁,我的代码很多事基于回调的方式,也许是来自另外一组线程触发这个操作 Callback
- 4写个使用例子给我
- 5我运行这个代码,IDE提示no instance(s) of type variable(s) T exist so that Future conforms
- 6我认为优化成这样更好,你认为呢 public static void lock(String lock, long wait, long leaseTime,
- User
public static void lock(String lock, long wait, long leaseTime, TimeUnit unit, Consumer action, Promise promise) { RLock rLock = redissonClient.getLock(lock); long mark = LockMark.mark();
rLock.tryLockAsync(wait, leaseTime, unit, mark) .thenAccept(locked -> { if (locked) { action.accept(true); promise.future().onComplete(ar -> rLock.isLockedAsync().thenAccept(might -> { if(might) { rLock.unlockAsync(mark); } else { log.info("lock:{} expire", lock); }} )); } else { log.info("lock:{} get fail", lock); } }) .exceptionally(ex -> { log.error("lock:{} error , {}", lock, ex); return null; }); }
- Assistant