Home  >  Article  >  Java  >  Example of Java implementing redisson distributed lock

Example of Java implementing redisson distributed lock

黄舟
黄舟Original
2017-10-17 09:32:452254browse

This article mainly introduces Java programming redisson to implement distributed lock code examples. The editor thinks it is quite good. I will share it with you here for the reference of friends in need.

Due to being very busy at work recently, I haven’t updated my blog for a long time. Today I bring you an article about Redisson’s implementation of distributed locks. Well, without further ado, let’s go directly to the topic.

1. Reentrant Lock

Redisson’s distributed reentrant lock RLock Java object implements java.util.concurrent. The locks.Lock interface also supports automatic expiration and unlocking.


public void testReentrantLock(RedissonClient redisson){ 
  RLock lock = redisson.getLock("anyLock"); 
  try{ 
    // 1. 最常见的使用方法 
    //lock.lock(); 
    // 2. 支持过期解锁功能,10秒钟以后自动解锁, 无需调用unlock方法手动解锁 
    //lock.lock(10, TimeUnit.SECONDS); 
    // 3. 尝试加锁,最多等待3秒,上锁以后10秒自动解锁 
    boolean res = lock.tryLock(3, 10, TimeUnit.SECONDS); 
    if(res){ //成功 
    // do your business 
    } 
  } catch (InterruptedException e) { 
    e.printStackTrace(); 
  } finally { 
    lock.unlock(); 
  } 
}

Redisson also provides asynchronous execution related methods for 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 an RLock object that implements the java.util.concurrent.locks.Lock interface. While providing the automatic expiration unlocking function, it also ensures that when multiple Redisson client threads request locks at the same time, priority is given to the thread that makes the request first.


public void testFairLock(RedissonClient redisson){ 
  RLock fairLock = redisson.getFairLock("anyLock"); 
  try{ 
    // 最常见的使用方法 
    fairLock.lock(); 
    // 支持过期解锁功能, 10秒钟以后自动解锁,无需调用unlock方法手动解锁 
    fairLock.lock(10, TimeUnit.SECONDS); 
    // 尝试加锁,最多等待100秒,上锁以后10秒自动解锁 
    boolean res = fairLock.tryLock(100, 10, TimeUnit.SECONDS); 
  } catch (InterruptedException e) { 
    e.printStackTrace(); 
  } finally { 
    fairLock.unlock(); 
  } 
}

Redisson also provides asynchronous execution methods for 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. Interlock (MultiLock)

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


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 { 
    // 同时加锁:lock1 lock2 lock3, 所有的锁都上锁成功才算成功。 
    lock.lock(); 
    // 尝试加锁,最多等待100秒,上锁以后10秒自动解锁 
    boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS); 
  } catch (InterruptedException e) { 
    e.printStackTrace(); 
  } finally { 
    lock.unlock(); 
  } 
}

4. Red Lock (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.


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 { 
    // 同时加锁:lock1 lock2 lock3, 红锁在大部分节点上加锁成功就算成功。 
    lock.lock(); 
    // 尝试加锁,最多等待100秒,上锁以后10秒自动解锁 
    boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS); 
  } catch (InterruptedException e) { 
    e.printStackTrace(); 
  } finally { 
    lock.unlock(); 
  } 
}

5. Read-write lock (ReadWriteLock)

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


RReadWriteLock rwlock = redisson.getLock("anyRWLock"); 
// 最常见的使用方法 
rwlock.readLock().lock(); 
// 或 
rwlock.writeLock().lock(); 
// 支持过期解锁功能 
// 10秒钟以后自动解锁 
// 无需调用unlock方法手动解锁 
rwlock.readLock().lock(10, TimeUnit.SECONDS); 
// 或 
rwlock.writeLock().lock(10, TimeUnit.SECONDS); 
// 尝试加锁,最多等待100秒,上锁以后10秒自动解锁 
boolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS); 
// 或 
boolean res = rwlock.writeLock().tryLock(100, 10, TimeUnit.SECONDS); 
... 
lock.unlock();

6. Semaphore

Redisson's distributed semaphore (Semaphore) Java object RSemaphore Adopts an interface and usage similar to java.util.concurrent.Semaphore.


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

7. Expirable semaphore (PermitExpirableSemaphore)

Redisson’s expirable semaphore (PermitExpirableSemaphore) ) Based on the RSemaphore object, an expiration time is added to each signal. Each signal can be identified by an independent ID, and it can only be released by submitting this ID.


RPermitExpirableSemaphore semaphore = redisson.getPermitExpirableSemaphore("mySemaphore"); 
String permitId = semaphore.acquire(); 
// 获取一个信号,有效期只有2秒钟。 
String permitId = semaphore.acquire(2, TimeUnit.SECONDS); 
// ... 
semaphore.release(permitId);

8. Locking (CountDownLatch)

Redisson's distributed locking (CountDownLatch) Java object RCountDownLatch adopts Similar interface and usage to java.util.concurrent.CountDownLatch.


RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch"); 
latch.trySetCount(1); 
latch.await(); 
// 在其他线程或其他JVM里 
RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch"); 
latch.countDown();

Summary

The above is the detailed content of Example of Java implementing redisson distributed lock. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn