Class AdjustableSemaphore

java.lang.Object
pl.allegro.tech.hermes.consumers.consumer.rate.AdjustableSemaphore

@ThreadSafe public final class AdjustableSemaphore extends Object
A simple implementation of an adjustable semaphore.
  • Constructor Details

    • AdjustableSemaphore

      public AdjustableSemaphore(int maxPermits)
      New instances should be configured with setMaxPermits().
  • Method Details

    • setMaxPermits

      public void setMaxPermits(int newMax)
      Set the max number of permits. Must be greater than zero.

      Note that if there are more than the new max number of permits currently outstanding, any currently blocking threads or any new threads that start to block after the call will wait until enough permits have been released to have the number of outstanding permits fall below the new maximum. In other words, it does what you probably think it should.

    • release

      public void release()
      Release a permit back to the semaphore. Make sure not to double-release.
    • acquire

      public void acquire() throws InterruptedException
      Get a permit, blocking if necessary.
      Throws:
      InterruptedException - if interrupted while waiting for a permit
    • tryAcquire

      public boolean tryAcquire(long timeout, TimeUnit unit) throws InterruptedException
      Throws:
      InterruptedException
    • availablePermits

      public int availablePermits()