@Autowired private RedissonClient redissonClient;
RLock rLock = redissonClient.getLock(RedisKeyPrefix.TRADE_PRODUCT_IN_LOCK.getVal() + tradeFinancingProduct.getId());
try{ //尝试加锁30秒 if (rLock.tryLock(30, TimeUnit.SECONDS)) { //some }else{ throw new SysException("tradeProductIn - acquire lock fail"); } }catch(InterruptedException e){ throw new SysException("tradeProductIn - InterruptedException"); }finally { //释放分布式锁 if (rLock.isLocked() && rLock.isHeldByCurrentThread()) { rLock.unlock(); log.info("tradeProductIn - lock release"); } }
isHeldByCurrentThread():查询当前线程是否保持此锁定
isFair():判断Lock是否为公平锁
isLocked():查询lock 是否被任意线程所持有。
很好的介绍博客:https://www.jianshu.com/p/cde0700f0128