Interface RSemaphore
-
- All Superinterfaces:
RExpirable
,RExpirableAsync
,RObject
,RObjectAsync
,RSemaphoreAsync
- All Known Implementing Classes:
RedissonQueueSemaphore
,RedissonSemaphore
public interface RSemaphore extends RExpirable, RSemaphoreAsync
Distributed and concurrent implementation ofSemaphore
.Works in non-fair mode. Therefore order of acquiring is unpredictable.
- Author:
- Nikita Koksharov
-
-
Method Summary
All Methods Instance Methods Abstract 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.int
availablePermits()
Returns the current number of available permits.int
drainPermits()
Acquires and returns all permits that are immediately available.void
reducePermits(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.boolean
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 waitTime, 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.boolean
trySetPermits(int permits)
Sets number of permits.-
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
-
Methods inherited from interface org.redisson.api.RSemaphoreAsync
acquireAsync, acquireAsync, reducePermitsAsync, releaseAsync, releaseAsync, tryAcquireAsync, tryAcquireAsync, tryAcquireAsync, tryAcquireAsync, trySetPermitsAsync
-
-
-
-
Method Detail
-
acquire
void acquire() throws InterruptedException
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
release()
method for this semaphore and the current thread is next to be assigned a permit; or - Some other thread interrupts the current thread.
- Throws:
InterruptedException
- if the current thread is interrupted
- Some other thread invokes the
-
acquire
void acquire(int permits) throws InterruptedException
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.
- Parameters:
permits
- the number of permits to acquire- Throws:
InterruptedException
- if the current thread is interruptedIllegalArgumentException
- ifpermits
is negative
- Some other thread invokes one of the
-
tryAcquire
boolean tryAcquire()
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
.- Returns:
true
if a permit was acquired andfalse
otherwise
-
tryAcquire
boolean tryAcquire(int permits)
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
.- Parameters:
permits
- the number of permits to acquire- Returns:
true
if a permit was acquired andfalse
otherwise
-
tryAcquire
boolean tryAcquire(long waitTime, TimeUnit unit) throws InterruptedException
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
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.- 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- Throws:
InterruptedException
- if the current thread is interrupted
- Some other thread invokes the
-
tryAcquire
boolean tryAcquire(int permits, long waitTime, TimeUnit unit) throws InterruptedException
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
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.- 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
-
release
void release()
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
acquire()
. Correct usage of a semaphore is established by programming convention in the application.
-
release
void release(int permits)
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
acquire()
. Correct usage of a semaphore is established by programming convention in the application.- Parameters:
permits
- amount
-
availablePermits
int availablePermits()
Returns the current number of available permits.- Returns:
- number of available permits
-
drainPermits
int drainPermits()
Acquires and returns all permits that are immediately available.- Returns:
- the number of permits acquired
-
trySetPermits
boolean trySetPermits(int permits)
Sets number of permits.- Parameters:
permits
- - number of permits- Returns:
true
if permits has been set successfully, otherwisefalse
.
-
reducePermits
void reducePermits(int permits)
Shrinks the number of available permits by the indicated reduction.- Parameters:
permits
- - reduction the number of permits to remove- Throws:
IllegalArgumentException
- ifreduction
is negative
-
-