-1
public void execute(){
        for (int i=0;i<=100000000;i++)
        {
            System.out.println(i);
        }
    }

This method has to be executed only for two seconds. That means after starting executing this method in the for loop while printing 88888 if two second finished then program has to stop there itself.Is that possible?

3 Answers3

1

You can use break; anytime inside of a loop to exit the loop immediately. This works for any loops in Java (e.g. for, while, do...while). Example:

public void execute() {
    long startTime = System.currentTimeMillis();
    for (int i = 0; i <= 100000000; i++) {
        long currentTime = System.currentTimeMillis();
        if ((currentTime - startTime) >= 2000) {
            break;
        }
        System.out.println(i);
    }
}

That will cause the loop to exit once the time elapsed it greater than or equal to 2 seconds (Roughly).

EDIT: As @LouisWasserman commented, you can use System.nanoTime() to be more precise and not be affected by things like leap seconds. If you use that instead, just check that the difference is greater than or equal to 2000000000 to account for the precision.

Marc Baumbach
  • 10,323
  • 2
  • 30
  • 45
  • 1
    it works in nearly every language ;) – duffy356 Feb 06 '13 at 15:56
  • @duffy356 Also true. :) – Marc Baumbach Feb 06 '13 at 15:58
  • 1
    I am sorry, but your loop is equivalent to `for(int i=0; i <= 1000; i++){...}` – AlexR Feb 06 '13 at 15:59
  • @AlexR Yes, the original loop I posted, but I was just explaining the semantics of the `break` keyword. Obviously if you wanted to stop your loop at a known iteration, you would just define it in the for loop. – Marc Baumbach Feb 06 '13 at 16:01
  • 1
    Never use `System.currentTimeMillis()` to detect relative times; use `System.nanoTime()` instead, which is not affected by e.g. leapseconds. – Louis Wasserman Feb 06 '13 at 16:08
  • thanks for you reply.in that code what I have written only one print statement.Instead of that assume there is 5 - 10 call going to different methods.So our specified time out can occur at any time at any method.Then how can we specify time out exactly – user2047507 Feb 06 '13 at 16:09
  • @user2047507 Getting an exact timeout is going to be difficult, but you can check between each method call if the time elapsed is greater than your desired timeout. Just get an updated `currentTime` and compare it to your `startTime` between each method call. If timing out is the most important aspect vs. completing your method calls, I'd suggest looking into threading. Check out http://stackoverflow.com/questions/2275443/how-to-timeout-a-thread – Marc Baumbach Feb 06 '13 at 16:20
1

You can use System.currentTimeMillis(). Something like this:

public void execute(){
  long beginMillis = System.currentTimeMillis();
  for (int i=0;i<=100000000;i++)
  {
    if (System.currentTimeMillis() - beginMillis > 2000) {
      break;
    } 
    System.out.println(i);
  }
}

A side node - getting the current time is actually quite slow so I suggest doing the check once a given number of iterations. You will not stop on 2 seconds sharp but the slowdown will be smaller.

Ivaylo Strandjev
  • 69,226
  • 18
  • 123
  • 176
  • thanks for you reply.in that code what I have published is just haveing only one print statement. – user2047507 Feb 06 '13 at 16:01
  • @user2047507 yes, but the time measurement I add will significantly slow things down, that is why I suggest you do it for instance once every 100 iterations. – Ivaylo Strandjev Feb 06 '13 at 16:02
  • Just assume instead of one print statement below there are call to 5 to – user2047507 Feb 06 '13 at 16:02
  • 10 different method.Then at that time where u will add break exactly – user2047507 Feb 06 '13 at 16:03
  • @user2047507 I would check once every iteration, or once per 5 iterations for instance. Also put the limit a bit earlier than 2000 milliseconds for instance 1910 to be sure the program will finish on time. – Ivaylo Strandjev Feb 06 '13 at 16:05
  • If there is different call to many method then at any method while executing any line our specified time out which may be 2 or 5 seconds will be reached.then there itself it has to stop.Am I clear enough to make you understand – user2047507 Feb 06 '13 at 16:06
0

As @Marc Baumbach has mentioned, you can use break; if you are in a loop.

Just to add though if you want to actually exit a method rather than just exit the loop, you can use the return; keyword

public class Tester {
    public static void main(String args[]) {
        for(int i=0; i<10; i++) {
            System.out.println(i);
            if(i==5) return;
        }
    }
}

Would print...

0
1
2
3
4
5
David
  • 19,577
  • 28
  • 108
  • 128