High CPU utilization in the child process

I am working on a java program that runs a batch file using ProcessBuilder .

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

StreamReader class is a Runnable that waits for input and prints every line it StreamReader .

The thebatch file launches another Java application, just by calling java -classpath... and make another one related to it (and not related) to this application.

I can not change this party.

To make it clear: Program A runs the batch file B , which runs the application C

When I run the B command file directly from the windows, the C java application barely uses more than 2% of the processor. But when I run it through the java program A , C consumes 25% of the processor load (1 full core). In both cases, the charge for downloading a batch to the CPU is ~ 0%.

I assume that due to the weekends and flow errors of the Java application that are not being processed correctly.

I'm right? How can i fix this? Is there a way to get the process thread of a child process (child process)?

+5
source share
1 answer

I assume that due to the weekends and flow errors of the Java application that are not being processed correctly.

I'm right?

Maybe. But this is a very vague description of what (what you think) is going on.

I suspect the problem is in your StreamReader class. For instance:

  • it can read a stream without buffering,
  • it can be a poll of a stream,
  • it can process (or accumulate) output in an inefficient way,
  • or something else.

If you need a better answer, we really need to see the code for this class.


I suspect these are calls to System.out.println .

Or it is possible that a Java child application writes its output without buffering.

+1
source

All Articles