title: Java run async processes
body: <p>I am trying to run an async process and I do not want the program to wait until the end of these processes executions. I found this question <a href="https://stackoverflow.com/questions/14190961/how-to-run-shell-script-asynchronously-from-within-java-program">how to run shell script asynchronously from within Java program</a> but it doesn't have the answer that I am looking for.</p>
  
  <p>What I am doing is I am simply running bash processes and after I run it, I do not want the Java program to wait until it's finished. This is what I have done:</p>
  
  <pre><code>public void runCommandLine(String directory) throws IOException {
      Thread commandLineThread = new Thread(() -&gt; {
          try {
              ProcessBuilder processBuilder = new ProcessBuilder(
                      "/bin/bash");
              processBuilder.directory(new File(directory));
              Process process = processBuilder.start();
              try (OutputStreamWriter osw = new OutputStreamWriter(process.getOutputStream())) {
                  osw.write(command);
              }
              printStream(process.getErrorStream(), true);
              printStream(process.getInputStream(), true);
          } catch (IOException ex) {
              ex.printStackTrace();
          }
      });
      commandLineThread.start();
      System.out.println("Task Dispatched");
  }
  </code></pre>
  
  <p>I also put another print out at the end of the main method so I get this output:</p>
  
  <pre><code>Task Dispatched
  Task Dispatched
  End of psvm
  </code></pre>
  
  <p>However the program does not terminate as these two processes have not terminated.</p>
  
  <p>How can I solve this issue?</p>