public class PoolBasedLimiter extends Object implements Limiter
Limiter that ony allows permits to be acquired from a pool.
This implementation uses a Semaphore as a permit pool. acquirePermits(long)
is blocking and will always return true after the permits are successfully acquired
(probably after being blocked for some amount of time). Permit refills are supported by this
implementation using Semaphore.release(int). Also acquirePermits(long) only
accepts input arguments that can be safely casted to an integer bounded by
Integer.MAX_VALUE.
| Modifier and Type | Class and Description |
|---|---|
static class |
PoolBasedLimiter.Factory |
| Constructor and Description |
|---|
PoolBasedLimiter(int poolSize) |
| Modifier and Type | Method and Description |
|---|---|
Closeable |
acquirePermits(long permits)
Acquire a given number of permits.
|
void |
start()
Start the
Limiter. |
void |
stop()
Stop the
Limiter. |
public void start()
LimiterLimiter.
See Limiter.stop()public Closeable acquirePermits(long permits) throws InterruptedException
LimiterDepending on the implementation, the caller of this method may be blocked. It is also up to the caller to decide how to deal with the return value.
acquirePermits in interface Limiterpermits - number of permits to getCloseable instance if the requested permits have been successfully acquired,
or null if otherwise; in the former case, calling Closeable.close() on
the returned Closeable instance will release the acquired permits.InterruptedException - if the caller is interrupted while being blockedpublic void stop()
LimiterLimiter.
See Limiter.start()