redissonredlock_redisson lock

redissonredlock_redisson lock1.现在错误的用法:RLocklock=redisson.getLock(String.format(LOCK_KEY,2));try{if(lock.tryLock()){//处理logger.info(“aaaaaaaaaaaaaaaaaa”);}catch(Exceptione){//处理异常}finally{…

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

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 <code>true</code> if locked otherwise <code>false</code>
 */
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

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/181741.html原文链接:https://javaforall.cn

【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛

【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...

(0)


相关推荐

发表回复

您的电子邮箱地址不会被公开。

关注全栈程序员社区公众号