-2

I want to check if an int value is higher than 20 for a certain amount of 15 minutes, if that int value stays higher than 20 in those 15 minutes, code will executed

I didn't understand the difference between a Handler and a Runnable, how to use them, What do they do...

My question is:

How can I run an if statement for a certain time using a Runnable/Handler

This is the if statement which I want to be checked for 15 mins,

if(Speed > 20){
           // Code that will run after 15 mins IF the speed is higher than 20 for all that time
}
captindfru
  • 233
  • 1
  • 3
  • 13
  • use a `while` loop and use `Thread.sleep(2000)` inside it for waiting – Ashishkumar Singh Aug 01 '18 at 13:03
  • 1
    Possible duplicate of [What is the different between Handler, Runnable, and Threads?](https://stackoverflow.com/questions/21194762/what-is-the-different-between-handler-runnable-and-threads) – Shubham AgaRwal Aug 01 '18 at 13:03
  • you want to execute your if statement for some time . e.g 5 mins or something else? please explain briefly – Ahsan Malik Aug 01 '18 at 13:05
  • I edited the question @AshishSingh – captindfru Aug 01 '18 at 13:06
  • Can you show us your code which will give us more insight of your issue – Ashishkumar Singh Aug 01 '18 at 13:07
  • Perhaps use an observer to listen for changes in the value. Whenever the value > 20 schedule a task for 20 minutes from now. If the value drops <= 20 then cancel the scheduled task. – Andrew S Aug 01 '18 at 13:20
  • @AndrewS +1 You solved it, Thank you (although i got banned for my question), but still it was answered – captindfru Aug 01 '18 at 13:24
  • Re, "I didn't understand...a Runnable...what [it does]." If you want to know what a `Runnable` object's `run()` method does, then you should ask the person who wrote it. Normally, that would be _you_. It's not what the `Runnable` does that's interesting: What's interesting usually is what _somebody else's_ code does with the `Runnable` object that you created. – Solomon Slow Aug 01 '18 at 17:05

2 Answers2

1

add this, this timer will execute after 1 sec you can add your time you want and put your if statement inside run function

  private Timer myTimer;
  myTimer = new Timer();
   myTimer.schedule(new TimerTask() {          
    @Override
    public void run() {
        TimerMethod();
    }

    }, 0, 1000);
Ahsan Malik
  • 399
  • 2
  • 3
  • 13
0

Try using below code. It uses a while loop which will keep of looping for-ever until two condition are met

1) i becomes greater than 20

2) flag is set to false

For each iteration, it will sleep for 1 minute

public static void main(String[] args) {
    int i = 0;
    boolean flag = true;
    try {
        while (i < 20 && flag) {
            TimeUnit.MINUTES.sleep(1);
            // Expecting some logic to increment the value of i

            // Or change the flag value of this to exit the while loop
        }
    } catch (Exception exp) {
        exp.printStackTrace();
    }
}
Ashishkumar Singh
  • 3,580
  • 1
  • 23
  • 41