Interface RSemaphore

    • 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
      • 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 interrupted
        IllegalArgumentException - if permits is negative
      • 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 and false 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 and false 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 permit
        unit - the time unit of the timeout argument
        Returns:
        true if a permit was acquired and false if the waiting time elapsed before a permit was acquired
        Throws:
        InterruptedException - if the current thread is interrupted
      • 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 - amount
        waitTime - the maximum time to wait for a permit
        unit - the time unit of the timeout argument
        Returns:
        true if a permit was acquired and false if the waiting time elapsed before a permit was acquired
        Throws:
        InterruptedException - if the current thread is interrupted
      • 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, otherwise false.
      • 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 - if reduction is negative