public interface Bucket
tryConsume(long)
, tryConsumeAsMuchAsPossible()
.consume(long, BlockingStrategy)
, consume(long, BlockingStrategy)
.Modifier and Type | Method and Description |
---|---|
void |
addTokens(long tokensToAdd)
Add tokensToAdd to each bandwidth of bucket.
|
void |
consume(long numTokens,
BlockingStrategy blockingStrategy)
Consumes
numTokens from the bucket. |
boolean |
consume(long numTokens,
long maxWaitTimeNanos,
BlockingStrategy blockingStrategy)
Consumes a specified number of tokens from the bucket.
|
void |
consumeUninterruptibly(long numTokens,
BlockingStrategy blockingStrategy)
Consumes
numTokens from the bucket. |
boolean |
consumeUninterruptibly(long numTokens,
long maxWaitTimeNanos,
BlockingStrategy blockingStrategy)
Consumes a specified number of tokens from the bucket.
|
BucketState |
createSnapshot()
Creates the copy of internal state.
|
BucketConfiguration |
getConfiguration()
Returns configuration of this bucket.
|
boolean |
tryConsume(long numTokens)
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 . |
boolean tryConsume(long numTokens)
numTokens
- The number of tokens to consume from the bucket, must be a positive number.true
if the tokens were consumed, false
otherwise.long tryConsumeAsMuchAsPossible()
long tryConsumeAsMuchAsPossible(long limit)
limit
.limit
- maximum number of tokens to consume, should be positive.boolean consume(long numTokens, long maxWaitTimeNanos, BlockingStrategy blockingStrategy) throws InterruptedException
maxWaitTimeNanos
has elapsed.numTokens
- 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 waitingIllegalArgumentException
- if numTokens is greater than capacity of bucketvoid consume(long numTokens, BlockingStrategy blockingStrategy) throws InterruptedException
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.numTokens
- 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 waitingIllegalArgumentException
- if numTokens is greater than capacity of bucketboolean consumeUninterruptibly(long numTokens, long maxWaitTimeNanos, BlockingStrategy blockingStrategy)
numTokens
- 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 consumedIllegalArgumentException
- if numTokens is greater than capacity of bucketvoid consumeUninterruptibly(long numTokens, BlockingStrategy blockingStrategy)
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.numTokens
- 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 bucketIllegalArgumentException
- if numTokens is greater than capacity of bucketvoid addTokens(long tokensToAdd)
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);
}
tokensToAdd
- number of tokens to addIllegalArgumentException
- in case of tokensToAdd less than 1BucketState createSnapshot()
This method is designed to be used only for monitoring and testing, you should never use this method for business cases.
BucketConfiguration getConfiguration()
Copyright © 2017. All rights reserved.