public abstract class AbstractBucket extends Object implements Bucket
Modifier and Type | Field and Description |
---|---|
protected static long |
UNSPECIFIED_WAITING_LIMIT |
Constructor and Description |
---|
AbstractBucket() |
Modifier and Type | Method and Description |
---|---|
void |
addTokens(long tokensToAdd)
Add tokensToAdd to each bandwidth of bucket.
|
protected abstract void |
addTokensImpl(long tokensToAdd) |
void |
consume(long tokensToConsume,
BlockingStrategy blockingStrategy)
Consumes
numTokens from the bucket. |
boolean |
consume(long tokensToConsume,
long maxWaitTimeNanos,
BlockingStrategy blockingStrategy)
Consumes a specified number of tokens from the bucket.
|
protected abstract long |
consumeAsMuchAsPossibleImpl(long limit) |
protected abstract boolean |
consumeOrAwaitImpl(long tokensToConsume,
long waitIfBusyNanos,
boolean uninterruptibly,
BlockingStrategy blockingStrategy) |
void |
consumeUninterruptibly(long tokensToConsume,
BlockingStrategy blockingStrategy)
Consumes
numTokens from the bucket. |
boolean |
consumeUninterruptibly(long tokensToConsume,
long maxWaitTimeNanos,
BlockingStrategy blockingStrategy)
Consumes a specified number of tokens from the bucket.
|
boolean |
tryConsume(long tokensToConsume)
Tries to consume a specified number of tokens from this bucket.
|
long |
tryConsumeAsMuchAsPossible()
Tries to consume as much tokens from this bucket as available at the moment of invocation.
|
long |
tryConsumeAsMuchAsPossible(long limit)
Tries to consume as much tokens from bucket as available in the bucket at the moment of invocation,
but tokens which should be consumed is limited by than not more than
limit . |
protected abstract boolean |
tryConsumeImpl(long tokensToConsume) |
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
createSnapshot, getConfiguration
protected static final long UNSPECIFIED_WAITING_LIMIT
protected abstract long consumeAsMuchAsPossibleImpl(long limit)
protected abstract boolean tryConsumeImpl(long tokensToConsume)
protected abstract boolean consumeOrAwaitImpl(long tokensToConsume, long waitIfBusyNanos, boolean uninterruptibly, BlockingStrategy blockingStrategy) throws InterruptedException
InterruptedException
protected abstract void addTokensImpl(long tokensToAdd)
public boolean tryConsume(long tokensToConsume)
Bucket
tryConsume
in interface Bucket
tokensToConsume
- The number of tokens to consume from the bucket, must be a positive number.true
if the tokens were consumed, false
otherwise.public void consume(long tokensToConsume, BlockingStrategy blockingStrategy) throws InterruptedException
Bucket
numTokens
from the bucket. If enough tokens are not currently available then this method will block
until required number of tokens will be available or current thread is interrupted.consume
in interface Bucket
tokensToConsume
- The number of tokens to consume from bucket, must be a positive number.blockingStrategy
- specifies the way to block current thread to amount of time required to refill missed number of tokens in the bucketInterruptedException
- in case of current thread has been interrupted during waitingpublic boolean consume(long tokensToConsume, long maxWaitTimeNanos, BlockingStrategy blockingStrategy) throws InterruptedException
Bucket
maxWaitTimeNanos
has elapsed.consume
in interface Bucket
tokensToConsume
- The number of tokens to consume from the bucket.maxWaitTimeNanos
- limit of time which thread can wait.blockingStrategy
- specifies the way to block current thread to amount of time required to refill missed number of tokens in the bucketnumTokens
has been consumed or false when numTokens
has not been consumedInterruptedException
- in case of current thread has been interrupted during waitingpublic void consumeUninterruptibly(long tokensToConsume, BlockingStrategy blockingStrategy)
Bucket
numTokens
from the bucket. If enough tokens are not currently available then this method will block
until required number of tokens will be available or current thread is interrupted.consumeUninterruptibly
in interface Bucket
tokensToConsume
- The number of tokens to consume from bucket, must be a positive number.blockingStrategy
- specifies the way to block current thread to amount of time required to refill missed number of tokens in the bucketpublic boolean consumeUninterruptibly(long tokensToConsume, long maxWaitTimeNanos, BlockingStrategy blockingStrategy)
Bucket
consumeUninterruptibly
in interface Bucket
tokensToConsume
- The number of tokens to consume from the bucket.maxWaitTimeNanos
- limit of time which thread can wait.blockingStrategy
- specifies the way to block current thread to amount of time required to refill missed number of tokens in the bucketnumTokens
has been consumed or false when numTokens
has not been consumedpublic long tryConsumeAsMuchAsPossible(long limit)
Bucket
limit
.tryConsumeAsMuchAsPossible
in interface Bucket
limit
- maximum number of tokens to consume, should be positive.public long tryConsumeAsMuchAsPossible()
Bucket
tryConsumeAsMuchAsPossible
in interface Bucket
public void addTokens(long tokensToAdd)
Bucket
newTokens = Math.min(capacity, currentTokens + tokensToAdd)in other words resulted number of tokens never exceeds capacity independent of tokensToAdd.
Bucket wallet;
...
wallet.consume(50); // get 50 cents from wallet
try {
buyCocaCola();
} catch(NoCocaColaException e) {
// return money to wallet
wallet.addTokens(50);
}
Copyright © 2017. All rights reserved.