Java--redis Concurrent Read and write locks, using Redisson to implement distributed locks

Source: Internet
Author: User
Tags semaphore
Original: http://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 expired unlocking.

 

[java] view plain copy
public void testReentrantLock (RedissonClient redisson) {
    RLock lock = redisson.getLock ("anyLock");
    try {
        // 1. the most common usage
        //lock.lock ();
        // 2. Support expired unlock function, automatically unlock after 10 seconds, no need to call the unlock method to manually unlock
        //lock.lock(10, TimeUnit.SECONDS);
        // 3. Attempt to lock, wait up to 3 seconds, unlock automatically 10 seconds after locking
        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 asynchronous execution related methods for distributed locks:

[java] view plain copy
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 an RLock object that implements the java.util.concurrent.locks.Lock interface. While providing the automatic expiration unlocking function, it guarantees that when multiple Redisson client threads request locks at the same time, they are preferentially allocated to the thread that sent the request first.

[java] view plain copy
public void testFairLock (RedissonClient redisson) {
    RLock fairLock = redisson.getFairLock ("anyLock");
    try {
        // most common usage
        fairLock.lock ();
        // Support expired unlock function, automatically unlock after 10 seconds, no need to call the unlock method to manually unlock
        fairLock.lock (10, TimeUnit.SECONDS);
        // Attempt to lock, wait up to 100 seconds, automatically unlock 10 seconds after locking
        boolean res = fairLock.tryLock (100, 10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace ();
    } finally {
        fairLock.unlock ();
    }
}
Redisson also provides asynchronous methods for distributed reentrant fair locks:

[java] view plain copy
RLock fairLock = redisson.getFairLock ("anyLock");
fairLock.lockAsync ();
fairLock.lockAsync (10, TimeUnit.SECONDS);
Future <Boolean> res = fairLock.tryLockAsync (100, 10, TimeUnit.SECONDS);
 

3. Interlock (MultiLock)
Redisson's RedissonMultiLock object can associate multiple RLock objects as an interlock. Each RLock object instance can come from a different Redisson instance.

[java] view plain copy
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 {
        // Lock at the same time: lock1 lock2 lock3, all locks are successfully locked to be considered successful.
        lock.lock ();
        // Attempt to lock, wait up to 100 seconds, automatically unlock 10 seconds after locking
        boolean res = lock.tryLock (100, 10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace ();
    } finally {
        lock.unlock ();
    }
}
 

4. RedLock
Redisson's RedissonRedLock object implements the locking algorithm introduced by Redlock. This object can also be used to associate multiple RLock objects as a red lock. Each RLock object instance can come from a different Redisson instance.

 

[java] view plain copy
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 {
        // Lock at the same time: lock1 lock2 lock3, Redlock successfully locks on most nodes even if it is successful.
        lock.lock ();
        // Attempt to lock, wait up to 100 seconds, automatically unlock 10 seconds after locking
        boolean res = lock.tryLock (100, 10, TimeUnit.SECONDS);
    } catch (InterruptedException e) {
        e.printStackTrace ();
    } finally {
        lock.unlock ();
    }
}
 

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

[java] view plain copy
RReadWriteLock rwlock = redisson.getLock ("anyRWLock");
// most common usage
rwlock.readLock (). lock ();
// or
rwlock.writeLock (). lock ();
// Support expired unlock function
// Automatically unlock after 10 seconds
// no need to call the unlock method to manually unlock
rwlock.readLock (). lock (10, TimeUnit.SECONDS);
// or
rwlock.writeLock (). lock (10, TimeUnit.SECONDS);
// Attempt to lock, wait up to 100 seconds, automatically unlock 10 seconds after locking
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 distributed semaphore (Semaphore) Java object RSemaphore uses a similar interface and usage as java.util.concurrent.Semaphore.

[java] view plain copy
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. PermitExpirableSemaphore
Redisson's PermitExpirableSemaphore is based on the RSemaphore object, adding an expiration time for each signal. Each signal can be identified by an independent ID, and can only be released by submitting this ID when released.

[java] view plain copy
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. Blocking (CountDownLatch)
Redisson's distributed lock (CountDownLatch) Java object RCountDownLatch uses a similar interface and usage as java.util.concurrent.CountDownLatch.

[java] view plain copy
RCountDownLatch latch = redisson.getCountDownLatch ("anyCountDownLatch");
latch.trySetCount (1);
latch.await ();
// in another thread or other JVM
RCountDownLatch latch = redisson.getCountDownLatch ("anyCountDownLatch");
latch.countDown ();
Java-redis concurrent read-write locks, using Redisson to achieve distributed locks

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.