在Java中并行运行程序的最佳方法是什么?

时间:2018-01-24 08:25:06

标签: java multithreading

我想并行运行命令行(程序)。所以 我正在使用主题流程 ProcessBuilder 类。 我的笔记本电脑有2个内核和4个逻辑处理器

在单独的线程中运行 my.exe 程序的测试结果:

  • 1个过程1个线程16分钟
  • 2个进程2个线程20分钟
  • 2个进程2个线程51分钟
  • 4个进程4个线程34分钟
  • 8个进程4个线程83分钟
  • 8个进程8个线程68分钟

所以我看到明显的趋势=>无论我将多少个进程分成不同的线程,我只能获得(约)50%的速度提升。所以我认为java会拆分2个核心之间的所有线程。我对吗?如何实现更好的性能?

//general idea to wrap process into thread
class myThread extends Thread{
  String cmd;
  myThread(String cmd){
    this.cmd=cmd;
  }
  public void run(){
    try {
           ProcessBuilder pb = new ProcessBuilder(cmd);
           //run command line
           Process proc = pb.start();
           //wait until process finished
           proc.waitFor();    
         } catch(Exception e){
           //something wrong
        }
  }
}

public class myClass {
  public static void main(String args[]){
   //let say I have String array of command lines
   // and let's imagine I'm reducing array when do tests 
   // -param doesn't mean anything, so each command is the same in array
    String commandLines[] = {
     "C:\\myfolder\\my.exe\" -param 1"
     "C:\\myfolder\\my.exe\" -param 2",
     "C:\\myfolder\\my.exe\" -param 3"
     "C:\\myfolder\\my.exe\" -param 4"
     "C:\\myfolder\\my.exe\" -param 5",
     "C:\\myfolder\\my.exe\" -param 6",
     "C:\\myfolder\\my.exe\" -param 7",
     "C:\\myfolder\\my.exe\" -param 8",
     "C:\\myfolder\\my.exe\" -param 9",
     "C:\\myfolder\\my.exe\" -param 10",
     "C:\\myfolder\\my.exe\" -param 11",
     "C:\\myfolder\\my.exe\" -param 12"};

        myThread myThreadsArr[] = new myThread[commandLines.length]; 
        for(int i=0;i<commandLines.length;i++){
           myThreadsArr[i]=new myThread(commandLines[i]);
           //this start thread which obviously call run()
           myThreadsArr[i].start();
        }

  }
}

1 个答案:

答案 0 :(得分:0)

线程的数量取决于您正在运行的进程。例如,如果您只处理CPU密集型工作并且您有2个核心,那么在2个线程运行时您将创建的线程。取决于您的系统创建一个线程池并将您的进程作为任务提交。

要使用的最佳线程数取决于几个因素,但主要是可用处理器的数量以及您的任务的CPU密集程度。 最佳线程数:

N_threads = N_cpu * U_cpu * (1 + W / C)
Where:

N_threads is the optimal number of threads
N_cpu is the number of prcessors, which you can obtain from Runtime.getRuntime().availableProcessors();
U_cpu is the target CPU utilization (1 if you want to use the full available resources)
W / C is the ratio of wait time to compute time (0 for CPU-bound task, maybe 10 or 100 for slow I/O tasks)