使用多个主机运行多线程程序

时间:2017-07-30 06:29:06

标签: java multithreading parallel-processing multiprocessing distributed-computing

我有一个程序可以旋转数千个线程。我目前正在为所有线程使用一个主机,这需要花费大量时间。如果我想使用多个主机(比如10个主机,每个主机运行100个不同的线程),我该怎么办?

2 个答案:

答案 0 :(得分:0)

在一个JVM上拥有数千个线程听起来像个坏主意 - 你可能花费大部分时间来完成上下文切换而不是做实际的工作。

要跨多个主机拆分您的工作,您不能使用由单个JVM管理的线程。您需要让每个主机公开一个可以接收部分工作的API并返回完成工作的结果。

一种方法是使用Java RMI(远程方法调用)来完成此任务,但实际上,您的问题缺乏对决定选择哪种架构很重要的许多细节。

答案 1 :(得分:0)

Creating 1000 threads in on JVM is very bad design and need to minimise count.
High thread count will not give you multi-threading benefit as context switching will be very frequent and will hit performance.
If you are thinking of dividing in multiple hosts then you need parallel processing system like Hadoop /Spark. 
They internally handles task allocation as well as central system for syncing all hosts on which threads/tasks are running.