基于redission的分布式锁


    public void testLock(){
        RLock lock = redissonClient.getLock("anyLock");
        try{
            //fairLock.lock();
            // 尝试加锁,最多等待5秒,上锁以后10秒自动解锁
            boolean res = lock.tryLock(5, 10, TimeUnit.SECONDS);
            if(res){
                System.out.println("\n\n\n\n\n"+Thread.currentThread().getName()+"=》》》》》》》》》》》》》》》》加锁了\n\n\n\n\n");
                Thread.sleep(10000L);
            }else{
                System.out.println("\n\n\n\n\n\n"+Thread.currentThread().getName()+"不拿了================》\n\n\n\n\n\n\n");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            try {
                lock.unlock();
            }catch (Exception e){

            }

        }
    }

猜你喜欢

转载自blog.csdn.net/qq779446849/article/details/80825864