多个生产者、单个消费者的情况

发布于 2024-11-26 12:54:52 字数 452 浏览 0 评论 0原文

我有一个任务T要以固定速率R运行。我有多个 java 程序 P1P2 等,使用不同类型的输入独立执行此任务。我希望在所有程序同时运行时以 R 速率完成任务。
所以我正在寻找的是实现一个不同的程序P,它仅以速率T执行任务,并使用来自所有程序P1的输入, P2 等。换句话说,P1P2 等生成数据,P 消耗这些数据来执行< em>T 速率 R
我不知道的是如何制作一个可以被不同程序访问的动态队列(可能是 LinkedBlockingQueue)。

有想法吗?

PS:如果不用Java,我也可以使用C/C++。我更喜欢 Java 中的解决方案,因为程序现在都是 Java 中的

I have a task T to be run at a fixed rate R. I have multiple java programs P1, P2, etc doing this task independently with different kinds inputs. I want the task to be done at a rate R while all programs are running concurrently.
So what I'm looking for is implementing a different program P, which solely does the task at rate T with inputs from all programs P1, P2, etc. In other words, P1, P2, etc generate data, which P consumes for performing T at rate R
What I don't know is how to make a dynamic queue (a LinkedBlockingQueue may be) which can be accessed by different programs.

Ideas?

PS: If not Java, I can also use C/C++. I'd prefer solutions in Java as the programs are now in Java

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

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

发布评论

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

评论(2

浪荡不羁 2024-12-03 12:54:52

好的,实现一个公开某种接口的程序。最简单的接口是从套接字读取,甚至只是 STDIN。您必须定义协议。例如,您可以使用标准 java 序列化。

现在您的所有程序 P1、P2 等都会将命令“写入”到流中。执行任务的程序将读取它们,存储在队列中,并使用您的自定义实现、java.util.Timer 或 Executors 按指定顺序执行。

守护线程在这里无关紧要。守护线程是当所有其他(非守护)线程已终止时,不会阻止程序终止的线程。

您可以按照您所描述的单独的 java 进程来实现所有这些。我真的不明白为什么。您可能考虑过使用 JMS 吗?它将允许您完全解耦模块,并将其用作一个进程,也可以用作单独的进程。

OK, implement a program that exposes some kind of interface. The simplest interface is reading from socket or even just STDIN. You have to define protocol. For example you can use standard java serialization.

Now all your programs P1, P2 etc will "write" commands into the stream. The program that executes tasks will read them, store in queue and execute in specified order using either your custom implementation, java.util.Timer or Executors.

Deamon thread is irrelevant here. Deamon thread is a thread that does not prevent program from terminating when all other (non deamon) threads has been terminated.

You can implement all this as you described as separate java processes. I just really do not understand why. Did you probably think about using JMS? It will allow you to completely decouple your modules and use the as one process and as separate processes too.

沫尐诺 2024-12-03 12:54:52

如果您实际上想要一个单独的线程,而不是一个单独的进程,那么我建议使用以下内容:

  • ScheduledThreadPoolExecutor 具有单个线程,负责执行任务。
  • 其他线程添加的 LinkedBlockingQueue任务到.使用阻塞队列很重要,即使你给它一个高容量,这样你就不会陷入生产速度快于消费速度的情况(或者,至少,识别这种情况并采取措施)无论您需要什么步骤)。
  • 一个 Runnable,用于从队列中取出项目并处决他们。您可以使用 scheduleAtFixedRate() 方法将此可运行项添加到线程池中。

If you actually want a separate thread, rather than a separate process, then I'd recommend the following:

  • A ScheduledThreadPoolExecutor with a single thread, that is responsible for executing tasks.
  • A LinkedBlockingQueue that the other threads add tasks to. It's important to use a blocking queue, even if you give it a high capacity, so that you don't get into a situation where you're producing faster than you're consuming (or, at least, to identify that situation and take whatever steps you need).
  • A Runnable that pulls items off the queue and executes them. You'd add this runnable to the threadpool with the scheduleAtFixedRate() method.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文