Redisson semaphore and expirable semaphore

1. Semaphore overview

Redisson's distributed semaphore (Semaphore) Java object RSemaphore uses a similar interface and usage to java.util.concurrent.Semaphore. It also provides asynchronous (Async), reflective (Reactive) and RxJava2 standard interfaces .

1.1 Practice
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();

2. Overview of expirable semaphore

Redisson expirable semaphore (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. It provides asynchronous (Async), reflective (Reactive) and RxJava2 standard interfaces.

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


Guess you like

Origin blog.csdn.net/sinat_34241861/article/details/112364481