简体   繁体   中英

What is the best way to run programs in parallel in Java

I want to run command lines (programs) in parallel. so I'm using Thread , Process and ProcessBuilder classes for that. My laptop has 2 cores and 4 logical processors.

Test results of running my.exe programs in separate threads:

  • 1 process 1 thread 16 min
  • 2 processes 2 threads 20 min
  • 2 processes 2 threads 51 min
  • 4 processes 4 threads 34 min
  • 8 processes 4 threads 83 min
  • 8 processes 8 threads 68 min

So I see obvious trend => doesn't matter how many processes I split into separate threads, I would get only (approx) 50% speed increase. So I'm thinking that java splits all threads between 2 cores. Am I right? how to achieve better performance?

//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();
        }

  }
}

The number of thread depends upon process you are running.For an example if you are processing only CPU intensive work and you have 2 core ,so what ever thread you will create at the time 2 thread will run.Depending your system create a thread pool and submit your process as task.

The optimal number of threads to use depends on several factors, but mostly the number of available processors and how cpu-intensive your tasks are. optimal number of threads:

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) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM