0

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.

3 Answers 3

2

Do you mean :

while ((System.currentTimeMillis() <= endTime ){
    if(line=stdInput.readLine())!= null){
        System.out.println(line);
    }        
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it looks fine but it's not working, I updated the question with more details
0

Try not using the short-circut &&.

Use &, this will make sure it both conditions are checked. With && if the first one was false the whole thing is short circutted.

You want the readLine() to execute even regardless right? with && the second condition will never even be looked at if the first condition is false.

& will check both then make a decision.

1 Comment

Thanks but I don't see how this will help me in my case
0

If stdInput comes from a scanner, it probably will never be null. Show a bit more of your code.

And as for what madmax said, yes, do short-circut. For 0 AND 1 = 0, which means 0 AND something = 0. It's math.

3 Comments

Yes it's a scanner
Then it's like I said. By stating while((line=stdInput.readLine()) != null){} you are basicaly saying "While the input that I receive from the scanner is not null, I continue the loop.". I don't see a way for it to end like that. <br> What You can do is while( !(line=stdInput.readLine()).isEmpty() && (System.currentTimeMillis() <= endTime ) ){}. Like this you are verifying if the input is an empty String AND if the current time is less than the endTime.
Taking what I said above, instead of !(line=stdInput.readLine()).isEmpty(), you could use a flag, so it would be something like: !(line=stdInput.readLine().equals( flag )).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.