com.jashmore.sqs.util.ResizableSemaphore Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of common-utils Show documentation
Show all versions of common-utils Show documentation
Utility methods for dealing with basic functionality for this library, split out so so it can be consumed by other extensions
package com.jashmore.sqs.util;
import java.util.concurrent.Semaphore;
/**
* Semaphore that is able to dynamically update the number of available permits.
*/
public class ResizableSemaphore extends Semaphore {
private int maximumPermits;
public ResizableSemaphore(final int permits) {
super(permits);
this.maximumPermits = permits;
}
/**
* Change the maximum number of permits available.
*
* The changing of permit size is not thread safe and therefore this method should only be used by a single thread. E.g. only one thread has the
* responsibility of changing the permit size.
*
* @param permits new max size for permits
*/
public void changePermitSize(final int permits) {
if (permits > this.maximumPermits) {
this.release(permits - this.maximumPermits);
} else if (permits < this.maximumPermits) {
this.reducePermits(this.maximumPermits - permits);
}
this.maximumPermits = permits;
}
public int getMaximumPermits() {
return maximumPermits;
}
}