Running java runtime.exec () for multiple processes

In my program, I have a list of n elements.

I will repeat the list and initiate such a process:

 Runtime.getRuntime.exec("cmd /C start abc.bat"+listitem() ) 

I need to support counting 4 processes. As soon as any of the processes is completed, I need to start the next process, so the number of processes should be 4.

I can initiate 4 processes at the same time, but I'm not sure how to save the score 4. In principle, I need some notification as soon as the process is completed, so I can start the next one, any streaming is possible.

Any help on how to implement this, can anyone share a fragment of this requirement?

+4
source share
4 answers

You should have four threads, each of which receives a task from the pool, then executes it, and then, when it is done, do the following task. It would be like this:

 class Whatever extends Thread { public void run() { while (!interrupted()) { String str = listitem(); if (str == null) // there are no more commands to run break; Runtime.getRuntime.exec(("cmd /C start abc.bat"+str).split("\\s")).waitFor(); } 

Then run four of these threads.

+1
source

Use the ThreadPoolExecutor implementation of size 4 and Runnable , which starts Process and then calls Process.waitFor() . Since the thread pool will be limited to 4 threads, and all 4 threads will start the process, and then wait for it, you will see that there will be no more than 4 child processes.

Sample code to help you along the way:

 ExecutorService executor = Executors.newFixedThreadPool(4); executor.execute(new Runnable() { public void run() { //use ProcessBuilder here to make the process Process p = processBuilder.start(); p.waitFor(); } }); 
+12
source
  public class ProcessRunner { public static void main(String[] args) throws IOException, InterruptedException { //Creating n threads as required ExecutorService exec = Executors.newFixedThreadPool(4); for(int i = 0; i < 4; i++){ exec.execute(new ProcessRunnable()); } Thread.sleep(10000); //whenever you want them to stop exec.shutdownNow(); } } class ProcessRunnable implements Runnable{ @Override public void run(){ do{ Process p; try { p = Runtime.getRuntime().exec("cd .."); p.waitFor(); } catch (IOException e) { //Take appropriate steps e.printStackTrace(); } catch (InterruptedException e) { //Take appropriate steps e.printStackTrace(); } }while(!Thread.interrupted()); } } 

Process # WAITFOR ()

Causes the current thread to wait, if necessary, until the process represented by this Process object has completed. This method returns immediately if the subprocess has already completed. If the subprocess has not yet completed, the calling thread will be blocked until the subprocess finishes.

+2
source

You can use a fixed pool of threads with a size of 4, which guarantees no more than 4 active threads at any given time

  final ExecutorService ex = Executors.newFixedThreadPool(4); for(int i = 0; i < 100; i++) { ex.execute(new Runnable() { @Override public void run() { ... run the process here and wait for it to end } }); } 
+1
source

Source: https://habr.com/ru/post/1494901/


All Articles