简体   繁体   中英

Do I create multiple processbuilders or just one?

I need to spawn multiple of the same processes. They use the same path but one of the contents of the file change in the qs args. Do I create a new process builder instance for each process? Or do I just have one processBuilder instance and just spawn multiple processes with processBuilder.start();

This

while(true) {
    ProcessBuilder processBuilder = new ProcessBuilder(path);
    processBuilder.redirectErrorStream(true);
    processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
    Process process = processBuilder.start();
}

or this

ProcessBuilder processBuilder = new ProcessBuilder(path);
processBuilder.redirectErrorStream(true);
processBuilder.redirectOutput(ProcessBuilder.Redirect.INHERIT);
while(true) { 
    Process process = processBuilder.start();
}

Java doc says "Each ProcessBuilder instance manages a collection of process attributes. The start() method creates a new Process instance with those attributes. The start() method can be invoked repeatedly from the same instance to create new subprocesses with identical or related attributes. "

https://docs.oracle.com/javase/7/docs/api/java/lang/ProcessBuilder.html

So second method should be ok.

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