开发者

Queue implementation with blocked 'take()' but with eviction policy

开发者 https://www.devze.com 2022-12-30 00:19 出处:网络
Is there an implementation with a blocking queue for take but bounded by a maximum size.When the size of the queue reaches a given max-size, instead of blocking \'put\', it will remove the head 开发者

Is there an implementation with a blocking queue for take but bounded by a maximum size. When the size of the queue reaches a given max-size, instead of blocking 'put', it will remove the head 开发者_如何学运维element and insert it. So put is not blocked() but take() is.

One usage is that if I have a very slow consumer, the system will not crash ( runs out of memory ) rather these message will be removed but I do not want to block the producer.

An example of this would stock trading system. When you get a spike in stock trade/quote data, if you haven't consumed data, you want to automatically throw away old stock trade/quote.


There currently isnt in Java a thread-safe queue that will do what you are looking for. However, there is a BlockingDequeue (Double Ended Queue) that you can write a wrapper in which you can take from the head and and tail as you see freely.

This class, similar to a BlockingQueue, is thread safe.


Several strategies are provided in ThreadPoolExecutor. Search for "AbortPolicy" in this javadoc . You can also implement your own policy if you want. Perhaps Discard is similar to what you want. Personally I think CallerRuns is what you want in most cases.

I think using these is a better solution, but if you absolutely want to implement it at the queue, I'd probably do it by composition. Perhaps use a LinkedList or something and wrap it with synchronize keyword.

EDIT:(some clarifications..) "Executor" is basically a thread pool combined with a blocking queue. It is the recommended way to implement a producer/consumer pattern in java. The authors of these libraries provides several strategies to cope with issues like you mentioned. If you are interested, here is another approach to specifically address the OOME issue (the source is framework specific and can't be used as is).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号