问题
I am trying to write a script that will run a .exe program 4 times with different parameters. I created one thread for each .exe run. Each thread will write an output file. My problem is that, it should write in parallel, but as you can you see on the screenshot below, the file write one after another. How should this be resolved?
Here's the main method:
public static void main (String args[]) {
ExecutorService executor = Executors.newFixedThreadPool(4);
executor.execute(new RunnableReader("myprogram.exe", param1, outputFile1));
executor.execute(new RunnableReader("myprogram.exe", param2, outputFile2));
executor.execute(new RunnableReader("myprogram.exe", param3, outputFile3));
executor.execute(new RunnableReader("myprogram.exe", param4, outputFile4));
executor.shutdown();
}
Here's the runnable class:
public class RunnableReader implements Runnable {
private String program;
private String param;
String outputFile;
public RunnableReader(String program, String param, String outputFile) {
this.program = program;
this.param = param;
this.outputFile = outputFile;
}
@Override
public void run() {
try {
ProcessBuilder pb = new ProcessBuilder(program, param);
pb.redirectOutput(ProcessBuilder.Redirect.PIPE);
pb.redirectErrorStream(true);
Process proc = pb.start();
InputStream stream = proc.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
BufferedWriter writer = new BufferedWriter(new FileWriter(outputFile, true));
for(String output; (output = reader.readLine()) != null) {
writer.append(output);
writer.append("\n");
}
writer.close();
reader.close();
stream.close();
} catch(IOException e) {
e.printStackTrace();
}
}
}
回答1:
I haven't been able to test this myself and I don't know if it actually causes the execution to block. But for what it's worth I thought I should point out that your reading of the process' InputStream
might be unnecessary.
As stated by the Oracle docs ProcessBuilder.redirectOutput(ProcessBuilder.Redirect.PIPE)
causes Process.getInputStream()
to return the process' standard output.
With that in mind you could get rid of the entire for-loop
and instead just do something like ProcessBuilder.redirectOutput(new File(outputFile))
so that your method instead looks like this
@Override
public void run() {
try {
ProcessBuilder pb = new ProcessBuilder(program, param);
pb.redirectOutput(new File(outputFile));
pb.redirectErrorStream(true);
Process proc = pb.start();
} catch(IOException e) {
e.printStackTrace();
}
来源:https://stackoverflow.com/questions/63384559/why-java-parallel-file-writing-is-not-working