可调整 Java BlockingQueue 大小
因此,我在生产者/消费者类型应用程序中使用固定大小的 BlockingQueue [ArrayBlockingQueue],但我希望用户能够动态更改队列大小。问题是没有一个 BlockingQueue 实现允许在创建后更改容量。有人以前遇到过这个吗?有什么想法吗?
So I am using a fixed sized BlockingQueue [ArrayBlockingQueue] in a producer/consumer type application, but I want the user to be able to change the queue size on the fly. Problem is there is not a BlockingQueue implementation that allows changing capacity after creation. Anyone ever come across this before? Any ideas?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
最明显的解决方案(可能适合也可能不适合,具体取决于您的情况)是简单地实例化一个具有所需更新容量的新队列。然后将旧队列中的所有内容推送到新队列中。
The most obvious solution (may or may not be appropriate depending on your circumstances) would be to simply instantiate a new queue with the updated capacity you want. Then push everything from the old queue into the new queue.
您可以扩展 LinkedBlockingQueue。它的限制是一个软限制(即只是一项额外的检查),您可以使其成为可以更改的东西(通过禁用内置限制并放入您自己的限制)
You could extend LinkedBlockingQueue. Its limit is is a soft limit (i.e. just an additional check) and you could make this something you can change (by disabling the built in one and putting in your own)