redis concurrent read and write locks, using Redisson to implement distributed locks

Keywords: Java jvm

Links to the original text: https://blog.csdn.net/l1028386804/article/details/73523810

1. Reentrant Lock
Redisson's distributed reentrant lock RLock Java object implements the java.util.concurrent.locks.Lock interface and also supports automatic expiration unlocking.

public void testReentrantLock(RedissonClient redisson){
    RLock lock = redisson.getLock("anyLock");
    try{
        // 1. The most common usage
        //lock.lock();
        // 2. Support expired unlock function, automatic unlock after 10 seconds, no need to call unlock method to unlock manually.
        //lock.lock(10, TimeUnit.SECONDS);
        // 3. Attempt to lock, wait up to 3 seconds, unlock automatically after 10 seconds.
        boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS);
        if(res){ //Success
        // do your business
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        lock.unlock();
    }
}


Redisson also provides methods for asynchronous execution of distributed locks:

public void testAsyncReentrantLock(RedissonClient redisson){
    RLock lock = redisson.getLock("anyLock");
    try{
        lock.lockAsync();
        lock.lockAsync(10, TimeUnit.SECONDS);
        Future<Boolean> res = lock.tryLockAsync(3, 10, TimeUnit.SECONDS);
        if(res.get()){
        // do your business
        }
    } catch (InterruptedException e) {
        e.printStackTrace();
    } catch (ExecutionException e) {
        e.printStackTrace();
    } finally {
        lock.unlock();
    }
}


2. Fair Lock
Redisson Distributed Reentrant Fair Lock is also a RLock object that implements the java.util.concurrent.locks.Lock interface. While providing automatic expiration unlocking, it ensures that when multiple Redisson client threads simultaneously request locking, priority is given to the threads that make the request first.

public void testFairLock(RedissonClient redisson){
    RLock fairLock = redisson.getFairLock("anyLock");
    try{
        // The most common method of use
        fairLock.lock();
        // Support expired unlock function, automatic unlock after 10 seconds, no need to call unlock method to unlock manually
        fairLock.lock(10, TimeUnit.SECONDS);
        // Try locking, wait up to 100 seconds, unlock automatically after 10 seconds
        boolean res = fairLock.tryLock(100, 10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        fairLock.unlock();
    }
}


Redisson also provides a method for asynchronous execution of distributed reentrant fair locks:

RLock fairLock = redisson.getFairLock("anyLock");
fairLock.lockAsync();
fairLock.lockAsync(10, TimeUnit.SECONDS);
Future<Boolean> res = fairLock.tryLockAsync(100, 10, TimeUnit.SECONDS);


3. MultiLock
Redisson's Edison MultiLock object can associate multiple RLock objects into an interlock, and each RLock object instance can come from a different Redisson instance.

public void testMultiLock(RedissonClient redisson1,RedissonClient redisson2, RedissonClient redisson3){
    RLock lock1 = redisson1.getLock("lock1");
    RLock lock2 = redisson2.getLock("lock2");
    RLock lock3 = redisson3.getLock("lock3");
    RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
    try {
        // At the same time lock: lock 1 lock 2 lock 3, all locks are locked successfully.
        lock.lock();
        // Try locking, wait up to 100 seconds, unlock automatically after 10 seconds
        boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        lock.unlock();
    }
}


4. Red Lock
Redisson's Edison RedLock object implements the locking algorithm introduced by Redlock. This object can also be used to associate multiple RLock objects into a red lock, and each RLock object instance can come from a different Redisson instance.

public void testRedLock(RedissonClient redisson1,RedissonClient redisson2, RedissonClient redisson3){
    RLock lock1 = redisson1.getLock("lock1");
    RLock lock2 = redisson2.getLock("lock2");
    RLock lock3 = redisson3.getLock("lock3");
    RedissonRedLock lock = new RedissonRedLock(lock1, lock2, lock3);
    try {
        // Locking at the same time: lock1 lock2 lock3, red locks on most nodes lock successfully even if successful.
        lock.lock();
        // Try locking, wait up to 100 seconds, unlock automatically after 10 seconds
        boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace();
    } finally {
        lock.unlock();
    }
}


5. ReadWriteLock
Redisson's distributed read-write lock RReadWriteLock implements the java.util.concurrent.locks.ReadWriteLock interface for Java objects. It also supports automatic expiration unlocking. This object allows multiple read locks at the same time, but only one write lock at most.

RReadWriteLock rwlock = redisson.getLock("anyRWLock");
// The most common method of use
rwlock.readLock().lock();
// or
rwlock.writeLock().lock();
// Support expired unlock function
// Automatic unlocking in 10 seconds
// No need to call unlock method to unlock manually
rwlock.readLock().lock(10, TimeUnit.SECONDS);
// or
rwlock.writeLock().lock(10, TimeUnit.SECONDS);
// Try locking, wait up to 100 seconds, unlock automatically after 10 seconds
boolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS);
// or
boolean res = rwlock.writeLock().tryLock(100, 10, TimeUnit.SECONDS);
...
lock.unlock();


6. Semaphore
Redisson's Semaphore Java object RSemaphore uses a similar interface and usage to java.util.concurrent.Semaphore.

RSemaphore semaphore = redisson.getSemaphore("semaphore");
semaphore.acquire();
//or
semaphore.acquireAsync();
semaphore.acquire(23);
semaphore.tryAcquire();
//or
semaphore.tryAcquireAsync();
semaphore.tryAcquire(23, TimeUnit.SECONDS);
//or
semaphore.tryAcquireAsync(23, TimeUnit.SECONDS);
semaphore.release(10);
semaphore.release();
//or
semaphore.releaseAsync();


7. Permit Expirable Semaphore
Redisson's Permit Expirable Semaphore adds an expiration time to each signal based on the RSemaphore object. Each signal can be identified by a separate ID, which can only be released by submitting the ID.

RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore");
String permitId = semaphore.acquire();
// Get a signal, valid for only 2 seconds.
String permitId = semaphore.acquire(2, TimeUnit.SECONDS);
// ...
semaphore.release(permitId);


8. Count Down Latch
Redisson's CountDownLatch Java object RCountDownLatch uses a similar interface and usage to java.util.concurrent.CountDownLatch.

RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
latch.trySetCount(1);
latch.await();
// In other threads or other JVM s
RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
latch.countDown();

Reprint: https://blog.csdn.net/l1028386804/article/details/73523810

Posted by etoast on Sun, 04 Aug 2019 01:04:56 -0700