多个生产者、单个消费者的情况
我有一个任务T要以固定速率R运行。我有多个 java 程序 P1、P2 等,使用不同类型的输入独立执行此任务。我希望在所有程序同时运行时以 R 速率完成任务。
所以我正在寻找的是实现一个不同的程序P,它仅以速率T执行任务,并使用来自所有程序P1的输入, P2 等。换句话说,P1、P2 等生成数据,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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的,实现一个公开某种接口的程序。最简单的接口是从套接字读取,甚至只是 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.
如果您实际上想要一个单独的线程,而不是一个单独的进程,那么我建议使用以下内容:
scheduleAtFixedRate()
方法将此可运行项添加到线程池中。If you actually want a separate thread, rather than a separate process, then I'd recommend the following:
scheduleAtFixedRate()
method.