Interface RPermitExpirableSemaphore

All Superinterfaces:
RExpirable, RExpirableAsync, RObject, RObjectAsync, RPermitExpirableSemaphoreAsync
All Known Implementing Classes:
RedissonPermitExpirableSemaphore

public interface RPermitExpirableSemaphore extends RExpirable, RPermitExpirableSemaphoreAsync
Semaphore object with lease time parameter support for each acquired permit.

Each permit identified by own id and could be released only using its id. Permit id is a 128-bits unique random identifier generated each time during acquiring.

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

Author:
Nikita Koksharov
  • Method Details

    • acquire

      String acquire() throws InterruptedException
      Acquires a permit and returns its id. Waits if necessary until a permit became available.
      Returns:
      permit id
      Throws:
      InterruptedException - if the current thread is interrupted
    • acquire

      List<String> acquire(int permits) throws InterruptedException
      Acquires defined amount of permits. Waits if necessary until enough permits became available.
      Parameters:
      permits - the number of permits to acquire
      Returns:
      permits ids
      Throws:
      InterruptedException - if the current thread is interrupted
      IllegalArgumentException - if permits is negative
    • acquire

      String acquire(long leaseTime, TimeUnit unit) throws InterruptedException
      Acquires a permit with defined leaseTime and return its id. Waits if necessary until a permit became available.
      Parameters:
      leaseTime - permit lease time
      unit - time unit
      Returns:
      permit id
      Throws:
      InterruptedException - if the current thread is interrupted
    • acquire

      List<String> acquire(int permits, long leaseTime, TimeUnit unit) throws InterruptedException
      Acquires defined amount of permits with defined leaseTime and returns ids. Waits if necessary until enough permits became available.
      Parameters:
      permits - the number of permits to acquire
      leaseTime - permit lease time
      unit - time unit
      Returns:
      permits ids
      Throws:
      InterruptedException - if the current thread is interrupted
      IllegalArgumentException - if permits is negative
    • tryAcquire

      String tryAcquire()
      Tries to acquire currently available permit and return its id.
      Returns:
      permit id if a permit was acquired and null otherwise
    • tryAcquire

      List<String> tryAcquire(int permits)
      Tries to acquire defined amount of currently available permits and returns ids.
      Parameters:
      permits - the number of permits to acquire
      Returns:
      permits ids if permits were acquired and empty collection otherwise
      Throws:
      IllegalArgumentException - if permits is negative
    • tryAcquire

      String tryAcquire(long waitTime, TimeUnit unit) throws InterruptedException
      Tries to acquire currently available permit and return its id. Waits up to defined waitTime if necessary until a permit became available.
      Parameters:
      waitTime - the maximum time to wait
      unit - the time unit
      Returns:
      permit id if a permit was acquired and null if the waiting time elapsed before a permit was acquired
      Throws:
      InterruptedException - if the current thread is interrupted
    • tryAcquire

      String tryAcquire(long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException
      Tries to acquire currently available permit with defined leaseTime and return its id. Waits up to defined waitTime if necessary until a permit became available.
      Parameters:
      waitTime - the maximum time to wait
      leaseTime - permit lease time, use -1 to make it permanent
      unit - the time unit
      Returns:
      permit id if a permit was acquired and null if the waiting time elapsed before a permit was acquired
      Throws:
      InterruptedException - if the current thread is interrupted
    • tryAcquire

      List<String> tryAcquire(int permits, long waitTime, long leaseTime, TimeUnit unit) throws InterruptedException
      Tries to acquire defined amount of currently available permits with defined leaseTime and return ids. Waits up to defined waitTime if necessary until enough permits became available.
      Parameters:
      permits - the number of permits to acquire
      waitTime - the maximum time to wait
      leaseTime - permit lease time, use -1 to make it permanent
      unit - the time unit
      Returns:
      permits ids if permits were acquired and empty collection if the waiting time elapsed before permits were acquired
      Throws:
      InterruptedException - if the current thread is interrupted
      IllegalArgumentException - if permits is negative
    • tryRelease

      boolean tryRelease(String permitId)
      Tries to release permit by its id.
      Parameters:
      permitId - permit id
      Returns:
      true if a permit has been released and false otherwise
      Throws:
      IllegalArgumentException - if permitId is null
    • tryRelease

      int tryRelease(List<String> permitsIds)
      Tries to release permits by their ids.
      Parameters:
      permitsIds - - permits ids
      Returns:
      amount of released permits
      Throws:
      IllegalArgumentException - if permitsIds is null or empty
    • release

      void release(String permitId)
      Releases a permit by its id. Increases the number of available permits. Throws an exception if permit id doesn't exist or has already been released.
      Parameters:
      permitId - - permit id
      Throws:
      IllegalArgumentException - if permitId is null
    • release

      void release(List<String> permitsIds)
      Releases permits by their ids. Increases the number of available permits. Throws an exception if permit id doesn't exist or has already been released.
      Parameters:
      permitsIds - - permits ids
      Throws:
      IllegalArgumentException - if permitsIds is null or empty
    • availablePermits

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

      int getPermits()
      Returns the number of permits.
      Returns:
      number of permits
    • acquiredPermits

      int acquiredPermits()
      Returns the number of acquired permits.
      Returns:
      number of acquired permits
    • trySetPermits

      boolean trySetPermits(int permits)
      Tries to set the initial number of available permits.
      Parameters:
      permits - - number of permits
      Returns:
      true if permits has been set successfully, otherwise false.
    • setPermits

      void setPermits(int permits)
      Sets the number of permits to the provided value. Calculates the delta between the given permits value and the current number of permits, then increases the number of available permits by delta.
      Parameters:
      permits - - number of permits
    • addPermits

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

      boolean updateLeaseTime(String permitId, long leaseTime, TimeUnit unit)
      Overrides and updates lease time for defined permit id.
      Parameters:
      permitId - permit id
      leaseTime - permit lease time, use -1 to make it permanent
      unit - the time unit
      Returns:
      true if permits has been updated successfully, otherwise false.