Class RedissonSemaphore
- java.lang.Object
-
- org.redisson.RedissonObject
-
- org.redisson.RedissonSemaphore
-
- All Implemented Interfaces:
RExpirable
,RExpirableAsync
,RObject
,RObjectAsync
,RSemaphore
,RSemaphoreAsync
- Direct Known Subclasses:
RedissonQueueSemaphore
public class RedissonSemaphore extends RedissonObject implements RSemaphore
Distributed and concurrent implementation ofSemaphore
.Works in non-fair mode. Therefore order of acquiring is unpredictable.
- Author:
- Nikita Koksharov
-
-
Field Summary
-
Fields inherited from class org.redisson.RedissonObject
codec
-
-
Constructor Summary
Constructors Constructor Description RedissonSemaphore(CommandAsyncExecutor commandExecutor, String name, SemaphorePubSub semaphorePubSub)
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
acquire()
Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.void
acquire(int permits)
Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is interrupted.RFuture<Void>
acquireAsync()
Acquires a permit from this semaphore.RFuture<Void>
acquireAsync(int permits)
Acquires the given number of permits, if they are available, and returns immediately, reducing the number of available permits by the given amount.int
availablePermits()
Returns the current number of available permits.boolean
clearExpire()
Clear an expire timeout or expire date for object.RFuture<Boolean>
clearExpireAsync()
Clear an expire timeout or expire date for object in async mode.int
drainPermits()
Acquires and returns all permits that are immediately available.boolean
expire(long timeToLive, TimeUnit timeUnit)
Set a timeout for object.RFuture<Boolean>
expireAsync(long timeToLive, TimeUnit timeUnit)
Set a timeout for object in async mode.boolean
expireAt(long timestamp)
Set an expire date for object.boolean
expireAt(Date timestamp)
Set an expire date for object.RFuture<Boolean>
expireAtAsync(long timestamp)
Set an expire date for object in async mode.RFuture<Boolean>
expireAtAsync(Date timestamp)
Set an expire date for object in async mode.static String
getChannelName(String name)
void
reducePermits(int permits)
Shrinks the number of available permits by the indicated reduction.RFuture<Void>
reducePermitsAsync(int permits)
Shrinks the number of available permits by the indicated reduction.void
release()
Releases a permit, returning it to the semaphore.void
release(int permits)
Releases the given number of permits, returning them to the semaphore.RFuture<Void>
releaseAsync()
Releases a permit, returning it to the semaphore.RFuture<Void>
releaseAsync(int permits)
Releases the given number of permits, returning them to the semaphore.long
remainTimeToLive()
Remaining time to live of Redisson object that has a timeoutRFuture<Long>
remainTimeToLiveAsync()
Remaining time to live of Redisson object that has a timeoutboolean
tryAcquire()
Acquires a permit only if one is available at the time of invocation.boolean
tryAcquire(int permits)
Acquires the given number of permits only if all are available at the time of invocation.boolean
tryAcquire(int permits, long waitTime, TimeUnit unit)
Acquires the given number of permits only if all are available within the given waiting time and the current thread has not been interrupted.boolean
tryAcquire(long time, TimeUnit unit)
Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been interrupted.RFuture<Boolean>
tryAcquireAsync()
Acquires a permit only if one is available at the time of invocation.RFuture<Boolean>
tryAcquireAsync(int permits)
Acquires the given number of permits only if all are available at the time of invocation.RFuture<Boolean>
tryAcquireAsync(int permits, long waitTime, TimeUnit unit)
Acquires the given number of permits only if all are available within the given waiting time.RFuture<Boolean>
tryAcquireAsync(long waitTime, TimeUnit unit)
Acquires a permit, if one is available and returns immediately, with the valuetrue
, reducing the number of available permits by one.boolean
trySetPermits(int permits)
Sets number of permits.RFuture<Boolean>
trySetPermitsAsync(int permits)
Sets number of permits.-
Methods inherited from class org.redisson.RedissonObject
await, copy, copyAsync, delete, deleteAsync, dump, dumpAsync, encode, encode, encode, encodeMapKey, encodeMapKeys, encodeMapValue, encodeMapValues, get, getCodec, getName, getName, isExists, isExistsAsync, migrate, migrateAsync, move, moveAsync, prefixName, rename, renameAsync, renamenx, renamenxAsync, restore, restore, restoreAndReplace, restoreAndReplace, restoreAndReplaceAsync, restoreAndReplaceAsync, restoreAsync, restoreAsync, suffixName, toSeconds, touch, touchAsync, unlink, unlinkAsync
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface org.redisson.api.RExpirable
clearExpire, expire, expireAt, expireAt, remainTimeToLive
-
Methods inherited from interface org.redisson.api.RExpirableAsync
clearExpireAsync, expireAsync, expireAtAsync, expireAtAsync, remainTimeToLiveAsync
-
Methods inherited from interface org.redisson.api.RObject
copy, delete, dump, getCodec, getName, isExists, migrate, move, rename, renamenx, restore, restore, restoreAndReplace, restoreAndReplace, touch, unlink
-
Methods inherited from interface org.redisson.api.RObjectAsync
copyAsync, deleteAsync, dumpAsync, isExistsAsync, migrateAsync, moveAsync, renameAsync, renamenxAsync, restoreAndReplaceAsync, restoreAndReplaceAsync, restoreAsync, restoreAsync, touchAsync, unlinkAsync
-
-
-
-
Constructor Detail
-
RedissonSemaphore
public RedissonSemaphore(CommandAsyncExecutor commandExecutor, String name, SemaphorePubSub semaphorePubSub)
-
-
Method Detail
-
acquire
public void acquire() throws InterruptedException
Description copied from interface:RSemaphore
Acquires a permit from this semaphore, blocking until one is available, or the thread is interrupted.Acquires a permit, if one is available and returns immediately, reducing the number of available permits by one.
If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
- Some other thread invokes the
RSemaphore.release()
method for this semaphore and the current thread is next to be assigned a permit; or - Some other thread interrupts the current thread.
- Specified by:
acquire
in interfaceRSemaphore
- Throws:
InterruptedException
- if the current thread is interrupted
- Some other thread invokes the
-
acquire
public void acquire(int permits) throws InterruptedException
Description copied from interface:RSemaphore
Acquires the given number of permits from this semaphore, blocking until all are available, or the thread is interrupted.Acquires the given number of permits, if they are available, and returns immediately, reducing the number of available permits by the given amount.
If insufficient permits are available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of two things happens:
- Some other thread invokes one of the
release
methods for this semaphore, the current thread is next to be assigned permits and the number of available permits satisfies this request; or - Some other thread interrupts the current thread.
- Specified by:
acquire
in interfaceRSemaphore
- Parameters:
permits
- the number of permits to acquire- Throws:
InterruptedException
- if the current thread is interrupted
- Some other thread invokes one of the
-
acquireAsync
public RFuture<Void> acquireAsync()
Description copied from interface:RSemaphoreAsync
Acquires a permit from this semaphore.Acquires a permit, if one is available and returns immediately, reducing the number of available permits by one.
- Specified by:
acquireAsync
in interfaceRSemaphoreAsync
- Returns:
- void
-
acquireAsync
public RFuture<Void> acquireAsync(int permits)
Description copied from interface:RSemaphoreAsync
Acquires the given number of permits, if they are available, and returns immediately, reducing the number of available permits by the given amount.- Specified by:
acquireAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- the number of permits to acquire- Returns:
- void
-
tryAcquire
public boolean tryAcquire()
Description copied from interface:RSemaphore
Acquires a permit only if one is available at the time of invocation.Acquires a permit, if one is available and returns immediately, with the value
true
, reducing the number of available permits by one.If no permit is available then this method will return immediately with the value
false
.- Specified by:
tryAcquire
in interfaceRSemaphore
- Returns:
true
if a permit was acquired andfalse
otherwise
-
tryAcquire
public boolean tryAcquire(int permits)
Description copied from interface:RSemaphore
Acquires the given number of permits only if all are available at the time of invocation.Acquires a permits, if all are available and returns immediately, with the value
true
, reducing the number of available permits by given number of permits.If no permits are available then this method will return immediately with the value
false
.- Specified by:
tryAcquire
in interfaceRSemaphore
- Parameters:
permits
- the number of permits to acquire- Returns:
true
if a permit was acquired andfalse
otherwise
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync()
Description copied from interface:RSemaphoreAsync
Acquires a permit only if one is available at the time of invocation.Acquires a permit, if one is available and returns immediately, with the value
true
, reducing the number of available permits by one.If no permit is available then this method will return immediately with the value
false
.- Specified by:
tryAcquireAsync
in interfaceRSemaphoreAsync
- Returns:
true
if a permit was acquired andfalse
otherwise
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync(int permits)
Description copied from interface:RSemaphoreAsync
Acquires the given number of permits only if all are available at the time of invocation.Acquires a permits, if all are available and returns immediately, with the value
true
, reducing the number of available permits by given number of permitss.If no permits are available then this method will return immediately with the value
false
.- Specified by:
tryAcquireAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- the number of permits to acquire- Returns:
true
if a permit was acquired andfalse
otherwise
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync(long waitTime, TimeUnit unit)
Description copied from interface:RSemaphoreAsync
Acquires a permit, if one is available and returns immediately, with the value
true
, reducing the number of available permits by one.If a permit is acquired then the value
true
is returned.If the specified waiting time elapses then the value
false
is returned. If the time is less than or equal to zero, the method will not wait at all.- Specified by:
tryAcquireAsync
in interfaceRSemaphoreAsync
- Parameters:
waitTime
- the maximum time to wait for a permitunit
- the time unit of thetimeout
argument- Returns:
true
if a permit was acquired andfalse
if the waiting time elapsed before a permit was acquired
-
tryAcquire
public boolean tryAcquire(int permits, long waitTime, TimeUnit unit) throws InterruptedException
Description copied from interface:RSemaphore
Acquires the given number of permits only if all are available within the given waiting time and the current thread has not been interrupted.Acquires a permits, if all are available and returns immediately, with the value
true
, reducing the number of available permits by one.If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
- Some other thread invokes the
RSemaphore.release()
method for this semaphore and the current thread is next to be assigned a permit; or - Some other thread interrupts the current thread; or
- The specified waiting time elapses.
If a permits is acquired then the value
true
is returned.If the specified waiting time elapses then the value
false
is returned. If the time is less than or equal to zero, the method will not wait at all.- Specified by:
tryAcquire
in interfaceRSemaphore
- Parameters:
permits
- amountwaitTime
- the maximum time to wait for a permitunit
- the time unit of thetimeout
argument- Returns:
true
if a permit was acquired andfalse
if the waiting time elapsed before a permit was acquired- Throws:
InterruptedException
- if the current thread is interrupted
- Some other thread invokes the
-
tryAcquireAsync
public RFuture<Boolean> tryAcquireAsync(int permits, long waitTime, TimeUnit unit)
Description copied from interface:RSemaphoreAsync
Acquires the given number of permits only if all are available within the given waiting time.Acquires a permits, if all are available and returns immediately, with the value
true
, reducing the number of available permits by one.If a permits is acquired then the value
true
is returned.If the specified waiting time elapses then the value
false
is returned. If the time is less than or equal to zero, the method will not wait at all.- Specified by:
tryAcquireAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- amountwaitTime
- the maximum time to wait for a available permitsunit
- the time unit of thetimeout
argument- Returns:
true
if a permit was acquired andfalse
if the waiting time elapsed before a permit was acquired
-
tryAcquire
public boolean tryAcquire(long time, TimeUnit unit) throws InterruptedException
Description copied from interface:RSemaphore
Acquires a permit from this semaphore, if one becomes available within the given waiting time and the current thread has not been interrupted.Acquires a permit, if one is available and returns immediately, with the value
true
, reducing the number of available permits by one.If no permit is available then the current thread becomes disabled for thread scheduling purposes and lies dormant until one of three things happens:
- Some other thread invokes the
RSemaphore.release()
method for this semaphore and the current thread is next to be assigned a permit; or - Some other thread interrupts the current thread; or
- The specified waiting time elapses.
If a permit is acquired then the value
true
is returned.If the specified waiting time elapses then the value
false
is returned. If the time is less than or equal to zero, the method will not wait at all.- Specified by:
tryAcquire
in interfaceRSemaphore
- Parameters:
time
- the maximum time to wait for a permitunit
- the time unit of thetimeout
argument- Returns:
true
if a permit was acquired andfalse
if the waiting time elapsed before a permit was acquired- Throws:
InterruptedException
- if the current thread is interrupted
- Some other thread invokes the
-
release
public void release()
Description copied from interface:RSemaphore
Releases a permit, returning it to the semaphore.Releases a permit, increasing the number of available permits by one. If any threads of Redisson client are trying to acquire a permit, then one is selected and given the permit that was just released.
There is no requirement that a thread that releases a permit must have acquired that permit by calling
RSemaphore.acquire()
. Correct usage of a semaphore is established by programming convention in the application.- Specified by:
release
in interfaceRSemaphore
-
release
public void release(int permits)
Description copied from interface:RSemaphore
Releases the given number of permits, returning them to the semaphore.Releases the given number of permits, increasing the number of available permits by the given number of permits. If any threads of Redisson client are trying to acquire a permits, then next threads is selected and tries to acquire the permits that was just released.
There is no requirement that a thread that releases a permits must have acquired that permit by calling
RSemaphore.acquire()
. Correct usage of a semaphore is established by programming convention in the application.- Specified by:
release
in interfaceRSemaphore
- Parameters:
permits
- amount
-
releaseAsync
public RFuture<Void> releaseAsync()
Description copied from interface:RSemaphoreAsync
Releases a permit, returning it to the semaphore.Releases a permit, increasing the number of available permits by one. If any threads of Redisson client are trying to acquire a permit, then one is selected and given the permit that was just released.
There is no requirement that a thread that releases a permit must have acquired that permit by calling
RSemaphoreAsync.acquireAsync()
. Correct usage of a semaphore is established by programming convention in the application.- Specified by:
releaseAsync
in interfaceRSemaphoreAsync
- Returns:
- void
-
releaseAsync
public RFuture<Void> releaseAsync(int permits)
Description copied from interface:RSemaphoreAsync
Releases the given number of permits, returning them to the semaphore.Releases the given number of permits, increasing the number of available permits by the given number of permits. If any threads of Redisson client are trying to acquire a permits, then next threads is selected and tries to acquire the permits that was just released.
There is no requirement that a thread that releases a permits must have acquired that permit by calling
RSemaphoreAsync.acquireAsync()
. Correct usage of a semaphore is established by programming convention in the application.- Specified by:
releaseAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- amount- Returns:
- void
-
drainPermits
public int drainPermits()
Description copied from interface:RSemaphore
Acquires and returns all permits that are immediately available.- Specified by:
drainPermits
in interfaceRSemaphore
- Returns:
- the number of permits acquired
-
availablePermits
public int availablePermits()
Description copied from interface:RSemaphore
Returns the current number of available permits.- Specified by:
availablePermits
in interfaceRSemaphore
- Returns:
- number of available permits
-
trySetPermits
public boolean trySetPermits(int permits)
Description copied from interface:RSemaphore
Sets number of permits.- Specified by:
trySetPermits
in interfaceRSemaphore
- Parameters:
permits
- - number of permits- Returns:
true
if permits has been set successfully, otherwisefalse
.
-
trySetPermitsAsync
public RFuture<Boolean> trySetPermitsAsync(int permits)
Description copied from interface:RSemaphoreAsync
Sets number of permits.- Specified by:
trySetPermitsAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- - number of permits- Returns:
true
if permits has been set successfully, otherwisefalse
.
-
reducePermits
public void reducePermits(int permits)
Description copied from interface:RSemaphore
Shrinks the number of available permits by the indicated reduction.- Specified by:
reducePermits
in interfaceRSemaphore
- Parameters:
permits
- - reduction the number of permits to remove
-
reducePermitsAsync
public RFuture<Void> reducePermitsAsync(int permits)
Description copied from interface:RSemaphoreAsync
Shrinks the number of available permits by the indicated reduction. This method can be useful in subclasses that use semaphores to track resources that become unavailable. This method differs fromRSemaphoreAsync.acquireAsync()
in that it does not block waiting for permits to become available.- Specified by:
reducePermitsAsync
in interfaceRSemaphoreAsync
- Parameters:
permits
- - reduction the number of permits to remove- Returns:
- void
-
expire
public boolean expire(long timeToLive, TimeUnit timeUnit)
Description copied from interface:RExpirable
Set a timeout for object. After the timeout has expired, the key will automatically be deleted.- Specified by:
expire
in interfaceRExpirable
- Parameters:
timeToLive
- - timeout before object will be deletedtimeUnit
- - timeout time unit- Returns:
true
if the timeout was set andfalse
if not
-
expireAsync
public RFuture<Boolean> expireAsync(long timeToLive, TimeUnit timeUnit)
Description copied from interface:RExpirableAsync
Set a timeout for object in async mode. After the timeout has expired, the key will automatically be deleted.- Specified by:
expireAsync
in interfaceRExpirableAsync
- Parameters:
timeToLive
- - timeout before object will be deletedtimeUnit
- - timeout time unit- Returns:
true
if the timeout was set andfalse
if not
-
expireAt
public boolean expireAt(long timestamp)
Description copied from interface:RExpirable
Set an expire date for object. When expire date comes the key will automatically be deleted.- Specified by:
expireAt
in interfaceRExpirable
- Parameters:
timestamp
- - expire date in milliseconds (Unix timestamp)- Returns:
true
if the timeout was set andfalse
if not
-
expireAtAsync
public RFuture<Boolean> expireAtAsync(long timestamp)
Description copied from interface:RExpirableAsync
Set an expire date for object in async mode. When expire date comes the key will automatically be deleted.- Specified by:
expireAtAsync
in interfaceRExpirableAsync
- Parameters:
timestamp
- - expire date in milliseconds (Unix timestamp)- Returns:
true
if the timeout was set andfalse
if not
-
expireAt
public boolean expireAt(Date timestamp)
Description copied from interface:RExpirable
Set an expire date for object. When expire date comes the key will automatically be deleted.- Specified by:
expireAt
in interfaceRExpirable
- Parameters:
timestamp
- - expire date- Returns:
true
if the timeout was set andfalse
if not
-
expireAtAsync
public RFuture<Boolean> expireAtAsync(Date timestamp)
Description copied from interface:RExpirableAsync
Set an expire date for object in async mode. When expire date comes the key will automatically be deleted.- Specified by:
expireAtAsync
in interfaceRExpirableAsync
- Parameters:
timestamp
- - expire date- Returns:
true
if the timeout was set andfalse
if not
-
clearExpire
public boolean clearExpire()
Description copied from interface:RExpirable
Clear an expire timeout or expire date for object.- Specified by:
clearExpire
in interfaceRExpirable
- Returns:
true
if timeout was removedfalse
if object does not exist or does not have an associated timeout
-
clearExpireAsync
public RFuture<Boolean> clearExpireAsync()
Description copied from interface:RExpirableAsync
Clear an expire timeout or expire date for object in async mode. Object will not be deleted.- Specified by:
clearExpireAsync
in interfaceRExpirableAsync
- Returns:
true
if the timeout was cleared andfalse
if not
-
remainTimeToLive
public long remainTimeToLive()
Description copied from interface:RExpirable
Remaining time to live of Redisson object that has a timeout- Specified by:
remainTimeToLive
in interfaceRExpirable
- Returns:
- time in milliseconds -2 if the key does not exist. -1 if the key exists but has no associated expire.
-
remainTimeToLiveAsync
public RFuture<Long> remainTimeToLiveAsync()
Description copied from interface:RExpirableAsync
Remaining time to live of Redisson object that has a timeout- Specified by:
remainTimeToLiveAsync
in interfaceRExpirableAsync
- Returns:
- time in milliseconds -2 if the key does not exist. -1 if the key exists but has no associated expire.
-
-