public class TimeBasedLimiter extends NonRefillableLimiter
Limiter that limits the time elapsed for some events.
This implementation uses a task scheduled in a ScheduledThreadPoolExecutor that will
fire once after a given amount of time has elapsed. The task once fired, will flip a boolean
flag that tells if permits should be issued. The flag is initially set to true. Thus,
no permits are issued once the flag is flipped after the given amount of time has elapsed.
acquirePermits(long) will return false once the time limit is reached. Permit
refills are not supported in this implementation.
| Modifier and Type | Class and Description |
|---|---|
static class |
TimeBasedLimiter.Factory |
NO_OP_CLOSEABLE| Constructor and Description |
|---|
TimeBasedLimiter(long timeLimit) |
TimeBasedLimiter(long timeLimit,
TimeUnit timeUnit) |
| 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 TimeBasedLimiter(long timeLimit)
public TimeBasedLimiter(long timeLimit,
TimeUnit timeUnit)
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 LimiteracquirePermits in class NonRefillableLimiterpermits - 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()