how to do multiprocessing in java, and what speed gains to expect?

前端 未结 6 1882
失恋的感觉
失恋的感觉 2021-02-02 14:44

I am a newbie using Java to do some data processing on csv files. For that I use the multithreading capabilities of Java (pools of threads) to batch-import the csv files into Ja

6条回答
  •  醉梦人生
    2021-02-02 15:22

    There are several ways to start a new process in Java:

    1. ProcessBuilder.start()
    2. Runtime.exec() works around ProcessBuilder
    3. Apache Commons Exec that works around Runtime.exec()

    With ProcessBuilder:

    ProcessBuilder pb =
    new ProcessBuilder("myCommand", "myArg1", "myArg2");
    Map env = pb.environment();
    env.put("VAR1", "myValue");
    env.remove("OTHERVAR");
    env.put("VAR2", env.get("VAR1") + "suffix");
    pb.directory(new File("myDir"));
    File log = new File("log");
    pb.redirectErrorStream(true);
    pb.redirectOutput(Redirect.appendTo(log));
    Process p = pb.start();
    assert pb.redirectInput() == Redirect.PIPE;
    assert pb.redirectOutput().file() == log;
    assert p.getInputStream().read() == -1;
    

    With Runtime:

    Runtime r = Runtime.getRuntime();
    Process p = r.exec("firefox");
    p.waitFor(10, TimeUnit.SECONDS);
    p.destroy();
    

    With Apache Commons Exec:

    String line = "AcroRd32.exe /p /h " + file.getAbsolutePath();
    CommandLine cmdLine = CommandLine.parse(line);
    DefaultExecutor executor = new DefaultExecutor();
    int exitValue = executor.execute(cmdLine);
    

    Key differences between Multiprocessing and Multithreading from this:

    • The key difference between multiprocessing and multithreading is that multiprocessing allows a system to have more than two CPUs added to the system whereas multithreading lets a process generate multiple threads to increase the computing speed of a system.
    • Multiprocessing system executes multiple processes simultaneously whereas, the multithreading system let execute multiple threads of a process simultaneously.
    • Creating a process can consume time and even exhaust the system resources. However creating threads is economical as threads belonging to the same process share the belongings of that process.
    • Multiprocessing can be classified into symmetric multiprocessing and asymmetric multiprocessing whereas, multithreading is not classified further.

    Additional links:

    • https://alvinalexander.com/java/java-exec-processbuilder-process-1
    • https://alvinalexander.com/java/java-exec-processbuilder-process-2
    • https://alvinalexander.com/java/java-exec-processbuilder-process-3

提交回复
热议问题