8.6. 信号量(Semaphore)

基于Redis的Redisson的分布式信号量(Semaphore)Java对象RSemaphore采用了与java.util.concurrent.Semaphore相似的接口和用法。同时还提供了异步(Async)反射式(Reactive)RxJava2标准的接口。

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