What's the best practice in Java to protect a method from being called excessively, while still keeping it non-blocking?
My usecase is a custom buffer for audio data. The buffer's method through which data can be polled from it must be non-blocking, and it is acceptable for the method to return null when no data is available. It's not acceptable for the method to be blocking, so I do not want to make it synchronized. When the method is called excessively the sound quality decreases. So how can I protect the method under heavy load, without relying on callers to behave themselves?
My current approach is to remember the timestamp of the last poll and return null if it's shorter ago than 3ms, but this feels a bit "hacky"...
ConcurrentLinkedQueue does just that. It is non-blocking and returns null if no messages are available. Either that or a ConcurrentLinkedDequeue
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With