生产者/消费者 - 生产者将数据添加到集合而不阻塞,消费者消耗来自批量收集的数据 [英] Producer/Consumer - producer adds data to collection without blocking, consumer consumes data from collection in batch

查看:239
本文介绍了生产者/消费者 - 生产者将数据添加到集合而不阻塞,消费者消耗来自批量收集的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个生产者/消费者的用例程序是有点不寻常。
我有一个真实世界的用例与一些生产者,我希望他们能够添加对象到一个集合没有阻塞。
消费者(只有一个)应该阻止,直到集合中有一定数量的对象可用(例如500),然后批量消费它们。虽然有少于500,它应该阻止并等待集合填充。我不介意如果队列在短时间内超过这个值(700,1000等)。



我目前似乎没有找到解决方案解决这个确切的问题。
我正在考虑使用ConcurrentLinkedQueue,并让消费者定期检查队列是否有足够的数据,但这似乎是反效果。



另一个想法是使用LinkedBlockingQueue。生产者不会阻塞(除非队列满了,这意味着它有Integer.MAX_VALUE值 - 这不是我的情况,所以它都是好的)。消费者将做一个queue.take()并将元素添加到内部集合中。当内部集合达到500个元素时,它将批量消耗它们。



您有任何提示吗?



谢谢!



生成器将项目添加到队列中。



消费者将做一个queue.poll()并将这些项目保存到内部集合中。当内部收集达到500个元素,它将消费他们散装。我也可以设置一些超时。例如,如果X秒已过,我会使用集合,即使它有比所需的更少的项目(例如。220)。


I have a Producer/Consumer usecase which is a bit unusual. I have a real world use case with some producers which I want them to be able to add objects into a collection without blocking. The consumer (just one) should block until a certain amount of objects are available in the collection (eg. 500) and then consume them in bulk. While there are less than 500 it should block and wait for the collection to fill. I don't mind if the queue exceeds this value (700, 1000 etc.) for short amount of times.

I currently don't seem to find a solution to fix this exact problem. I was thinking about using a ConcurrentLinkedQueue and to have the consumer periodically check if the queue has enough data, but that seems rather counterproductive.

Another idea is to use a LinkedBlockingQueue. The producers are not going to block (unless the queue is full which means it has Integer.MAX_VALUE values - this is not my case, so it's all good). The consumer will do a queue.take() and add the elements into an internal collection. When the internal collection reaches 500 elements it is going to consume them in batch.

Do you have any tips?

Thanks!

解决方案

In the end I went with the LinkedBlockingQueue.

The producers add items into the queue.

The consumer will do a queue.poll() and keep the items into an internal collection. When the internal collection reaches 500 elements it is going to consume them in bulk. I can also set some timeouts. For instance, if X seconds has passed I'll consume the collection even if it has less items than required (eg. 220).

这篇关于生产者/消费者 - 生产者将数据添加到集合而不阻塞,消费者消耗来自批量收集的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆