对于类似生产者-消费者的场景,LinkedBlockingQueue 是正确的选择吗?

发布于 2024-09-05 07:55:12 字数 140 浏览 8 评论 0原文

我有一个类似生产者-消费者的场景。 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

三岁铭 2024-09-12 07:55:12

如果您使用有界队列,您还可以考虑 ArrayBlockingQueue

另外,来自 LinkedBlockingQueue Javadoc:

链接队列通常具有更高的
吞吐量高于基于数组的队列,但是
大多数情况下的性能不太可预测
并发应用程序。

If you are using a bounded queue, you could also consider an ArrayBlockingQueue.

Also, from the LinkedBlockingQueue Javadoc:

Linked queues typically have higher
throughput than array-based queues but
less predictable performance in most
concurrent applications.

胡渣熟男 2024-09-12 07:55:12

如果生产者和消费者在两个不同的线程中运行,那么LinkedBlockingQueue 将是一个不错的选择。

If the producer and consumer runs in two different threads, then yes, a LinkedBlockingQueue<E> would be a good choice.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文