Java 中具有多个约束(时间和大小)的集合
我有两个进程(生产者/消费者)。第一个将元素放入集合中,第二个读取它们。
我希望第二个进程不要读取每个单独的元素,而是等到:
- 集合中至少有 N 个元素 OR
- 最后一个元素是在 T 秒前收到的。
Java 5+ 中是否有允许这种行为的集合?我正在考虑 Queue,但我只发现 DelayQueue 这并不完全是我所需要的。
谢谢。
I have two processes (producer/consumer). The first one puts elements in a Collection, the second one reads them.
I want the second process not to read every individual element, but wait until:
- There are at least N elements in the collection OR
- The last element was received T seconds ago.
Is there any Collection in Java 5+ that allows this kind of behaviour? I was thinking about an implementation of Queue, but I've only found DelayQueue that is not exactly what I need.
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我将实现一个可观察集合。第二个进程将监听事件,表示集合中有
N
个元素(基于size
属性的事件),并且在一定时间内没有添加任何元素(需要一个计时器,在每次添加
操作时都会重置)类似这样的东西(只需起草大小要求):
I'd implement an observable collection. The second process will listen to events, signalling that
N
elements are in the collection (events based onsize
attribute) and that no element has been added for a certain time (needs a timer, that is reset on everyadd
operation)Something like this (just drafting the size requirement):