- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有一个生产者消费者模型,arduino 生成数据包,在我的电脑内我有一个 java 程序,它获取这些数据包并将它们放入 BlockingQueue
中。现在有线程处理这些数据包。
假设 X
是生产者线程,A
、B
和 C
是消费者线程。有一个队列,所有消费者都可以引用它。消息(数据包)是不可变的对象(即消费者无法更改元素的状态)。我的问题是我如何知道所有线程都已使用队列内的特定元素完成,以便我可以将其删除?
这是我的消费者 run()
方法:
@Override
public void run()
{
while (isStarted && !queue.isEmpty()) {
updateMap(queue.peek());
}
}
我正在考虑的一种设计是使用有界
队列。当生产者发现队列已满时,它会删除第一个元素。但我不确定这是否是一种安全的方法。我读过this教程和其他一些内容,我得到的是:
Producer should wait if Queue or bucket is full and Consumer should wait if queue or bucket is empty.
如果这听起来很明显,我很抱歉,但我是多线程编程的新手,而且代码的并发性对我来说听起来很可怕。
编辑:
所有A
、B
和C
都独立。一个用于统计,一个用于更新网络 map 等。
编辑2:
正如 @Augusto 所建议的,还有另一种方法,其中 A
、B
和 C
都有自己的队列。我的网络监听器将数据包传递到每个队列并由它们处理。它可以工作,但是如何仅使用一个队列来完成此操作?或是否可以仅使用一个队列来实现此场景?以及如果答案是肯定的。 我需要如何以及何时从队列中删除元素?
最佳答案
(编辑后我的评论的后续内容)我建议每个线程有一个不同的队列。这实际上是一个众所周知的模式,称为 publish-subscribe .
取自上面的链接:
A Publish-Subscribe Channel works like this: It has one input channel that splits into multiple output channels, one for each subscriber. When an event is published into the channel, the Publish-Subscribe Channel delivers a copy of the message to each of the output channels. Each output channel has only one subscriber, which is only allowed to consume a message once. In this way, each subscriber only gets the message once and consumed copies disappear from their channels.
ConcurrentLinkedQueue
和 BlockingQueue
之间的主要区别在于,您可以添加对 BlockingQueue
上元素数量的限制。如果从网络读取的生产者生成数据的速度比消费者处理数据的速度快,那么这很好。如果您使用无界队列并且这种情况持续一段时间,您最终会遇到 OutOfMemoryError
。
关于java多线程: How to know when a consumer thread should remove an element from queue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33721110/
我已经开始研究 MassTransit 并正在编写将处理消息的类。当我从 Consumes 实现接口(interface)时我有四个选项:All , Selected , For和 Context .
我正在尝试找出消费者群体级别是否也有任何抵消。在 Kafka 中,Consumer Offset 是在 Consumer group 级别还是在该 consumer group 内的单个消费者? 最佳
我有一个我不理解的 java 编译器错误。看来消费者 和 Consumer(带有 T 扩展对象)在方法签名参数中不等效。请查看以下代码: import java.util.function.Consu
我在泛型方面遇到了一些麻烦,尽管找到了解决方法,但我不明白是什么阻止了我的代码编译。 我有一个显示 TreeTableView 的 JavaFX 项目:
C++11 标准定义了一个内存模型(1.7、1.10),其中包含内存排序,大致为“顺序一致”、“获取”、“消耗”、“释放”和“放松”。同样粗略地,一个程序只有在它是无种族的情况下才是正确的,如果所有
我有一个 kafka 主题,我正在通过 Kafka Producer 发送数据。现在,在消费者方面,我有两种选择。 1。使用 KafkaConsumer - 下面是 kafkaConsumer 的代码
我有四个当前消费者在 Amazon AWS 上收听同一个队列。从队列中拉取消息时,有时会出现同一条消息被两个不同的消费者消费的情况。请看下面的日志: 18:01:46,515 [jmsContaine
我正在设计一个系统,其中将有 n 个生产者和 m 个消费者,其中 n 和 m 是数字,n != m。 我想这样设计系统, 任何生产者在生产时不得阻止其他生产者 任何消费者都不应在消费时阻止其他消费者
我们有一个系统,我们希望将记录(例如联系人、客户、机会)从我们的系统推送到 SalesForce。 为此,我们使用了 ForceToolKit for .Net .我们成功地将联系人记录从我们的系统推
我怎样才能写一个方法来组合 Stream的 Consumers成单个 Consumer使用 Consumer.andThen(Consumer) ? 我的第一个版本是: Consumer combi
我需要开始使用 kafka。我很难理解消费者应该收到什么:据我了解,我们可以通过多种方式配置消费者: 示例 1: @KafkaListener(topics = "topic_name) public
我需要开始使用 kafka。我很难理解消费者应该收到什么:据我了解,我们可以通过多种方式配置消费者: 示例 1: @KafkaListener(topics = "topic_name) public
我正在尝试在我的 scala play 应用程序中创建消费者 secret / key 对,但我似乎无法让它正常工作。我有以下代码 import org.apache.commons.codec.bi
我通过传递用户(消费者)名称使用 .NET 应用程序,我需要从 Salesforce 检索消费者 key 和消费者 key ,我该如何实现。 最佳答案 Consumer Key 和 Consumer
我想设置 至 0 .这似乎是另一个问题 ( JMS queue with multiple consumers ) 的答案,并在此 article 中进行了描述。在第 17.1.1 章中。我使用 JN
I have send message api to my users.When I send to message from my x numbers I need to wait 10-15
我有一个 java Kafka 消费者,我在其中批量获取 ConsumerRecords 进行处理。示例代码如下- while (true) { ConsumerRecords records
我正在为 iPhone 编写 Twitter/Facebook 应用程序。我有自己的 Apache/PHP 服务器。我只想把Consumer Key放在app里,然后我把Consumer Secret
Spring AMQP:比较多个消费者与每个消费者多个线程的性能 我正处于从 Spring 文档学习 Spring AMQP 的阶段。我不清楚提高异步消息消费率的首选方法:根据 Spring 文档 (
我正在制作一个需要 oAuth 1.0 身份验证的应用程序。我可以访问客户提供的消费者 key 和消费者 secret 。我曾尝试使用 AFNetworking 进行此操作,但效果不佳。有人可以建议我
我是一名优秀的程序员,十分优秀!