0

I have one infinite for loop and i want to run this for loop for 20 seconds after that i want to terminate the loop so what can i do for this?

Here is my code in android:

do {
    rl += Character.toString((char) in.read());
    // Log.i("RL", "" + rl);
    if (rl.contains("<") && checkStream(rl)) {
        // Log.i("RL INSIDE", "" + rl);
        getNumerAndStatusFromRLString(rl);
        rl = "";
    }
    if (20 SECONDS COMPLETED) {
        Log.i("<rl>", "exit");
        break;
    }
} while (true);

So what can i do to check that 20 seconds is compeleted and now its time to terminate.

Any idea will be appreciated.

2 Answers 2

2

I think one of the options may be get time before the loop and while looping check 20 seconds elapsed or not.

Something like below: Not sure about the efficiency.

   long startTime=   System.currentTimeMillis();
do {

......

     long currTime=  System.currentTimeMillis();
     long elapsedtime = currTime - startTime;
     if(elapsedtime > 20000)
      {
      break;
      }
}
14
  • yes i have same idea but how can i get the difference of 20 seconds? Aug 17, 2012 at 13:42
  • 1
    elapsed time should be compared with 20*1000. It's in milliseconds. Aug 17, 2012 at 13:44
  • 1
    @RushabhPatel: You can't exactly catch 20 SECONDS, that's why > symbol is for. Because of processing time we don't on which second next iteration executes.
    – kosa
    Aug 17, 2012 at 13:56
  • 1
    @thinksteep: a 1 millisecond sleep time will make the loop pause for 1/1000th of a second. You won't be able to notice it but to a CPU a millisecond may as well be a year. Such a change can reduce 100% cpu usage to virtually 0% cpu usage.
    – Nick
    Aug 17, 2012 at 13:57
  • 2
    (...CONTINUED) If all the loop is doing is checking a boolean to see whether there is work to do, its kind of a waste unless you absolutely need the most responsive loop you can possibly get. (In which case you should be doing something with interrupts instead, and probably not using java in the first place) If on the other hand you can live with a ~1000hz loop frequency then you want to add a 1 ms sleep into the loop. This allows the thread to stop for a moment and allow the cpu to do other stuff. (CONTINUED)
    – Nick
    Aug 17, 2012 at 14:12
0

Use the Timer Task which you can specify the length of time for a task to run.

One caveat emptor: this set of functions used requires a recurring frequency but as its a one shot timing thing, you can get around it by using a handler in which you can signal to cancel!

An example would be something like this:

private Timer myTimer = new Timer();
private final int CANCEL_EXECUTION = 0x100;
private Handler myHandler = new Handler();

public void runFor20Sec(){
   myTimer.schedule(new TimerTask(){
       @Override
       public void run(){
          // Do something here
          myHandler.obtainMessage(CANCEL_EXECUTION).sendToTarget();
       }
    }, System.currentTimeMillis(), 2000);
}
public class myHandler extends Handler{
    @Override
    public void handleMessage(Message msg){
        switch(msg.what){
           case CANCEL_EXECUTION :
               myTimer.cancel();
               break;
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.