I am able to launch Process
with the help of below command and after launching multiple processes I want to control how many processes I want to keep at some point.
For example:
- Initiate a
Process
inside afor
loop of range 0 to 50 - Pause the
for
loop once total active processes are 5 - Resume
for
loop once it drop from 5 to 4 or 3 ...
I tried below code, but I am missing something.
public class OpenTerminal {
public static void main(String[] args) throws Exception {
int counter = 0;
for (int i = 0; i < 50; i++) {
while (counter < 5) {
if (runTheProc().isAlive()) {
counter = counter + 1;
}else if(!runTheProc().isAlive()) {
counter = counter-1;
}
}
}
}
private static Process runTheProc() throws Exception {
return Runtime.getRuntime().exec("cmd /c start cmd.exe /c \"dir && ping localhost\"");
}
}
Also, how to find out how many process are active? So that I can control active processes at a time.
You can use thread pool with fixed size. For example: