public abstract class TokenBucket
extends java.lang.Object
Constructor and Description |
---|
TokenBucket(long capacity,
long nanosBetweenTokens) |
Modifier and Type | Method and Description |
---|---|
abstract long |
currentTime()
The current time in nanos.
|
void |
init()
This method must be called before the token bucket can be used.
|
long |
offer(long cost)
Call this (side-effecting) method whenever an element should be passed through the token-bucket.
|
public void init()
public abstract long currentTime()
public long offer(long cost)
If a non-zero value is returned, it is the responsibility of the caller to not call this method before the returned delay has been elapsed (but can be called later). This class does not check or protect against early calls.
cost
- How many tokens the element costs. Can be larger than the capacity of the bucket.