title: High CPU load in sub-child process
body: <p>I'm working on a java program that run a batch file using a <code>ProcessBuilder</code>.</p>
  
  <pre><code>public class Test {
  
      public static void main(String[] args){
          try {
              ProcessBuilder processBuilder = new ProcessBuilder("pathToMyBatch.bat");
              Process process = processBuilder.start();
              StreamReader fluxSortie = new StreamReader(process.getInputStream());
              StreamReader fluxErreur = new StreamReader(process.getErrorStream());
              new Thread(fluxSortie).start();
              new Thread(fluxErreur).start();
          } catch (IOException e) {
              e.printStackTrace();
          }
      }
  
      static class StreamReader implements Runnable {
  
          private final InputStream inputStream;
  
          StreamReader(InputStream inputStream) {
              this.inputStream = inputStream;
          }
  
          private BufferedReader getBufferedReader(InputStream is) {
              return new BufferedReader(new InputStreamReader(is));
          }
  
          @Override
          public void run() {
              BufferedReader br = getBufferedReader(inputStream);
              String ligne = "";
              try {
                  while ((ligne = br.readLine()) != null) {
                      System.out.println(ligne);
                  }
              } catch (IOException e) {
                  e.printStackTrace();
              }
          }
      }
  
  }
  </code></pre>
  
  <p><code>StreamReader</code> class is a <code>Runnable</code>that wait for inputs and print every read line.</p>
  
  <p>Thebatch file launches an other java application simply by calling <code>java -classpath...</code>, and do other stuff related (and unrelated) to this application.</p>
  
  <p>I can't modify this batch.</p>
  
  <p>To make it clear: 
  Program <code>A</code> launches batch file <code>B</code> that launches application <code>C</code></p>
  
  <p>When I run the batch file <code>B</code> directly from windows, the java application <code>C</code> barely use more than 2% of CPU. But when I run it through my java program <code>A</code>, <code>C</code> consumes 25% of CPU load (1 full core). 
  In both cases, the batch execution charge on CPU is ~0%.</p>
  
  <p>I guess that's due to the output and error streams of the java application that are not handled correctly.</p>
  
  <p>Am I right? How could I fix this? Is there any way to get the sub-child (the child of the process) process stream?</p>