I run analysis on a file directly from an analyzer program and it finishes under one minute. But if I make jar of analyzer and run it via ProcessBuilder
, it does not get finished even in 8 minutes (500 sec). Here is the code I am using for ProcessBuilder
. Can someone please explain the reason?
I cannot run the analyzer program directly because depending upon the input file, it may actually take 15-20 minutes which I don't want. I want to finish it under 8 minutes.
Here is code for ProcessBuilder.
public void myFun(){
String fileName = file.getName();
System.out.println(file.getAbsolutePath());
Process p;
ProcessBuilder pb;
String filePath = file.getAbsolutePath();
pb = new ProcessBuilder("C:\\Program Files\\Java\\jdk1.7.0_13\\bin\\java.exe", "-jar", "ta.jar", filePath);
pb.directory(new File("D:\\Softwares\\analyzerRun\\bin"));
long startTime = System.currentTimeMillis();
try {
p = pb.start();
long currTime = System.currentTimeMillis();
long diff = currTime - startTime;
boolean isBreak = false;
while(diff < 500000)
{
currTime = System.currentTimeMillis();
diff = currTime - startTime;
if(processIsTerminated(p))
{
isBreak = true;
break;
}
}
if(!isBreak)
{
System.out.println("Interrupting current thread!!");
p.destroy();
Thread.currentThread().interrupt();
}
else
{
System.out.println("process terminated peacefully");
}
System.out.println("Done with "+ fileName);
} catch (IOException e) {
e.printStackTrace();
}
}
}
private static boolean processIsTerminated (Process process) {
try {
process.exitValue();
} catch (IllegalThreadStateException itse) {
return false;
}
return true;
}
Edit1:
So after given suggestions, I removed while loop and my try code looks like below now:
try {
p = pb.start();
p.waitFor();
}
Interestingly, I run this program and it does not get finished under 5 minutes. But if I run my analyzer directly, it gets finished under 30 seconds. Does ProcessBuilder not get enough priority from CPU?
To answer this, "Interestingly, I run this program and it does not get finished under 5 minutes. But if I run my analyzer directly, it gets finished under 30 seconds. Does ProcessBuilder not get enough priority from CPU? "
After a long-time, I finally figured out that I was writing two lines on standard output in my original program. So, when I ran the jar
file using ProcessBuilder
, I didn't read from standard output. The process was getting hanged ultimately getting killed by the main-class thread. I just removed those two lines from the original program and now it takes equal time in both ways.