redisson 잠금tryLock의 올바른 사용 방법

4334 단어 질문
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

좋은 웹페이지 즐겨찾기