redissonロックtryLockの正しい使い方


1.現在の間違った使い方:
RLock lock = redisson.getLock(String.format(LOCK_KEY, 2));
try {
    if (lock.tryLock()) {
    // 
    logger.info("aaaaaaaaaaaaaaaaaa");
} catch(Exception e) {
    // 
} finally {
    if (lock.isLocked()) {
        lock.unlock();
    }
}
 :

TwoThread.java
public class TwoThread implements Runnable {
    private RedissonClient redisson;

    public TwoThread(RedissonClient redisson) {
        this.redisson = redisson;
    }

    private Logger logger = LogManager.getLogger(getClass());
    private String REWARD_CARD_LOCK_KEY = "444:lock:%d";

    public void run() {
        RLock lock = redisson.getLock(String.format(REWARD_CARD_LOCK_KEY, 2));
        logger.info("thread---{}, lock:{}",Thread.currentThread().getId(), lock);
        try {
            if (lock.tryLock()) {
                logger.info("tryLock thread---{}, lock:{}",Thread.currentThread().getId(),lock);
                logger.info("aaaaaaaaaaaaaaaaaa");
            }
        } finally {
            boolean locked = lock.isLocked();
            logger.info("thread---{},lock:{},locked:{}",Thread.currentThread().getId(),lock,locked);
            if (locked) {
                logger.info("thread unlock---{}, lock:{}",Thread.currentThread().getId(),lock);
                lock.unlock();
            }

        }
    }

テストクラス:
Thread one = new Thread(new TwoThread(redisson));
Thread two = new Thread(new TwoThread(redisson));
Thread three = new Thread(new TwoThread(redisson));
Thread four = new Thread(new TwoThread(redisson));
one.start();
two.start();
three.start();
four.start();

印刷結果:
thread---111, lock:org.redisson.RedissonLock@1cb2393f
thread---112, lock:org.redisson.RedissonLock@70feb82b
thread---114, lock:org.redisson.RedissonLock@50384a5d
thread---113, lock:org.redisson.RedissonLock@42232129
thread---111,lock:org.redisson.RedissonLock@1cb2393f,locked:true
aaaaaaaaaaaaaaaaaa
thread---113,lock:org.redisson.RedissonLock@42232129,locked:true
thread---112,lock:org.redisson.RedissonLock@70feb82b,locked:true
thread unlock---111, lock:org.redisson.RedissonLock@1cb2393f
thread---114,lock:org.redisson.RedissonLock@50384a5d,locked:true
thread unlock---114, lock:org.redisson.RedissonLock@50384a5d
thread unlock---113, lock:org.redisson.RedissonLock@42232129
thread unlock---112, lock:org.redisson.RedissonLock@70feb82b
Exception in thread "Thread-20" Exception in thread "Thread-19" java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 54882863-3389-40e7-ba4d-58ecae0a7155 thread-id: 114
at org.redisson.RedissonLock.unlock(RedissonLock.java:367)
	at xx.TwoThread.run(TwoThread.java:36)
	at java.lang.Thread.run(Thread.java:745)
java.lang.IllegalMonitorStateException: attempt to unlock lock, not locked by current thread by node id: 54882863-3389-40e7-ba4d-58ecae0a7155 thread-id: 113
	at org.redisson.RedissonLock.unlock(RedissonLock.java:367)
	at xx.TwoThread.run(TwoThread.java:36)
	at java.lang.Thread.run(Thread.java:745)

ロックを解除しようとしましたが、現在のスレッドにロックされていません.
boolean locked = lock.isLocked()はなぜtrueを返し続けるのか、ソースコードを表示します.このロックが任意のスレッドにロックされている限り、trueを返します.
/**
 * Checks if this lock locked by any thread
 *
 * @return true if locked otherwise false
 */
boolean isLocked();

だから、finallyの中でロックされていないとロックを解除して、間違いを報告するに違いありません.
2.tryLock正しい使い方:
RLock lock = redisson.getLock(String.format(REWARD_CARD_LOCK_KEY, 2));
try {
    // , 10 , 10 
    if (lock.tryLock(10,10, TimeUnit.SECONDS)) {
        try {
            // 
            logger.info("tryLock thread---{}, lock:{}", Thread.currentThread().getId(), lock);
        } catch (Exception e) {
        } finally {
            // 
            lock.unlock();
        }
    }
} catch (InterruptedException e) {
    // 
    // , , 
    Thread.currentThread().interrupt();
}

InterruptedExceptionの処理については、次のドキュメントを参照してください.
https://www.ibm.com/developerworks/cn/java/j-jtp05236.html
割り込み状態のシーンの一例を復元する必要があります.
https://www.cnblogs.com/softidea/p/4413374.html