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