RedissonClient 锁的使用

2023-11-17  本文已影响0人  一滴矿泉水

RedissonClient是一个基于Redis的分布式锁实现,它提供了许多高级分布式锁功能,如分布式锁、分布式信号量、分布式读写锁等。以下是RedissonClient的常用锁方法介绍:

这些方法可以帮助你在分布式系统中实现同步访问共享资源的机制,确保数据一致性和并发问题的解决。

锁的使用

lock() 与 lockInterruptibly() 方法的使用

    RLock lock = redissonClient.getLock("lock_key");
    try {
        // 上锁
        lock.lock();
        //lock.lockInterruptibly();

        System.out.println("开始进入业务代码--->当前线程--->"+Thread.currentThread().getId());
        //业务代码
        Thread.sleep(6000);
        System.out.println("业务代码执行完毕--->当前线程--->" + Thread.currentThread().getId());
    } catch (InterruptedException e){
        System.out.println("IllegalStateException 错误--->"+ e +"--->当前线程--->" + Thread.currentThread().getId());
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception 错误--->" + e + "--->当前线程--->" + Thread.currentThread().getId());
        e.printStackTrace();
    } finally {
        lock.unlock();
        System.out.println("解锁 成功--->当前线程--->" + Thread.currentThread().getId());
    }

lock.lock(-1L, TimeUnit.SECONDS) 与 lock.lockInterruptibly(5,TimeUnit.SECONDS) 方法的使用

    RLock lock = redissonClient.getLock("lock_key");
    try {
        // 上锁
        lock.lock(5, TimeUnit.SECONDS);
        //lock.lockInterruptibly(5,TimeUnit.SECONDS);
        
        System.out.println("开始进入业务代码--->当前线程--->"+Thread.currentThread().getId());
        //业务代码
        Thread.sleep(6000);
        System.out.println("业务代码执行完毕--->当前线程--->" + Thread.currentThread().getId());
    } catch (InterruptedException e){
        System.out.println("IllegalStateException 错误--->"+ e +"--->当前线程--->" + Thread.currentThread().getId());
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception 错误--->" + e + "--->当前线程--->" + Thread.currentThread().getId());
        e.printStackTrace();
    } finally {
        if (lock.isHeldByCurrentThread() && lock.isLocked()){
            lock.unlock();
            System.out.println("解锁 成功--->当前线程--->" + Thread.currentThread().getId())
        }
    }

lock.tryLock() 与 lock.tryLock(5, TimeUnit.SECONDS) 方法的使用

    RLock lock = redissonClient.getLock("lock_key");
    try {
        // 上锁
        boolean b = lock.tryLock();
        //boolean b = lock.tryLock(5, TimeUnit.SECONDS);
        if (!b){
            System.out.println("获取锁不成功--->当前线程--->"+Thread.currentThread().getId());
            // 此处抛出异常 活业务处理
            return;
        }
        System.out.println("开始进入业务代码--->当前线程--->"+Thread.currentThread().getId());
        //业务代码
        Thread.sleep(6000);
        System.out.println("业务代码执行完毕--->当前线程--->" + Thread.currentThread().getId());
    } catch (InterruptedException e){
        System.out.println("IllegalStateException 错误--->"+ e +"--->当前线程--->" + Thread.currentThread().getId());
        e.printStackTrace();
    } catch (Exception e) {
        System.out.println("Exception 错误--->" + e + "--->当前线程--->" + Thread.currentThread().getId());
        e.printStackTrace();
    } finally {
        lock.unlock();
        System.out.println("解锁 成功--->当前线程--->" + Thread.currentThread().getId());
    }

文章持续更新中、希望对各位有所帮助、有问题可留言 大家共同学习 !

上一篇 下一篇

猜你喜欢

热点阅读