Class AdjustableSemaphore
java.lang.Object
pl.allegro.tech.hermes.consumers.consumer.rate.AdjustableSemaphore
A simple implementation of an adjustable semaphore.
-
Constructor Summary
ConstructorsConstructorDescriptionAdjustableSemaphore(int maxPermits) New instances should be configured with setMaxPermits(). -
Method Summary
Modifier and TypeMethodDescriptionvoidacquire()Get a permit, blocking if necessary.intvoidrelease()Release a permit back to the semaphore.voidsetMaxPermits(int newMax) Set the max number of permits.booleantryAcquire(long timeout, TimeUnit unit)
-
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
Get a permit, blocking if necessary.- Throws:
InterruptedException- if interrupted while waiting for a permit
-
tryAcquire
- Throws:
InterruptedException
-
availablePermits
public int availablePermits()
-