Redissonセマフォと有効期限が切れるセマフォ

1.セマフォの概要

Redissonの分散セマフォ(Semaphore)JavaオブジェクトRSemaphoreは、java.util.concurrent.Semaphoreと同様のインターフェースと使用法を使用します。また、非同期(Async)、リフレクティブ(Reactive)、およびRxJava2標準インターフェースも提供します

1.1練習
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.有効期限が切れるセマフォの概要

Redissonの有効期限が切れるセマフォ(PermitExpirableSemaphore)は、RSemaphoreオブジェクトに基づいており、各信号の有効期限が追加されています。各信号は独立したIDで識別でき、解放されたときにこのIDを送信することによってのみ解放できます。非同期(非同期)、リフレクティブ(リアクティブ)、およびRxJava2標準インターフェースを提供します。

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


おすすめ

転載: blog.csdn.net/sinat_34241861/article/details/112364481