Interface RSemaphore

All Superinterfaces:
RExpirable, RExpirableAsync, RObject, RObjectAsync, RSemaphoreAsync
All Known Implementing Classes:
RedissonQueueSemaphore, RedissonSemaphore

public interface RSemaphore extends RExpirable, RSemaphoreAsync
Redis based implementation of Semaphore.

Works in non-fair mode. Therefore order of acquiring is unpredictable.

Author:
Nikita Koksharov
  • Method Details

    • acquire

      void acquire() throws InterruptedException
      Acquires a permit. Waits if necessary until a permit became available.
      Throws:
      InterruptedException - if the current thread was interrupted
    • acquire

      void acquire(int permits) throws InterruptedException
      Acquires defined amount of permits. Waits if necessary until all permits became available.
      Parameters:
      permits - the number of permits to acquire
      Throws:
      InterruptedException - if the current thread is interrupted
      IllegalArgumentException - if permits is negative
    • tryAcquire

      boolean tryAcquire()
      Tries to acquire currently available permit.
      Returns:
      true if a permit was acquired and false otherwise
    • tryAcquire

      boolean tryAcquire(int permits)
      Tries to acquire defined amount of currently available permits.
      Parameters:
      permits - the number of permits to acquire
      Returns:
      true if permits were acquired and false otherwise
    • tryAcquire

      boolean tryAcquire(long waitTime, TimeUnit unit) throws InterruptedException
      Tries to acquire currently available permit. Waits up to defined waitTime if necessary until a permit became available.
      Parameters:
      waitTime - the maximum time to wait
      unit - the time unit
      Returns:
      true if a permit was acquired and false otherwise
      Throws:
      InterruptedException - if the current thread was interrupted
    • tryAcquire

      boolean tryAcquire(int permits, long waitTime, TimeUnit unit) throws InterruptedException
      Tries to acquire defined amount of currently available permits. Waits up to defined waitTime if necessary until all permits became available.
      Parameters:
      permits - amount of permits
      waitTime - the maximum time to wait
      unit - the time unit
      Returns:
      true if permits were acquired and false otherwise
      Throws:
      InterruptedException - if the current thread was interrupted
    • release

      void release()
      Releases a permit. Increases the number of available permits.
    • release

      void release(int permits)
      Releases defined amount of permits. Increases the number of available permits by permits amount.
      Parameters:
      permits - amount of permits
    • availablePermits

      int availablePermits()
      Returns amount of available permits.
      Returns:
      number of permits
    • drainPermits

      int drainPermits()
      Acquires and returns all permits that are immediately available.
      Returns:
      number of permits
    • trySetPermits

      boolean trySetPermits(int permits)
      Tries to set number of permits.
      Parameters:
      permits - - number of permits
      Returns:
      true if permits has been set successfully, otherwise false if permits were already set.
    • addPermits

      void addPermits(int permits)
      Increases or decreases the number of available permits by defined value.
      Parameters:
      permits - amount of permits to add/remove