当没有消费者准备好时处理生产者线程

发布于 2024-12-10 01:59:31 字数 640 浏览 0 评论 0原文

关于处理以下场景的模式的建议:

将事件分派给消费者的单个线程。每个事件和消费者之间存在 1:1 的关系(每个事件根据事件/消费者 ID 匹配分派给单个消费者)。

消费者以不同的速度处理事件,并且可以以可配置的批量大小消费事件(例如,消费者一次可以消费 20 个事件)。

生产者线程应该始终能够将事件分派给能够消费的消费者。每个消费者维护一个它已消费的事件队列(可能是批量)并在自己的线程上处理这些事件,因此从生产者到消费者的切换是异步的。

如果任何时间点都没有消费者可以消费,那么调度线程会发生什么情况?

  • yield()
  • wait() & 调用 notify()
  • 强制消费者在固定的时间段内
  • sleep()是否

有理由选择其中之一?

一些优点和缺点缺点:

  • yield 很简单,
  • 强制消费者调用通知会增加复杂性,
  • 固定时间的睡眠将适合非时间敏感的要求,
  • 旋转会消耗 CPU,除非我们需要尽可能快的事件传递,否则没有必要

。还有其他考虑因素吗?

Suggestions on patterns for handling the following scenario:

A single thread that dispatches events to consumers. There is a 1:1 between each event and a consumer (each event is dispatched to a single consumer based on event/consumer id match).

Consumers process events at varying speeds and can consume events in configurable batch sizes (e.g. a consumer could consume 20 events at a time).

The producer thread should always be able to dispatch events to consumers that are capable of consuming. Each consumer maintains a queue of events it has consumed (possibly in batch) and processes these on its own thread, so the hand-off from producer to consumer is asynchronous.

If no consumers can consume at any point in time, what should happen to the dispatch thread?

  • yield() it
  • wait() & force consumers to call notify() on it
  • sleep() for a fixed time period
  • spin

Any reason to prefer one over the other?

Some pros & cons:

  • yield is simple
  • forcing consumers to call notify adds complexity
  • sleep for a fixed time would suit for non time sensitive requirements
  • spinning eats up a CPU, unnecessary unless we need as fast as possible event delivery

Any other considerations?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

ヤ经典坏疍 2024-12-17 01:59:31

您应该考虑的另一种方法是将其写入 BlockingQueue。让队列管理在没有侦听器的情况下发送的请求。

更好的是:编写一个拥有 BlockingQueue 并维护 ConsumersListBroker。当 Producer 发送新的 Event 时,让 Broker 通知 ConsumersList

我将使用自 JDK 1.0 以来内置于 Java Bean 中的 PropertyChangeListenerEventObject 在内存中执行此操作。

Another way you should consider would be writing it to a BlockingQueue. Let the queue manage requests sent without listeners.

Even better: write a Broker that owns a BlockingQueue and maintains a List of Consumers. Have the Broker notify the List of Consumers when a Producer sends a new Event.

I'd use the PropertyChangeListener and EventObject built into Java Beans since JDK 1.0 to do this in memory.

浅黛梨妆こ 2024-12-17 01:59:31

a) 您可以选择yield,但根据环境的好坏,这实际上可能会变成无操作。所以这基本上会产生与旋转相同的结果。

b) 睡眠是一个简单的选择,但你应该想出睡多长时间。执行 sleep(0) 也无济于事,因为它与执行 (a) 相同。

通知的力量更复杂,但您可以完全控制流程。

a) You could choose yield but depending on how good the environment is, this could essentially become a no-op. So this would essentially have the same result as spinning.

b) Sleep is an easy choice but then you should come up with how long to sleep. Doing sleep(0) also will not help as it will be same as doing (a)

The force of notification is more complicated but you have complete control of your flow.

愚人国度 2024-12-17 01:59:31

查看 JMS。 JMS 正是为处理此类用例而设计的。

在您的场景中,全面的 JMS 安装可能有点过分 – 您没有提供足够的信息。

Take a look at JMS. JMS is designed to handle exactly this kind of use case.

A full scale JMS installation might be overkill in your scenario – you don't provide enough information.

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