对于类似生产者-消费者的场景,LinkedBlockingQueue 是正确的选择吗?
我有一个类似生产者-消费者的场景。 A 类生成 E 类型的对象。我必须将其保存在 A 类的静态数据结构中,因为消费者逻辑应该在 B 类中处理,而 B 类没有对 A 对象的引用。LinkedBlockingQueue 是正确的数据类型吗?队列?或者有什么更好的选择吗?
I have a producer-consumer like scenario. Class A produces objects of type E. I have to hold it in a static data structure in class A, because the consumer logic should be handled in a class B, which has no reference to object of A. Is LinkedBlockingQueue the correct data type for the queue? Or are there any better selection for this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您使用有界队列,您还可以考虑
ArrayBlockingQueue
。另外,来自
LinkedBlockingQueue
Javadoc:If you are using a bounded queue, you could also consider an
ArrayBlockingQueue
.Also, from the
LinkedBlockingQueue
Javadoc:如果生产者和消费者在两个不同的线程中运行,那么是,
LinkedBlockingQueue
将是一个不错的选择。If the producer and consumer runs in two different threads, then yes, a
LinkedBlockingQueue<E>
would be a good choice.