Class QuotaSemaphore

  • All Implemented Interfaces:
    Runnable

    public class QuotaSemaphore
    extends Object
    implements Runnable
    A counting semaphore that represents a quota. When a quota is reached, further acquisition are blocked until the quota is reset.
    • Constructor Detail

      • QuotaSemaphore

        public QuotaSemaphore​(int quota)
        Creates a new QuotaSemaphore with the given quota count.
        Parameters:
        quota - the quota
    • Method Detail

      • run

        public void run()
        Resets the quota.
        Specified by:
        run in interface Runnable
      • tryAquire

        public boolean tryAquire​(long timeout,
                                 TimeUnit unit)
                          throws InterruptedException
        Acquires semaphore if quota not reached. Once quota is reached, acquisitions will block until timeout or until quota is reset by calling run().
        Parameters:
        timeout - 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 an interrupt occurs