attempt to unlock lock, not locked by current thread by node id

attempt to unlock lock, not locked by current thread by node id_第1张图片
attempt to unlock lock, not locked by current thread by node id: ee1333c7-c195-4d3a-80af-f39f3f8e17df thread-id: 69
当时的代码是这样写的

public void execute() {
        String timerName = HairDetectionJob.class.getName()
                + Thread.currentThread().getStackTrace()[1].getMethodName();
        RLock lock = redissonClient.getLock(RedisConstant.REDIS_PREFIX + timerName);
        boolean tryLock = lock.tryLock();
        try{
            if(tryLock){
               this.handleSign();
            }else{
                log.info("CalendarDetailJob.execute 未获取到锁"+lock.getName());
            }
        }catch (Exception exception){
            log.error("CalendarDetailJob.execute",exception);
        }finally {
            if (lock.isLocked()) {
                lock.unlock();
            }
        }

    }

最后改成

    public void execute() {
        String timerName = HairDetectionJob.class.getName()
                + Thread.currentThread().getStackTrace()[1].getMethodName();
        RLock lock = redissonClient.getLock(RedisConstant.REDIS_PREFIX + timerName);
        boolean tryLock = lock.tryLock();
        try{
            if(tryLock){
               this.handleSign();
            }else{
                log.info("CalendarDetailJob.execute 未获取到锁"+lock.getName());
            }
        }catch (Exception exception){
            log.error("CalendarDetailJob.execute",exception);
        }finally {
            if (lock.isLocked() && lock.isHeldByCurrentThread()) {
                lock.unlock();
            }
        }


    }

解锁时,加了lock.isHeldByCurrentThread(),它的意思是查询当前线程是否持有此锁定,如果还持有,则释放,如果未持有,则说明已被释放;

你可能感兴趣的:(redis,java微服务,java,开发语言)