Why Java parallel file writing is not working?

坚强是说给别人听的谎言 提交于 2020-08-20 08:53:10

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!