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, waitcreateSnapshot, getConfigurationprotected 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
InterruptedExceptionprotected abstract void addTokensImpl(long tokensToAdd)
public boolean tryConsume(long tokensToConsume)
BuckettryConsume in interface BuckettokensToConsume - 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
BucketnumTokens 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 BuckettokensToConsume - 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
BucketmaxWaitTimeNanos has elapsed.consume in interface BuckettokensToConsume - 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)
BucketnumTokens 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 BuckettokensToConsume - 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)
BucketconsumeUninterruptibly in interface BuckettokensToConsume - 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)
Bucketlimit.tryConsumeAsMuchAsPossible in interface Bucketlimit - maximum number of tokens to consume, should be positive.public long tryConsumeAsMuchAsPossible()
BuckettryConsumeAsMuchAsPossible in interface Bucketpublic void addTokens(long tokensToAdd)
BucketnewTokens = 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.