I execute a process with Java. The process never terminate and scan the machine looking for some file, when it found one of them it prints the path of the file. I want to execute the process for a certain time and get the output. This is a piece of my code
BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));
Process p = pb.start();
while ((System.currentTimeMillis() <= endTime )){ //loop 1
while((line=stdInput.readLine()) != null){ loop 2
System.out.println(line);
}
}
p.destroy();
the problem is that if the script doesn't find a result for a short time loop 2 will exit and never run again during the period.
I also tried
while ((System.currentTimeMillis() <= endTime ) || (line=stdInput.readLine()) != null))
But in this case if stdInput.readLine() still returning results the loop will not end even if the time is ended.
while ((System.currentTimeMillis() <= endTime ) && (line=stdInput.readLine()) != null))
In this case the loop will exit if line=stdInput.readLine() doesn't return a result even if the time is not ended
Edit: I tried this code
System.out.println("Before while");
while ((System.currentTimeMillis() <= endTime ){
System.out.println("while");
if(line=stdInput.readLine())!= null){
System.out.println("One line");
}
}
p.destroy();
If the process is not detecting anything the loop never end, this is the output
ok before while while
System.out.println("while"); was called just one time when it was supposed to be called for 5 seconds.