-
Hi, My name is David and I'm using this great library to implement a token based throttling mechanism.
The general scenario I want to use this bucket in consists of a queue getting filled by some producer thread(s), which simultaneously getting consumed by some consumer thread(s). As the scenario I described above involves some busy waiting (sleeping for some time), I wonder if there's some way to register/listen to the event of the bucket refill. Any ideas will be highly appreciated :) Thanks in advance, |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
Looks like I found a way to avoid the busy waiting with the sleep time. When
Which leads to the consumer thread being blocked until the bucket get refilled. When it wakes up, it immediately returns the consumed token to the bucket. What do you think about this solution? is it a recommended way to achieve this behavior? Thanks again, |
Beta Was this translation helpful? Give feedback.
Looks like I found a way to avoid the busy waiting with the sleep time. When
bucket.tryConsume(1)
returns false, I run the following code:bucket.asBlocking().consume(1); bucket.addTokens(1);
Which leads to the consumer thread being blocked until the bucket get refilled. When it wakes up, it immediately returns the consumed token to the bucket.
What do you think about this solution? is it a recommended way to achieve this behavior?
Thanks again,
David