分布式锁和同步器

TOC

Lock

Redisson 分布式可重入锁,实现了 java.util.concurrent.locks.Lock 接口并支持 TTL。

  1. RLock lock = redisson.getLock("anyLock");
  2. // Most familiar locking method
  3. lock.lock();
  4. // Lock time-to-live support
  5. // releases lock automatically after 10 seconds
  6. // if unlock method not invoked
  7. lock.lock(10, TimeUnit.SECONDS);
  8. // Wait for 100 seconds and automatically unlock it after 10 seconds
  9. boolean res = lock.tryLock(100, 10, TimeUnit.SECONDS);
  10. ...
  11. lock.unlock();

Redisson 也支持 Lock 对象的异步方法:

  1. RLock lock = redisson.getLock("anyLock");
  2. lock.lockAsync();
  3. lock.lockAsync(10, TimeUnit.SECONDS);
  4. Future<Boolean> res = lock.tryLockAsync(100, 10, TimeUnit.SECONDS);

Fair Lock

Redisson 分布式可重入公平锁,实现了 java.util.concurrent.locks.Lock 接口并支持 TTL,
并且保证 Redisson 客户端线程将以其请求的顺序获得锁。
它和简单的 Lock 对象有相同的接口。

  1. RLock fairLock = redisson.getFairLock("anyLock");
  2. // Most familiar locking method
  3. fairLock.lock();
  4. // Lock time-to-live support
  5. // releases lock automatically after 10 seconds
  6. // if unlock method not invoked
  7. fairLock.lock(10, TimeUnit.SECONDS);
  8. // Wait for 100 seconds and automatically unlock it after 10 seconds
  9. boolean res = fairLock.tryLock(100, 10, TimeUnit.SECONDS);
  10. ...
  11. fairLock.unlock();

Redisson 也支持公平锁对象的异步方法:

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

MultiLock

RedissonMultiLock 对象可用于实现 Redlock 锁算法。
它将多个 RLock 对象划为一组并且将它们当作一个锁来处理。
每个 RLock 对象可以属于不同的 Redisson 实例。

  1. RLock lock1 = redissonInstance1.getLock("lock1");
  2. RLock lock2 = redissonInstance2.getLock("lock2");
  3. RLock lock3 = redissonInstance3.getLock("lock3");
  4. RedissonMultiLock lock = new RedissonMultiLock(lock1, lock2, lock3);
  5. // locks: lock1 lock2 lock3
  6. lock.lock();
  7. ...
  8. lock.unlock();

ReadWriteLock

Redisson 分布式可重入 ReadWriteLock 对象,实现了 java.util.concurrent.locks.ReadWriteLock 接口并支持 TTL。
可以同时存在多个 ReadLock 拥有者,但仅允许有一个 WriteLock

  1. RReadWriteLock rwlock = redisson.getLock("anyRWLock");
  2. // Most familiar locking method
  3. rwlock.readLock().lock();
  4. // or
  5. rwlock.writeLock().lock();
  6. // Lock time-to-live support
  7. // releases lock automatically after 10 seconds
  8. // if unlock method not invoked
  9. rwlock.readLock().lock(10, TimeUnit.SECONDS);
  10. // or
  11. rwlock.writeLock().lock(10, TimeUnit.SECONDS);
  12. // Wait for 100 seconds and automatically unlock it after 10 seconds
  13. boolean res = rwlock.readLock().tryLock(100, 10, TimeUnit.SECONDS);
  14. // or
  15. boolean res = rwlock.writeLock().tryLock(100, 10, TimeUnit.SECONDS);
  16. ...
  17. lock.unlock();

Semaphore

Redisson 分布式 Semaphore 对象,类似于 java.util.concurrent.Semaphore 对象。

  1. RSemaphore semaphore = redisson.getSemaphore("semaphore");
  2. semaphore.acquire();
  3. //or
  4. semaphore.acquireAsync();
  5. semaphore.acquire(23);
  6. semaphore.tryAcquire();
  7. //or
  8. semaphore.tryAcquireAsync();
  9. semaphore.tryAcquire(23, TimeUnit.SECONDS);
  10. //or
  11. semaphore.tryAcquireAsync(23, TimeUnit.SECONDS);
  12. semaphore.release(10);
  13. semaphore.release();
  14. //or
  15. semaphore.releaseAsync();

CountDownLatch

Redisson 分布式 CountDownLatch 对象,结构类似于 java.util.concurrent.CountDownLatch 对象。

  1. RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
  2. latch.trySetCount(1);
  3. latch.await();
  4. // in other thread or other JVM
  5. RCountDownLatch latch = redisson.getCountDownLatch("anyCountDownLatch");
  6. latch.countDown();